gradio/demo/digit_classifier.py

28 lines
618 B
Python
Raw Normal View History

2020-08-22 05:20:05 +08:00
# Demo: (Image) -> (Label)
2020-06-10 08:59:21 +08:00
import tensorflow as tf
import gradio
import gradio as gr
2020-07-09 06:09:52 +08:00
from urllib.request import urlretrieve
2020-06-10 08:59:21 +08:00
2020-07-09 09:01:43 +08:00
urlretrieve("https://gr-models.s3-us-west-2.amazonaws.com/mnist-model.h5", "mnist-model.h5")
2020-07-09 06:09:52 +08:00
model = tf.keras.models.load_model("mnist-model.h5")
2020-06-10 08:59:21 +08:00
2020-08-28 23:56:03 +08:00
2020-06-10 08:59:21 +08:00
def recognize_digit(image):
2020-08-20 05:27:22 +08:00
image = image.reshape(1, -1)
2020-07-09 06:09:52 +08:00
prediction = model.predict(image).tolist()[0]
2020-06-11 02:34:53 +08:00
return {str(i): prediction[i] for i in range(10)}
2020-06-10 08:59:21 +08:00
2020-07-09 05:53:24 +08:00
2020-08-28 23:56:03 +08:00
io = gr.Interface(
2020-06-11 02:34:53 +08:00
recognize_digit,
2020-08-22 05:20:05 +08:00
"sketchpad",
2020-06-11 02:34:53 +08:00
gradio.outputs.Label(num_top_classes=3),
2020-07-09 06:09:52 +08:00
live=True,
capture_session=True,
2020-08-28 23:56:03 +08:00
)
io.test_launch()
io.launch()