File size: 1,132 Bytes
7b3a7b6
 
 
 
 
 
 
ca0cc81
 
7b3a7b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93cd6b7
7b3a7b6
 
 
 
 
 
 
 
c50bffa
 
 
7b3a7b6
 
 
34158cf
 
 
 
 
 
 
 
 
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
# Load the model from gluten.py
# Create a gradio interface for the model
# Run the model on the interface

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

import fastai


# This method is required for unpickling
def label_for_path(path):
    return {
        'bread': 'glutenful',
        'gluten_free': 'glutenfree',
        'man-made': 'glutenfree',
        'fruit': 'glutenfree',
        'malt_beverage': 'glutenful',
        'meat': 'glutenfree',
        'soy_sauce': 'glutenful',
        'tamari': 'glutenfree'
    }[str(list(path.parts)[1])]


learn_inf = load_learner(Path() / 'gluten.pkl')
categories = ('gluten-free', 'glutenful')


def classify_image(img):
    img = PILImage.create(img)
    pred, pred_idx, probs = learn_inf.predict(img)
    return {categories[i]: float(probs[i]) for i in range(len(categories))}


image = gr.Image()
label = gr.Label()

# examples = all files in the /examples folder
examples = [f"examples/{i}" for i in os.listdir("examples")]

intf = gr.Interface(
    fn=classify_image,
    inputs=image,
    outputs=label,
    examples=examples
)

if __name__ == "__main__":
    intf.launch()