Skip to content

Open source CapCut alternative.

TypeScript

Add RunPod vision worker

David Le3f14370d187a
diff --git a/vision/Dockerfile b/vision/Dockerfile
new file mode 100644
index 00000000..ffcfce5f
--- /dev/null
+++ b/vision/Dockerfile
@@ -0,0 +1,42 @@
+# OmniParser V2 RunPod Serverless worker.
+#
+# RunPod builds this image from GitHub (Serverless -> Import Git Repository ->
+# Dockerfile Path = vision/Dockerfile). Build for linux/amd64 if building
+# locally on Apple Silicon: docker build --platform linux/amd64 ...
+FROM runpod/pytorch:2.4.0-py3.11-cuda12.1.0-devel
+
+WORKDIR /app
+
+RUN apt-get update && apt-get install -y \
+ git \
+ ffmpeg \
+ libgl1 \
+ libglib2.0-0 \
+ && rm -rf /var/lib/apt/lists/*
+
+# Pin OmniParser for reproducible builds. Bump deliberately and retest.
+ARG OMNIPARSER_COMMIT=b0d5c9f5701f7e2be4771872e6e928da77759df3
+RUN git clone https://github.com/microsoft/OmniParser.git \
+ && cd OmniParser \
+ && git checkout ${OMNIPARSER_COMMIT}
+
+WORKDIR /app/OmniParser
+
+RUN pip install --upgrade pip \
+ && pip install -r requirements.txt \
+ && pip install runpod huggingface_hub
+
+# Bake OmniParser V2 weights into the image so workers don't download on cold
+# start. icon_detect (YOLO) weights are AGPL-3.0; icon_caption is MIT.
+RUN mkdir -p weights && \
+ for f in icon_detect/train_args.yaml icon_detect/model.pt icon_detect/model.yaml \
+ icon_caption/config.json icon_caption/generation_config.json icon_caption/model.safetensors; do \
+ huggingface-cli download microsoft/OmniParser-v2.0 "$f" --local-dir weights; \
+ done && \
+ mv weights/icon_caption weights/icon_caption_florence
+
+COPY handler.py /app/handler.py
+
+WORKDIR /app
+
+CMD ["python", "-u", "handler.py"]
diff --git a/vision/README.md b/vision/README.md
new file mode 100644
index 00000000..c26f1dad
--- /dev/null
+++ b/vision/README.md
@@ -0,0 +1,114 @@
+# Vision RunPod Serverless Worker
+
+A [RunPod Serverless](https://docs.runpod.io/serverless/overview) worker that
+wraps Microsoft [OmniParser V2](https://github.com/microsoft/OmniParser) — YOLO
+icon detection + OCR + Florence-2 captioning — and parses a screenshot into
+labeled UI regions.
+
+The worker returns the model's **raw** output. Normalization into Donkey's
+screenshot-parser contract lives in the site adapter
+(`site/src/lib/inference/screenshot-parsing/vision.ts`), not here. The site
+serves it on its own route, `POST /api/inference/vision`, separate from the
+gemini-flash parser at `/api/inference/screenshots/parse`.
+
+## Files
+
+| File | Purpose |
+| --- | --- |
+| `handler.py` | RunPod handler: base64 image in, raw model output out |
+| `Dockerfile` | Builds the worker image; bakes V2 weights in at build time |
+| `requirements-worker.txt` | Worker-only deps (`runpod`, `huggingface_hub`) |
+| `test_input.json` | Sample payload for local handler testing |
+
+## Request / response contract
+
+Request `input`:
+
+```jsonc
+{
+ "image": "<base64 screenshot, no data: prefix>",
+ "box_threshold": 0.05, // optional
+ "iou_threshold": 0.1, // optional
+ "imgsz": 640 // optional
+}
+```
+
+Response:
+
+```jsonc
+{
+ "labeled_image_base64": "<base64 PNG with boxes drawn>",
+ "label_coordinates": { "0": [x, y, w, h], ... }, // ratio space (0-1)
+ "parsed_content_list": [
+ {
+ "type": "text" | "icon",
+ "bbox": [x1, y1, x2, y2], // ratio space (0-1), NOT pixels
+ "interactivity": true,
+ "content": "Sign in",
+ "source": "box_ocr_content_ocr" // present for OCR text elements
+ }
+ ]
+}
+```
+
+Boxes are image-size-independent ratios. To get Donkey's
+`box_2d = [ymin, xmin, ymax, xmax]` in 0-1000 space, multiply by 1000 and
+reorder: `[y1*1000, x1*1000, y2*1000, x2*1000]` (the site adapter does this).
+
+## Deploy via RunPod GitHub integration
+
+RunPod builds and hosts the image directly from this repo — no Docker Hub push.
+
+1. RunPod console → **Settings → Connections → GitHub → Connect** and authorize
+ access to this repository.
+2. **Serverless → New Endpoint → Import Git Repository** → select this repo.
+3. Set:
+ - **Branch**: `main` (RunPod rebuilds on every push to it)
+ - **Dockerfile Path**: `vision/Dockerfile`
+4. Pick a GPU (RTX 4090 / A40 / L40S is a good start).
+5. **Active workers = 0** for cheap testing (cold starts apply) or `1` for low
+ latency. Set sensible execution/idle timeouts.
+6. Deploy. Note the **Endpoint ID** — the site adapter needs it.
+
+See RunPod's [GitHub deployment docs](https://docs.runpod.io/serverless/github-integration).
+
+## Local handler test
+
+You don't need a GPU to exercise the handler shape (CPU works, just slow).
+
+```bash
+# Put a real screenshot's base64 into test_input.json (replace the placeholder).
+base64 -i screenshot.png | tr -d '\n' # macOS: copy into "image"
+
+# Inside the built image (or a venv with OmniParser + its requirements):
+python handler.py # RunPod SDK auto-runs ./test_input.json
+```
+
+Confirm `parsed_content_list` is non-empty and each `bbox` is four values in
+0-1. Build the image for local runs with:
+
+```bash
+docker build --platform linux/amd64 -t vision-worker:dev vision/
+```
+
+## Calling the deployed endpoint
+
+```bash
+curl -s -X POST \
+ "https://api.runpod.ai/v2/${RUNPOD_VISION_ENDPOINT_ID}/runsync" \
+ -H "Authorization: Bearer ${RUNPOD_API_KEY}" \
+ -H "Content-Type: application/json" \
+ -d '{"input":{"image":"<base64>","box_threshold":0.05,"iou_threshold":0.1,"imgsz":640}}'
+```
+
+## Licensing
+
+OmniParser's `icon_detect` (YOLO) weights are **AGPL-3.0**; `icon_caption`
+(Florence-2) is **MIT**. This worker bakes both into the image. Keep that in
+mind before redistributing the built image — it carries AGPL-3.0 weights.
+
+## Latency note
+
+Serverless inference with zero active workers has real cold-start cost. Treat
+it as a fallback/evaluation parser; for low-latency UX run with active
+workers ≥ 1 or a persistent pod.
diff --git a/vision/handler.py b/vision/handler.py
new file mode 100644
index 00000000..04b45262
--- /dev/null
+++ b/vision/handler.py
@@ -0,0 +1,104 @@
+"""RunPod Serverless handler for Microsoft OmniParser V2.
+
+Wraps OmniParser's screen-parsing pipeline (YOLO icon detection + OCR +
+Florence-2 captioning) behind a RunPod serverless endpoint. The handler returns
+OmniParser's *raw* output verbatim; downstream callers (e.g. the Donkey site
+adapter) own any normalization into product-specific coordinate contracts.
+
+Coordinate note: every `bbox` in `parsed_content_list` is `[x1, y1, x2, y2]`
+in ratio space (0-1), independent of image size. `label_coordinates` is
+`[x, y, w, h]` in ratio space. See OmniParser util/utils.get_som_labeled_img.
+"""
+
+import base64
+import io
+import os
+
+import runpod
+import torch
+from PIL import Image
+
+# OmniParser resolves its weights relative to the working directory.
+os.chdir("/app/OmniParser")
+
+from util.utils import ( # noqa: E402 (import after chdir, by design)
+ check_ocr_box,
+ get_caption_model_processor,
+ get_som_labeled_img,
+ get_yolo_model,
+)
+
+DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
+
+# Load once at cold start so warm invocations reuse the resident models.
+yolo_model = get_yolo_model(model_path="weights/icon_detect/model.pt")
+caption_model_processor = get_caption_model_processor(
+ model_name="florence2",
+ model_name_or_path="weights/icon_caption_florence",
+)
+
+
+def parse_image(image, box_threshold=0.05, iou_threshold=0.1, imgsz=640):
+ box_overlay_ratio = image.size[0] / 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),
+ }
+
+ ocr_bbox_rslt, _ = check_ocr_box(
+ image,
+ display_img=False,
+ output_bb_format="xyxy",
+ goal_filtering=None,
+ easyocr_args={"paragraph": False, "text_threshold": 0.9},
+ use_paddleocr=False,
+ )
+ text, ocr_bbox = ocr_bbox_rslt
+
+ labeled_img_b64, label_coordinates, parsed_content_list = get_som_labeled_img(
+ image,
+ yolo_model,
+ BOX_TRESHOLD=box_threshold,
+ output_coord_in_ratio=True,
+ ocr_bbox=ocr_bbox,
+ draw_bbox_config=draw_bbox_config,
+ caption_model_processor=caption_model_processor,
+ ocr_text=text,
+ iou_threshold=iou_threshold,
+ imgsz=imgsz,
+ )
+
+ return {
+ # SOM-annotated PNG (base64). Useful for debugging overlays.
+ "labeled_image_base64": labeled_img_b64,
+ # Pixel dimensions of the parsed image, so callers can convert the ratio
+ # bboxes below into pixel coordinates.
+ "image_size": {"width": image.size[0], "height": image.size[1]},
+ # {label: [x, y, w, h]} in ratio space.
+ "label_coordinates": label_coordinates,
+ # [{type, bbox:[x1,y1,x2,y2] ratio, interactivity, content, source?}]
+ "parsed_content_list": parsed_content_list,
+ }
+
+
+def handler(event):
+ inp = event["input"]
+ image_b64 = inp["image"]
+ box_threshold = inp.get("box_threshold", 0.05)
+ iou_threshold = inp.get("iou_threshold", 0.1)
+ imgsz = inp.get("imgsz", 640)
+
+ image_bytes = base64.b64decode(image_b64)
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
+
+ return parse_image(
+ image,
+ box_threshold=box_threshold,
+ iou_threshold=iou_threshold,
+ imgsz=imgsz,
+ )
+
+
+runpod.serverless.start({"handler": handler})
diff --git a/vision/requirements-worker.txt b/vision/requirements-worker.txt
new file mode 100644
index 00000000..f14f182f
--- /dev/null
+++ b/vision/requirements-worker.txt
@@ -0,0 +1,6 @@
+# Worker-only deps layered on top of OmniParser's own requirements.txt
+# (installed from the cloned repo inside the Docker build). These are also
+# pip-installed in the Dockerfile; this file documents the worker surface and
+# supports local handler testing.
+runpod
+huggingface_hub
diff --git a/vision/test_input.json b/vision/test_input.json
new file mode 100644
index 00000000..2c4ec5ac
--- /dev/null
+++ b/vision/test_input.json
@@ -0,0 +1,8 @@
+{
+ "input": {
+ "image": "REPLACE_WITH_BASE64_SCREENSHOT",
+ "box_threshold": 0.05,
+ "iou_threshold": 0.1,
+ "imgsz": 640
+ }
+}