Spaces:
Sleeping
Sleeping
__all__ = ['learn', 'categories', 'classify_image', 'image', 'label', 'examples', 'intf'] | |
# Cell | |
from fastai.vision.all import * | |
import gradio as gr | |
# Cell | |
learn = load_learner('bear_model.pkl') | |
# Cell | |
categories = ('black', 'grizzly', 'teddy') | |
def classify_image(img): | |
pred, idx, probs = learn.predict(img) | |
if max(probs).item() < 0.6: | |
return "Less than 60% confidence it's one of these bears." | |
else: | |
return dict(zip(categories, map(float, probs))) | |
# Cell | |
# Description texts | |
gColab = """ | |
My model training code is in [Google Colab Notebook](https://colab.research.google.com/drive/1N592yRBIituoNB8kIh8TekGbkzufuP-P?usp=sharing) | |
""" | |
heading = """ | |
## What bear is it--grizzly, black, or teddy? | |
""" | |
# Example images | |
examples = ['images/grizzly_bear.jpg', 'images/sloth_stuffed_animal.jpg', 'images/teddy_bear.jpeg', 'images/sailboat.jpg'] | |
with gr.Blocks() as app: | |
gr.Markdown(heading) | |
# Define inputs and outputs | |
with gr.Row(): | |
with gr.Column(): | |
image = gr.Image(height=224, width=224, label="Input Image") | |
with gr.Column(): | |
label = gr.Label(label="Classification Result") | |
# Add image examples | |
gr.Examples(examples=examples, inputs=image, outputs=label, fn=classify_image) | |
# Define what happens when submitted | |
image.change(fn=classify_image, inputs=image, outputs=label) | |
# Place description sub-block here | |
gr.Markdown(gColab) | |
app.launch(inline=False) |