File size: 2,142 Bytes
67976d4
3f839e6
67976d4
 
 
 
6c55d33
cf9ee52
 
3f839e6
67976d4
 
cf9ee52
 
 
055c0f0
cf9ee52
 
 
 
 
 
 
 
67976d4
 
 
 
 
b9212ba
cf9ee52
 
055c0f0
 
 
cf9ee52
67976d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cf9ee52
 
 
 
 
 
 
 
67976d4
 
 
cf9ee52
 
 
 
 
 
 
 
 
 
 
67976d4
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
# AUTOGENERATED! DO NOT EDIT! File to edit: ../app.ipynb.

# %% auto 0
__all__ = ['EXPORT_PATH', 'categories', 'inp_img', 'labels', 'example_img', 'intf', 'set_posix_windows', 'classify_images']

# %% ../app.ipynb 3
import fastai
from fastai.vision.all import *
import gradio as gr 


# %% ../app.ipynb 5
from contextlib import contextmanager
import pathlib

'''@contextmanager
def set_posix_windows():
    posix_backup = pathlib.PosixPath
    try:
        pathlib.PosixPath = pathlib.WindowsPath
        yield
    finally:
        pathlib.PosixPath = posix_backup
        

# %% ../app.ipynb 6
#Exporting our model file:


EXPORT_PATH = pathlib.Path('model.pkl')

with set_posix_windows():
    learn = load_learner(EXPORT_PATH)'''

learn = load_learner('model.pkl')

# %% ../app.ipynb 9
'''categories = ('EASTERN TOWEE',
    'RING-BILLED GULL',
    'LILAC ROLLER',
    'CACTUS WREN',
    'MALACHITE KINGFISHER',
    'EURASIAN MAGPIE',
    'TRUMPTER SWAN',
    'HOODED MERGANSER',
    'RAZORBILL',
    'TREE SWALLOW',
    'MOURNING DOVE',
    'TURKEY VULTURE',
    'PEREGRINE FALCON',
    'BAR-TAILED GODWIT',
    'BLACK SWAN',
    'BALTIMORE ORIOLE',
    'BLUE HERON',
    'MIKADO PHEASANT',
    'WHITE CHEEKED TURACO',
    'GOLDEN CHLOROPHONIA')'''

categories = learn.dls.vocab
print(categories)


def classify_images(image):
    pred, idx, probs = learn.predict(image)
    return dict(zip(categories, map(float, probs)))

# %% ../app.ipynb 11
# Building the gradio application interface:


inp_img = gr.inputs.Image(shape=(192, 192))
labels = gr.outputs.Label()
example_img = [ 'baltimore_oriole.jpg', 'bar_tailed_godwit.jpg',
               'black_swan.jpg', 'blue_heron.jpg', 'cactus_wren.jpg', 'eastern_towee.jpg', 'golden_chlorophonia.jpg',
               'lilac_roller.jpg', 'malachite_kingfisher.jpg', 'mikado_pheasant.jpg', 'mourning_dove.jpg',
               'peregine_falcon.jpg', 'razorbill.jpg', 'ring_billed_gull.jpg', 
               'tree_swallow.jpg', 'trumpter_swan.jpg', 'white_cheeked_turaco.jpg']


intf = gr.Interface(fn=classify_images, inputs=inp_img, outputs=labels, examples=example_img)
intf.launch(inline=False)