2022-01-21 21:44:12 +08:00
|
|
|
from urllib.request import urlretrieve
|
|
|
|
|
2021-05-24 23:31:44 +08:00
|
|
|
import tensorflow as tf
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2021-05-24 23:31:44 +08:00
|
|
|
import gradio as gr
|
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
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)}
|
|
|
|
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2023-04-20 03:55:37 +08:00
|
|
|
im = gr.Image(shape=(28, 28), image_mode="L", invert_colors=False, source="canvas")
|
2021-05-24 23:31:44 +08:00
|
|
|
|
2022-03-28 23:11:14 +08:00
|
|
|
demo = gr.Interface(
|
2022-01-21 21:44:12 +08:00
|
|
|
recognize_digit,
|
|
|
|
im,
|
2023-04-20 03:55:37 +08:00
|
|
|
gr.Label(num_top_classes=3),
|
2021-05-24 23:31:44 +08:00
|
|
|
live=True,
|
|
|
|
interpretation="default",
|
|
|
|
capture_session=True,
|
|
|
|
)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-03-28 23:11:14 +08:00
|
|
|
demo.launch()
|