Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,67 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
try:
|
| 6 |
+
import torch
|
| 7 |
+
except ImportError:
|
| 8 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install",
|
| 9 |
+
"torch==2.0.1+cpu",
|
| 10 |
+
"torchvision==0.15.2+cpu",
|
| 11 |
+
"-f", "https://download.pytorch.org/whl/torch_stable.html"])
|
| 12 |
+
|
| 13 |
+
import torch
|
| 14 |
+
import torch.nn as nn
|
| 15 |
+
import torchvision.transforms as transforms
|
| 16 |
+
from PIL import Image
|
| 17 |
+
import gradio as gr
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
class ModifiedLargeNet(nn.Module):
|
| 22 |
+
def __init__(self):
|
| 23 |
+
super(ModifiedLargeNet, self).__init__()
|
| 24 |
+
self.name = "modified_large"
|
| 25 |
+
self.fc1 = nn.Linear(128 * 128 * 3, 256)
|
| 26 |
+
self.fc2 = nn.Linear(256, 128)
|
| 27 |
+
self.fc3 = nn.Linear(128, 3) # 3 classes: Rope, Hammer, Other
|
| 28 |
+
|
| 29 |
+
def forward(self, x):
|
| 30 |
+
x = x.view(-1, 128 * 128 * 3)
|
| 31 |
+
x = torch.relu(self.fc1(x))
|
| 32 |
+
x = torch.relu(self.fc2(x))
|
| 33 |
+
x = self.fc3(x)
|
| 34 |
+
return x
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
model = ModifiedLargeNet()
|
| 38 |
+
model.load_state_dict(torch.load("modified_large_net.pt", map_location=torch.device("cpu")))
|
| 39 |
+
model.eval()
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
transform = transforms.Compose([
|
| 43 |
+
transforms.Resize((128, 128)),
|
| 44 |
+
transforms.ToTensor(),
|
| 45 |
+
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def predict(image):
|
| 50 |
+
|
| 51 |
+
image = transform(image).unsqueeze(0)
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
outputs = model(image)
|
| 54 |
+
probabilities = torch.softmax(outputs, dim=1).numpy()[0]
|
| 55 |
+
classes = ["Rope", "Hammer", "Other"]
|
| 56 |
+
return {cls: float(prob) for cls, prob in zip(classes, probabilities)}
|
| 57 |
+
|
| 58 |
+
interface = gr.Interface(
|
| 59 |
+
fn=predict,
|
| 60 |
+
inputs=gr.Image(type="pil"),
|
| 61 |
+
outputs=gr.Label(num_top_classes=3),
|
| 62 |
+
title="Mechanical Tools Classifier",
|
| 63 |
+
description="Upload an image of a tool to classify it as 'Rope', 'Hammer', or 'Other'.",
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
if __name__ == "__main__":
|
| 67 |
+
interface.launch()
|