Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from torchvision import transforms
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# 1. Load your model (Ensure this matches your training architecture)
|
| 7 |
+
# Change 'models.resnet18' if you used a different one
|
| 8 |
+
from torchvision import models
|
| 9 |
+
model = models.resnet18()
|
| 10 |
+
model.fc = torch.nn.Linear(model.fc.in_features, 2)
|
| 11 |
+
model.load_state_dict(torch.load("tile_model.pt", map_location="cpu"))
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
# 2. Define labels based on your dataset folders
|
| 15 |
+
labels = ["Defect", "Normal"]
|
| 16 |
+
|
| 17 |
+
def predict(img):
|
| 18 |
+
transform = transforms.Compose([
|
| 19 |
+
transforms.Resize((224, 224)),
|
| 20 |
+
transforms.ToTensor(),
|
| 21 |
+
])
|
| 22 |
+
img = transform(img).unsqueeze(0)
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
prediction = torch.nn.functional.softmax(model(img)[0], dim=0)
|
| 25 |
+
confidences = {labels[i]: float(prediction[i]) for i in range(2)}
|
| 26 |
+
return confidences
|
| 27 |
+
|
| 28 |
+
# 3. Create the Interface
|
| 29 |
+
interface = gr.Interface(
|
| 30 |
+
fn=predict,
|
| 31 |
+
inputs=gr.Image(type="pil"),
|
| 32 |
+
outputs=gr.Label(num_top_classes=2),
|
| 33 |
+
title="Wall/Floor Tile Defect Inspector"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
interface.launch()
|