beunique commited on
Commit
715cbbf
1 Parent(s): bb20efe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio
2
+ from fastai.vision.all import *
3
+
4
+ MODELS_PATH = Path('./models')
5
+ EXAMPLES_PATH = Path('./examples')
6
+
7
+ # Required function expected by fastai learn object
8
+ # it wasn't exported as a part of the pickle
9
+ # as it was defined externally to the learner object
10
+ # during the training time dataloaders setup
11
+ def label_func(filepath):
12
+ return filepath.parent.name
13
+
14
+ LEARN = load_learner(MODELS_PATH/'flowers-fruits-resnet50-model.pkl')
15
+ LABELS = LEARN.dls.vocab
16
+
17
+ def gradio_predict(img):
18
+ img = PILImage.create(img)
19
+ _pred, _pred_idx, probs = LEARN.predict(img)
20
+ labels_probs = {LABELS[i]: float(probs[i]) for i, _ in enumerate(LABELS)}
21
+ return labels_probs
22
+
23
+ with open('gradio_article.md') as f:
24
+ article = f.read()
25
+
26
+ interface_options = {
27
+ "title": "Food Image Classifier (Food-101|ResNet50|fast.ai)",
28
+ "description": "A Flowers-Fruits image classifier trained on the 'https://duckduckgo.com/' dataset, using ResNet50 via fast.ai.",
29
+ "article": article,
30
+ "examples" : [f'{EXAMPLES_PATH}/{f.name}' for f in EXAMPLES_PATH.iterdir()],
31
+ "layout": "horizontal",
32
+ "theme": "default",
33
+ }
34
+
35
+ demo = gradio.Interface(fn=gradio_predict,
36
+ inputs=gradio.inputs.Image(shape=(512, 512)),
37
+ outputs=gradio.outputs.Label(num_top_classes=5),
38
+ **interface_options)
39
+
40
+ launch_options = {
41
+ "enable_queue": True,
42
+ "share": False,
43
+ }
44
+
45
+ demo.launch(**launch_options)