olmOCR - PDF文档处理工具包

在这里插入图片描述


一、关于 olmOCR

olmOCR 是用于训练语言模型处理PDF文档的工具包,支持大规模PDF文本解析和转换。


相关资源


包含内容


团队

olmOCR 由AllenNLP团队开发并维护,由艾伦人工智能研究所(AI2)支持。
AI2是一个非营利性研究所,其使命是通过高影响力的AI研究和工程为人类做出贡献。
有关具体贡献此代码库的人员信息,请参阅我们的贡献者页面


二、安装

要求:

  • 近期NVIDIA GPU(已在RTX 4090、L40S、A100、H100上测试),至少20 GB的GPU RAM
  • 30GB的可用磁盘空间

您需要安装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

设置conda环境并安装olmocr

conda create -n olmocr python=3.11
conda activate olmocr

git clone https://github.com/allenai/olmocr.git
cd olmocr

# 对于仅CPU操作,例如运行基准测试
pip install -e .

# 对于实际上传文件并使用自己的GPU进行转换
pip install -e .[gpu] --find-links https://flashinfer.ai/whl/cu124/torch2.4/flashinfer/

三、本地使用示例

为了快速测试,请尝试网络演示。要本地运行,需要GPU,因为推理由sglang提供支持。
转换单个PDF:

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

转换图像文件:

python -m olmocr.pipeline ./localworkspace --pdfs random_page.png

转换多个PDF:

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

结果将存储为JSON在./localworkspace中。


查看结果

提取的文本存储为Dolma风格的JSONL在./localworkspace/results目录中。

cat localworkspace/results/output_*.jsonl

与原始PDF并排查看结果(使用dolmaviewer命令):

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

现在在您喜欢的浏览器中打开./dolma_previews/tests_gnarly_pdfs_horribleocr_pdf.html


在这里插入图片描述


多节点/集群使用

如果您想使用多个并行运行的节点转换数百万PDF文件,那么olmOCR支持从AWS S3读取PDF,并使用AWS S3输出存储桶协调工作。

例如,您可以在第一个工作节点上启动此命令,它将在您的AWS存储桶中设置一个简单的工作队列,并开始转换PDF。

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

现在在任意后续节点上运行此命令,它们将开始从相同的工作区队列中获取项目。

python -m olmocr.pipeline s3://my_s3_bucket/pdfworkspaces/exampleworkspace

如果您在Ai2,并想使用beaker高效地线性化数百万PDF文件,只需添加--beaker标志。这将准备您本地机器上的工作区,然后在集群中启动N个GPU工作节点以开始转换PDF。

例如:

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
usage: pipeline.py [-h] [--pdfs PDFS] [--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] [--model MODEL]
                   [--model_max_context MODEL_MAX_CONTEXT] [--model_chat_template MODEL_CHAT_TEMPLATE] [--target_longest_image_dim TARGET_LONGEST_IMAGE_DIM]
                   [--target_anchor_text_len TARGET_ANCHOR_TEXT_LEN] [--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
  --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
  --model MODEL         List of paths where you can find the model to convert this pdf. You can specify several different paths here, and the script will try to use the
                        one which is fastest to access
  --model_max_context MODEL_MAX_CONTEXT
                        Maximum context length that the model was fine tuned under
  --model_chat_template MODEL_CHAT_TEMPLATE
                        Chat template to pass to sglang server
  --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)
  --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

伊织 xAI 2025-05-01(二)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

富婆E

请我喝杯伯爵奶茶~!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值