surya ocr 2
Overview
Highlights
- High-precision text detection in complex document layouts
- Robust multi-lingual support for global data extraction
- Optimized for RAG pipeline preprocessing and digitization
- OpenRail license allowing flexible commercial integration
- Superior accuracy over traditional heuristic OCR engines
Usage
# Install Hugging Face transformers
pip install transformers torch
# Load model with transformers
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("datalab-to/surya-ocr-2")
tokenizer = AutoTokenizer.from_pretrained("datalab-to/surya-ocr-2")
Hugging Face Download
We recommend downloading the model via the Hugging Face CLI or Hub SDK.
Guidance:Before downloading, install huggingface_hub with:
pip install -U huggingface_hub
CLI Download
Download the full repository
huggingface-cli download datalab-to/surya-ocr-2
Download a single file to a local folder (e.g. config.json into ./dir)
huggingface-cli download datalab-to/surya-ocr-2 config.json --local-dir ./dir
See the official docs for more CLI options
SDK Download
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('datalab-to/surya-ocr-2')
Git Download
Make sure git-lfs is installed first
git lfs install
git clone https://huggingface.co/datalab-to/surya-ocr-2
To skip LFS large-file downloads, use:
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/datalab-to/surya-ocr-2
Model files are hosted on the Hugging Face Hub — download directly via HF CLI / SDK / Git, not through this site.
PyTorch / Transformers Usage
Install Transformers
pip install -U transformers torch
Load the model and run inference
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained('datalab-to/surya-ocr-2')
tokenizer = AutoTokenizer.from_pretrained('datalab-to/surya-ocr-2')
Model Download
We recommend downloading the model via the ModelScope CLI or SDK.
Guidance:Before downloading, install ModelScope with:
pip install modelscope
CLI Download
Download the full repository
modelscope download --model datalab-to/surya-ocr-2
Download a single file to a local folder (e.g. README.md into ./dir)
modelscope download --model datalab-to/surya-ocr-2 README.md --local_dir ./dir
See the docs for more CLI options
SDK Download
# 模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('datalab-to/surya-ocr-2')
Git Download
Make sure git-lfs is installed first
git lfs install
git clone https://www.modelscope.cn/datalab-to/surya-ocr-2.git
To skip LFS large-file downloads, use:
GIT_LFS_SKIP_SMUDGE=1 git clone https://www.modelscope.cn/datalab-to/surya-ocr-2.git
ModelScope 模型页直接下载模型文件;无需将模型文件放在本站服务器。
Notebook Quickstart
Install the ModelScope library
pip install "modelscope[audio,cv,nlp,multi-modal,science]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
Load the model and run inference
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
p = pipeline('text-generation', 'datalab-to/surya-ocr-2')
Full Documentation
---
library_name: transformers
license: openrail
license_link: LICENSE
tags:
- ocr
- pdf
- markdown
- layout
---
<p align="center">
<img src="datalab-logo.png" alt="Datalab Logo" width="150"/>
</p>
<h1 align="center">Datalab</h1>
<p align="center">
<strong>State of the Art models for Document Intelligence</strong>
</p>
<p align="center">
<a href="https://www.datalab.to"><img src="https://img.shields.io/badge/Homepage-datalab.to-blue" alt="Homepage"></a>
<a href="https://documentation.datalab.to"><img src="https://img.shields.io/badge/Docs-Read%20the%20docs-blue" alt="Docs"></a>
<a href="https://www.datalab.to/playground"><img src="https://img.shields.io/badge/Datalab Playground-Try%20it-orange" alt="Datalab Playground"></a>
</p>
<hr/>
Surya
Surya is a 650M param OCR model with these features:
- Accuracy - scores 83.3% on olmOCR-bench (top under 3B params)
- Speed - throughput of 5 pages/s on an RTX 5090
- Multilingual - scores 87.2% on an internal benchmark set of 91 languages (more here)
- Layout analysis (table, image, header, etc.) with reading order
- Table recognition (rows + columns)
It works on a range of documents (see usage and benchmarks).
Try Datalab's Managed Platform
Our managed platform runs both Surya, and variants of our highest accuracy model, Chandra.
Get started with $5 in free credits — sign up (takes under 30 seconds) or try our free public playground.
Model Information
<img src="assets/olmocr_size_chart.png" width="700"/>
| Detection | OCR |
|:----------------------------------------------------------------:|:-----------------------------------------------------------------------:|
| <img src="assets/excerpt.png" width="140"/> | <img src="assets/excerpt_text.png" width="140"/> |
| Layout | Table Recognition |
|:------------------------------------------------------------------:|:-------------------------------------------------------------:|
| <img src="assets/excerpt_layout.png" width="140"/> | <img src="assets/scanned_tablerec.png" width="140"/> |
Surya is named for the Hindu sun god, who has universal vision.
Examples
| Name | Detection | OCR | Layout | Order | Table Rec |
|-------------------|:-----------------------------------:|------------------------------------------:|---------------------------------------------:|------------------------------------------------:|------------------------------------------------:|
| Newspaper | Image | Image | Image | Image | |
| Textbook | Image | Image | Image | Image | |
| Tax Form | Image | Image | Image | Image | Image |
| Handwritten Notes | Image | Image | Image | Image | Image |
| Corporate Doc | Image | Image | Image | Image | Image |
Commercial usage
The Surya code is licensed under Apache 2.0. The model weights use a modified AI Pubs Open Rail-M license (free for research, personal use, and startups under $5M funding/revenue). For broader commercial licensing of the model weights, visit our pricing page here.
Installation
Install with:
pip install surya-ocrUsage
Surya 2 runs layout, OCR, and table recognition through a single VLM served
by vllm (GPU) or llama.cpp (CPU / Apple Silicon). The inference manager
will spawn one for you on first use; you can also point it at an existing
server via SURYA_INFERENCE_URL=http://host:port/v1.
- Inspect the settings in
surya/settings.py. You can override any setting via env var (e.g.SURYA_INFERENCE_BACKEND=vllm).
- Text detection and OCR errors are separate models.
Interactive App
I've included a streamlit app that lets you interactively try Surya on images or PDF files. Run it with:
pip install streamlit pdftext
surya_guiOCR (text recognition)
This command will write out a json file with the detected text and bboxes:
surya_ocr DATA_PATHDATA_PATHcan be an image, pdf, or folder of images/pdfs
--imageswill save images of the pages and detected blocks (optional)
--output_dirspecifies the directory to save results to instead of the default
--page_rangespecifies the page range to process in the PDF, specified as a single number, a comma separated list, a range, or comma separated ranges - example:0,5-10,20.
The results.json file contains a dict keyed by input filename (no extension). Each value is a list of page dicts. Each page dict contains:
blocks- per-block OCR results in reading order
label - canonicalized layout label (e.g. Text, SectionHeader, Table, Equation, Picture, Form, PageHeader, ...). See surya/layout/label.py:LAYOUT_PRED_RELABEL for the full canonical-name set.
- raw_label - original label emitted by the model, before canonicalization
- reading_order - 0-indexed position in layout output
- html - block content as HTML (math wrapped in <math>...</math>, tables as <table>...</table>, etc.). "" if the block was skipped
- polygon - 4-corner polygon in [[x0,y0],[x1,y0],[x1,y1],[x0,y1]] order
- bbox - axis-aligned [x0, y0, x1, y1] derived from the polygon
- confidence - mean per-token probability across the block's decode (0-1)
- skipped - true if the block was a visual label (e.g. Picture) and not OCR'd
- error - true if the block OCR call failed
image_bbox-[0, 0, width, height]for the page image
Performance tips
Throughput is governed by the inference backend, not a RECOGNITION_BATCH_SIZE env var. With vllm, raise --max-num-seqs / --max-num-batched-tokens (or SURYA_INFERENCE_PARALLEL on the client side) to keep more pages in flight. With llama.cpp, set SURYA_INFERENCE_PARALLEL to match --parallel on llama-server.
From python
from PIL import Image
from surya.inference import SuryaInferenceManager
from surya.recognition import RecognitionPredictor
manager = SuryaInferenceManager()
recognition_predictor = RecognitionPredictor(manager)
Default: full-page OCR. One VLM call per page; returns layout + content as
HTML <div data-bbox=... data-label=...> blocks.
predictions = recognition_predictor([Image.open(IMAGE_PATH)])
Block mode: pre-run layout, then per-block OCR. Auto-selected when
layout_results is passed.
from surya.layout import LayoutPredictor
layout = LayoutPredictor(manager)
layouts = layout([Image.open(IMAGE_PATH)])
predictions = recognition_predictor([Image.open(IMAGE_PATH)], layouts)##