Update app.py
Browse files
app.py
CHANGED
|
@@ -2,23 +2,31 @@ import gradio as gr
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# ✅
|
|
|
|
|
|
|
|
|
|
| 7 |
model_path = hf_hub_download(repo_id="Safi029/ABD-model", filename="ABD.pt")
|
|
|
|
|
|
|
| 8 |
model = YOLO(model_path)
|
| 9 |
|
| 10 |
def detect_structure(image):
|
| 11 |
results = model(image)
|
| 12 |
-
|
| 13 |
-
return Image.fromarray(annotated_img)
|
| 14 |
|
| 15 |
demo = gr.Interface(
|
| 16 |
fn=detect_structure,
|
| 17 |
inputs=gr.Image(type="pil"),
|
| 18 |
outputs=gr.Image(type="pil"),
|
| 19 |
-
title="YOLO Molecular Structure
|
| 20 |
-
description="Upload a molecular
|
| 21 |
)
|
| 22 |
|
| 23 |
demo.launch()
|
| 24 |
|
|
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from huggingface_hub import hf_hub_download
|
| 4 |
from PIL import Image
|
| 5 |
+
import torch
|
| 6 |
+
from torch.serialization import add_safe_globals
|
| 7 |
+
from ultralytics.nn.tasks import DetectionModel # Needed for safe loading
|
| 8 |
|
| 9 |
+
# ✅ Allow loading DetectionModel (trusting your own model)
|
| 10 |
+
add_safe_globals({'DetectionModel': DetectionModel})
|
| 11 |
+
|
| 12 |
+
# ✅ Download model from Hugging Face Hub
|
| 13 |
model_path = hf_hub_download(repo_id="Safi029/ABD-model", filename="ABD.pt")
|
| 14 |
+
|
| 15 |
+
# ✅ Load model with trust
|
| 16 |
model = YOLO(model_path)
|
| 17 |
|
| 18 |
def detect_structure(image):
|
| 19 |
results = model(image)
|
| 20 |
+
return Image.fromarray(results[0].plot())
|
|
|
|
| 21 |
|
| 22 |
demo = gr.Interface(
|
| 23 |
fn=detect_structure,
|
| 24 |
inputs=gr.Image(type="pil"),
|
| 25 |
outputs=gr.Image(type="pil"),
|
| 26 |
+
title="YOLO Molecular Structure Detector",
|
| 27 |
+
description="Upload a molecular image. YOLOv8 will detect atoms or bonds."
|
| 28 |
)
|
| 29 |
|
| 30 |
demo.launch()
|
| 31 |
|
| 32 |
+
|