olmOCR是一个用于处理复杂PDF文档的工具包,由艾伦人工智能研究所(AI2)开发。

olmOCR利用大型语言模型(例如ChatGPT 4)对PDF文档进行自然文本解析。olmOCR并非简单的OCR,而是整合了多种技术,包括:巧妙的提示策略以获得高质量的文本解析;用于比较不同处理流程版本的评估工具;基于语言和SEO垃圾内容的过滤功能;针对特定模型(如Qwen2-VL和Molmo-O)的微调代码;以及使用Sglang进行大规模PDF处理的流水线工具。

olmOCR支持本地和多节点(集群)运行,尤其适合处理海量PDF文档,能与AWS S3和Beaker平台集成。olmOCR的结果以Dolma格式的JSONL文件存储,提供可视化工具方便查看。

olmOCR面向语言模型训练需求,提供PDF文档内容解析与格式转换的解决方案。核心功能包括文本提取优化、处理效果评估、内容过滤及模型微调支持。

olmOCR技术组件

ChatGPT 4o文本解析策略(buildsilver.py)

多版本处理流程对比评估工具(runeval.py)

语言过滤与SEO垃圾内容清除模块(filter.py)

Qwen2-VL/Molmo-O模型微调代码(train.py)

Sglang驱动的批量处理系统(pipeline.py)

硬件要求

NVIDIA GPU(RTX 4090/L40S/A100/H100)

30GB可用存储空间

Poppler-utils及扩展字体支持

环境配置(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

conda create -n olmocr python=3.11
conda activate olmocr
git clone https://github.com/allenai/olmocr.git 
cd olmocr
pip install -e .

pip install sgl-kernel==0.0.3.post1 --force-reinstall --no-deps
pip install "sglang[all]==0.4.2" --find-links https://flashinfer.ai/whl/cu124/torch2.4/flashinfer/

基础使用

单文件处理:

python -m olmocr.pipeline ./localworkspace --pdfs tests/gnarly_pdfs/horribleocr.pdf

批量处理:

python -m olmocr.pipeline ./localworkspace --pdfs tests/gnarly_pdfs/*.pdf

JSONL格式输出:

cat localworkspace/results/output_*.jsonl

可视化对比:

python -m olmocr.viewer.dolmaviewer localworkspace/results/output_*.jsonl

集群部署

AWS S3工作流:

python -m olmocr.pipeline s3://my_s3_bucket/pdfworkspaces/exampleworkspace --pdfs s3://my_s3_bucket/jakep/gnarly_pdfs/*.pdf

Beaker集群支持(AI2内部):

python -m olmocr.pipeline s3://my_s3_bucket/pdfworkspaces/exampleworkspace --pdfs s3://my_s3_bucket/jakep/gnarly_pdfs/*.pdf --beaker --beaker_gpus 4

参数说明

python -m olmocr.pipeline --help
# 显示完整参数列表,包括:
# --pages_per_group 页面分组数量
# --max_page_retries 单页重试次数
# --target_longest_image_dim 图像最大边长
# --model_max_context 模型上下文长度限制