File size: 1,790 Bytes
894ea4f
 
 
 
 
 
 
 
 
 
 
938e2a7
 
894ea4f
 
 
 
 
 
25bfb8a
 
 
 
894ea4f
 
 
 
 
 
 
25bfb8a
 
bbd8916
894ea4f
 
 
 
 
 
25bfb8a
894ea4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25bfb8a
894ea4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25bfb8a
894ea4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
938e2a7
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# %%
#|default_exp app

# %% [markdown]
# # Bear Classifier App
# 
# This notebook creates uses an exported model `export.pkl` for a bear classifier, to create a python script which can run the model on HuggingFace. 

# %%
#|export
from fastai.vision.all import *
import gradio as gr

# %% [markdown]
# Let's take a look at an example picture:

# %%
#im = PILImage.create('teddybear.jpg')
#im = PILImage.create('grizzly.jpg')
im = PILImage.create('blackbear.jpg')

im.thumbnail((192, 192))
im


# %% [markdown]
# Let's import the model and create the learner:

# %%
#|export
import pathlib
plt = platform.system()
if plt == 'Windows': pathlib.WindowsPath = pathlib.PosixPath
learn = load_learner('export.pkl')

# %% [markdown]
# With the learner we can to the predictions (inference):

# %%
learn.predict(im)

# %% [markdown]
# The available categories are contained in the vocab:

# %%
learn.dls.vocab

# %% [markdown]
# This is the function to classify the images:

# %%
#|export
def classify_image(img):
    pred,pred_idx,probs = learn.predict(img)
    return dict(zip(learn.dls.vocab, map(float, probs)))

# %% [markdown]
# Testing the function:

# %%
classify_image(im)

# %% [markdown]
# ## Gradio App
# 
# Now it is time to create the gradio app:

# %%
# commented, because it produced warnings

#image = gr.inputs.Image(shape=(192,192))
#label = gr.outputs.Label()

# %%
#|export
image = gr.components.Image(shape=(192,192))
label = gr.components.Label()
examples = ['teddybear.jpg', 'grizzly.jpg', 'blackbear.jpg']

intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch(inline=False)

# %%
intf.close()

# %% [markdown]
# ## Export
# 
# Finally, we export the code in the cells which are marked with `#|export`:

# %%


# %%
 

# %%