文章目錄
- 一、關于 olmOCR
- 相關資源
- 包含內容
- 團隊
- 二、安裝
- 三、本地使用示例
- 查看結果
- 多節點/集群使用
- 管道完整文檔
一、關于 olmOCR
olmOCR 是用于訓練語言模型處理PDF文檔的工具包,支持大規模PDF文本解析和轉換。
相關資源
- 源碼:https://github.com/allenai/olmocr
- 技術報告:https://olmocr.allenai.org/papers/olmocr.pdf
- 在線演示:https://olmocr.allenai.org/
- 社區交流:https://discord.gg/sZq3jTNVNG
- LICENSE:Apache 2.0
包含內容
- 使用ChatGPT 4o獲取高質量自然文本解析的提示策略 - buildsilver.py
- 比較不同管道版本的并排評估工具 - runeval.py
- 基于語言和SEO垃圾郵件移除的基本過濾 - filter.py
- Qwen2-VL和Molmo-O的微調代碼 - train.py
- 使用Sglang通過微調模型處理數百萬PDF文件 - pipeline.py
- 查看從PDF創建的Dolma文檔 - dolmaviewer.py
團隊
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 olmocrgit 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]workspaceManager for running millions of PDFs through a batch inference pipelinepositional 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_PROFILES3 configuration profile for accessing the workspace--pdf_profile PDF_PROFILES3 configuration profile for accessing the raw pdf documents--pages_per_group PAGES_PER_GROUPAiming for this many pdf pages per work item group--max_page_retries MAX_PAGE_RETRIESMax number of times we will retry rendering a page--max_page_error_rate MAX_PAGE_ERROR_RATERate 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 theone which is fastest to access--model_max_context MODEL_MAX_CONTEXTMaximum context length that the model was fine tuned under--model_chat_template MODEL_CHAT_TEMPLATEChat template to pass to sglang server--target_longest_image_dim TARGET_LONGEST_IMAGE_DIMDimension on longest side to use for rendering the pdf pages--target_anchor_text_len TARGET_ANCHOR_TEXT_LENMaximum amount of anchor text to use (characters)--beaker Submit this job to beaker instead of running locally--beaker_workspace BEAKER_WORKSPACEBeaker workspace to submit to--beaker_cluster BEAKER_CLUSTERBeaker clusters you want to run on--beaker_gpus BEAKER_GPUSNumber of gpu replicas to run--beaker_priority BEAKER_PRIORITYBeaker priority level for the job
伊織 xAI 2025-05-01(二)