Update app.py
Browse files
app.py
CHANGED
@@ -6,9 +6,34 @@ import tensorflow as tf
|
|
6 |
#Load the model
|
7 |
model = tf.keras.models.load_model("cnn_model.h5")
|
8 |
|
9 |
-
def predict(
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
gr.Interface(
|
13 |
fn=predict,
|
14 |
inputs=gr.Sketchpad(image_mode="L", canvas_size=(200, 200)),
|
|
|
6 |
#Load the model
|
7 |
model = tf.keras.models.load_model("cnn_model.h5")
|
8 |
|
9 |
+
def predict(image_array):
|
10 |
+
try:
|
11 |
+
print("Function called!")
|
12 |
+
|
13 |
+
# Check for blank or None input
|
14 |
+
if image_array is None or np.sum(image_array) == 0:
|
15 |
+
return "Please draw a digit."
|
16 |
+
|
17 |
+
# Convert to PIL image and process
|
18 |
+
image = Image.fromarray(image_array.astype("uint8"), mode="L")
|
19 |
+
image = ImageOps.invert(image).resize((28, 28))
|
20 |
+
|
21 |
+
# Normalize and reshape for model input
|
22 |
+
image_array = np.array(image).astype("float32") / 255.0
|
23 |
+
image_array = image_array.reshape(1, 28, 28, 1)
|
24 |
+
|
25 |
+
# Predict
|
26 |
+
logits = model.predict(image_array)
|
27 |
+
prediction = int(np.argmax(logits))
|
28 |
+
confidence = float(tf.nn.softmax(logits)[0][prediction])
|
29 |
+
|
30 |
+
return f"Digit: {prediction} (confidence: {confidence:.2%})"
|
31 |
+
|
32 |
+
except Exception as err:
|
33 |
+
return f"💥 Runtime error: {str(err)}"
|
34 |
+
|
35 |
+
|
36 |
+
gr.Sketchpad(image_mode="L", canvas_size=(200, 200), type="numpy")
|
37 |
gr.Interface(
|
38 |
fn=predict,
|
39 |
inputs=gr.Sketchpad(image_mode="L", canvas_size=(200, 200)),
|