Update app.py
Browse files
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(
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
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 |
-
|
|
|
|
|
|
|
40 |
fn=predict,
|
41 |
-
inputs=
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|