File size: 1,204 Bytes
733611c
 
72751df
 
 
 
733611c
72751df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733611c
 
 
72751df
 
 
 
 
 
733611c
72751df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
733611c
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
import os
import joblib
import gradio as gr
import torch
import torch.nn as nn
from torchvision import models, transforms
from huggingface_hub import hf_hub_download



device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class_names = [
    'Baked Potato',
    'Burger',
    'Crispy Chicken',
    'Donut',
    'Fries',
    'Hot Dog',
    'Pizza',
    'Sandwich',
    'Taco',
    'Taquito'
 ]

file_path = hf_hub_download("abnordist/fastfoodResnet18_ft",
                            "model_fastfoodclf.pth")

model = models.resnet18(pretrained=True)
num_features = model.fc.in_features
model.fc = nn.Linear(num_features, len(class_names))

# model = model.to(device)
# load model from ./models
model.load_state_dict(torch.load(file_path))
model.eval()

def predict(inp):
    inp = transforms.ToTensor()(inp).unsqueeze(0)
    with torch.no_grad():
        prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
        confidences = {class_names[i]: float(prediction[i]) for i in range(len(class_names))}
    return confidences

app = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs=gr.Label(num_top_classes=10)    
)

# display the app
app.launch()