Nawinkumar15 commited on
Commit
c689755
·
verified ·
1 Parent(s): d4b9129

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -26
app.py CHANGED
@@ -1,33 +1,10 @@
1
  import gradio as gr
2
- import torch
3
- import os
4
-
5
  from ultralytics import YOLO
6
- model = YOLO("best.pt")
7
-
8
- # --- Configuration ---
9
- MODEL_REPO_ID = "Nawinkumar15/my-yolov8-model" # Replace with YOUR model repository ID
10
- MODEL_FILENAME = "best.pt"
11
- CACHE_DIR = "model_cache"
12
 
13
- # Ensure cache directory exists
14
- os.makedirs(CACHE_DIR, exist_ok=True)
15
-
16
- # Function to load the YOLOv8 model from Hugging Face Hub
17
- def load_model():
18
- try:
19
- model = torch.hub.load('ultralytics/yolov8', 'custom', path=f'{CACHE_DIR}/{MODEL_FILENAME}', trust_repo=True)
20
- return model
21
- except Exception as e:
22
- print(f"Error loading model: {e}")
23
- return None
24
-
25
- # Load the model when the app starts
26
- model = load_model()
27
 
28
  def predict(image):
29
- if model is None:
30
- return "Model not loaded."
31
  results = model(image)
32
  return results[0].plot()
33
 
@@ -39,4 +16,4 @@ iface = gr.Interface(
39
  description="Upload an image to detect objects using your YOLOv8 model trained with Roboflow."
40
  )
41
 
42
- iface.launch()
 
1
  import gradio as gr
 
 
 
2
  from ultralytics import YOLO
 
 
 
 
 
 
3
 
4
+ # Load model directly from local file (same folder as app.py)
5
+ model = YOLO("best.pt")
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def predict(image):
 
 
8
  results = model(image)
9
  return results[0].plot()
10
 
 
16
  description="Upload an image to detect objects using your YOLOv8 model trained with Roboflow."
17
  )
18
 
19
+ iface.launch()