modal-omniparser

A serverless compute port of the OmniParser vision model for the Modal platform.
git clone https://unitehenry.com/git/modal-omniparser.git
Log | Files | Refs | README

parse.py (3187B)


      1 import app
      2 import image
      3 import volume
      4 
      5 
      6 @app.app.function(
      7     gpu="h100", image=image.omniparser_v2_0_1, volumes={"/data": volume.omniparser}
      8 )
      9 @volume.cache
     10 def parse(file_url: str):
     11     import time
     12     import json
     13     import importlib
     14     import urllib.request
     15     import tempfile
     16     import os
     17     import io
     18     import base64
     19     import subprocess
     20     from util.utils import (
     21         get_som_labeled_img,
     22         check_ocr_box,
     23         get_caption_model_processor,
     24         get_yolo_model,
     25     )
     26     import torch
     27     from urllib.parse import urlparse
     28     from ultralytics import YOLO
     29     from PIL import Image
     30 
     31     device = "cuda"
     32 
     33     model_path = "weights/icon_detect/model.pt"
     34 
     35     som_model = get_yolo_model(model_path)
     36 
     37     som_model.to(device)
     38 
     39     caption_model_processor = get_caption_model_processor(
     40         model_name="florence2",
     41         model_name_or_path="weights/icon_caption_florence",
     42         device=device,
     43     )
     44 
     45     with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmp_file:
     46         tmp_path = tmp_file.name
     47 
     48     urllib.request.urlretrieve(file_url, tmp_path)
     49 
     50     image_path = tmp_path
     51 
     52     image = Image.open(image_path)
     53 
     54     image_rgb = image.convert("RGB")
     55 
     56     box_overlay_ratio = max(image.size) / 3200
     57 
     58     draw_bbox_config = {
     59         "text_scale": 0.8 * box_overlay_ratio,
     60         "text_thickness": max(int(2 * box_overlay_ratio), 1),
     61         "text_padding": max(int(3 * box_overlay_ratio), 1),
     62         "thickness": max(int(3 * box_overlay_ratio), 1),
     63     }
     64 
     65     BOX_TRESHOLD = 0.05
     66 
     67     start = time.time()
     68 
     69     ocr_bbox_rslt, is_goal_filtered = check_ocr_box(
     70         image_path,
     71         display_img=False,
     72         output_bb_format="xyxy",
     73         goal_filtering=None,
     74         easyocr_args={"paragraph": False, "text_threshold": 0.9},
     75         use_paddleocr=True,
     76     )
     77 
     78     text, ocr_bbox = ocr_bbox_rslt
     79 
     80     cur_time_ocr = time.time()
     81 
     82     dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img(
     83         image_path,
     84         som_model,
     85         BOX_TRESHOLD=BOX_TRESHOLD,
     86         output_coord_in_ratio=True,
     87         ocr_bbox=ocr_bbox,
     88         draw_bbox_config=draw_bbox_config,
     89         caption_model_processor=caption_model_processor,
     90         ocr_text=text,
     91         use_local_semantics=True,
     92         iou_threshold=0.7,
     93         scale_img=False,
     94         batch_size=128,
     95     )
     96 
     97     cur_time_caption = time.time()
     98 
     99     labeled_image = Image.open(io.BytesIO(base64.b64decode(dino_labled_img)))
    100 
    101     os.makedirs("/root/output", exist_ok=True)
    102 
    103     output_file_name = f"{os.path.basename(urlparse(file_url).path).split('.')[0]}.png"
    104 
    105     output_file_path = f"/root/output/{output_file_name}"
    106 
    107     with open(output_file_path, "wb") as output_file:
    108         labeled_image.save(output_file.name)
    109 
    110     os.makedirs("/data/output", exist_ok=True)
    111 
    112     subprocess.run(
    113         ["mv", "-f", "--", output_file_path, f"/data/output/{output_file_name}"],
    114         check=False,
    115     )
    116 
    117     return json.dumps(
    118         {
    119             "cur_time_ocr": cur_time_ocr,
    120             "cur_time_caption": cur_time_caption,
    121             "parsed_content_list": parsed_content_list,
    122             "label_coordinates": label_coordinates,
    123         }
    124     )