File size: 1,559 Bytes
e0ef435
 
 
625fbd2
 
e0ef435
 
625fbd2
0d78b6c
625fbd2
 
 
 
 
 
0d78b6c
e0ef435
625fbd2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0ef435
 
 
625fbd2
e0ef435
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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)