Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import torchvision.models as models
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
|
| 8 |
+
# Classes must match your training dataset
|
| 9 |
+
class_names = [ "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" ]
|
| 10 |
+
|
| 11 |
+
# Transform (same as training)
|
| 12 |
+
transform = transforms.Compose([
|
| 13 |
+
transforms.Resize((224,224)),
|
| 14 |
+
transforms.ToTensor(),
|
| 15 |
+
transforms.Normalize([0.5]*3, [0.5]*3)
|
| 16 |
+
])
|
| 17 |
+
|
| 18 |
+
# Load model
|
| 19 |
+
def load_model():
|
| 20 |
+
model = models.mobilenet_v2(pretrained=False)
|
| 21 |
+
model.classifier[1] = nn.Linear(model.classifier[1].in_features, len(class_names))
|
| 22 |
+
model.load_state_dict(torch.load("isl_model.pth", map_location="cpu"))
|
| 23 |
+
model.eval()
|
| 24 |
+
return model
|
| 25 |
+
|
| 26 |
+
model = load_model()
|
| 27 |
+
|
| 28 |
+
# Prediction function
|
| 29 |
+
def predict(img: Image.Image):
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
x = transform(img).unsqueeze(0)
|
| 32 |
+
out = model(x)
|
| 33 |
+
return class_names[out.argmax(1).item()]
|
| 34 |
+
|
| 35 |
+
# Gradio interface
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=predict,
|
| 38 |
+
inputs=gr.Image(type="pil"),
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="ISL Alphabet Recognition",
|
| 41 |
+
description="Upload a hand sign image (A–Z) to get the predicted letter."
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
demo.launch()
|