File size: 1,125 Bytes
72e97d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastai.vision.learner import load_learner
import gradio as gr

MODELPATH = Path().cwd().parent/'model'/'cygnet-vs-duckling.pkl'
EXAMPLEPATH = Path().cwd().parent/'example-images'

learn = load_learner(MODELPATH)
categories = learn.dls.vocab

def classify_image(image):
    _, _, probs = learn.predict(image)
    return dict(zip(categories, map(float, probs)))

title = 'Mirror, mirror on the wall am I a duckling or a cygnet after all?'
description = """Hans Christian Andersen's tale of the ugly duckling tells us about the sad youth of a cygnet acidentally being brought up in a family of ducks and being ostrized for its ugliness.\n But what if the poor cygnet had a magic mirror to tell it that it was in fact a young swan after all? Machine learning to the rescue!"""

examples = EXAMPLEPATH.glob('*.jpg')

app = gr.Interface(fn=classify_image,
                   inputs=gr.components.Image(),
                   outputs=gr.components.Label(),
                   examples=examples,
                   title=title,
                   description=description,
                   allow_flagging='never')

app.launch()