surya ocr 2
简介
核心亮点
- 强大的版面分析能力,精准识别文档结构
- 支持多语言识别,适配复杂排版场景
- 开源可本地部署,无需依赖云端接口
- 理想的 PDF 转 Markdown 前端处理工具
使用方法
# 安装 Hugging Face transformers
pip install transformers torch
# 使用 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 下载
我们推荐使用命令行或者 Hugging Face Hub SDK 来进行模型的下载。
操作指引:在下载前,请先通过如下命令安装 huggingface_hub:
pip install -U huggingface_hub
命令行下载
下载完整模型库
huggingface-cli download datalab-to/surya-ocr-2
下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
huggingface-cli download datalab-to/surya-ocr-2 config.json --local-dir ./dir
SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('datalab-to/surya-ocr-2')
Git 下载
请确保 lfs 已经被正确安装
git lfs install
git clone https://huggingface.co/datalab-to/surya-ocr-2
如果您希望跳过 lfs 大文件下载,可以使用如下命令
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/datalab-to/surya-ocr-2
模型文件托管在 Hugging Face Hub,使用 HF CLI / SDK / Git 直接下载,不经过本站。
PyTorch / Transformers 使用
安装 Transformers
pip install -U transformers torch
模型加载和推理
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained('datalab-to/surya-ocr-2')
tokenizer = AutoTokenizer.from_pretrained('datalab-to/surya-ocr-2')
模型下载
我们推荐使用命令行或者 ModelScope SDK 来进行模型的下载。
操作指引:在下载前,请先通过如下命令安装 ModelScope:
pip install modelscope
命令行下载
下载完整模型库
modelscope download --model datalab-to/surya-ocr-2
下载单个文件到指定本地文件夹(以下载 README.md 到当前路径下 dir 目录为例)
modelscope download --model datalab-to/surya-ocr-2 README.md --local_dir ./dir
SDK 下载
# 模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('datalab-to/surya-ocr-2')
Git 下载
请确保 lfs 已经被正确安装
git lfs install
git clone https://www.modelscope.cn/datalab-to/surya-ocr-2.git
如果您希望跳过 lfs 大文件下载,可以使用如下命令
GIT_LFS_SKIP_SMUDGE=1 git clone https://www.modelscope.cn/datalab-to/surya-ocr-2.git
ModelScope 模型页直接下载模型文件;无需将模型文件放在本站服务器。
Notebook 快速开发
下载并安装 ModelScope library
pip install "modelscope[audio,cv,nlp,multi-modal,science]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html
模型加载和推理
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
p = pipeline('text-generation', 'datalab-to/surya-ocr-2')
完整文档
---
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)##