File size: 1,329 Bytes
c2763a7
 
 
 
 
 
 
 
 
 
 
 
 
b3324d1
c2763a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.

# %% auto 0
__all__ = ['learner', 'cats', 'img', 'lbl', 'examples', 'title', 'description', 'article', 'interface', 'classify_img']

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

# %% app.ipynb 6
learner = load_learner('model/export.pkl')

# %% app.ipynb 9
cats = ('Black Bear', 'Grizzly Bear', 'Teddy Bear',)

def classify_img(img):
    preds, idx, probs = learner.predict(img)
    return dict(zip(cats, map(float, probs)))

# %% app.ipynb 12
img = gr.Image()
lbl = gr.Label()
examples = [str(img_path) for img_path in Path('example_images/').rglob('*.jpg')]

title = 'Bear Classifier'
description = 'My first AI model that can tell you whether an image contains a grizzly bear, a black bear, or a teddy bear. This model was trained on the ' \
              'ResNet18 architecture and used the fastai library.'
article = "<p style='text-align: center; font-size: 36px'><a href='https://forbo7.github.io/forblog/posts/2_bear_classifier_model.html' " \
          "targets='_blank'><Blog Post</a></p>"

# %% app.ipynb 15
interface = gr.Interface(
    fn=classify_img,
    inputs='image',
    outputs='label',
    examples=examples,
    title=title,
    description=description,
    article=article
)

interface.launch(inline=False, enable_queue=True)