Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn.functional as F
|
3 |
+
import torchvision.transforms as transforms
|
4 |
+
from PIL import Image
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
model_path = hf_hub_download(repo_id="Ayamohamed/DiaClassification", filename="dia_none_classifier_full.pth")
|
8 |
+
model = torch.load(model_path, map_location=torch.device("cpu"))
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
transform = transforms.Compose([
|
12 |
+
transforms.Resize((224, 224)),
|
13 |
+
transforms.ToTensor(),
|
14 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
15 |
+
])
|
16 |
+
def predict(image):
|
17 |
+
image = transform(image).unsqueeze(0)
|
18 |
+
with torch.no_grad():
|
19 |
+
output = local_model(image)
|
20 |
+
probabilities = F.softmax(output, dim=1)
|
21 |
+
class_idx = torch.argmax(probabilities, dim=1).item()
|
22 |
+
return "Diagram" if class_idx == 0 else "Not Diagram"
|
23 |
+
|
24 |
+
gr.Interface(
|
25 |
+
fn=predict,
|
26 |
+
inputs=gr.Image(type="pil"),
|
27 |
+
outputs="text",
|
28 |
+
title="Diagram Classifier"
|
29 |
+
).launch(share=True)
|