How to Parse Long Documents with High Precision Using Baidu's Open-Source Unlimited-OCR
A practical step-by-step guide to setting up and using Baidu's `Unlimited-OCR` for batch-parsing lengthy documents (contracts, invoices, academic papers) while maintaining cross-page context accuracy and extracting structured data.
As a backend developer, have you ever faced this scenario: the business side drops a 200-page contract PDF on you and asks you to extract all amounts, dates, and client info? Run it through traditional OCR, and you get broken context, misaligned tables, and header/footer noise. Today, we'll use Baidu's newly open-sourced
Unlimited-OCRto step-by-step build an OCR parsing pipeline that handles ultra-long documents in a single pass.
Why You Need Unlimited-OCR?
Traditional OCR models are limited by context windows. Documents exceeding a few dozen pages get "truncated," causing cross-page tables to lose relationships and chapter structures to break. Unlimited-OCR's core breakthrough is its single-pass long-context parsing architecture. Through dynamic context expansion and layout-aware networks, it maintains complete semantic coherence across dozens or even hundreds of pages. With 27k+ GitHub stars, it's proven to boost accuracy by over 40% in mixed invoice-contract parsing.
Prerequisites (5-Minute Setup)
Before we start, ensure your environment meets:
- Python 3.8+ (3.10 recommended)
- CUDA 11.7+ (GPU acceleration is optional but highly recommended for long docs)
- At least 8GB RAM and 10GB disk space
- Basic Linux command-line experience
Pro Tip: No GPU? The project supports CPU mode. Processing will be 3-5x slower, which is perfectly fine for development and debugging.
Step 1: Installation & Configuration
Let's start from a clean environment to avoid dependency conflicts. A virtual environment is highly recommended:
bash
## Create and activate a virtual environment
python -m venv ocr-env
source ocr-env/bin/activate
## Install core dependencies (automatically handles torch version matching)
pip install unlimited-ocr[full] -q
Why do this? Unlimited-OCR wraps a complex dependency tree. The [full] tag automatically pulls compatible torch and transformers versions, saving you the headache of manual installation. If you hit permission issues, just append --user.
After installation, run python -c "import unlimited_ocr; print(unlimited_ocr.__version__)" to verify the output. If it prints a version number, your environment is ready.
Step 2: Model Configuration & First Run
Unlimited-OCR uses pre-trained models and will automatically download checkpoints on first use. Create a config.yaml file:
yaml
model:
base: "baidu/unlimited-ocr-base-zh" # Chinese-optimized version
device: "cuda" # Change to "cpu" if no GPU
max_context: 8192 # Maximum supported token length
pipeline:
layout_analysis: true # Enable layout analysis (table/paragraph detection)
output_format: "json" # Structured output
Key Parameter Breakdown:
max_contextdictates how long a document can be processed at once. 8192 tokens roughly cover a 50-page pure-text contract.layout_analysisautomatically identifies headers, footers, and table blocks, preventing information misalignment.
Now, let's run a quick test:
python
from unlimited_ocr import Pipeline
## Load configuration
pipe = Pipeline.from_config("config.yaml")
## Parse a sample PDF
result = pipe.parse("test_invoice.pdf")
print(f"Identified {len(result.pages)} pages, extracted text length: {len(result.full_text)} characters")
Upon successful execution, it will output OCR results per page and global structured data.
Hands-on: Batch Process Contract PDFs & Extract Key Clauses
Assume we have a batch of procurement contract PDFs stored in the contracts/ directory. Our goal is to extract three fields: Contract Amount, Liability for Breach, and Effective Date.
python
import os
import json
from unlimited_ocr import Pipeline
## Initialize pipeline (reuses the config above)
pipe = Pipeline.from_config("config.yaml")
contracts_dir = "./contracts"
extracted_data = []
## Iterate through the directory
for filename in os.listdir(contracts_dir):
if filename.endswith(".pdf"):
filepath = os.path.join(contracts_dir, filename)
doc = pipe.parse(filepath)
# Extract target fields using basic string matching (can be enhanced with LLMs in production)
amount = next((line for line in doc.full_text.split('\n') if '合同总价' in line), None)
date = next((line for line in doc.full_text.split('\n') if '生效日期' in line), None)
extracted_data.append({
"file": filename,
"amount": amount,
"effective_date": date,
"full_text_path": f"./output/{filename}.txt"
})
# Persist full text for backup
with open(f"./output/{filename}.txt", "w", encoding="utf-8") as f:
f.write(doc.full_text)
## Export structured results
with open("contracts_summary.json", "w", encoding="utf-8") as f:
json.dump(extracted_data, f, ensure_ascii=False, indent=2)
print("✅ Batch parsing complete. Results saved to contracts_summary.json")
Code Logic Breakdown:
- The
Pipelineinstance reuses the loaded model, making batch processing highly efficient. - Basic string matching is used initially for keyword extraction. Can be upgraded to Regex or LLMs later for better accuracy.
- Raw text is persisted, facilitating version comparison or manual review later.
Troubleshooting Guide (Hard-Earned Tips)
- CUDA Out of Memory: Long documents consume significant VRAM by default. Add
batch_size: 4to your config for chunked processing. - Garbled Chinese Tables: Ensure
layout_analysis: trueis enabled, and verify thedoc.tablesfield in the output. - Model Download Stuck: Users in mainland China can switch to a mirror source by adding
model_source: "https://hf-mirror.com"to the config. - Scanned PDFs Included:
Unlimited-OCRhas a built-in image enhancement module. If accuracy is low, addimage_enhance: trueto the configuration.
What's Next?
You now have a complete workflow from environment setup to batch parsing using Unlimited-OCR. To go deeper:
- Integrate outputs with an RAG/QA system using LangChain.
- Optimize field extraction rules with Regex.
- Try fine-tuning industry-specific layout recognition models (official fine-tuning scripts are provided).
Open-source tools save us from reinventing the wheel. Next time you face a hundred-page contract, annual report, or academic paper, grab a cup of tea and let the code run. Feel free to drop questions in the comments; I'll reply as soon as possible.
Repository: https://github.com/baidu/Unlimited-OCR
Official Documentation: Contains advanced configs and API references. Highly recommended to bookmark.