File size: 1,655 Bytes
43ed166
cdccfc2
 
6365cbd
 
cdccfc2
 
 
 
 
748e698
 
cdccfc2
b3e9924
 
cdccfc2
 
 
43ed166
94e2dc3
748e698
 
cdccfc2
ee8b4f1
 
 
 
 
 
 
 
 
 
 
cdccfc2
43ed166
 
 
cdccfc2
43ed166
 
cdccfc2
43ed166
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from fastai.vision.all import *

# MODELS_PATH = Path('./models')
# EXAMPLES_PATH = Path('./examples')

# Required function expected by fastai learn object
# it wasn't exported as a part of the pickle
# as it was defined externally to the learner object
# during the training time dataloaders setup
# def label_func(filepath):
#     return filepath.parent.name

LEARN = load_learner('Garbage_Classification.pkl')
# LEARN = load_learner('Garbage_Classification (1).pkl')
LABELS = LEARN.dls.vocab

def gradio_predict(img):
    img = img.resize((512, 512))
    # img = PILImage.create(img)
    pred, pred_idx, probs = LEARN.predict(img)
    return dict(zip(LABELS, map(float,probs)))

# with open('gradio_article.md') as f:
#     article = f.read()

# interface_options = {
#     "title": "Food Image Classifier (Food-101|ResNet50|fast.ai)",
#     "description": "",
#     "article": article,
#     "examples" : [f'{EXAMPLES_PATH}/{f.name}' for f in EXAMPLES_PATH.iterdir()],
#     "layout": "horizontal",
#     "theme": "default",
# }

image = gr.Image(type="pil", label="Upload an image")
label = gr.Label()
examples = ['battery.jpg', 'biological.jpg', 'cardboard.jpg']

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

# demo = gr.Interface(fn=gradio_predict,
#                       inputs=gradio.Image(type="pil", label="Upload an image"),
#                       outputs=gradio.outputs.Label(num_top_classes=5),
#                       **interface_options)

# launch_options = {
#     "enable_queue": True,
#     "share": False, 
# }

# demo.launch(**launch_options)