Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,35 +0,0 @@
|
|
| 1 |
-
# app.py
|
| 2 |
-
import torch
|
| 3 |
-
import torch.nn.functional as F
|
| 4 |
-
import gradio as gr
|
| 5 |
-
import numpy as np
|
| 6 |
-
from PIL import Image
|
| 7 |
-
from model import CNN
|
| 8 |
-
|
| 9 |
-
# Load model
|
| 10 |
-
model = CNN()
|
| 11 |
-
model.load_state_dict(torch.load("pytorch_model.bin", map_location="cpu"))
|
| 12 |
-
model.eval()
|
| 13 |
-
|
| 14 |
-
# Inference function
|
| 15 |
-
def predict_digit(image):
|
| 16 |
-
image = image.convert("L").resize((28, 28)) # Convert to grayscale
|
| 17 |
-
image = np.array(image) / 255.0 # Normalize
|
| 18 |
-
image = torch.tensor(image).unsqueeze(0).unsqueeze(0).float() # (1, 1, 28, 28)
|
| 19 |
-
with torch.no_grad():
|
| 20 |
-
logits = model(image)
|
| 21 |
-
probs = F.softmax(logits, dim=1).numpy().flatten()
|
| 22 |
-
predicted = np.argmax(probs)
|
| 23 |
-
return {str(i): float(probs[i]) for i in range(10)}
|
| 24 |
-
|
| 25 |
-
# Gradio UI
|
| 26 |
-
interface = gr.Interface(
|
| 27 |
-
fn=predict_digit,
|
| 28 |
-
inputs=gr.Image(type="pil", shape=(280, 280), tool="editor"),
|
| 29 |
-
outputs=gr.Label(num_top_classes=3),
|
| 30 |
-
title="Handwritten Digit Classifier",
|
| 31 |
-
description="Draw a digit or upload a digit image."
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
if __name__ == "__main__":
|
| 35 |
-
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|