crush_matching / app.py
irakli-ff
first_deployment
40dbd48
raw
history blame
1.07 kB
# AUTOGENERATED! DO NOT EDIT! File to edit: ../app.ipynb.
# %% auto 0
__all__ = ['learn', 'image', 'label', 'intf', 'pet_class', 'classify_image']
# %% ../app.ipynb 1
from fastai.vision.all import *
import gradio as gr
import io
from PIL import Image
def pet_class(x): return x
# %% ../app.ipynb 2
learn = load_learner('model.pkl')
# %% ../app.ipynb 3
#categories = ('basketball ball','golf ball', 'rugby ball', 'soccer ball')
def classify_image(img, top_k=5):
pred_class, pred_idx, probs = learn.predict(img)
categories = learn.dls.vocab
sorted_probs_indices = probs.argsort(descending=True)
top_categories = [categories[i] for i in sorted_probs_indices[:top_k]]
top_probs = probs[sorted_probs_indices[:top_k]]
return dict(zip(top_categories, map(float, top_probs)))
# %% ../app.ipynb 4
image = gr.components.Image(shape=(192,192))
label = gr.components.Label()
#examples = ['basketball.png', 'golf_ball.jpg', 'rugby_ball.jpg', 'soccer_ball.jpg']
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)
intf.launch(inline=False)