rezaenayati commited on
Commit
b0bbb9b
·
verified ·
1 Parent(s): 696c785

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -41
app.py CHANGED
@@ -1,52 +1,44 @@
1
  import gradio as gr
2
  import numpy as np
3
- from PIL import Image, ImageOps
4
  import tensorflow as tf
5
 
6
  # Load the model
7
  model = tf.keras.models.load_model("cnn_model.h5")
8
 
9
 
10
- def predict(image_array):
11
- try:
12
- # Handle dictionary input format from newer Gradio versions
13
- if isinstance(image_array, dict) and 'composite' in image_array:
14
- # Extract the image data from the 'composite' key
15
- image_array = image_array['composite']
16
-
17
- if image_array is None or np.sum(image_array) == 0:
18
- return "Please draw a digit."
19
-
20
- # Process the image for EMNIST format
21
- image = Image.fromarray(image_array.astype("uint8"), mode="L")
22
- image = image.resize((28, 28), Image.LANCZOS)
23
- image = ImageOps.invert(image) # Invert colors
24
-
25
- # Convert to model input format
26
- image_array = np.array(image).astype("float32") / 255.0
27
- image_array = image_array.reshape(1, 28, 28, 1)
28
-
29
- # Make prediction
30
- logits = model.predict(image_array, verbose=0)
31
- prediction = int(np.argmax(logits))
32
- confidence = float(tf.nn.softmax(logits)[0][prediction])
33
-
34
- return f"Digit: {prediction} (confidence: {confidence:.2%})"
35
- except Exception as err:
36
- return f"Runtime error: {str(err)}"
37
 
38
  # Create the Gradio interface with appropriate settings
39
- interface = gr.Interface(
 
 
 
40
  fn=predict,
41
- inputs=gr.Sketchpad(
42
- image_mode="L",
43
- canvas_size=(280, 280),
44
- type="numpy",
45
- brush=gr.Brush() # Using default brush settings
46
- ),
47
- outputs="text",
48
- title="EMNIST Digit Classifier",
49
- description="Draw a digit (0-9) in the center of the canvas. Make it large and clear for best results."
50
- )
51
-
52
- interface.launch()
 
1
  import gradio as gr
2
  import numpy as np
 
3
  import tensorflow as tf
4
 
5
  # Load the model
6
  model = tf.keras.models.load_model("cnn_model.h5")
7
 
8
 
9
+ def predict(input_dict):
10
+ if not input_dict or "composite" not in input_dict:
11
+ return {str(i): 0.0 for i in range(10)}
12
+
13
+ image = input_dict["composite"]
14
+
15
+ # Remove alpha channel (RGBA → RGB)
16
+ image = image[:, :, :3]
17
+
18
+ # Convert to grayscale
19
+ image = tf.image.rgb_to_grayscale(image)
20
+
21
+ # Resize to 28x28
22
+ image = tf.image.resize(image, [28, 28])
23
+
24
+ # Invert colors and normalize
25
+ image = 255 - image
26
+ image = image / 255.0
27
+ image = image.numpy().reshape(-1, 28, 28, 1)
28
+
29
+ # Predict
30
+ prediction = model_cnn.predict(image)
31
+ probs = tf.nn.softmax(prediction[0]).numpy()
32
+ return {str(i): float(probs[i]) for i in range(10)}
 
 
 
33
 
34
  # Create the Gradio interface with appropriate settings
35
+ sketchpad = gr.Sketchpad() # <- clean and version-safe
36
+ label = gr.Label(num_top_classes=3)
37
+
38
+ gr.Interface(
39
  fn=predict,
40
+ inputs=sketchpad,
41
+ outputs=label,
42
+ title="MNIST Digit Sketch Pad",
43
+ description="Draw a digit (0–9) and this model will try to guess what number you wrote! It’s trained on the original MNIST dataset of handwritten digits.",
44
+ ).launch(debug = False)