jflo commited on
Commit
dec1eb1
1 Parent(s): 3aa461a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -2
app.py CHANGED
@@ -1,3 +1,49 @@
1
- import os
2
 
3
- os.system("pip3 install --upgrade gradio")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
 
3
+ import torch
4
+ import torch.nn.functional as F
5
+ from torchvision import transforms
6
+
7
+ # load model
8
+ model = torch.jit.load("food_classifier.ptl")
9
+
10
+ # Transformations that will be applied
11
+ the_transform = transforms.Compose([
12
+ transforms.Resize((224,224)),
13
+ transforms.CenterCrop((224,224)),
14
+ transforms.ToTensor(),
15
+ transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])
16
+ ])
17
+
18
+ # Classes
19
+ class_names = ['Apple Pie','Bibimbap','Cannoli','Edamame','Falafel','French Toast','Ice Cream','Ramen','Sushi','Tiramisu']
20
+
21
+ # Returns transformed image
22
+ def transform_img(img):
23
+ return the_transform(img)
24
+
25
+ # Returns string with class and probability
26
+ def classify_img(img):
27
+ # Applying transformation to the image
28
+ model_img = transform_img(img)
29
+ model_img = model_img.view(1,3,224,224)
30
+
31
+ # Running image through the model
32
+ model.eval()
33
+ with torch.no_grad():
34
+ result = model(model_img)
35
+
36
+ # Converting values to softmax values
37
+ result = F.softmax(result,dim=1)
38
+ probability = round(result[0][result.argmax()].item() * 100, 2)
39
+
40
+ # Returning class name and probability
41
+ return f'{class_names[result.argmax()]} : {probability}% confident'
42
+
43
+ demo = gr.Interface(classify_img,
44
+ inputs = gr.inputs.Image(type="pil"),
45
+ outputs = "text",
46
+ title = "Food Classifier!",
47
+ description="Insert food you would like to classify! <br> Categories: Apple Pie, Bibimbap, Cannoli, Edamame, Falafel, French Toast, Ice Cream, Ramen, Sushi, Tiramisu")
48
+
49
+ demo.launch(inline=False)