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

commit 541ea5ee97bb51d7e1ca5580d3a063e7e7c58ee9
parent 0b09f89917a90c5554f5d401eab86caa89934c9d
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sat, 14 Mar 2026 19:12:28 +0000

refactor: parse module

Diffstat:
Mapp.py | 93++++++-------------------------------------------------------------------------
Mimage.py | 2++
Aparse.py | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 91 insertions(+), 87 deletions(-)

diff --git a/app.py b/app.py @@ -1,96 +1,15 @@ import modal -import volume import image +import volume app = modal.App("omniparser") -@app.function(gpu="h100", image=image.omniparser_v2_0_1, volumes={"/data": volume.vol}) -@volume.cache -def parse(): - import time - import importlib - from util.utils import ( - get_som_labeled_img, - check_ocr_box, - get_caption_model_processor, - get_yolo_model, - ) - import torch - from ultralytics import YOLO - from PIL import Image - - device = "cuda" - - model_path = "weights/icon_detect/model.pt" - - som_model = get_yolo_model(model_path) - - som_model.to(device) - - caption_model_processor = get_caption_model_processor( - model_name="florence2", - model_name_or_path="weights/icon_caption_florence", - device=device, - ) - - # image_path = "imgs/google_page.png" - # image_path = "imgs/windows_home.png" - # image_path = 'imgs/windows_multitab.png' - # image_path = 'imgs/omni3.jpg' - # image_path = 'imgs/ios.png' - # image_path = 'imgs/excel2.png' - image_path = "imgs/word.png" - - image = Image.open(image_path) - - image_rgb = image.convert("RGB") - - box_overlay_ratio = max(image.size) / 3200 - - draw_bbox_config = { - "text_scale": 0.8 * box_overlay_ratio, - "text_thickness": max(int(2 * box_overlay_ratio), 1), - "text_padding": max(int(3 * box_overlay_ratio), 1), - "thickness": max(int(3 * box_overlay_ratio), 1), - } - - BOX_TRESHOLD = 0.05 - - start = time.time() - - ocr_bbox_rslt, is_goal_filtered = check_ocr_box( - image_path, - display_img=False, - output_bb_format="xyxy", - goal_filtering=None, - easyocr_args={"paragraph": False, "text_threshold": 0.9}, - use_paddleocr=True, - ) - - text, ocr_bbox = ocr_bbox_rslt - - cur_time_ocr = time.time() - - dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img( - image_path, - som_model, - BOX_TRESHOLD=BOX_TRESHOLD, - output_coord_in_ratio=True, - ocr_bbox=ocr_bbox, - draw_bbox_config=draw_bbox_config, - caption_model_processor=caption_model_processor, - ocr_text=text, - use_local_semantics=True, - iou_threshold=0.7, - scale_img=False, - batch_size=128, - ) - - cur_time_caption = time.time() - - return parsed_content_list +if __name__ == "__main__": + import parse + parse = app.function( + gpu="h100", image=image.omniparser_v2_0_1, volumes={"/data": volume.vol} + )(volume.cache(parse.parse)) -if __name__ == "__main__": with app.run(): print(parse.remote()) diff --git a/image.py b/image.py @@ -58,4 +58,6 @@ omniparser_v2_0_1 = ( ]) .add_local_python_source("volume") .add_local_python_source("image") + .add_local_python_source("parse") + .add_local_python_source("app") ) diff --git a/parse.py b/parse.py @@ -0,0 +1,83 @@ +def parse(): + import time + import importlib + from util.utils import ( + get_som_labeled_img, + check_ocr_box, + get_caption_model_processor, + get_yolo_model, + ) + import torch + from ultralytics import YOLO + from PIL import Image + + device = "cuda" + + model_path = "weights/icon_detect/model.pt" + + som_model = get_yolo_model(model_path) + + som_model.to(device) + + caption_model_processor = get_caption_model_processor( + model_name="florence2", + model_name_or_path="weights/icon_caption_florence", + device=device, + ) + + # image_path = "imgs/google_page.png" + # image_path = "imgs/windows_home.png" + # image_path = 'imgs/windows_multitab.png' + # image_path = 'imgs/omni3.jpg' + # image_path = 'imgs/ios.png' + # image_path = 'imgs/excel2.png' + image_path = "imgs/word.png" + + image = Image.open(image_path) + + image_rgb = image.convert("RGB") + + box_overlay_ratio = max(image.size) / 3200 + + draw_bbox_config = { + "text_scale": 0.8 * box_overlay_ratio, + "text_thickness": max(int(2 * box_overlay_ratio), 1), + "text_padding": max(int(3 * box_overlay_ratio), 1), + "thickness": max(int(3 * box_overlay_ratio), 1), + } + + BOX_TRESHOLD = 0.05 + + start = time.time() + + ocr_bbox_rslt, is_goal_filtered = check_ocr_box( + image_path, + display_img=False, + output_bb_format="xyxy", + goal_filtering=None, + easyocr_args={"paragraph": False, "text_threshold": 0.9}, + use_paddleocr=True, + ) + + text, ocr_bbox = ocr_bbox_rslt + + cur_time_ocr = time.time() + + dino_labled_img, label_coordinates, parsed_content_list = get_som_labeled_img( + image_path, + som_model, + BOX_TRESHOLD=BOX_TRESHOLD, + output_coord_in_ratio=True, + ocr_bbox=ocr_bbox, + draw_bbox_config=draw_bbox_config, + caption_model_processor=caption_model_processor, + ocr_text=text, + use_local_semantics=True, + iou_threshold=0.7, + scale_img=False, + batch_size=128, + ) + + cur_time_caption = time.time() + + return parsed_content_list