Update app.py
Browse files
app.py
CHANGED
@@ -3,26 +3,20 @@ 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 |
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])
|
@@ -30,13 +24,11 @@ def predict(image_array):
|
|
30 |
return f"Digit: {prediction} (confidence: {confidence:.2%})"
|
31 |
|
32 |
except Exception as err:
|
33 |
-
return f"
|
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)),
|
40 |
outputs="text",
|
41 |
-
title="Digit Classifier"
|
42 |
).launch()
|
|
|
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 |
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])
|
|
|
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,
|
31 |
+
inputs=gr.Sketchpad(image_mode="L", canvas_size=(200, 200), type="numpy"),
|
32 |
outputs="text",
|
33 |
+
title="Digit Classifier"
|
34 |
).launch()
|