Vidyuth commited on
Commit
ea1c882
1 Parent(s): a1763fe

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Pictionary_with_Gradio.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/17zR3z7tyss0M9EpMfcM7DSzG-WdE3-Kc
8
+ """
9
+
10
+ import torch
11
+ from torch import nn
12
+ from pathlib import Path
13
+
14
+ !pip3 install gradio
15
+
16
+ LABELS=Path('class_names.txt').read_text().splitlines()
17
+
18
+ model = nn.Sequential(
19
+ nn.Conv2d(1, 32, 3, padding='same'),
20
+ nn.ReLU(),
21
+ nn.MaxPool2d(2),
22
+ nn.Conv2d(32, 64, 3, padding='same'),
23
+ nn.ReLU(),
24
+ nn.MaxPool2d(2),
25
+ nn.Conv2d(64, 128, 3, padding='same'),
26
+ nn.ReLU(),
27
+ nn.MaxPool2d(2),
28
+ nn.Flatten(),
29
+ nn.Linear(1152, 256),
30
+ nn.ReLU(),
31
+ nn.Linear(256, len(LABELS)),
32
+ )
33
+
34
+ state_dict=torch.load('pytorch_model.bin',map_location='cpu')
35
+
36
+ import gradio as gr
37
+
38
+ model.load_state_dict(state_dict, strict=False)
39
+ model.eval()
40
+
41
+ def predict(im):
42
+ x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.
43
+
44
+ with torch.no_grad():
45
+ out = model(x)
46
+
47
+ probabilities = torch.nn.functional.softmax(out[0], dim=0)
48
+
49
+ values, indices = torch.topk(probabilities, 5)
50
+
51
+ return {LABELS[i]: v.item() for i, v in zip(indices, values)}
52
+
53
+ interface=gr.Interface(
54
+ predict,
55
+ inputs="sketchpad",
56
+ outputs='label',
57
+ theme="huggingface",
58
+ title="Sketch Recognition",
59
+ description="Sketch something for the model to guess in realtime",
60
+ article = "<p style='text-align: center'>Sketch Recognition | Demo Model</p>",
61
+ live=True)
62
+ interface.launch(share=True)
63
+