Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
|
5 |
+
# --- Configuration ---
|
6 |
+
MODEL_REPO_ID = "Nawinkumar15/my-yolov8-model" # Replace with YOUR model repository ID
|
7 |
+
MODEL_FILENAME = "best.pt"
|
8 |
+
CACHE_DIR = "model_cache"
|
9 |
+
|
10 |
+
# Ensure cache directory exists
|
11 |
+
os.makedirs(CACHE_DIR, exist_ok=True)
|
12 |
+
|
13 |
+
# Function to load the YOLOv8 model from Hugging Face Hub
|
14 |
+
def load_model():
|
15 |
+
try:
|
16 |
+
model = torch.hub.load('ultralytics/yolov8', 'custom', path=f'{CACHE_DIR}/{MODEL_FILENAME}', trust_repo=True)
|
17 |
+
return model
|
18 |
+
except Exception as e:
|
19 |
+
print(f"Error loading model: {e}")
|
20 |
+
return None
|
21 |
+
|
22 |
+
# Load the model when the app starts
|
23 |
+
model = load_model()
|
24 |
+
|
25 |
+
def predict(image):
|
26 |
+
if model is None:
|
27 |
+
return "Model not loaded."
|
28 |
+
results = model(image)
|
29 |
+
return results[0].plot()
|
30 |
+
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=predict,
|
33 |
+
inputs=gr.Image(type="pil"),
|
34 |
+
outputs=gr.Image(type="pil", label="Detected Objects"),
|
35 |
+
title="Roboflow YOLOv8 Detector",
|
36 |
+
description="Upload an image to detect objects using your YOLOv8 model trained with Roboflow."
|
37 |
+
)
|
38 |
+
|
39 |
+
iface.launch()
|