dgomes03 commited on
Commit
178a0a5
·
verified ·
1 Parent(s): e202f19

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn.functional as F
3
+ import gradio as gr
4
+ import numpy as np
5
+ from PIL import Image
6
+ from model import CNN
7
+
8
+ # Load model
9
+ model = CNN()
10
+ model.load_state_dict(torch.load("pytorch_model.bin", map_location="cpu"))
11
+ model.eval()
12
+
13
+ # Prediction function
14
+ def predict_digit(image):
15
+ if image is None:
16
+ return "No image"
17
+ image = Image.fromarray(image).convert("L").resize((28, 28))
18
+ image = np.array(image) / 255.0
19
+ image = torch.tensor(image).unsqueeze(0).unsqueeze(0).float()
20
+ with torch.no_grad():
21
+ output = model(image)
22
+ probabilities = F.softmax(output, dim=1).numpy().flatten()
23
+ return {str(i): float(probabilities[i]) for i in range(10)}
24
+
25
+ # Interface (no 'tool', 'type', or other unsupported args)
26
+ gr.Interface(
27
+ fn=predict_digit,
28
+ inputs=gr.Image(label="Upload a digit image"),
29
+ outputs=gr.Label(num_top_classes=3),
30
+ title="Digit Classifier",
31
+ description="Upload a 28x28 grayscale image of a handwritten digit (0–9)."
32
+ ).launch()