rezaenayati commited on
Commit
58ef32e
·
verified ·
1 Parent(s): ab4956e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -8,23 +8,30 @@ model = tf.keras.models.load_model("cnn_model.h5")
8
 
9
  def predict(image_array):
10
  try:
 
 
 
 
 
 
 
 
 
 
 
11
  if image_array is None or np.sum(image_array) == 0:
12
  return "Please draw a digit."
13
-
14
  image = Image.fromarray(image_array.astype("uint8"), mode="L")
15
  image = ImageOps.invert(image).resize((28, 28))
16
-
17
  image_array = np.array(image).astype("float32") / 255.0
18
  image_array = image_array.reshape(1, 28, 28, 1)
19
-
20
  logits = model.predict(image_array)
21
  prediction = int(np.argmax(logits))
22
  confidence = float(tf.nn.softmax(logits)[0][prediction])
23
-
24
  return f"Digit: {prediction} (confidence: {confidence:.2%})"
25
-
26
  except Exception as err:
27
- return f"Runtime error: {str(err)}"
28
 
29
  gr.Interface(
30
  fn=predict,
 
8
 
9
  def predict(image_array):
10
  try:
11
+ # Debug the input type
12
+ print(f"Type of input: {type(image_array)}")
13
+
14
+ # Handle if input is a dictionary (which appears to be happening)
15
+ if isinstance(image_array, dict):
16
+ # If it's a dict, try to get the image data
17
+ if "image" in image_array:
18
+ image_array = image_array["image"]
19
+ else:
20
+ return "Error: Received dictionary input but couldn't find image data"
21
+
22
  if image_array is None or np.sum(image_array) == 0:
23
  return "Please draw a digit."
24
+
25
  image = Image.fromarray(image_array.astype("uint8"), mode="L")
26
  image = ImageOps.invert(image).resize((28, 28))
 
27
  image_array = np.array(image).astype("float32") / 255.0
28
  image_array = image_array.reshape(1, 28, 28, 1)
 
29
  logits = model.predict(image_array)
30
  prediction = int(np.argmax(logits))
31
  confidence = float(tf.nn.softmax(logits)[0][prediction])
 
32
  return f"Digit: {prediction} (confidence: {confidence:.2%})"
 
33
  except Exception as err:
34
+ return f"Runtime error: {str(err)}\nInput type: {type(image_array)}"
35
 
36
  gr.Interface(
37
  fn=predict,