Update app.py
Browse files
app.py
CHANGED
|
@@ -5,34 +5,49 @@ from PIL import Image
|
|
| 5 |
import torch
|
| 6 |
import torch.serialization
|
| 7 |
|
| 8 |
-
# β
|
| 9 |
-
from
|
| 10 |
from ultralytics.nn.modules.conv import Conv
|
| 11 |
-
from
|
|
|
|
| 12 |
|
|
|
|
| 13 |
torch.serialization.add_safe_globals([
|
| 14 |
-
DetectionModel,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
])
|
| 16 |
|
| 17 |
-
# π₯
|
| 18 |
model_path = hf_hub_download(repo_id="Safi029/ABD-model", filename="ABD.pt")
|
|
|
|
|
|
|
| 19 |
model = YOLO(model_path)
|
| 20 |
|
| 21 |
-
# π§
|
| 22 |
def detect_structure(image):
|
| 23 |
results = model(image)
|
| 24 |
return Image.fromarray(results[0].plot())
|
| 25 |
|
| 26 |
-
# ποΈ
|
| 27 |
demo = gr.Interface(
|
| 28 |
fn=detect_structure,
|
| 29 |
inputs=gr.Image(type="pil"),
|
| 30 |
outputs=gr.Image(type="pil"),
|
| 31 |
title="YOLO Molecular Detector",
|
| 32 |
-
description="Upload a
|
| 33 |
)
|
| 34 |
|
| 35 |
demo.launch()
|
| 36 |
|
| 37 |
|
| 38 |
|
|
|
|
|
|
| 5 |
import torch
|
| 6 |
import torch.serialization
|
| 7 |
|
| 8 |
+
# β
Import all model components that may appear in custom YOLOv8 models
|
| 9 |
+
from torch.nn import Sequential, Conv2d, BatchNorm2d, SiLU, ReLU, LeakyReLU
|
| 10 |
from ultralytics.nn.modules.conv import Conv
|
| 11 |
+
from ultralytics.nn.modules.block import Bottleneck, C2f, SPPF
|
| 12 |
+
from ultralytics.nn.tasks import DetectionModel
|
| 13 |
|
| 14 |
+
# β
Add all components to safe globals
|
| 15 |
torch.serialization.add_safe_globals([
|
| 16 |
+
DetectionModel,
|
| 17 |
+
Sequential,
|
| 18 |
+
Conv,
|
| 19 |
+
Conv2d,
|
| 20 |
+
BatchNorm2d,
|
| 21 |
+
SiLU,
|
| 22 |
+
ReLU,
|
| 23 |
+
LeakyReLU,
|
| 24 |
+
Bottleneck,
|
| 25 |
+
C2f,
|
| 26 |
+
SPPF,
|
| 27 |
])
|
| 28 |
|
| 29 |
+
# π₯ Download model
|
| 30 |
model_path = hf_hub_download(repo_id="Safi029/ABD-model", filename="ABD.pt")
|
| 31 |
+
|
| 32 |
+
# β
Load model safely
|
| 33 |
model = YOLO(model_path)
|
| 34 |
|
| 35 |
+
# π§ Inference function
|
| 36 |
def detect_structure(image):
|
| 37 |
results = model(image)
|
| 38 |
return Image.fromarray(results[0].plot())
|
| 39 |
|
| 40 |
+
# ποΈ Gradio UI
|
| 41 |
demo = gr.Interface(
|
| 42 |
fn=detect_structure,
|
| 43 |
inputs=gr.Image(type="pil"),
|
| 44 |
outputs=gr.Image(type="pil"),
|
| 45 |
title="YOLO Molecular Detector",
|
| 46 |
+
description="Upload a molecular structure image. YOLOv8 will detect atoms and bonds."
|
| 47 |
)
|
| 48 |
|
| 49 |
demo.launch()
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
+
|