Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Load your model from your HF repo
|
| 7 |
+
model = torch.load(
|
| 8 |
+
hf_hub_download(repo_id="newtechdevng/detect", filename="your_model_file.pt"),
|
| 9 |
+
map_location="cpu"
|
| 10 |
+
)
|
| 11 |
+
model.eval()
|
| 12 |
+
|
| 13 |
+
# Define labels
|
| 14 |
+
labels = ["car", "bike", "mountain", "road"] # update to match your model
|
| 15 |
+
|
| 16 |
+
def predict(image):
|
| 17 |
+
# Preprocess
|
| 18 |
+
from torchvision import transforms
|
| 19 |
+
transform = transforms.Compose([
|
| 20 |
+
transforms.Resize((224, 224)),
|
| 21 |
+
transforms.ToTensor(),
|
| 22 |
+
])
|
| 23 |
+
tensor = transform(image).unsqueeze(0)
|
| 24 |
+
|
| 25 |
+
with torch.no_grad():
|
| 26 |
+
output = model(tensor)
|
| 27 |
+
predicted = torch.argmax(output, dim=1).item()
|
| 28 |
+
|
| 29 |
+
return labels[predicted]
|
| 30 |
+
|
| 31 |
+
gr.Interface(
|
| 32 |
+
fn=predict,
|
| 33 |
+
inputs=gr.Image(type="pil"),
|
| 34 |
+
outputs=gr.Label(),
|
| 35 |
+
title="Car/Bike/Mountain/Road Detector"
|
| 36 |
+
).launch()
|