gradio/demo/digit_classifier/run.py

38 lines
770 B
Python
Raw Normal View History

import os
from urllib.request import urlretrieve
2021-05-24 23:31:44 +08:00
import tensorflow as tf
2021-05-24 23:31:44 +08:00
import gradio
import gradio as gr
urlretrieve(
"https://gr-models.s3-us-west-2.amazonaws.com/mnist-model.h5", "mnist-model.h5"
)
2021-05-24 23:31:44 +08:00
model = tf.keras.models.load_model("mnist-model.h5")
def recognize_digit(image):
image = image.reshape(1, -1)
prediction = model.predict(image).tolist()[0]
return {str(i): prediction[i] for i in range(10)}
im = gradio.inputs.Image(
shape=(28, 28), image_mode="L", invert_colors=False, source="canvas"
)
2021-05-24 23:31:44 +08:00
iface = gr.Interface(
recognize_digit,
im,
2021-05-24 23:31:44 +08:00
gradio.outputs.Label(num_top_classes=3),
live=True,
interpretation="default",
capture_session=True,
)
iface.test_launch()
if __name__ == "__main__":
iface.launch()