commit 953853bd4d13f710ac8a173d129960eca0efc63e
parent 0d5e679ac4466837027c283c474ec38e57fbe0c2
Author: Henry Unite <unitehenry@gmail.com>
Date: Sat, 14 Mar 2026 09:05:51 +0000
refactor: cache volume
Diffstat:
| M | src/app.py | | | 187 | +++++++++++++++++++++++++++++++++++++++---------------------------------------- |
1 file changed, 92 insertions(+), 95 deletions(-)
diff --git a/src/app.py b/src/app.py
@@ -1,4 +1,6 @@
+import functools
import modal
+from functools import wraps
app = modal.App("omniparser")
vol = modal.Volume.from_name("omniparser", create_if_missing=True)
@@ -37,7 +39,6 @@ image = (
)
.run_commands(
[
- # "$CONDA_DIR/bin/conda run -n omni pip install flash-attn==2.5.8 --no-build-isolation"
"wget https://github.com/Dao-AILab/flash-attention/releases/download/v2.6.0/flash_attn-2.6.0+cu122torch2.2cxx11abiFALSE-cp312-cp312-linux_x86_64.whl",
"$CONDA_DIR/bin/conda run -n omni pip install --no-dependencies flash_attn-2.6.0+cu122torch2.2cxx11abiFALSE-cp312-cp312-linux_x86_64.whl",
]
@@ -57,127 +58,123 @@ image = (
)
)
-
-def setup():
+def cache(func):
import os
import subprocess
- subprocess.run(["mv", "-f", "--", "/data/.paddleocr", "/root"], check=False)
-
- subprocess.run(["mv", "-f", "--", "/data/.EasyOCR", "/root"], check=False)
+ @wraps(func)
+ def wrapper(*args, **kwargs):
+ subprocess.run(["mv", "-f", "--", "/data/.paddleocr", "/root"], check=False)
- os.makedirs(os.path.dirname("/root/.config/Ultralytics"), exist_ok=True)
+ subprocess.run(["mv", "-f", "--", "/data/.EasyOCR", "/root"], check=False)
- subprocess.run(
- ["mv", "-f", "--", "/data/.config/Ultralytics", "/root/.config/Ultralytics"],
- check=False,
- )
+ os.makedirs(os.path.dirname("/root/.config/Ultralytics"), exist_ok=True)
+ subprocess.run(
+ ["mv", "-f", "--", "/data/.config/Ultralytics", "/root/.config/Ultralytics"],
+ check=False,
+ )
+ try:
+ return func(*args, **kwargs)
+ finally:
+ subprocess.run(
+ ["mv", "-f", "--", "/root/.paddleocr", "/data/.paddleocr"], check=False
+ )
-def cleanup():
- import os
- import subprocess
-
- subprocess.run(
- ["mv", "-f", "--", "/root/.paddleocr", "/data/.paddleocr"], check=False
- )
+ subprocess.run(["mv", "-f", "--", "/root/.EasyOCR", "/data/.EasyOCR"], check=False)
- subprocess.run(["mv", "-f", "--", "/root/.EasyOCR", "/data/.EasyOCR"], check=False)
+ os.makedirs(os.path.dirname("/data/.config/Ultralytics"), exist_ok=True)
- os.makedirs(os.path.dirname("/data/.config/Ultralytics"), exist_ok=True)
+ subprocess.run(
+ ["mv", "-f", "--", "/root/.config/Ultralytics", "/data/.config/Ultralytics"],
+ check=False,
+ )
- subprocess.run(
- ["mv", "-f", "--", "/root/.config/Ultralytics", "/data/.config/Ultralytics"],
- check=False,
- )
+ return wrapper
@app.function(gpu="h100", image=image, volumes={"/data": vol})
+@cache
def parse():
- try:
- setup()
-
- 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
+ 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"
+ 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)
- caption_model_processor = get_caption_model_processor(
- model_name="florence2",
- model_name_or_path="weights/icon_caption_florence",
- device=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/word.png"
- # image_path = 'imgs/excel2.png'
+ 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/word.png"
+ # image_path = 'imgs/excel2.png'
- image = Image.open(image_path)
+ image = Image.open(image_path)
- image_rgb = image.convert("RGB")
+ image_rgb = image.convert("RGB")
- box_overlay_ratio = max(image.size) / 3200
+ 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),
- }
+ 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
+ BOX_TRESHOLD = 0.05
- start = time.time()
+ 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,
- )
+ 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,
- )
+ 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()
+ cur_time_caption = time.time()
- return parsed_content_list
- finally:
- cleanup()
+ return parsed_content_list