allenai/olmocr

GitHub: allenai/olmocr

Allen AI 开源的基于视觉语言模型的文档 OCR 工具包,将 PDF 等文档高质量线性化为 Markdown 文本,用于构建 LLM 训练数据集。

Stars: 19090 | Forks: 1570

olmocr-2-full@2x

GitHub License GitHub release Tech Report v1 Tech Report v2 Demo Discord

一个用于将 PDF 和其他基于图像的文档格式转换为干净、易读的纯文本格式的工具包。 试用在线演示:[https://olmocr.allenai.org/](https://olmocr.allenai.org/) 功能: - 将基于 PDF、PNG 和 JPEG 的文档转换为干净的 Markdown - 支持公式、表格、手写内容和复杂格式 - 自动移除页眉和页脚 - 即使存在图表、多栏布局和插图,也能转换为具有自然阅读顺序的文本 - 高效,每转换一百万页的成本不到 200 美元 - (基于 7B 参数的 VLM,因此需要 GPU) ### 新闻 - 2025 年 10 月 21 日 - v0.4.0 - [新模型发布](https://huggingface.co/allenai/olmOCR-2-7B-1025-FP8),通过使用合成数据将 olmOCR-bench 分数提升了约 4 分,并引入了 RL 训练。 - 2025 年 8 月 13 日 - v0.3.0 - [新模型发布](https://huggingface.co/allenai/olmOCR-7B-0825-FP8),修复了自动旋转检测以及空白文档上的幻觉问题。 - 2025 年 7 月 24 日 - v0.2.1 - [新模型发布](https://huggingface.co/allenai/olmOCR-7B-0725-FP8),在 [olmOCR-Bench](https://github.com/allenai/olmocr/tree/main/olmocr/bench) 上的得分高出 3 分,由于默认使用 FP8,运行速度也显著加快,并且每个文档需要重试的次数大大减少。 - 2025 年 7 月 23 日 - v0.2.0 - 全新清理后的[训练器代码](https://github.com/allenai/olmocr/tree/main/olmocr/train),让您自己训练 olmOCR 模型变得更加简单。 - 2025 年 6 月 17 日 - v0.1.75 - 从 sglang 切换到基于 vllm 的推理 pipeline,将 docker 镜像更新为 CUDA 12.8。 - 2025 年 5 月 23 日 - v0.1.70 - 现已支持官方 docker 并提供镜像![查看 Docker 用法](#using-docker) - 2025 年 5 月 19 日 - v0.1.68 - [olmOCR-Bench](https://github.com/allenai/olmocr/tree/main/olmocr/bench) 发布,得分为 77.4。由于修复了与 prompt 相关的 bug,发布时 olmOCR pipeline 的性能提升了 2 个百分点。 - 2025 年 3 月 17 日 - v0.1.60 - 由于在采样中选择了更好的温度参数,性能得到了提升。 - 2025 年 2 月 25 日 - v0.1.58 - 初始公开发布和演示。 ### 基准测试 [**olmOCR-Bench**](https://github.com/allenai/olmocr/tree/main/olmocr/bench): 我们还提供了一个全面的基准测试套件,涵盖了 1400 份文档中的超过 7000 个测试用例,以帮助衡量 OCR 系统的性能。
ArXiv
扫描件
数学
表格
扫描件
页眉

页脚

长篇
微小
文本
基础 总体
Mistral OCR API 77.2 67.5 60.6 29.3 93.6 71.3 77.1 99.4 72.0±1.1
Marker 1.10.1 83.8 66.8 72.9 33.5 86.6 80.0 85.7 99.3 76.1±1.1
MinerU 2.5.4* 76.6 54.6 84.9 33.7 96.6 78.2 83.5 93.7 75.2±1.1
DeepSeek-OCR 77.2 73.6 80.2 33.3 96.1 66.4 79.4 99.8 75.7±1.0
Nanonets-OCR2-3B 75.4 46.1 86.8 40.9 32.1 81.9 93.0 99.6 69.5±1.1
PaddleOCR-VL* 85.7 71.0 84.1 37.8 97.0 79.9 85.7 98.5 80.0±1.0
Infinity-Parser 7B* 84.4 83.8 85.0 47.9 88.7 84.2 86.4 99.8 82.5±?
Chandra OCR 0.1.0* 82.2 80.3 88.0 50.4 90.8 81.2 92.3 99.9 83.1±0.9

olmOCR v0.4.0 83.0 82.3 84.9 47.7 96.1 83.7 81.9 99.7 82.4±1.1
### 安装 #### 系统依赖 您需要安装 poppler-utils 和额外的字体来渲染 PDF 图像。 安装依赖 (Ubuntu/Debian): ``` sudo apt-get update sudo apt-get install poppler-utils ttf-mscorefonts-installer msttcorefonts fonts-crosextra-caladea fonts-crosextra-carlito gsfonts lcdf-typetools ``` #### Python 安装 设置一个 conda 环境并安装 olmocr。运行 olmOCR 的依赖项在现有的 python 环境中很难安装,因此请务必创建一个干净的 python 环境来进行安装。 ``` conda create -n olmocr python=3.11 conda activate olmocr ``` 选择与您的使用场景相匹配的安装选项: **选项 1:远程推理(轻量级)** 如果您计划将远程 vLLM 服务器与 `--server` 标志结合使用,请安装基础包: ``` pip install olmocr ``` 这样可以避免安装像 PyTorch(约 2GB 以上)这样庞大的 GPU 依赖项。 **选项 2:本地 GPU 推理** 要求: - 较新的 NVIDIA GPU(已在 RTX 4090、L40S、A100、H100 上测试过),且具有至少 12 GB 的 GPU 显存 - 30GB 的可用磁盘空间 要使用您自己的 GPU 运行推理: ``` pip install olmocr[gpu] --extra-index-url https://download.pytorch.org/whl/cu128 # 推荐:安装 flash infer 以在 GPU 上加速推理 pip install https://download.pytorch.org/whl/cu128/flashinfer/flashinfer_python-0.2.5%2Bcu128torch2.7-cp38-abi3-linux_x86_64.whl ``` **选项 3:Beaker 集群执行** 要使用 `--beaker` 标志将作业提交到 Beaker 集群: ``` pip install olmocr[beaker] ``` **选项 4:基准测试套件** 用于运行 olmOCR 基准测试套件: ``` pip install olmocr[bench] ``` **组合安装** 您可以组合多个选项: ``` # 支持 GPU + Beaker pip install olmocr[gpu,beaker] --extra-index-url https://download.pytorch.org/whl/cu128 # 支持 GPU + Benchmark pip install olmocr[gpu,bench] --extra-index-url https://download.pytorch.org/whl/cu128 ``` **故障排除** 如果您遇到有关 `too many open files` 的错误,请更新您的 ulimit: ``` ulimit -n 65536 ``` ### 用法示例 如需快速测试,请尝试 [Web 演示](https://olmocr.allen.ai/)。 **转换单个 PDF(本地 GPU):** ``` # 下载示例 PDF curl -o olmocr-sample.pdf https://olmocr.allenai.org/papers/olmocr_3pg_sample.pdf # 将其转换为 markdown olmocr ./localworkspace --markdown --pdfs olmocr-sample.pdf ``` **转换图像文件:** ``` olmocr ./localworkspace --markdown --pdfs random_page.png ``` **转换多个 PDF:** ``` olmocr ./localworkspace --markdown --pdfs tests/gnarly_pdfs/*.pdf ``` **使用远程推理服务器:** ``` olmocr ./localworkspace --server http://remote-server:8000/v1 --model allenai/olmOCR-2-7B-1025-FP8 --markdown --pdfs *.pdf ``` 使用 `--markdown` 标志,结果将作为 markdown 文件存储在 `./localworkspace/markdown/` 内部。 #### 查看结果 随后,`./localworkspace/` 工作区文件夹中将同时包含 [Dolma](https://github.com/allenai/dolma) 文件和 markdown 文件(如果使用了 `--markdown`)。 ``` cat localworkspace/markdown/olmocr-sample.md ``` ``` olmOCR: Unlocking Trillions of Tokens in PDFs with Vision Language Models ... ``` ### 使用推理提供商或外部服务器 如果您已经在其他地方运行了 vLLM 服务器(或者是任何实现了 OpenAI API 的推理平台),您可以将 olmOCR 指向该服务器,而无需生成本地实例。 **用于远程推理的安装:** ``` # 轻量级安装 - 无需 GPU 依赖 pip install olmocr ``` **使用外部服务器:** ``` # 使用外部 vLLM 服务器替代本地服务器 olmocr ./localworkspace --server http://remote-server:8000/v1 --model allenai/olmOCR-2-7B-1025-FP8 --markdown --pdfs tests/gnarly_pdfs/*.pdf ``` vLLM 中提供的模型名称需要与 `--model` 中提供的值相匹配。 **vLLM 服务器启动示例:** ``` vllm serve allenai/olmOCR-2-7B-1025-FP8 --max-model-len 16384 ``` #### 已验证的外部提供商 我们已经在这些外部模型提供商上测试了 `olmOCR-2-7B-1025-FP8`,并确认它们可以正常工作 | | $/1M 输入 token | $/1M 输出 token | 示例命令 | |-----------------------------------------------------------------------------|-------------------|--------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [Cirrascale](https://ai2endpoints.cirrascale.ai/models/overview) | $0.07 | $0.15 | `olmocr ./workspace --server https://ai2endpoints.cirrascale.ai/api --api_key sk-XXXXXXX --workers 1 --max_concurrent_requests 20 --model olmOCR-2-7B-1025 --pdfs tests/gnarly_pdfs/*.pdf` | | [DeepInfra](https://deepinfra.com/) | $0.09 | $0.19 | `olmocr ./workspace --server https://api.deepinfra.com/v1/openai --api_key DfXXXXXXX --workers 1 --max_concurrent_requests 20 --model allenai/olmOCR-2-7B-1025 --pdfs tests/gnarly_pdfs/*.pdf` | | [Parasail](https://www.saas.parasail.io/serverless?name=olmocr-7b-1025-fp8) | $0.10 | $0.20 | `olmocr ./workspace --server https://api.parasail.io/v1 --api_key psk-XXXXX --workers 1 --max_concurrent_requests 20 --model allenai/olmOCR-2-7B-1025 --pdfs tests/gnarly_pdfs/*.pdf` | 参数说明 - `--server`:定义兼容 OpenAI 的 endpoint,例如 `https://api.deepinfra.com/v1/openai` - `--api_key`:您的 API 密钥,通过 Authorization Bearer HTTP 标头传递 - `--max_concurrent_requests`:同时发送给推理提供商的最大并发请求数 - `--workers`:同时处理的最大页面组数。您可能希望将其设置为 `1`,以便在继续下一项之前完成一组内容的处理。 - `--pages_per_group`:您可能需要每组包含较少的页面数,因为许多外部提供商对并发请求的限制较低 - `--model`:模型标识符,例如 `allenai/olmOCR-2-7B-1025`,不同的提供商有不同的名称,如果您在本地运行,则可以使用 `olmocr` - 其他参数的工作方式与本地推理 ### 多节点 / 集群用法 如果您想使用并行运行的多个节点来转换数以百万计的 PDF,olmOCR 支持从 AWS S3 读取 PDF,并使用 AWS S3 输出存储桶来协调工作。 **启动第一个工作节点:** ``` olmocr s3://my_s3_bucket/pdfworkspaces/exampleworkspace --pdfs s3://my_s3_bucket/jakep/gnarly_pdfs/*.pdf ``` 这将在您的 AWS 存储桶中设置一个简单的工作队列,并开始转换 PDF。 **在后续的工作节点上:** ``` olmocr s3://my_s3_bucket/pdfworkspaces/exampleworkspace ``` 它们将自动开始从同一个工作区队列中获取任务。 #### 使用 Beaker 进行集群执行 如果您在 Ai2,并且想要使用 [beaker](https://www.beaker.org) 高效地将数百万个 PDF 线性化,请安装带有 Beaker 支持的版本: ``` pip install olmocr[gpu,beaker] --extra-index-url https://download.pytorch.org/whl/cu128 ``` 然后使用 `--beaker` 标志在本地准备工作区,并在集群中启动 N 个 GPU worker: ``` olmocr s3://my_s3_bucket/pdfworkspaces/exampleworkspace --pdfs s3://my_s3_bucket/jakep/gnarly_pdfs/*.pdf --beaker --beaker_gpus 4 ``` ### 使用 Docker 拉取 Docker 镜像(体积较大,包含模型,约 30GB): ``` docker pull alleninstituteforai/olmocr:latest-with-model ``` 对于希望自己管理模型下载的高级用户,我们还提供了一个不含模型的基础镜像: ``` docker pull alleninstituteforai/olmocr:latest ``` #### 快速开始 - 处理 PDF 处理您当前目录中的单个 PDF: ``` docker run --gpus all \ -v $(pwd):/workspace \ alleninstituteforai/olmocr:latest-with-model \ -c "olmocr /workspace/output --markdown --pdfs /workspace/sample.pdf" ``` 处理多个 PDF: ``` docker run --gpus all \ -v /path/to/pdfs:/input \ -v /path/to/output:/output \ alleninstituteforai/olmocr:latest-with-model \ -c "olmocr /output --markdown --pdfs /input/*.pdf" ``` #### 交互模式 以交互模式运行容器,用于探索和调试: ``` docker run -it --gpus all alleninstituteforai/olmocr:latest-with-model ``` ### 完整文档 要查看所有可用选项: ``` olmocr --help usage: pipeline.py [-h] [--pdfs [PDFS ...]] [--model MODEL] [--workspace_profile WORKSPACE_PROFILE] [--pdf_profile PDF_PROFILE] [--pages_per_group PAGES_PER_GROUP] [--max_page_retries MAX_PAGE_RETRIES] [--max_page_error_rate MAX_PAGE_ERROR_RATE] [--workers WORKERS] [--apply_filter] [--stats] [--markdown] [--target_longest_image_dim TARGET_LONGEST_IMAGE_DIM] [--target_anchor_text_len TARGET_ANCHOR_TEXT_LEN] [--guided_decoding] [--gpu-memory-utilization GPU_MEMORY_UTILIZATION] [--max_model_len MAX_MODEL_LEN] [--tensor-parallel-size TENSOR_PARALLEL_SIZE] [--data-parallel-size DATA_PARALLEL_SIZE] [--port PORT] [--server SERVER] [--beaker] [--beaker_workspace BEAKER_WORKSPACE] [--beaker_cluster BEAKER_CLUSTER] [--beaker_gpus BEAKER_GPUS] [--beaker_priority BEAKER_PRIORITY] workspace Manager for running millions of PDFs through a batch inference pipeline positional arguments: workspace The filesystem path where work will be stored, can be a local folder, or an s3 path if coordinating work with many workers, s3://bucket/prefix/ options: -h, --help show this help message and exit --pdfs [PDFS ...] Path to add pdfs stored in s3 to the workspace, can be a glob path s3://bucket/prefix/*.pdf or path to file containing list of pdf paths --model MODEL Path where the model is located, allenai/olmOCR-7B-0725-FP8 is the default, can be local, s3, or hugging face. --workspace_profile WORKSPACE_PROFILE S3 configuration profile for accessing the workspace --pdf_profile PDF_PROFILE S3 configuration profile for accessing the raw pdf documents --pages_per_group PAGES_PER_GROUP Aiming for this many pdf pages per work item group --max_page_retries MAX_PAGE_RETRIES Max number of times we will retry rendering a page --max_page_error_rate MAX_PAGE_ERROR_RATE Rate of allowable failed pages in a document, 1/250 by default --workers WORKERS Number of workers to run at a time --apply_filter Apply basic filtering to English pdfs which are not forms, and not likely seo spam --stats Instead of running any job, reports some statistics about the current workspace --markdown Also write natural text to markdown files preserving the folder structure of the input pdfs --target_longest_image_dim TARGET_LONGEST_IMAGE_DIM Dimension on longest side to use for rendering the pdf pages --target_anchor_text_len TARGET_ANCHOR_TEXT_LEN Maximum amount of anchor text to use (characters), not used for new models --guided_decoding Enable guided decoding for model YAML type outputs VLLM arguments: --gpu-memory-utilization GPU_MEMORY_UTILIZATION Fraction of VRAM vLLM may pre-allocate for KV-cache (passed through to vllm serve). --max_model_len MAX_MODEL_LEN Upper bound (tokens) vLLM will allocate KV-cache for, lower if VLLM won't start --tensor-parallel-size TENSOR_PARALLEL_SIZE, -tp TENSOR_PARALLEL_SIZE Tensor parallel size for vLLM --data-parallel-size DATA_PARALLEL_SIZE, -dp DATA_PARALLEL_SIZE Data parallel size for vLLM --port PORT Port to use for the VLLM server --server SERVER URL of external vLLM (or other compatible provider) server (e.g., http://hostname:port). If provided, skips spawning local vLLM instance beaker/cluster execution: --beaker Submit this job to beaker instead of running locally --beaker_workspace BEAKER_WORKSPACE Beaker workspace to submit to --beaker_cluster BEAKER_CLUSTER Beaker clusters you want to run on --beaker_gpus BEAKER_GPUS Number of gpu replicas to run --beaker_priority BEAKER_PRIORITY Beaker priority level for the job ``` ## 代码概述 代码中有一些不错的可重用部分,可能对您自己的项目有用: - 一种使用 ChatGPT 4o 获得非常好的自然文本解析的 prompt 策略 - [buildsilver.py](https://github.com/allenai/olmocr/blob/main/olmocr/data/buildsilver.py) - 按语言进行的基础过滤和 SEO 垃圾内容移除 - [filter.py](https://github.com/allenai/olmocr/blob/main/olmocr/filter/filter.py) - 用于 Qwen2.5-VL 的 SFT 微调代码 - [train.py](https://github.com/allenai/olmocr/blob/main/olmocr/train/train.py) - GRPO RL 训练器 - [grpo_train.py](https://github.com/allenai/olmocr/blob/main/olmocr/train/grpo_train.py) - 合成数据生成 - [mine_html_templates.py](https://github.com/allenai/olmocr/blob/main/olmocr/synth/mine_html_templates.py) - 使用 VLLM 通过微调模型处理数百万个 PDF - [pipeline.py](https://github.com/allenai/olmocr/blob/main/olmocr/pipeline.py) - 查看由 PDF 创建的 [Dolma 文档](https://github.com/allenai/dolma) - [dolmaviewer.py](https://github.com/allenai/olmocr/blob/main/olmocr/viewer/dolmaviewer.py) ## 团队 **olmOCR** 由 AllenNLP 团队开发和维护,并由 [Allen Institute for Artificial Intelligence (AI2)](https://allenai.org/) 提供支持。 AI2 是一家非营利性机构,其使命是通过高影响力的 AI 研究和工程为人类做出贡献。 要了解具体是谁对这个代码库做出了贡献,请参阅[我们的贡献者](https://github.com/allenai/olmocr/graphs/contributors)页面。 ## 许可证 **olmOCR** 采用 [Apache 2.0](https://www.apache.org/licenses/LICENSE-2.0) 许可。 可在 [GitHub 上](https://github.com/allenai/olmocr/blob/main/LICENSE)找到完整的许可证副本。 ## 引用 对于 olmOCR v1 和 OlmOCR-bench: ``` @misc{olmocrbench, title={{olmOCR: Unlocking Trillions of Tokens in PDFs with Vision Language Models}}, author={Jake Poznanski and Jon Borchardt and Jason Dunkelberger and Regan Huff and Daniel Lin and Aman Rangapur and Christopher Wilhelm and Kyle Lo and Luca Soldaini}, year={2025}, eprint={2502.18443}, archivePrefix={arXiv}, primaryClass={cs.CL}, url={https://arxiv.org/abs/2502.18443}, } ``` 对于 olmOCR v2 结合 RL 的单元测试奖励(Unit Testing Rewards with RL): ``` @misc{olmocr2, title={olmOCR 2: Unit Test Rewards for Document OCR}, author={Jake Poznanski and Luca Soldaini and Kyle Lo}, year={2025}, eprint={2510.19817}, archivePrefix={arXiv}, primaryClass={cs.CV}, url={https://arxiv.org/abs/2510.19817}, } ```
标签:DLL 劫持, Markdown, OCR, PDF处理, Vectored Exception Handling, 凭据扫描, 大语言模型, 数据集构建, 文档解析, 视觉语言模型, 请求拦截, 逆向工具