Sparsh141 commited on
Commit
2b32461
·
verified ·
1 Parent(s): 27377c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py CHANGED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import gradio as gr
4
+ import numpy as np
5
+
6
+ class CNN(nn.Module):
7
+ def __init__(self):
8
+ super().__init__()
9
+
10
+ self.conv = nn.Sequential(
11
+ nn.Conv2d(1,32,3,padding=1),
12
+ nn.ReLU(),
13
+ nn.MaxPool2d(2),
14
+
15
+ nn.Conv2d(32,64,3,padding=1),
16
+ nn.ReLU(),
17
+ nn.MaxPool2d(2)
18
+ )
19
+
20
+ self.fc = nn.Sequential(
21
+ nn.Linear(64*7*7,128),
22
+ nn.ReLU(),
23
+ nn.Linear(128,10)
24
+ )
25
+
26
+ def forward(self,x):
27
+ x = self.conv(x)
28
+ x = x.view(x.size(0),-1)
29
+ x = self.fc(x)
30
+ return x
31
+
32
+ model = CNN()
33
+ model.load_state_dict(torch.load("model.pth",map_location="cpu"))
34
+ model.eval()
35
+
36
+ def predict(image):
37
+
38
+ image = np.array(image)
39
+ image = image/255.0
40
+ image = image.reshape(1,1,28,28)
41
+
42
+ image = torch.tensor(image,dtype=torch.float32)
43
+
44
+ output = model(image)
45
+ pred = torch.argmax(output,1).item()
46
+
47
+ return pred
48
+
49
+ interface = gr.Interface(
50
+ fn=predict,
51
+ inputs=gr.Image(shape=(28,28),image_mode="L"),
52
+ outputs="label",
53
+ title="Handwritten Digit Classifier"
54
+ )
55
+
56
+ interface.launch()