From 7a9f42a92ecf0f8226f6b74f68849edb4bc82591 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Thu, 9 May 2024 05:24:22 -0700 Subject: [PATCH] Add script for launching a gradio app for model inference --- .../lesson-2/predict_app.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 practical-deep-learning-for-coders/lesson-2/predict_app.py diff --git a/practical-deep-learning-for-coders/lesson-2/predict_app.py b/practical-deep-learning-for-coders/lesson-2/predict_app.py new file mode 100644 index 0000000..371693b --- /dev/null +++ b/practical-deep-learning-for-coders/lesson-2/predict_app.py @@ -0,0 +1,28 @@ +import gradio as gr +import os + +from fastai.learner import load_learner +from pathlib import Path + +path = Path(os.path.dirname(os.path.realpath(__file__))) + +# Make sure to export the model first via the notebook. +# The exported model is not included in the repository. +model = load_learner(path/'export.pkl') +labels = model.dls.vocab + +def predict(img): + _, _, probabilities = model.predict(img) + return {labels[i]: float(probabilities[i]) for i in range(len(labels))} + +title = "Grizzly, Black, or Teddy Bear?" +description = "This is a simple image classifier that can predict whether a given image contains a grizzly bear, black bear, or teddy bear." + +gr.Interface( + fn = predict, + inputs = gr.Image(width = 512, height = 512), + outputs = gr.Label(num_top_classes = 3), + title = title, + description = description, + allow_flagging = 'never' +).launch() \ No newline at end of file