GLM-OCR Documentation

repository·main·Indexed 27 days ago

https://github.com/zai-org/glm-ocr

GLM-OCR is a multimodal OCR model for complex document understanding, including formula, table, and information extraction, utilizing a two-stage pipeline of layout analysis and parallel recognition. The project includes a FastAPI-based asynchronous backend, a Z-OCR frontend, and a Python SDK. It supports multiple deployment options including Zhipu MaaS API, vLLM, SGLang, and Docker.

Tokens
17.4K
Snippets
50
Records
114
Agent score
93%

What's inside glm-ocr

  1. Build and preview Z-OCR Frontend production builds

    main

    To prepare the application for production, use the following commands to build the project and preview the resulting build locally:

    1. Build the production version: pnpm build
    2. Preview the production build: pnpm preview
    pnpm build
    pnpm preview
  2. Deploy GLM-OCR using vLLM

    main

    To self-host the model using vLLM, ensure you have installed the selfhosted dependencies and a compatible version of vLLM. You can use the --speculative-config flag to enable Multi-Token Prediction (MTP) for better performance.

    # Install dependencies
    pip install -U "vllm>=0.19.0"
    pip install "transformers>=5.3.0"
    
    # Start vLLM service
    vllm serve zai-org/GLM-OCR  --port 8080 --speculative-config '{"method": "mtp", "num_speculative_tokens": 3}' --served-model-name glm-ocr
  3. Self-host GLM-OCR with vLLM

    main

    Deploy the model locally using vLLM for full control over the pipeline (layout detection, OCR, and formatting).

    Note: You may need to adjust --max-model-len and --gpu-memory-utilization based on your hardware to handle large images or PDFs.

    # Install vLLM
    pip install -U "vllm>=0.19.0"
    
    # Launch the service
    pip install "transformers>=5.3.0"
    
    vllm serve zai-org/GLM-OCR  --port 8080 --speculative-config '{"method": "mtp", "num_speculative_tokens": 3}' --served-model-name glm-ocr
  4. Deploy GLM-OCR via Zhipu MaaS API

    main

    The easiest way to use GLM-OCR without a GPU is via the Zhipu cloud API. The SDK acts as a thin wrapper that forwards documents to the cloud and returns Markdown and JSON layout details.

    1. Obtain an API key from https://open.bigmodel.cn.
    2. Configure your config.yaml to enable MaaS mode.
    pipeline:
      maas:
        enabled: true # Enable MaaS mode
        api_key: your-api-key # Required
  5. Configure ZHIPU_API_KEY for GLM-OCR Handwriting

    main

    The glmocr-handwriting skill requires a ZHIPU_API_KEY environment variable to access the ZhiPu GLM-OCR layout parsing API. You can configure this in three ways:

    1. Global config (Recommended): Set it in openclaw.json under env.vars to share it across all Zhipu skills.
    2. Skill-level config: Set it specifically for this skill in openclaw.json under skills.entries.glmocr-handwriting.env.
    3. Shell environment variable: Add export ZHIPU_API_KEY="your_key" to your shell profile (e.g., ~/.zshrc).

    Obtain your key from the Zhipu Open Platform.

    {
      "env": {
        "vars": {
          "ZHIPU_API_KEY": "你的密钥"
        }
      }
    }
  6. Extend Processing Flows

    main

    To add a new processing flow, create a new class in backend/core/flows/ that inherits from TaskProcessingFlow and implement the process() method. Finally, register it with the FlowFactory.

    from backend.core.flows.base import TaskProcessingFlow, FlowFactory
    
    class MyCustomFlow(TaskProcessingFlow):
        async def process(self) -> Dict[str, Any]:
            # Implement processing logic
            pass
    
    # Register the flow
    FlowFactory.register_flow("custom", MyCustomFlow)
    from backend.core.flows.base import TaskProcessingFlow, FlowFactory
    
    class MyCustomFlow(TaskProcessingFlow):
        async def process(self) -> Dict[str, Any]:
            # Implement processing logic
            pass
    
    # Register the flow
    FlowFactory.register_flow("custom", MyCustomFlow)
  7. Install and Setup GLM OCR Backend

    main

    The GLM OCR backend is an asynchronous document processing system built with FastAPI. It requires Python 3.12+ and SQLite (or another supported database).

    1. Install uv package manager

    macOS / Linux:

    curl -LsSf https://astral.sh/uv/install.sh | sh

    Windows:

    powershell -c "irm https://astral.sh/uv/install.ps1 | iex"

    2. Install dependencies

    uv sync

    3. Configure Environment

    Create a .env file to configure the system:

    APP_NAME=OCR Task System
    DEBUG=False
    HOST=0.0.0.0
    PORT=8000
    DATABASE_URL=sqlite+aiosqlite:///./tasks.db
    OUTPUT_DIR=./data
    WORKER_COUNT=5
    TASK_TIMEOUT=3600

    4. Start the service

    uv run uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000

    Access the interactive Swagger/OpenAPI documentation at http://localhost:8000/docs.

    uv run uvicorn backend.main:app --reload --host 0.0.0.0 --port 8000
  8. Set up the ZHIPU_API_KEY for GLM-OCR Formula Recognition

    main

    The glmocr-formula skill requires a ZHIPU_API_KEY to access the ZhiPu GLM-OCR layout parsing API. You can obtain a key from the Zhipu Open Platform.

    Choose one of the following configuration methods:

    Add the key to your openclaw.json file under env.vars. This allows all Zhipu-related skills to share the same key.

    {
      "env": {
        "vars": {
          "ZHIPU_API_KEY": "YOUR_API_KEY"
        }
      }
    }

    2. Skill-level Configuration

    Set the key specifically for the glmocr-formula skill in openclaw.json:

    {
      "skills": {
        "entries": {
          "glmocr-formula": {
            "env": {
              "ZHIPU_API_KEY": "YOUR_API_KEY"
            }
          }
        }
      }
    }

    3. Shell Environment Variable

    Add the following to your shell profile (e.g., ~/.zshrc or ~/.bashrc):

    export ZHIPU_API_KEY="YOUR_API_KEY"
    {
      "env": {
        "vars": {
          "ZHIPU_API_KEY": "你的密钥"
        }
      }
    }