timothepearce commited on
Commit
a872736
1 Parent(s): 9ee3283

Add app.py & examples

Browse files
Files changed (4) hide show
  1. app.py +44 -4
  2. mnist_0.png +0 -0
  3. mnist_2.png +0 -0
  4. mnist_3.png +0 -0
app.py CHANGED
@@ -1,7 +1,47 @@
 
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
+ import torch
2
  import gradio as gr
3
+ from PIL import Image
4
+ from torch import nn
5
+ from torchvision import transforms
6
 
7
+ classes = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
 
8
 
9
+
10
+ class NeuralNetwork(nn.Module):
11
+ def __init__(self):
12
+ super(NeuralNetwork, self).__init__()
13
+ self.flatten = nn.Flatten()
14
+ self.linear_relu_stack = nn.Sequential(
15
+ nn.Linear(28 * 28, 784),
16
+ nn.ReLU(),
17
+ nn.Linear(784, 784),
18
+ nn.ReLU(),
19
+ nn.Linear(784, 10)
20
+ )
21
+
22
+ def forward(self, x):
23
+ x = self.flatten(x)
24
+ logits = self.linear_relu_stack(x)
25
+ return logits
26
+
27
+
28
+ model = NeuralNetwork()
29
+ model.load_state_dict(torch.load("model.pth", map_location=torch.device('cpu')))
30
+ model.eval()
31
+
32
+
33
+ def image_classifier(img_input):
34
+ img = Image.fromarray(img_input.astype('uint8'), 'RGB')
35
+ img = transforms.ToTensor()(img)
36
+
37
+ with torch.no_grad():
38
+ pred = model(img)[0]
39
+ pred -= pred.min()
40
+ pred /= pred.max()
41
+ return {classes[i]: float(pred[i]) for i in range(10)}
42
+
43
+
44
+ gr.Interface(fn=image_classifier,
45
+ inputs=gr.Image(shape=(28, 28)),
46
+ outputs="label",
47
+ examples=["mnist_0.png", "mnist_2.png", "mnist_3.png"]).launch()
mnist_0.png ADDED
mnist_2.png ADDED
mnist_3.png ADDED