File size: 1,480 Bytes
7db4557
 
 
 
 
86b89b0
7db4557
 
 
 
 
698f242
7db4557
 
 
65c1411
1d7945e
95978d1
 
7db4557
 
 
1d7945e
 
 
 
7db4557
1d7945e
 
 
86b89b0
1d7945e
 
86b89b0
1d7945e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

__all__ = ['learn', 'categories', 'classify_image', 'image', 'label', 'examples', 'intf']

# Cell
from fastai.vision.all import *
import gradio as gr

# Cell
learn = load_learner('bear_model.pkl')

# Cell
categories = ('black', 'grizzly', 'teddy')

def classify_image(img):
    pred, idx, probs = learn.predict(img)
    if max(probs).item() < 0.6:
        return "Less than 60% confidence it's one of these bears."
    else:
        return dict(zip(categories, map(float, probs)))

# Cell

# Description texts
gColab = """
My model training code is in [Google Colab Notebook](https://colab.research.google.com/drive/1N592yRBIituoNB8kIh8TekGbkzufuP-P?usp=sharing)
"""

heading = """
## What bear is it--grizzly, black, or teddy?
"""

# Example images
examples = ['images/grizzly_bear.jpg', 'images/sloth_stuffed_animal.jpg', 'images/teddy_bear.jpeg', 'images/sailboat.jpg']

with gr.Blocks() as app:

    gr.Markdown(heading)
    
    # Define inputs and outputs
    with gr.Row():
        with gr.Column():
            image = gr.Image(height=224, width=224, label="Input Image")
        with gr.Column():
            label = gr.Label(label="Classification Result")
    
    # Add image examples
    gr.Examples(examples=examples, inputs=image, outputs=label, fn=classify_image)
    
    # Define what happens when submitted
    image.change(fn=classify_image, inputs=image, outputs=label)

    # Place description sub-block here
    gr.Markdown(gColab)

app.launch(inline=False)