Unlimited OCR: One-Shot Long-Horizon Parsing for Modern Docs — Key
The world of Optical Character Recognition (OCR) has seen significant advancements, but challenges persist when dealing with complex, multi-page, or lengthy documents. Traditional OCR often struggles with maintaining
The world of Optical Character Recognition (OCR) has seen significant advancements, but challenges persist when dealing with complex, multi-page, or lengthy documents. Traditional OCR often struggles with maintaining contextual coherence across pages or handling extremely long text outputs without fragmentation.
Welcome to the era of Unlimited OCR, a powerful evolution building upon the Deepseek-OCR foundation. This new system introduces a groundbreaking approach: One-shot Long-horizon Parsing. In essence, it aims to process entire documents, potentially spanning many pages and thousands of words, in a single, coherent operation. This 'one-shot' capability promises to unlock richer contextual understanding and more consistent extraction of information, sidestepping the limitations of page-by-page processing.
Released recently (as of June 2026), Unlimited OCR is making waves in the research community, with its paper available on arXiv and a ready-to-use model integrated into ModelScope. For developers, it offers two primary avenues for integration: the familiar Hugging Face Transformers library for direct application and SGLang for high-throughput, scalable inference serving.
Getting Started with Hugging Face Transformers
For developers looking to quickly integrate Unlimited OCR into their Python applications, the Hugging Face Transformers library provides a straightforward interface. You'll need a Python 3.12.3 environment with CUDA 12.9 and the specified dependencies to ensure compatibility.
python import os import torch from transformers import AutoModel, AutoTokenizer
Required dependencies:
torch==2.10.0
torchvision==0.25.0
transformers==4.57.1
Pillow==12.1.1
matplotlib==3.10.8
einops==0.8.2
addict==2.4.0
easydict==1.13
pymupdf==1.27.2.2
psutil==7.2.2
model_name = 'baidu/Unlimited-OCR' tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) model = AutoModel.from_pretrained( model_name, trust_remote_code=True, use_safetensors=True, torch_dtype=torch.bfloat16, ) model = model.eval().cuda()
The model supports two main inference functions: infer for single images and infer_multi for multi-page documents or PDFs.
Single Image Parsing:
For individual images, you can choose between two configurations: gundam or base. The gundam mode, typically used with crop_mode=True and a smaller image_size (e.g., 640), is optimized for specific image crops, potentially for higher detail on focused regions. The base mode uses a larger image_size (e.g., 1024) and crop_mode=False, suitable for full-page processing.
python model.infer( tokenizer, prompt='<image>document parsing.', image_file='your_image.jpg', output_path='your/output/dir', base_size=1024, image_size=640, crop_mode=True, # Example using 'gundam' config max_length=32768, # Max output length no_repeat_ngram_size=35, ngram_window=128, # Generation parameters save_results=True, )
Multi-page and PDF Parsing:
For documents with multiple pages or PDFs, infer_multi is your go-to, exclusively using the base image configuration (image_size=1024). The system includes a convenient utility, pdf_to_images, leveraging PyMuPDF to convert PDF pages into image files, which can then be fed into infer_multi.
python import tempfile, fitz # PyMuPDF
def pdf_to_images(pdf_path, dpi=300): doc = fitz.open(pdf_path) tmp_dir = tempfile.mkdtemp(prefix='pdf_ocr_') mat = fitz.Matrix(dpi / 72, dpi / 72) paths = [] for i, page in enumerate(doc): out = os.path.join(tmp_dir, f'page_{i+1:04d}.png') page.get_pixmap(matrix=mat).save(out) paths.append(out) doc.close() return paths
model.infer_multi( tokenizer, prompt='<image>Multi page parsing.', image_files=pdf_to_images('your_doc.pdf', dpi=300), output_path='your/output/dir', image_size=1024, max_length=32768, no_repeat_ngram_size=35, ngram_window=1024, save_results=True, )
Notice the max_length parameter, set to a substantial 32768. This highlights the 'long-horizon' capability, allowing the model to generate very extensive text outputs, crucial for comprehensive document parsing.
SGLang for Scalable Inference Serving
For production environments requiring high concurrency and optimized performance, Unlimited OCR integrates seamlessly with SGLang. This provides a robust server-client architecture, allowing you to deploy the model as an OpenAI-compatible API endpoint.
First, set up your SGLang environment, installing the necessary packages, including pymupdf for PDF processing:
bash uv venv --python 3.12 source .venv/bin/activate uv pip install wheel/sglang-0.0.0.dev11416+g92e8bb79e-py3-none-any.whl uv pip install kernels==0.11.7 uv pip install pymupdf==1.27.2.2
Then, launch the SGLang server. Key parameters like --attention-backend fa3, --context-length 32768, and --enable-custom-logit-processor are configured for optimal performance and long context handling:
bash
python -m sglang.launch_server
--model baidu/Unlimited-OCR
--served-model-name Unlimited-OCR
--attention-backend fa3
--page-size 1
--mem-fraction-static 0.8
--context-length 32768
--enable-custom-logit-processor
--disable-overlap-schedule
--skip-server-warmup
--host 0.0.0.0
--port 10000
With the server running, you can send streaming requests via Python, utilizing helper functions to encode images and construct the API payload. The custom_logit_processor and custom_params (like ngram_size and window_size) are crucial for managing output quality and preventing repetition, especially over long parsing tasks.
python import base64 import json import os import tempfile import fitz import requests from sglang.srt.sampling.custom_logit_processor import DeepseekOCRNoRepeatNGramLogitProcessor
server_url = "http://127.0.0.1:10000" session = requests.Session() session.trust_env = False
def pdf_to_images(pdf_path, dpi=300): # ... (same as in Huggingface example) ... doc = fitz.open(pdf_path) tmp_dir = tempfile.mkdtemp(prefix="pdf_ocr_") mat = fitz.Matrix(dpi / 72, dpi / 72) image_paths = [] for i, page in enumerate(doc): image_path = os.path.join(tmp_dir, f"page_{i + 1:04d}.png") page.get_pixmap(matrix=mat).save(image_path) image_paths.append(image_path) doc.close() return image_paths
def encode_image(image_path): ext = os.path.splitext(image_path)[1].lower() mime = "image/jpeg" if ext in (".jpg", ".jpeg") else f"image/{ext.lstrip('.')}" with open(image_path, "rb") as f: data = base64.b64encode(f.read()).decode("utf-8") return {"type": "image_url", "image_url": {"url": f"data:{mime};base64,{data}"}}
def build_content(prompt, image_paths): return [{"type": "text", "text": prompt}] + [encode_image(path) for path in image_paths]
def generate(prompt, image_paths, image_mode, ngram_window): payload = { "model": "Unlimited-OCR", "messages": [{"role": "user", "content": build_content(prompt, image_paths)}], "temperature": 0, "skip_special_tokens": False, "images_config": {"image_mode": image_mode}, "custom_logit_processor": DeepseekOCRNoRepeatNGramLogitProcessor.to_str(), "custom_params": { "ngram_size": 35, "window_size": ngram_window, }, "stream": True, } response = session.post( f"{server_url}/v1/chat/completions", headers={"Content-Type": "application/json"}, data=json.dumps(payload), timeout=1200, stream=True, ) response.raise_for_status() chunks = [] for line in response.iter_lines(chunk_size=1, decode_unicode=True): if not line or not line.startswith("data: "): continue data = line[len("data: "):] if data == "[DONE]": break event = json.loads(data) delta = event["choices"][0].get("delta", {}).get("content", "") if delta: print(delta, end="", flush=True) chunks.append(delta) print() return "".join(chunks)
Example for PDF parsing using SGLang server
generate("Multi page parsing.", pdf_to_images("your_doc.pdf", dpi=300), image_mode="base", ngram_window=1024)
For batch inference, infer.py is provided as a command-line tool that can automatically manage the SGLang server and send concurrent requests, making it ideal for processing directories of images or large PDFs efficiently.
bash
python infer.py
--pdf ./examples/document.pdf
--output_dir ./outputs
--concurrency 8
--image_mode gundam
Practical Takeaways for Developers
- Choose your integration: For quick prototyping or smaller-scale tasks, the Hugging Face Transformers library offers a direct, code-centric approach. For high-performance, scalable inference serving, SGLang is the recommended path, providing an OpenAI-compatible API.
- Handle PDFs seamlessly: Both integration methods provide utilities to convert PDFs into image sequences, abstracting away the multi-page complexity.
- Understand image modes: Leverage
gundamfor focused, cropped single-image analysis where fine detail might be critical, andbasefor full-page or multi-page documents to ensure comprehensive coverage. - Long-horizon capabilities: The
max_length=32768parameter is a key enabler for the 'one-shot long-horizon parsing'. Ensure your infrastructure can support the memory and compute required for such extensive outputs, especially on GPUs. - Refine output quality: Parameters like
no_repeat_ngram_sizeandngram_windoware vital for controlling the quality and preventing repetitive text in the long generated outputs. Experiment with these values to fine-tune results for your specific document types.
Unlimited OCR represents a significant step forward in document understanding, offering robust tools for handling the most demanding parsing tasks with unprecedented contextual awareness.
FAQ
Q: What does "one-shot long-horizon parsing" mean in the context of Unlimited OCR?
A: It refers to the model's ability to process entire documents, potentially spanning multiple pages and containing extensive text, in a single pass. Unlike traditional OCR that might process each page independently, Unlimited OCR aims to understand and parse the document as a whole, maintaining context and consistency across very long textual outputs. This is facilitated by its ability to handle extremely long output lengths, up to 32768 tokens.
Q: When should I use the Hugging Face Transformers approach versus SGLang for Unlimited OCR?
A: The Hugging Face Transformers approach is ideal for direct, programmatic integration within Python applications, prototyping, or scenarios with lower inference volume. It's straightforward to set up and use. SGLang, on the other hand, is designed for high-performance, scalable inference serving. If you need to handle concurrent requests, achieve higher throughput, or deploy the model as a robust, easily consumable API (e.g., in a microservices architecture), SGLang with its dedicated server is the better choice.
Q: What's the difference between the gundam and base image modes for parsing?
A: The gundam mode is typically configured with crop_mode=True and a smaller image_size (e.g., 640), suggesting it's optimized for processing specific, possibly cropped, regions of an image where fine detail is paramount. The base mode uses a larger image_size (e.g., 1024) and crop_mode=False, designed for processing full pages or entire multi-page documents where comprehensive coverage is more important than extreme localized detail. For multi-page and PDF parsing, only the base mode is supported.
Related articles
Is Your Smart Fridge a Scraper? New Data Uncovers Hidden Botnets
New data from Anubis' honeypot reveals a pervasive scraping problem, with nearly 90% of observed scraper IPs not on traditional threat lists. This global phenomenon is likely driven by compromised smart appliances, highlighting a hidden botnet threat. The findings underscore the need for advanced WAFs and user vigilance in securing IoT devices.
Build Your First Multi-Agent AI System with Python and LangGraph
Building Multi-Agent AI Systems: Plain Python vs. LangGraph As developers, we often tackle complex tasks by breaking them down into smaller, manageable pieces. This principle applies equally to AI systems, especially
Unpacking the 'No Spanish Reading Crisis': Lessons for Developers
The Perceived Crisis of Attention in the Digital Age As software developers, we operate in an ecosystem defined by constant information flow and rapid technological shifts. We're acutely aware of the challenges posed by
OpenClaw Machines: Scaling Enterprise AI Agents with Bare Metal
OpenClaw Machines offers an open-source, self-hosted platform for running AI agents with enterprise-grade security and cost efficiency. It utilizes Firecracker microVMs for hardware isolation on your own Linux servers, providing full data sovereignty and predictable costs, especially at scale. The platform includes a control plane for orchestration, a Cloudflare data plane for secure access, and integrated LLM proxying.
Datacenter Emissions: A Looming Challenge for Sustainable Tech
The rapid expansion of cloud services and AI, driven by companies like Microsoft, Amazon, and Google, is causing a significant surge in their carbon emissions. This article explores the scale of this environmental impact, detailing the recent increases and the challenges it poses to their sustainability goals. We examine why this boom is affecting climate ambitions and what developers should consider for a more sustainable future.
Vim in the AI Era: Evolving Workflows for Developers
The landscape of software development is undergoing a profound transformation, largely driven by the rapid advancements in artificial intelligence. For many seasoned developers, this shift has prompted a re-evaluation



