asparabamboo / app.py
exponential's picture
Update app.py
4ad8e31
Raw History Blame Contribute Delete
1.7 kB
#!/usr/bin/env python
# coding: utf-8
# Cell
from fastai.vision.all import *
import gradio as gr
# Cell
learn = load_learner('export.pkl')
def classify_image(img):
pred,idx,probs = learn.predict(img)
return f'Prediction: {pred}; Probability: {probs[idx]:.04f}'
# Cell
image = gr.inputs.Image(shape=(192, 192))
label = gr.outputs.Label()
examples = ['images/A_000.png']
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch(inline=False)
"""
from fastai.vision.all import *
import gradio as gr
from fastai.vision.widgets import *
from IPython.display import display
#|export
learn_inf = load_learner('export.pkl')
# %%
learn_inf.dls.vocab
#|export
btn_upload = widgets.FileUpload()
btn_upload
btn_upload = SimpleNamespace(data = ['images/A_000.png'])
#|export
img = PILImage.create(btn_upload.data[-1])
#|export
out_pl = widgets.Output()
out_pl.clear_output()
with out_pl: display(img.to_thumb(128,128))
out_pl
#|export
pred,pred_idx,probs = learn_inf.predict(img)
#|export
lbl_pred = widgets.Label()
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
lbl_pred
#|export
btn_run = widgets.Button(description='Classify')
btn_run
#|export
def on_click_classify(change):
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}'
btn_run.on_click(on_click_classify)
#|export
btn_upload = widgets.FileUpload()
#|export
VBox([widgets.Label('Select your green!'),
btn_upload, btn_run, out_pl, lbl_pred])
"""