Safi029 commited on
Commit
7832262
·
verified ·
1 Parent(s): 73b704b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -3,23 +3,24 @@ from ultralytics import YOLO
3
  from huggingface_hub import hf_hub_download
4
  from PIL import Image
5
  import torch
 
6
 
7
- # Download the model file from Hugging Face
8
- model_path = hf_hub_download(repo_id="Safi029/ABD-model", filename="ABD.pt")
 
9
 
10
- # Explicitly load with weights_only=False
11
- ckpt = torch.load(model_path, map_location="cpu", weights_only=False)
12
 
13
- # Now manually initialize YOLO model from checkpoint
14
- model = YOLO()
15
- model.model.load_state_dict(ckpt['model'].float().state_dict())
16
 
17
- # Function to run detection
18
  def detect_structure(image):
19
  results = model(image)
20
  return Image.fromarray(results[0].plot())
21
 
22
- # Gradio interface
23
  demo = gr.Interface(
24
  fn=detect_structure,
25
  inputs=gr.Image(type="pil"),
 
3
  from huggingface_hub import hf_hub_download
4
  from PIL import Image
5
  import torch
6
+ import torch.serialization
7
 
8
+ # 🛡️ ALLOW custom classes in checkpoint (required for torch >=2.6)
9
+ from ultralytics.nn.tasks import DetectionModel
10
+ torch.serialization.add_safe_globals([DetectionModel])
11
 
12
+ # 📥 Download model from Hugging Face repo
13
+ model_path = hf_hub_download(repo_id="Safi029/ABD-model", filename="ABD.pt")
14
 
15
+ # Load full model (not weights only)
16
+ model = YOLO(model_path)
 
17
 
18
+ # 🔍 Detection function
19
  def detect_structure(image):
20
  results = model(image)
21
  return Image.fromarray(results[0].plot())
22
 
23
+ # 🎛️ Gradio UI
24
  demo = gr.Interface(
25
  fn=detect_structure,
26
  inputs=gr.Image(type="pil"),