File size: 1,000 Bytes
bee17ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import torch
from torchvision import transforms

from model import ExampleModel

model_path = "model/animal_7.pth"
labels = ["bird", "cat", "dog", "horse"]
num_classes = len(labels)
device = torch.device("cuda:0" if torch.cuda.is_available() else "mps")


# Preprocess
preprocess = transforms.Compose([transforms.Resize((224, 224)), transforms.ToTensor()])

# Load Model
model = ExampleModel(num_classes=num_classes).to(device)
model.load_state_dict(torch.load(model_path, map_location=device))
model.eval()


# Prediction function
def predict(image):
    img_tensor = preprocess(image).unsqueeze(0).to(device)
    with torch.no_grad():
        prediction = model(img_tensor)
    prediction = torch.nn.functional.softmax(prediction, dim=1).squeeze()
    confidences = {labels[i]: float(prediction[i]) for i in range(num_classes)}
    return confidences


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