File size: 853 Bytes
6497665
 
f20e7b8
 
 
 
6497665
f20e7b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6497665
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
import gradio as gr

import torch
from torch.nn import Softmax
from torchvision.io import read_image
from torchvision.transforms.functional import rgb_to_grayscale

from model import NeuralNet

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

model = NeuralNet().to(device)
model.load_state_dict(torch.load("models/model.pth"))


labels = [
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine"
]

def predict(image):
    img = read_image(image).to(device)
    img = rgb_to_grayscale(img) / 255
    pred = model(img)[0]
    prob = Softmax(dim=0)(pred).tolist()
    return {l:p for l, p in zip(labels, prob)}


iface = gr.Interface(
    fn=predict,
    inputs=gr.Image(label="Upload", type="filepath", shape=(28, 28)),
    outputs=gr.Label(num_top_classes=10),
    )
iface.launch()