Spaces:
Runtime error
Runtime error
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb. | |
# %% auto 0 | |
__all__ = ['btn_upload', 'out_pl', 'lbl_pred', 'learn', 'categories', 'image', 'label', 'examples', 'intf', 'DataLoaders', | |
'is_cat', 'classify_img'] | |
# %% app.ipynb 2 | |
from fastai.vision.all import * | |
import gradio as gr | |
from fastbook import * | |
from fastai.vision.widgets import * | |
import gradio as gr | |
btn_upload = widgets.FileUpload() | |
out_pl = widgets.Output() | |
lbl_pred = widgets.Label() | |
# %% app.ipynb 3 | |
# def on_data_change(change): | |
# lbl_pred.value = '' | |
# img = PILImage.create(btn_upload.data[-1]) | |
# out_pl.clear_output() | |
# with out_pl: display(img.to_thumb(128,128)) | |
# pred,pred_idx,probs = learn_inf.predict(img) | |
# lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}' | |
class DataLoaders(GetAttr): | |
def __init__(self, *loaders): self.loaders = loaders | |
def __getitem__(self, i): return self.loaders[i] | |
train,valid = add_props(lambda i,self: self[i]) | |
def is_cat(x): return x[0].isupper() | |
# %% app.ipynb 4 | |
learn = load_learner('export.pkl') | |
print(type(learn)) | |
# %% app.ipynb 7 | |
categories = ('black', 'grizzly', 'teddy') | |
# %% app.ipynb 8 | |
def classify_img(img): | |
cat,idx, prob = learn.predict(img) | |
return dict(zip(categories, map(float,prob))) | |
# %% app.ipynb 10 | |
image = gr.inputs.Image(shape = (192,192)) | |
label = gr.outputs.Label() | |
examples = ['teddy.png', 'grizzly.jpg','black.jpeg'] | |
intf = gr.Interface(fn = classify_img, inputs = image, outputs = label, examples = examples) | |
intf.launch(inline = False) | |