MonkeyOCR Documentation

repository·main·Indexed 27 days ago

https://github.com/yuliang-liu/monkeyocr

A high-performance document parsing system using a Structure-Recognition-Relation (SRR) paradigm for English and Chinese documents. It offers model variants including MonkeyOCR-pro-1.2B and MonkeyOCR-pro-3B. The system provides efficient OCR and structure extraction for text, formulas, and tables, outputting results in Markdown, PDF layout visualizations, and JSON metadata. It supports deployment via FastAPI, Gradio, and Docker, with recommended inference via LMDeploy.

Tokens
6.2K
Snippets
17
Records
38
Agent score
92%

What's inside MonkeyOCR

  1. Overview of MonkeyOCR

    main

    MonkeyOCR is a document parsing system based on the Structure-Recognition-Relation (SRR) triplet paradigm. It is designed to simplify multi-tool pipelines while maintaining efficiency by avoiding the use of large multimodal models for full-page processing.

    Key model variants include:

    • MonkeyOCR-pro-1.2B: A leaner, faster version that outperforms the 3B version in accuracy, speed, and efficiency (specifically on Chinese documents).
    • MonkeyOCR-pro-3B: A larger model that achieves high performance on English and Chinese documents, outperforming several closed-source and extra-large open-source VLMs on benchmarks like OmniDocBench.
  2. Check MonkeyOCR license and commercial usage terms

    main

    MonkeyOCR has different licensing terms for code and model weights:

    • Source Code: Released under the Apache License 2.0.
    • Model Weights (v1): Provided for academic research and non-commercial evaluation only.

    Commercial Use: Any commercial deployment, SaaS integration, redistribution, or production use requires a separate written commercial license. For inquiries, contact xbai@hust.edu.cn or ylliu@hust.edu.cn.

  3. Install vLLM as the Inference Backend

    main

    vLLM is an optional inference backend supporting CUDA 12.6 and 11.8. After installation, you must update the chat_config.backend field in your model_configs.yaml file to vllm.

    pip install uv --upgrade
    uv pip install vllm==0.9.1 --torch-backend=cu${CUDA_VERSION}
    pip install -e .

    In model_configs.yaml:

    chat_config:
        backend: vllm
  4. Install MonkeyOCR with transformers backend

    main

    The transformers backend supports CUDA 12.4 and 12.1.

    1. Create and activate a Python 3.10 environment.
    2. Clone the repository and enter the directory.
    3. Install MonkeyOCR in editable mode.
    4. Install PyTorch matching your CUDA version.
    5. Install flash-attn==2.7.4.post1 with --no-build-isolation.
    6. Update model_configs.yaml to set chat_config.backend to transformers and optionally adjust batch_size.
    conda create -n MonkeyOCR python=3.10
    conda activate MonkeyOCR
    
    git clone https://github.com/Yuliang-Liu/MonkeyOCR.git
    cd MonkeyOCR
    
    pip install -e .
    
    export CUDA_VERSION=124
    pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu${CUDA_VERSION}
    
    pip install flash-attn==2.7.4.post1 --no-build-isolation
    # model_configs.yaml
    chat_config:
      backend: transformers
      batch_size: 10
  5. Run MonkeyOCR using Docker on Windows

    main

    To run MonkeyOCR inside a container using WSL2 and Docker Desktop:

    1. Ensure Docker Desktop is running.
    2. Open your WSL terminal:
      wsl
      cd ~
    3. Clone the repository and navigate to the directory:
      git clone https://github.com/Yuliang-Liu/MonkeyOCR
      cd MonkeyOCR
    4. Follow the 'Docker Deployment' instructions in the main README.md to build and run the image.

    Tip: You can use the VS Code Dev Containers extension to connect directly to the container for easier development and modification.

    wsl
    cd ~
    git clone https://github.com/Yuliang-Liu/MonkeyOCR
    cd MonkeyOCR
  6. Install transformers as the Inference Backend

    main

    The transformers backend is an optional method supporting CUDA 12.6. It requires PyTorch and Flash Attention 2. After installation, update the chat_config in model_configs.yaml with the transformers backend and an appropriate batch_size.

    pip install torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 --index-url https://download.pytorch.org/whl/cu126
    pip install -e .
    pip install flash-attn==2.7.4.post1 --no-build-isolation

    In model_configs.yaml:

    chat_config:
      backend: transformers
      batch_size: 10  # Adjust based on your available GPU memory
  7. Install MonkeyOCR with LMDeploy backend

    main

    LMDeploy is the recommended inference backend for MonkeyOCR. Follow the instructions corresponding to your CUDA version.

    For CUDA 12.4 or 12.1

    1. Create and activate a Python 3.10 environment.
    2. Clone the repository and enter the directory.
    3. Set CUDA_VERSION to 124 or 121.
    4. Install PyTorch using the specific index URL for your CUDA version.
    5. Install MonkeyOCR in editable mode.
    6. Install lmdeploy==0.8.0.

    For CUDA 11.8

    1. Create and activate a Python 3.10 environment.
    2. Clone the repository and enter the directory.
    3. Install PyTorch using the index URL for cu118.
    4. Install MonkeyOCR in editable mode.
    5. Install the specific LMDeploy wheel for CUDA 11.8.
    # Example for CUDA 12.4
    conda create -n MonkeyOCR python=3.10
    conda activate MonkeyOCR
    
    git clone https://github.com/Yuliang-Liu/MonkeyOCR.git
    cd MonkeyOCR
    
    export CUDA_VERSION=124
    pip install torch==2.5.1 torchvision==0.20.1 torchaudio==2.5.1 --index-url https://download.pytorch.org/whl/cu${CUDA_VERSION}
    
    pip install -e .
    pip install lmdeploy==0.8.0
  8. Install dependencies for PP-DocLayout_plus-L

    main

    To use the PP-DocLayout_plus-L model, you must install PaddlePaddle and PaddleX in addition to the project's base requirements.

    1. Install PaddlePaddle

    Choose the command based on your NVIDIA driver version to install the GPU-accelerated version. Ensure your PyTorch version is compatible with the installed PaddlePaddle version.

    • For GPU driver version ≥450.80.02 (Linux) or ≥452.39 (Windows): Use the cu118 package.
    • For GPU driver version ≥550.54.14 (Linux) or ≥550.54.14 (Windows): Use the cu126 package.

    2. Install PaddleX

    Install the base version of PaddleX using pip.

  9. Install MonkeyOCR natively on Windows

    main

    To run MonkeyOCR directly on Windows, follow the standard installation guide with CUDA support. After environment setup, you must download the model weights and install a specific Triton version for Windows compatibility.

    1. Download Model Weights

    You can use either Huggingface or ModelScope:

    Using Huggingface:

    pip install huggingface_hub
    python tools/download_model.py

    Using ModelScope:

    pip install modelscope
    python tools/download_model.py -t modelscope

    2. Install Triton for Windows

    Run the following command to ensure compatibility:

    pip install -U "triton-windows<3.4"
    pip install huggingface_hub
    python tools/download_model.py
    
    pip install -U "triton-windows<3.4"
  10. Install PaddleX for PP-DocLayoutV2 support

    main

    To use PP-DocLayoutV2, you must install PaddlePaddle and PaddleX. Ensure your PyTorch version is compatible with the PaddlePaddle version you are installing. It is recommended to use a dedicated Conda environment.

    conda create -n MonkeyOCR python=3.10
    conda activate MonkeyOCR
    
    git clone https://github.com/Yuliang-Liu/MonkeyOCR.git
    cd MonkeyOCR
    
    export CUDA_VERSION=126 # for CUDA 12.6
    # export CUDA_VERSION=118 # for CUDA 11.8
    
    pip install paddlepaddle-gpu==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu${CUDA_VERSION}/
    pip install paddlex[base]==3.3.0
    pip install langchain==0.3.26
  11. Deploy MonkeyOCR via Docker

    main

    Setup

    1. Navigate to the docker directory.
    2. Ensure nvidia-docker2 is installed. If not, run bash env.sh to set up the environment.

    Build and Run

    Standard Build:

    docker compose build monkeyocr

    IMPORTANT: GPU Compatibility Fix If using NVIDIA GPUs from the 20/30/40-series, V100, L20/L40, or similar, you must build the patched image to avoid triton.runtime.errors.OutOfResources: out of resource: shared memory errors:

    docker compose build monkeyocr-fix

    Running Services:

    • Gradio Demo (Port 7860): docker compose up monkeyocr-demo
    • FastAPI Service (Port 7861): docker compose up monkeyocr-api
    • Interactive Dev Environment: docker compose run --rm monkeyocr-dev