|
|
import os |
|
|
import tempfile |
|
|
import fitz |
|
|
import gradio as gr |
|
|
from ultralytics import YOLO |
|
|
from PIL import Image |
|
|
|
|
|
|
|
|
model_doc = YOLO("moldet_yolo11l_960_doc.pt") |
|
|
model_img = YOLO("moldet_yolo11l_640_general.pt") |
|
|
|
|
|
def process_file(file): |
|
|
with tempfile.TemporaryDirectory() as tmpdir: |
|
|
input_path = os.path.join(tmpdir, "input.png") |
|
|
|
|
|
|
|
|
if file.name.lower().endswith(".pdf"): |
|
|
pdf = fitz.open(file.name) |
|
|
page = pdf[0] |
|
|
pix = page.get_pixmap(dpi=300) |
|
|
pix.save(input_path) |
|
|
|
|
|
results = model_doc.predict(input_path, save=True, imgsz=960, conf=0.5) |
|
|
|
|
|
else: |
|
|
Image.open(file.name).save(input_path) |
|
|
|
|
|
results = model_img.predict(input_path, save=True, imgsz=640, conf=0.5) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result_img = results[0].plot() |
|
|
result_pil = Image.fromarray(result_img) |
|
|
|
|
|
|
|
|
|
|
|
return result_img |
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=process_file, |
|
|
inputs=gr.File(label="Upload an image or a single PDF file", file_types=[".png", ".jpg", ".jpeg", ".pdf"]), |
|
|
outputs=gr.Image(type="pil", label="Detection results"), |
|
|
title="Molecule detection", |
|
|
description=( |
|
|
"Upload an image or a single PDF file to detect molecules using a YOLO-based deep learning model.\n" |
|
|
"The system processes the input and returns an annotated image with bounding boxes and labels around the detected molecular structures." |
|
|
) |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(mcp_server=True) |
|
|
|