@jerryjliu0:今天我们开源了 OCRia-1 的权重,这是我们旗舰级的 4B 文档 OCR 视觉语言模型。这是我们的第二次开放权重……
摘要
LlamaIndex 开源了 OCRia-1,一款用于文档 OCR 的旗舰级 4B 视觉语言模型,可将页面图像转换为 Markdown/HTML,并在多个文档解析基准测试中取得领先成绩,包括 ParseBench 和 olmOCR-bench。权重已在 Hugging Face 和 Ollama 上发布,同时提供经生产环境调优的版本(通过 LlamaParse 获取)。
查看缓存全文
缓存时间: 2026/11/24 17:21
今天,我们开源了 OCRia-1 的权重——我们的旗舰 4B 文档 OCR 视觉语言模型 🔥 这是我们两天内的第二次开源权重发布(昨天我们刚刚发布了 OCRia-nano-1!!)。在多个基准测试上,相对于其他 4B 模型,OCRia-1 处于精度与成本的帕累托前沿,这些基准包括 ParseBench、olmOCR-bench(表格)、PulseBench-Tab 和 fr-bench-pdf2md。它是一个极其通用的模型,能够解析哪怕最复杂的文档类型,包括复杂的表格/图表/格式/版面布局。我们发布这些权重是为了推动围绕 OCR 的开放研究,发布在 @huggingface 🤗 和 @ollama 🦙 上。如果你想在生产环境中使用它,欢迎来找我们聊聊。如果你想使用 OCRia 模型家族中针对生产环境调优的最新 SOTA,可以看看 LlamaParse!
在 @huggingface 发布:https://huggingface.co/llamaindex/OCRia-1… 在 @ollama 发布:https://ollama.com/llamaindex/ocria-1… LlamaParse:https://llamaparse.ai
llamaindex/OCRia-1 · Hugging Face
来源:https://huggingface.co/llamaindex/OCRia-1
LlamaIndex 技术报告 · 博客文章 · 推理博客 · LlamaParse · ParseBench · Ollama
OCRia-1 是 LlamaIndex 推出的 4B 视觉语言模型,用于文档解析。它读取一页图像,然后将该页输出为 Markdown,表格则用 HTML 表示;它专为文档解析器最容易出错的内容而构建:合并单元格与跨行/跨列表头、包含数千个单元格的表格、需要从图形上读取数据的图表,以及承载含义的格式,例如删除线、下划线、上下标和复选框。
- 精度: 在包含视觉定位(visual grounding)的两个 ParseBench 排行榜上,它是得分最高的开源权重模型:ParseBench 2 得分 85.2(六个维度,在 61 个系统中排名第七),ParseBench 1 得分 84.7(五个维度,在 158 个系统中排名第二)。它还在 olmOCR-bench 的表格与公式、PulseBench-Tab 和 fr-bench-pdf2md 上均排名第一。参见基准测试。
- 表格: 嵌套表头、以
colspan/rowspan表示的合并单元格、不规则网格以及超长财务表格。 - 图表: 每个图表都会转换为一个 HTML 表格来呈现其数据点,每个值都对应到正确的数据系列和标签(ParseBench 1 图表项得分 82.5,而第二名的开源权重模型为 69.1)。
- 格式与数学: 粗体、斜体、删除线、
<u>下划线、<mark>高亮、[ ]/[x]复选框、[___]空字段,以及 LaTeX 公式,包括手写公式。 - 页面结构: 页眉、页脚、页码和水印分别放在各自的标签中,多栏文本被展开为单栏,图表以 Mermaid 表示。
- 每页一次调用: 文本仅来自页面图像,不使用版面检测器、OCR 引擎或文本层。版面框是事后添加的。
本仓库保存的是未优化的 BF16 权重。为 LlamaParse 提供服务的 OCRia-1 是同一个模型,但经过了量化,并使用投机解码器和自定义算子提供服务,速度因此提升了约十倍。参见速度和OCRia 背后的推理技术栈。
OCRia-1 运行在 LlamaParse 内部,后者同样能够处理多页文档。
OCRia-1 与领先的文档解析 VLM 对比
示例
取自 ParseBench 的页面,以 150 DPI 渲染,并使用贪婪解码转写。右侧图像呈现的是模型写出的内容,未经任何编辑。更多示例(包括现金流量表、银行对账单、时刻表和经济图表)见博客文章。
快速上手
使用 vLLM 0.30 或更高版本提供服务。vLLM 会在启动时为该架构编译算子,需要 CUDA 工具包(nvcc);vllm/vllm-openai Docker 镜像中已包含它。
vllm serve llamaindex/OCRia-1 --served-model-name ocria-1 --max-model-len 32768 \
--limit-mm-per-prompt '{"image": 1}'
每页作为一张图片发送,系统消息使用 prompt.txt,不发送用户文本,并且关闭 thinking。Qwen3.5 的聊天模板默认会开启 thinking,开启后模型会以错误的格式作答。
import base64
from huggingface_hub import hf_hub_download
from openai import OpenAI
prompt = open(hf_hub_download("llamaindex/OCRia-1", "prompt.txt")).read()
page = base64.b64encode(open("page.png", "rb").read()).decode()
client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")
resp = client.chat.completions.create(
model="ocria-1",
messages=[
{"role": "system", "content": prompt},
{"role": "user", "content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{page}"}},
]},
],
temperature=0.0,
max_tokens=24576,
extra_body={"chat_template_kwargs": {"enable_thinking": False}},
)
print(resp.choices[0].message.content)
使用 vLLM 离线推理
from vllm import LLM, SamplingParams
llm = LLM("llamaindex/OCRia-1", max_model_len=32768, limit_mm_per_prompt={"image": 1})
messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": [
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{page}"}},
]},
]
out = llm.chat(messages, SamplingParams(temperature=0.0, max_tokens=24576),
chat_template_kwargs={"enable_thinking": False})
print(out[0].outputs[0].text)
使用 Hugging Face Transformers
import torch
from PIL import Image
from transformers import AutoModelForImageTextToText, AutoProcessor
model = AutoModelForImageTextToText.from_pretrained(
"llamaindex/OCRia-1", dtype=torch.bfloat16, device_map="auto").eval()
processor = AutoProcessor.from_pretrained("llamaindex/OCRia-1")
messages = [
{"role": "system", "content": [{"type": "text", "text": prompt}]},
{"role": "user", "content": [
{"type": "image", "image": Image.open("page.png").convert("RGB")},
]},
]
inputs = processor.apply_chat_template(
messages, add_generation_prompt=True, tokenize=True, return_dict=True,
return_tensors="pt", enable_thinking=False).to(model.device)
ids = model.generate(**inputs, max_new_tokens=24576, do_sample=False)
print(processor.decode(ids[0, inputs["input_ids"].shape[1]:], skip_special_tokens=True))
需要 pip install transformers accelerate torchvision(Transformers 5.3 或更高版本)。用它来试用模型;追求吞吐量请使用 vLLM。
输入
- 每次请求一页。 将 PDF 页面渲染为 300 DPI,例如使用
pymupdf:page.get_pixmap(dpi=300)。 - 图像尺寸。 下面的基准测试数字使用约 410 万像素的图像预算,对于 letter 尺寸的页面约合 210 DPI。若要对齐这些数字,请将更大的渲染图缩小:
from PIL import Image
img = Image.open("page.png")
scale = min(1.0, (4_100_000 / (img.width * img.height)) ** 0.5)
img = img.resize((round(img.width * scale), round(img.height * scale)), Image.LANCZOS)
更大的图像同样可用,但会消耗更多图像 token;该模型是在约 1,024 像素的页面上训练的。
- 不使用 PDF 文本。 模型只读取图像。
输出
Markdown 位于 ```markdown 围栏内。去掉围栏:
import re
m = re.search(r"```markdown\n(.*?)(?:\n```|$)", text, re.S)
markdown = m.group(1) if m else text
| 内容 | OCRia-1 的写法 |
|---|---|
| 表格,以及图表转为数据 | HTML <table>,带 colspan/rowspan |
| 图表和流程图 | ```mermaid 块 |
| 公式 | $...$ 行内,$$...$$ 独立成行 |
| 化学式 | 纯文本、不加 $,例如 K3Fe(CN)6 |
| 复选框和空字段 | [ ]、[x]、[___] |
| 图片 | ![label: description],label 取自 logo、icon、photo、illustration、screenshot、map、signature、seal、code、drawing、image 之一 |
| 页面装饰元素 | <page_header>、<page_footer>(位于页眉或页脚内)、<page_number> |
使用贪婪解码(temperature=0),max_tokens 设为 24,576:内容密集的页面可能需要超过 20,000 个输出 token。
prompt
模型在训练和评估时使用的就是下面这个系统提示词,它同样保存在 prompt.txt 中。请原样使用;更换提示词会改变输出格式。
Show the prompt
Transcribe this Page in markdown, be as accurate as possible in your transcription, do not describe/summarize.
Formatting:
- Use ** for bold text and * for italic text.
- Use ~~ text ~~ for strikethrough text. Only use this for text that is visually stricken through.
- Use and tags for highlighted text, aka text to wich a stabilo hilight was applied
Only use to hilight sentence or words, not full block of text/tables.
Use HTML color names for different colors of highlight, e.g. text.
Do not use mark for first cell of a table, as it is often used for the header and should not be highlighted.
- Use and tags for underlined text.
- Use \* for bullet points and - for sub-bullets.
- Use > for blockquotes.
- Use ` for inline code.
- Use `` for code blocks.
- Use [ ] for empty checkboxes.
- Use [x] for checked checkboxes.
- Use and for subscript text.
- Use and for superscript text.
- Use [___] for empty fields to fill, if the field is already filled, represent them as field content in markdown.
- Use $...$ for inline equations and $$...$$ for display equations. Do NOT use \(...\) or \[...\] — the corpus uses dollar delimiters (43,604 inline occurrences against 35 in the parenthesis form).
- Chemical formulas are NOT LaTeX: write them as plain text with and , e.g. K3Fe(CN)6 and 2H2O, never $K_3Fe(CN)_6$. Reserve $...$ for real mathematics.
Tables:
- Output tables in HTML when possible.
- Table should not be use for page formatting (column or grid like layout), but only for tabular data.
- Some table may be irregular, with merged cells, missing cells, or inconsistent formatting, or have an exotic structure (like a triangular table). Do your best to represent the table accurately with correct reading order.
Charts, Graphs, and Figures:
- IMPORTANT: Every chart or graph MUST be converted into an HTML . This is non-negotiable. The table represent the chart data, not the chart layout. Focus on accurately capturing the underlying data and relationships in the table format.
- FORBIDDEN outputs for charts: , <embed>, <canvas>, <img>, <object>, links, URLs, or plain-text descriptions. Using any of these for a chart is an automatic FAIL.
- MERMAID FALLBACK: For diagrams, figures and flowcharts that are better represented as a graph than as a table (architecture diagrams, flowcharts, org charts, entity relationships), use Mermaid syntax INSTEAD of an HTML table and output it between ``mermaid and `` tags. This exception applies ONLY to diagrams; a chart or graph plotting data values is still an HTML <table>.
Images:
- Transcribe any text printed inside the image as markdown.
- Reference the image itself as ![label: description], where label is exactly one of: logo, icon, photo, illustration, screenshot, map, signature, seal, code, drawing, image. Use the caption or wording the page itself prints for the description; do not invent one, and do not describe an image the page says nothing about.
Page furniture:
- Output the page header (if any) between <page_header> and </page_header> tags.
- Output the page footer (if any) between <page_footer> and </page_footer> tags.
- Output the printed page number (if any) between <page_number> and </page_number> tags, nested inside the header or footer band it sits in.
Watermarks:
- Output a watermark stamped across the page (DRAFT, OBSOLETE, CONFIDENTIAL) between <watermark> and </watermark> tags, not as body text.
Multi-column Text:
- Unroll multi-column text into a single column format.
Completeness:
- Ensure ALL content from the page is transcribed without omissions.
- As this is a transcription task, fidelity is of upmost importance. You should NEVER truncate or shorten tabular data. Any instance of table data that looks like "[...table data continues in rows n - m ...]" or omits rows or columns will result in an automatic FAIL.
Accuracy:
- Maintain text and formatting as close to the original as possible within the constraints of Markdown syntax.
Handwriting / Cursive Text:
- If the page contains handwriting or cursive text, ignore the OCR output for those sections and rely solely on the screenshot to transcribe that content.
Output result between ``markdown and `` ``
版面框(实验性)
OCRia-1 本身不输出版面框。ocria_grounding/ 包会在生成之后添加它们:
- 版面检测器找出页面上的各个区域。使用的是 Docling 的 Heron(docling-project/docling-layout-heron,Apache-2.0),首次使用时从其自身的仓库下载。
- OCRia-1 答案中的每个词都会获得一个位置。在带有文本层的 PDF 页面上,这就是该词在 PDF 中的精确方框;否则,用同一份权重对页面和答案再跑一遍,即可看出模型在书写每个词时看向了哪里,然后把该位置吸附到页面上的笔迹。
- 每个区域变成一个条目,包含落在其内部的答案词元:一个标题、一个段落、一个表格、一幅图片、一个页眉或页脚。
文本可以来自任意服务端,包括 vLLM。这些方框需要 Transformers 和一块 GPU。
运行它
pip install "transformers==5.22.2" accelerate torchvision timm scipy pypdfium2 huggingface_hub
huggingface-cli download llamaindex/OCRia-1 --local-dir OCRia-1
python OCRia-1/examples/boxes.py doc.pdf --page 0 # PDF 页:来自文本层的精确词位置
python OCRia-1/examples/boxes.py page.png # 图片:来自模型注意力的词位置
该脚本会把转写结果和各个条目写入 <name>.boxes.json,并绘制到 <name>.boxes.png 上。传入 --attention-only 可以跳过版面检测器。
也可以在自己的代码中调用该包:
import sys
from huggingface_hub import snapshot_download
sys.path.insert(0, snapshot_download("llamaindex/OCRia-1",
allow_patterns=["ocria_grounding/*", "prompt.txt"]))
from ocria_grounding import (load, load_detector, transcribe,
ground_layout, render_pdf_page)
g = load() # OCRia-1,使用 Transformers,运行在 CUDA 上
det = load_detector() # Heron,运行在 CUDA 上(否则 MPS,再否则 CPU)
image, words = render_pdf_page("doc.pdf", 0) # 以 300 DPI 渲染的页面及其文本层词元
answer = transcribe(g, image) # 或者来自 vLLM 或其他服务端的、该图像的原始答案
page = ground_layout(g, image, answer, detector=det, words=words)
for it in page["items"]:
print(it["type"], it["box"], it["md"][:60])
如果输入是图片而非 PDF,请传入 image = Image.open("page.png").convert("RGB") 且 words=None。
若要为别处提供服务的文本(例如另一台机器上的 vLLM)添加方框,请把方框服务跑在 GPU 旁边:python OCRia-1/examples/serve_boxes.py --port 8100。POST /ground 接收页面图像(base64 PNG)、原始答案以及页面的文本层词元(ocria_grounding.pdf.page_words,图片则传 null),返回与 ground_layout 相同的 items。
ParseBench 的 ocria_1 流水线使用的就是它。
page["items"] 按阅读顺序列出各个条目。每个条目都有一个 type(Text、`S
相似文章
llamaindex/OCRia-1 · Hugging Face
LlamaIndex 发布 OCRia-1,一个用于文档解析的 4B 视觉语言模型,可将页面图像直接转为 Markdown/HTML,并在 ParseBench 等多项基准上取得开源权重模型最高分。
@jerryjliu0:为庆祝黄仁勋的回归及其对开源 AI 的承诺:今天,我们发布 OCRia-nano-1,一款……
LlamaIndex 发布了 OCRia-nano-1,这是一款拥有 810 万参数的开放权重 OCR 模型,在 CPU 上解析页面仅需 426 毫秒,性能优于 Marker、PP-OCRv6 和 Tesseract,同时运行速度快 2–5 倍,专为 PB 级文档摄取设计,用作首轮解析器。
@jerryjliu0:今天我们发布 OCRia-1,一款用于文档解析的前沿 VLM,在准确率、成本和延迟方面均达到新的 SOTA……
Jerry Liu 宣布发布 OCRia-1,一款面向文档解析的前沿 VLM,在准确率、成本和延迟上均达到新的 SOTA,并在 ParseBench 及多个第三方基准上取得领先成绩。
@jerryjliu0:我们当前的核心使命是利用 AI 解决文档 OCR 问题。我们所有的产品线,从商业产品(LlamaParse)到……
LlamaIndex 对其官网进行了全面改版,并重申了以 AI 驱动文档 OCR 的核心使命,旗下产品涵盖商业产品 LlamaParse 以及开源工具 LiteParse 和 ParseBench。LlamaParse 采用基于 VLM 的智能文档理解技术,可大规模处理复杂版式、表格、图表及手写文字。
@jerryjliu0:OCRia-1.5 是解析 PDF 中最复杂表格最精准的模型之一——密集、长表格……
LlamaIndex 推出 OCRia-1.5,这是一款文档解析模型(提供 4B 和 2B 两种规格),在复杂表格、图表和表单解析以及视觉定位方面均有提升,在 LlamaParse 的 ParseBench-2 基准上树立了成本与准确率的新帕累托前沿。