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