Zai commited on
Commit
e34d96b
·
1 Parent(s): 765c0c2
Files changed (5) hide show
  1. __pycache__/model.cpython-310.pyc +0 -0
  2. app.py +29 -0
  3. mnist_model.pth +3 -0
  4. model.py +36 -0
  5. requirements.txt +0 -0
__pycache__/model.cpython-310.pyc ADDED
Binary file (1.36 kB). View file
 
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from model import Net, predict
3
+ import torch
4
+ import torchvision.transforms as transforms
5
+ from PIL import Image
6
+
7
+ model = Net()
8
+ model.load_state_dict(torch.load("mnist_model.pth", map_location=torch.device("cpu")))
9
+ model.eval()
10
+
11
+ transform = transforms.Compose([
12
+ transforms.Grayscale(), # Convert to grayscale if needed
13
+ transforms.Resize((28, 28)), # Fix: pass size as a tuple
14
+ transforms.ToTensor() # Convert to a PyTorch tensor
15
+ ])
16
+
17
+
18
+ def predict_image(image):
19
+ input_tensors = transform(Image.fromarray(image)).unsqueeze(0)
20
+
21
+ result = predict(model,input_tensors)
22
+
23
+ return result
24
+
25
+
26
+ app = gr.Interface(predict_image, gr.Image(), "text")
27
+
28
+
29
+ app.launch()
mnist_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a49b6e2a632bf6efd53d49aab0ec533481c431d7404429b6a6147221c942d0f7
3
+ size 1689904
model.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from torch import nn
3
+
4
+
5
+ class Net(nn.Module):
6
+ def __init__(self):
7
+ super(Net, self).__init__()
8
+ self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)
9
+ self.relu = nn.ReLU()
10
+ self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
11
+ self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)
12
+ self.flatten = nn.Flatten()
13
+ self.fc1 = nn.Linear(64 * 7 * 7, 128)
14
+ self.fc2 = nn.Linear(128, 10)
15
+
16
+ def forward(self, x):
17
+ x = self.conv1(x)
18
+ x = self.relu(x)
19
+ x = self.maxpool(x)
20
+ x = self.conv2(x)
21
+ x = self.relu(x)
22
+ x = self.maxpool(x)
23
+ x = self.flatten(x)
24
+ x = self.fc1(x)
25
+ x = self.relu(x)
26
+ x = self.fc2(x)
27
+ return x
28
+
29
+
30
+ def predict(model, image):
31
+ model.eval()
32
+ with torch.no_grad():
33
+ output = model(image)
34
+
35
+ result = torch.argmax(output,dim=1).item()
36
+ return result
requirements.txt ADDED
File without changes