The OCR recipes in this folder run as batch jobs (dataset in → dataset out). To call a model
interactively, from an agent, or with ad-hoc concurrent requests, you can instead run it as a
temporary HTTP endpoint. HF Jobs serving exposes a
port on a GPU Job, giving an OpenAI-compatible endpoint that runs until the job is cancelled or its
--timeout is reached.
This is a worked example for baidu/Unlimited-OCR (3B, MIT, based on DeepSeek-OCR; supports multi-page parsing in a single request). Two server options below: vLLM on Baidu's official image (the newer official path, OpenAI-compatible), or SGLang on the stock image with the model's own wheel. Either gives an OpenAI-compatible endpoint.
Single-image vs multi-page — pick the engine by task:
- Single-page OCR (one image → markdown): both engines work. For a whole corpus, the batch recipe
unlimited-ocr-vllm.py(offline vLLM, resumable, no network) is the better fit than a client loop; for interactive/agent use, serve with vLLM (Option A).- Multi-page / long-horizon parsing (the model's headline feature): both engines do it (validated 2026-06-28 — a clean 2-page doc read back both pages,
<PAGE>-separated, on both vLLM and SGLang). The difference is robustness on hard inputs: on degraded historical scans / newspaper clippings, vLLM multi-page degraded to hallucination in our tests while SGLang (Option B) read real content — so SGLang is the more robust multi-page path (it's also the authors' documented one, viaimages_config). Use vLLM multi-page for clean docs; reach for SGLang for hard scans. (vLLM's upstream PR #46564 benchmarks single-page only.)
vLLM support landed upstream; Baidu ships a dedicated image (the architecture isn't in a stable pip
wheel yet). Use the default :unlimited-ocr tag on L4/A100, or :unlimited-ocr-cu129 on Hopper.
Runs on l4x1, no fa3/Hopper requirement. Single-image is validated; multi-page also works on
clean docs (both pages, <PAGE>-separated) but degraded to hallucination on hard scans in our tests
— for hard/degraded inputs prefer Option B (SGLang). For multi-page on vLLM, the request takes one
<image> per page in the text and window_size=1024 in vllm_xargs (it has no images_config).
hf jobs run --detach --expose 8000 --flavor l4x1 -s HF_TOKEN --timeout 30m \
vllm/vllm-openai:unlimited-ocr -- \
vllm serve baidu/Unlimited-OCR --served-model-name Unlimited-OCR \
--trust-remote-code --max-model-len 32768 --host 0.0.0.0 --port 8000 \
--logits_processors vllm.model_executor.models.unlimited_ocr:NGramPerReqLogitsProcessor \
--no-enable-prefix-caching --mm-processor-cache-gb 0Per-request, vLLM takes the no-repeat n-gram knobs via vllm_xargs and needs skip_special_tokens
off (it has no images_config — that's an SGLang param):
r = client.chat.completions.create(
model="Unlimited-OCR",
messages=[{"role": "user", "content": [
{"type": "text", "text": "<image>document parsing."}, # literal <image> prefix is required
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img}"}},
]}],
temperature=0,
extra_body={"skip_special_tokens": False, "vllm_xargs": {"ngram_size": 35, "window_size": 128}},
)The model also ships its own SGLang build, installed at startup from a 12 MB wheel. This is the
more robust path for multi-page / long-horizon parsing (§3) — the model authors' documented route
(images_config), and the one that held up on hard scans where vLLM multi-page hallucinated. Two
pins matter (both learned the hard way, 2026-06-28):
- Pin the image to
lmsysorg/sglang:v0.5.10.post1— not:latest.:latestdrifted to torch 2.11 / cu130, incompatible with the wheel (torch 2.9.1 / cuda-python 12.9); v0.5.10.post1 is the last release that matches the wheel exactly. - Run on
a100-largewith--attention-backend flashinfer, noth200/fa3.fa3needs a Hopper GPU, but HF'sh200nodes currently fail GPU init withCUDA error 802: system not yet initialized(3/3 attempts) — an infra issue, not the model.a100+flashinfersidesteps it and works.
hf jobs run --detach --expose 10000 --flavor a100-large -s HF_TOKEN --timeout 30m \
lmsysorg/sglang:v0.5.10.post1 -- \
bash -lc 'pip install --no-deps https://github.com/baidu/Unlimited-OCR/raw/main/wheel/sglang-0.0.0.dev11416+g92e8bb79e-py3-none-any.whl \
&& pip install -q kernels==0.11.7 \
&& python -m sglang.launch_server --model baidu/Unlimited-OCR --served-model-name Unlimited-OCR \
--attention-backend flashinfer --page-size 1 --mem-fraction-static 0.85 --context-length 32768 \
--enable-custom-logit-processor --disable-overlap-schedule --skip-server-warmup \
--host 0.0.0.0 --port 10000'Notes:
--beforebashis required, or the CLI parses-lcas its own flags.--timeoutstops the endpoint (and billing) at the deadline;hf jobs cancel <id>stops it earlier.- Validated 2026-06-28 on
a100-large: server came up, single-image and multi-page both read correctly (a clean 2-page doc returned both pages verbatim,<PAGE>-separated). The model card's "official" backend isfa3on Hopper for exact R-SWA — switch back to--attention-backend fa3 --flavor h200once the h200802infra issue clears;flashinferona100is the working fallback. - Follow startup with
hf jobs logs -f <id>; ready atThe server is fired up/Application startup complete(a few minutes cold; the wheel + model download dominate).
The client examples below use the SGLang request format (images_config in extra_body,
port 10000). The single-image call (§2) also works on the vLLM server — just use the Option A
extra_body and your exposed port. Multi-page (§3) is SGLang-only.
The exposed port is at https://<job_id>--10000.hf.jobs; the OpenAI base URL is that plus /v1.
import base64, os
from openai import OpenAI
client = OpenAI(base_url="https://<job_id>--10000.hf.jobs/v1", api_key=os.environ["HF_TOKEN"])
img = base64.b64encode(open("page.jpg", "rb").read()).decode()
r = client.chat.completions.create(
model="Unlimited-OCR",
messages=[{"role": "user", "content": [
{"type": "text", "text": "document parsing."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{img}"}},
]}],
temperature=0,
extra_body={"images_config": {"image_mode": "gundam"}}, # "gundam" (crop-tiling) or "base"
)
print(r.choices[0].message.content)Output is layout-grounded markdown: each block is tagged <|det|>type [x1,y1,x2,y2]<|/det|> text,
with coordinates normalized to 0–1000. Remove the tags for plain text
(re.sub(r'<\|det\|>.*?<\|/det\|>', '', text)) or keep them for structure.
✅ This SGLang flow (Option B) is validated working 2026-06-28 (a clean 2-page doc read back both pages verbatim,
<PAGE>-separated) and follows the model card's multi-page example. Theimages_config/image_modeparam is SGLang-specific — vLLM ignores it; on vLLM, do multi-page with one<image>per page in the text +window_size=1024invllm_xargs(noimages_config). Both engines read clean multi-page docs; SGLang was the more robust on hard/degraded scans, where vLLM multi-page hallucinated in our tests. (vLLM's upstream PR #46564 benchmarks single-page only.)
Send multiple page images in one request with the Multi page parsing. prompt and image_mode="base":
parts = [{"type": "text", "text": "Multi page parsing."}]
for page_png in page_images: # e.g. PDF pages rendered with pymupdf at ~150 dpi
b64 = base64.b64encode(open(page_png, "rb").read()).decode()
parts.append({"type": "image_url", "image_url": {"url": f"data:image/png;base64,{b64}"}})
r = client.chat.completions.create(
model="Unlimited-OCR",
messages=[{"role": "user", "content": parts}],
temperature=0, max_tokens=16384,
extra_body={"images_config": {"image_mode": "base"}},
)Pages are separated by <PAGE>; tables are returned as HTML and equations as LaTeX, with reading
order preserved across pages. The context length is 32k tokens, so split longer documents.
SGLang batches concurrent requests, so a client can send many requests in parallel to one endpoint;
the upstream infer.py uses a
ThreadPoolExecutor at concurrency=8. For a large corpus, a batch job that runs next to the data
(resumable, no network transfer) is usually a better fit than a client-to-endpoint loop.
hf jobs cancel <job_id>Billing is per-minute for the GPU flavor plus a small flat fee for the exposed port; scheduling time
is not billed. Run hf jobs hardware for current flavors and prices.