Nawinkumar15 commited on
Commit
817ac03
·
verified ·
1 Parent(s): 89f8629

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -39
app.py CHANGED
@@ -1,48 +1,20 @@
1
  import gradio as gr
2
- import torch
3
  from ultralytics import YOLO
4
 
5
- # ✅ Import modules needed for safe unpickling
6
- import ultralytics.nn.tasks
7
- import ultralytics.nn.modules.conv
8
- import torch.nn.modules.container
9
- import torch.nn.modules.conv
10
 
11
- # ✅ Add all required classes to the safe globals list
12
- torch.serialization.add_safe_globals([
13
- ultralytics.nn.tasks.DetectionModel,
14
- ultralytics.nn.modules.conv.Conv,
15
- torch.nn.modules.container.Sequential,
16
- torch.nn.modules.conv.Conv2d,
17
- ])
18
-
19
- # ✅ Load YOLOv8 model
20
- try:
21
- print("⚙️ Loading YOLOv8 model...")
22
- model = YOLO("best.pt")
23
- except Exception as e:
24
- raise RuntimeError(f"❌ Failed to load YOLOv8 model:\n{e}")
25
-
26
- # 🧠 Inference function
27
- def detect_objects(image):
28
  results = model(image)
29
- annotated_image = results[0].plot()
30
- return annotated_image
31
 
32
- # 🎛️ Gradio interface
33
- description = """
34
- 📉 **Panel Fault D C Detection**
35
- Upload an image to detect panel faults using your custom-trained YOLOv8 model.
36
- 🔗 [View Dataset](https://universe.roboflow.com/naveen-kumar-9emxl/my-project-1-scnhw)
37
- """
38
-
39
- demo = gr.Interface(
40
- fn=detect_objects,
41
  inputs=gr.Image(type="pil"),
42
- outputs=gr.Image(type="pil"),
43
- title="Panel Fault D C",
44
- description=description
45
  )
46
 
47
- if __name__ == "__main__":
48
- demo.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
 
11
+ iface = gr.Interface(
12
+ fn=predict,
 
 
 
 
 
 
 
13
  inputs=gr.Image(type="pil"),
14
+ outputs=gr.Image(type="pil", label="Detected Objects"),
15
+ title="Roboflow YOLOv8 Detector",
16
+ description="Upload an image to detect objects using your YOLOv8 model trained with Roboflow."
17
  )
18
 
19
+ iface.launch()
20
+