Spaces:
Runtime error
Runtime error
Added app files
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("mnist.h5")
|
7 |
+
|
8 |
+
|
9 |
+
# Define a function to predict the digit
|
10 |
+
def predict_digit(img):
|
11 |
+
# Resize the image to 28x28 pixels
|
12 |
+
img = img.reshape((1, 28, 28, 1))
|
13 |
+
# Normalize the image
|
14 |
+
if img.max() > 1:
|
15 |
+
img = img / 255.0
|
16 |
+
# Predict the class
|
17 |
+
res = model.predict([img])[0]
|
18 |
+
# create result dictionary
|
19 |
+
return {str(i): float(res[i]) for i in range(10)}
|
20 |
+
|
21 |
+
|
22 |
+
with gr.Blocks(
|
23 |
+
css="style.css",
|
24 |
+
theme=gr.themes.Default(primary_hue="blue", secondary_hue="cyan"),
|
25 |
+
) as app:
|
26 |
+
# create a header and a para
|
27 |
+
with gr.Row():
|
28 |
+
gr.Markdown(
|
29 |
+
"""# MNIST Digit Recognizer
|
30 |
+
This app recognizes handwritten digits. The app uses a sketchpad to get the input image.
|
31 |
+
|
32 |
+
Model used is a two layered Convolution network, followed by a fully connected layer and a softmax layer.
|
33 |
+
""",
|
34 |
+
)
|
35 |
+
with gr.Row():
|
36 |
+
gr.Markdown("## Sketchpad")
|
37 |
+
gr.Markdown("## Prediction")
|
38 |
+
# create a row
|
39 |
+
with gr.Row():
|
40 |
+
# create a sketchpad
|
41 |
+
sketchpad = gr.Sketchpad(
|
42 |
+
shape=(28, 28),
|
43 |
+
brush_radius=2,
|
44 |
+
elem_id="sketchpad",
|
45 |
+
label="Draw a digit here",
|
46 |
+
)
|
47 |
+
blank_sketchpad = gr.Sketchpad(
|
48 |
+
invert_colors=True, brush_radius=2, visible=False
|
49 |
+
)
|
50 |
+
# create a label
|
51 |
+
label = gr.Label(
|
52 |
+
num_top_classes=3,
|
53 |
+
elem_id="label",
|
54 |
+
label="Prediction",
|
55 |
+
)
|
56 |
+
|
57 |
+
# create a button
|
58 |
+
button = gr.Button("Predict", elem_id="btn_pred")
|
59 |
+
# bind the button to predict the digit
|
60 |
+
button.click(
|
61 |
+
predict_digit,
|
62 |
+
inputs=sketchpad,
|
63 |
+
outputs=label,
|
64 |
+
)
|
65 |
+
|
66 |
+
# create a clear button for sketchpad
|
67 |
+
clear_button = gr.Button("Clear", elem_id="btn_clr")
|
68 |
+
clear_button.click(lambda a: None, inputs=blank_sketchpad, outputs=sketchpad)
|
69 |
+
|
70 |
+
app.launch(share=False)
|
mnist.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:06b1503d9e13866c2aa1123972a65fa0843141661e494f84bdf5782056fa1d16
|
3 |
+
size 465008
|
requirements.text
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
#requirements for the app
|
2 |
+
tensorflow
|
3 |
+
gradio
|
style.css
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#btn_pred {
|
2 |
+
margin-left: 37.5%;
|
3 |
+
width: 25%;
|
4 |
+
}
|
5 |
+
|
6 |
+
#btn_clr {
|
7 |
+
margin-left: 37.5%;
|
8 |
+
width: 25%;
|
9 |
+
}
|