Add script for launching a gradio app for model inference
This commit is contained in:
parent
611cb19740
commit
7a9f42a92e
28
practical-deep-learning-for-coders/lesson-2/predict_app.py
Normal file
28
practical-deep-learning-for-coders/lesson-2/predict_app.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user