Nawinkumar15 commited on
Commit
ad6c417
Β·
verified Β·
1 Parent(s): 695266b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -1,13 +1,21 @@
1
  import gradio as gr
2
- from ultralytics import YOLO
3
  import torch
4
- import ultralytics.nn.tasks # required for safe unpickling
 
 
 
 
5
 
6
- # πŸ›  Allow YOLOv8's custom model class to be deserialized
7
- torch.serialization.add_safe_globals([ultralytics.nn.tasks.DetectionModel])
 
 
8
 
9
- # βœ… Load the trained YOLOv8 model
10
- model = YOLO("best.pt") # Make sure best.pt is in the same directory
 
 
 
11
 
12
  # πŸ” Inference function
13
  def detect_objects(image):
@@ -15,11 +23,11 @@ def detect_objects(image):
15
  annotated_image = results[0].plot()
16
  return annotated_image
17
 
18
- # πŸŽ›οΈ Gradio interface
19
  description = """
20
- πŸ“‰ **Panel Fault D C Detection**
21
- Upload an image to detect panel faults using a YOLOv8 model trained on Roboflow.
22
- [View Dataset](https://universe.roboflow.com/naveen-kumar-9emxl/my-project-1-scnhw)
23
  """
24
 
25
  demo = gr.Interface(
 
1
  import gradio as gr
 
2
  import torch
3
+ from ultralytics import YOLO
4
+
5
+ # βœ… Allowlist required classes for safe loading
6
+ import ultralytics.nn.tasks
7
+ import torch.nn.modules.container
8
 
9
+ torch.serialization.add_safe_globals([
10
+ ultralytics.nn.tasks.DetectionModel,
11
+ torch.nn.modules.container.Sequential
12
+ ])
13
 
14
+ # βœ… Load the YOLOv8 model (from trusted source like Roboflow)
15
+ try:
16
+ model = YOLO("best.pt")
17
+ except Exception as e:
18
+ raise RuntimeError(f"Failed to load YOLOv8 model: {e}")
19
 
20
  # πŸ” Inference function
21
  def detect_objects(image):
 
23
  annotated_image = results[0].plot()
24
  return annotated_image
25
 
26
+ # πŸŽ›οΈ Gradio UI
27
  description = """
28
+ πŸ“‰ **Panel Fault D C Detection App**
29
+ Detect panel faults using a YOLOv8 model trained on Roboflow.
30
+ πŸ”— [Dataset Link](https://universe.roboflow.com/naveen-kumar-9emxl/my-project-1-scnhw)
31
  """
32
 
33
  demo = gr.Interface(