louisedrumm commited on
Commit
f806c23
1 Parent(s): e4ba885

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -4
app.py CHANGED
@@ -1,7 +1,46 @@
 
 
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ import torch
3
  import gradio as gr
4
+ from torch import nn
5
 
6
+ LABELS = Path("class_names.txt").read_text().splitlines()
 
7
 
8
+ model = nn.Sequential(
9
+ nn.Conv2d(1, 32, 3, padding="same"),
10
+ nn.ReLU(),
11
+ nn.MaxPool2d(2),
12
+ nn.Conv2d(32, 64, 3, padding="same"),
13
+ nn.ReLU(),
14
+ nn.MaxPool2d(2),
15
+ nn.Conv2d(64, 128, 3, padding="same"),
16
+ nn.ReLU(),
17
+ nn.MaxPool2d(2),
18
+ nn.Flatten(),
19
+ nn.Linear(1152, 256),
20
+ nn.ReLU(),
21
+ nn.Linear(256, len(LABELS)),
22
+ )
23
+ state_dict = torch.load("pytorch_model.bin", map_location="cpu")
24
+ model.load_state_dict(state_dict, strict=False)
25
+ model.eval()
26
+
27
+
28
+ def predict(im):
29
+ x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.0
30
+ with torch.no_grad():
31
+ out = model(x)
32
+ probabilities = torch.nn.functional.softmax(out[0], dim=0)
33
+ values, indices = torch.topk(probabilities, 5)
34
+ return {LABELS[i]: v.item() for i, v in zip(indices, values)}
35
+
36
+ interface = gr.Interface(
37
+ predict,
38
+ inputs="sketchpad",
39
+ outputs="label",
40
+ theme="huggingface",
41
+ title="Sketch Recognition",
42
+ description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!",
43
+ article="<p style='text-align: center'>Sketch Recognition | Demo Model</p>",
44
+ live=True,
45
+ )
46
+ interface.launch(share=True)