Wootang01 commited on
Commit
cdfbbe4
·
1 Parent(s): 0cfe9a1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from torch import nn
4
+ from pathlib import Path
5
+
6
+ model = nn.Sequential(
7
+ nn.Conv2d(1, 32, 3, padding='same'),
8
+ nn.ReLU(),
9
+ nn.MaxPool2d(2),
10
+ nn.Conv2d(32, 64, 3, padding='same'),
11
+ nn.ReLU(),
12
+ nn.MaxPool2d(2),
13
+ nn.Conv2d(64, 128, 3, padding='same'),
14
+ nn.ReLU(),
15
+ nn.MaxPool2d(2),
16
+ nn.Flatten(),
17
+ nn.Linear(1152, 256),
18
+ nn.ReLU(),
19
+ nn.Linear(256, len(LABELS)),
20
+ )
21
+ state_dict = torch.load('pytorch_model.bin', map_location='cpu')
22
+ model.load_state_dict(state_dict, strict=False)
23
+ model.eval()
24
+
25
+ LABELS = Path('class_names.txt').read_text().splitlines()
26
+
27
+ def predict(img):
28
+ x = torch.tensor(img, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.
29
+ with torch.no_grad():
30
+ out = model(x)
31
+ probabilities = torch.nn.functional.softmax(out[0], dim=0)
32
+ values, indices = torch.topk(probabilities, 3)
33
+ confidences = {LABELS[i]: v.item() for i, v in zip(indices, values)}
34
+ return confidences
35
+
36
+ gr.Interface(fn=predict,
37
+ inputs="sketchpad",
38
+ outputs="label",
39
+ live=True).launch(debug=True)