hp1318 commited on
Commit
35c9708
·
verified ·
1 Parent(s): 7b92ffb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torchvision.transforms as transforms
3
+ from PIL import Image
4
+ import gradio as gr
5
+
6
+
7
+ classes = ['airplane', 'automobile', 'bird', 'cat', 'deer',
8
+ 'dog', 'frog', 'horse', 'ship', 'truck']
9
+
10
+
11
+ transform = transforms.Compose([
12
+ transforms.Resize((32, 32)),
13
+ transforms.ToTensor(),
14
+ transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
15
+ ])
16
+
17
+
18
+ model = torch.load('model.pth', map_location=torch.device('cpu'))
19
+ model.eval()
20
+
21
+
22
+ def predict(image):
23
+ image = transform(image).unsqueeze(0)
24
+ with torch.no_grad():
25
+ output = model(image)
26
+ _, predicted = torch.max(output, 1)
27
+ return classes[predicted.item()]
28
+
29
+
30
+ interface = gr.Interface(fn=predict,
31
+ inputs=gr.Image(type="pil"),
32
+ outputs="label",
33
+ title="CIFAR-10 Image Classification with ViT")
34
+
35
+
36
+ interface.launch()