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 d73b65537d697db112201094c3b15fc2075142e9
parent 38a14bdede3113cd048b411887ed2a8e0404086e
Author: Henry Unite <unitehenry@gmail.com>
Date:   Fri, 13 Mar 2026 19:03:41 +0000

fix: pin package versions

Diffstat:
Msrc/app.py | 109+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
1 file changed, 71 insertions(+), 38 deletions(-)

diff --git a/src/app.py b/src/app.py @@ -5,77 +5,110 @@ vol = modal.Volume.from_name("omniparser", create_if_missing=True) image = ( modal.Image.debian_slim(python_version="3.12") - .apt_install([ "git", "wget", "curl", "libgl1", "libglib2.0-0" ]) - .shell(["/bin/bash", "-c"]) - .env({ 'CONDA_DIR': '/opt/conda' }) - .run_commands([ + .apt_install(["git", "wget", "curl", "libgl1", "libglib2.0-0"]) + .shell(["/bin/bash", "-c"]) + .env({"CONDA_DIR": "/opt/conda"}) + .run_commands( + [ "mkdir -p $CONDA_DIR", "wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O $CONDA_DIR/miniconda.sh", "bash $CONDA_DIR/miniconda.sh -b -u -p $CONDA_DIR", "rm $CONDA_DIR/miniconda.sh", "$CONDA_DIR/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main", "$CONDA_DIR/bin/conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r", - "$CONDA_DIR/bin/conda init --all" - ]) - .run_commands("git clone https://github.com/microsoft/OmniParser") - .workdir("OmniParser") - .run_commands([ - "sed -i 's/\<paddleocr\>/paddleocr==2.7.3/g' requirements.txt", + "$CONDA_DIR/bin/conda init --all", + ] + ) + .run_commands("git clone https://github.com/microsoft/OmniParser") + .workdir("OmniParser") + .run_commands( + [ + "sed -i 's/\<paddleocr\>/paddleocr<=2.8/g' requirements.txt", + "sed -i 's/\<torch\>/torch==2.2.0/g' requirements.txt", + "sed -i 's/\<transformers\>/transformers==4.38.2/g' requirements.txt", + "sed -i 's/\<paddlepaddle\>/paddlepaddle<=2.8/g' requirements.txt", "$CONDA_DIR/bin/conda create -n 'omni' python==3.12 -y", "$CONDA_DIR/bin/conda run -n omni pip install -r requirements.txt", - ]) - .run_commands("curl -LsSf https://hf.co/cli/install.sh | bash") - .run_commands([ - "; ".join([ - 'for f in icon_detect/{train_args.yaml,model.pt,model.yaml} icon_caption/{config.json,generation_config.json,model.safetensors}', - 'do env PATH="/root/.local/bin:$PATH" hf download microsoft/OmniParser-v2.0 "$f" --local-dir weights', - 'done' - ]), - "mv weights/icon_caption weights/icon_caption_florence" - ]) - .env({ "PATH": "$CONDA_DIR/envs/omni/bin:$PATH" }) + ] + ) + .run_commands("curl -LsSf https://hf.co/cli/install.sh | bash") + .run_commands( + [ + "; ".join( + [ + "for f in icon_detect/{train_args.yaml,model.pt,model.yaml} icon_caption/{config.json,generation_config.json,model.safetensors}", + 'do env PATH="/root/.local/bin:$PATH" hf download microsoft/OmniParser-v2.0 "$f" --local-dir weights', + "done", + ] + ), + "mv weights/icon_caption weights/icon_caption_florence", + ] + ) + .env({"PATH": "$CONDA_DIR/envs/omni/bin:$PATH"}) ) + def setup(): import subprocess subprocess.run(["mv", "-f", "--", "/data/.paddleocr", "/root"], check=False) subprocess.run(["mv", "-f", "--", "/data/.EasyOCR", "/root"], check=False) + def cleanup(): import subprocess - subprocess.run(["mv", "-f", "--", "/root/.paddleocr", "/data/.paddleocr"], check=False) + subprocess.run( + ["mv", "-f", "--", "/root/.paddleocr", "/data/.paddleocr"], check=False + ) subprocess.run(["mv", "-f", "--", "/root/.EasyOCR", "/data/.EasyOCR"], check=False) + @app.function(gpu="h100", image=image, volumes={"/data": vol}) def model_to_cuda(): - setup() + try: + setup() - 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 + 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' + device = "cuda" - model_path='weights/icon_detect/model.pt' + model_path = "weights/icon_detect/model.pt" - som_model = get_yolo_model(model_path) + som_model = get_yolo_model(model_path) - som_model.to(device) + som_model.to(device) - print('model to {}'.format(device)) + print("model to {}".format(device)) + finally: + cleanup() - cleanup() @app.function(gpu="h100", image=image, volumes={"/data": vol}) def get_caption_model_processor(): - setup() - try: - from util.utils import get_som_labeled_img, check_ocr_box, get_caption_model_processor, get_yolo_model - get_caption_model_processor(model_name="florence2", model_name_or_path="weights/icon_caption_florence", device='cuda') + setup() + + from util.utils import ( + get_som_labeled_img, + check_ocr_box, + get_caption_model_processor, + get_yolo_model, + ) + + get_caption_model_processor( + model_name="florence2", + model_name_or_path="weights/icon_caption_florence", + device="cuda", + ) finally: cleanup()