rezaenayati commited on
Commit
e3ef36b
·
verified ·
1 Parent(s): cb87471

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def predict(image_array):
10
+ if image_array is None or np.sum(image_array) == 0:
11
+ return "Please draw a digit."
12
+
13
+ image = Image.fromarray(image_array.astype("uint8"), mode="L")
14
+ image = ImageOps.invert(image).resize((28, 28))
15
+
16
+ image_array = np.array(image).astype("float32") / 255.0
17
+ image_array = image_array.reshape(1, 28, 28, 1)
18
+
19
+ logits = model.predict(image_array)
20
+ prediction = np.argmax(logits)
21
+ confidence = tf.nn.softmax(logits)[0][prediction].numpy()
22
+
23
+ return f"Digit: {prediction} (confidence: {confidence:.2%})"
24
+
25
+ gr.Interface(
26
+ fn=predict,
27
+ inputs=gr.Sketchpad(image_mode="L", canvas_size=(200, 200)),
28
+ outputs="text",
29
+ title="Digit Classifier",
30
+ ).launch()