Chandra OCR

repository·master·Indexed 11 days ago

https://github.com/datalab-to/chandra

A document intelligence model (version 0.2.0) that converts images and PDFs into structured Markdown, HTML, or JSON. It preserves complex layouts, math, tables, and handwriting, supporting 90 languages. Features include a CLI for batch processing, a vLLM server for production throughput, and an interactive Streamlit app.

Tokens
5.5K
Snippets
20
Records
25
Agent score
92%

What's inside Chandra OCR

  1. Reproduce Chandra OCR 2 on olmOCR-bench

    master

    To reproduce the Chandra OCR 2 benchmark score on the upstream olmOCR-bench, follow these steps:

    1. Clone and Install Chandra: Install the package in editable mode and include necessary dependencies for the benchmark.
    2. Serve the Model: Use vLLM to serve the datalab-to/chandra-ocr-2 model on port :8000.
    3. Install olmOCR Bench: Install the olmocr[bench] package and the Playwright dependencies required for KaTeX math rendering.
    4. Run the Benchmark: Execute the benchmark script pointing to your benchmark data directory. The script will download the benchmark (on first run), perform OCR on approximately 1,400 pages via the vLLM server, postprocess, and output the score.

    Reference Result: ~85.8% overall.

    # 1. Clone + install Chandra
    git clone https://github.com/datalab-to/chandra.git
    cd chandra
    pip install -e .
    pip install huggingface_hub unicodeit
    
    # 2. Serve the model with vLLM
    chandra_vllm
    
    # 3. Install the upstream olmOCR bench
    pip install "olmocr[bench]"
    playwright install-deps && playwright install chromium
    
    # 4. Run the benchmark
    python -m chandra.scripts.olmocr_bench --bench-dir ./olmOCR-bench/bench_data
  2. Run the vLLM Server for production

    master

    For production deployments or high-volume batch processing, you can launch a vLLM server using the chandra_vllm command. This launches a Docker container with optimized inference settings.

    Configure the server using the following environment variables:

    • VLLM_API_BASE: Server URL (default: http://localhost:8000/v1)
    • VLLM_MODEL_NAME: Model name for the server (default: chandra)
    • VLLM_GPUS: GPU device IDs (default: 0)
    chandra_vllm
  3. Use the Chandra CLI to process documents

    master

    The chandra CLI tool allows you to process single files or entire directories. You can specify the inference method using the --method flag.

    Inference Methods:

    • vllm: Uses a vLLM server (default).
    • hf: Uses the local HuggingFace implementation (requires chandra-ocr[hf]).

    Output Structure: Each processed file generates a subdirectory containing:

    • <filename>.md: Markdown output
    • <filename>.html: HTML output
    • <filename>_metadata.json: Metadata (page info, token count, etc.)
    • Extracted images saved directly in the output directory.
    # Single file, with vllm server
    chandra input.pdf ./output --method vllm
    
    # Process all files in a directory with local model
    chandra ./documents ./output --method hf
  4. Install Chandra OCR

    master

    You can install Chandra OCR using pip. Depending on your preferred inference backend, choose one of the following installation methods:

    • Base install: Optimized for the vLLM backend (recommended for lightweight installs).
    • HuggingFace backend: Includes torch and transformers. For better performance, it is recommended to also install flash attention.
    • All extras: Installs all dependencies, including the Streamlit interactive app.
    # Base install (for vLLM backend)
    pip install chandra-ocr
    
    # With HuggingFace backend (includes torch, transformers)
    pip install chandra-ocr[hf]
    
    # With all extras
    pip install chandra-ocr[all]
  5. Configure Chandra via environment variables

    master

    Chandra settings can be configured using environment variables or a local.env file. This is useful for managing model checkpoints and vLLM server connections.

    # Model settings
    MODEL_CHECKPOINT=datalab-to/chandra-ocr-2
    MAX_OUTPUT_TOKENS=12384
    
    # vLLM settings
    VLLM_API_BASE=http://localhost:8000/v1
    VLLM_MODEL_NAME=chandra
    VLLM_GPUS=0
  6. Inference Method Defaults and Behavior

    master

    The behavior of the CLI changes based on the selected --method:

    • vllm (Default):

      • Optimized for high throughput.
      • If --batch-size is not provided, it defaults to 28.
      • Supports --max-workers and --max-retries to manage parallel inference and reliability.
    • hf (HuggingFace):

      • Uses a local model.
      • If --batch-size is not provided, it defaults to 1 to avoid memory issues.
      • Does not utilize --max-workers or --max-retries (these are specific to the vLLM implementation in the CLI).
  7. Run the Chandra screenshot visualization Flask app

    master

    The screenshot_app.py script provides a Flask-based web server designed to generate screenshot-ready OCR visualizations. It displays the original image with layout overlays on one side and the extracted markdown on the other.

    To run the application, execute the script directly. By default, it listens on 0.0.0.0:8503.

    python chandra/scripts/screenshot_app.py
  8. Compare Chandra 2 performance via the 90-language benchmark

    master
    Chandra 2 has been evaluated against Gemini 2.5 Flash across 90 languages. The benchmark includes a wide range of high-resource and low-resource languages. On average, Chandra 2 achieves a score of 72.7% +/- 1.2%, compared to Gemini 2.5 Flash's 60.8% +/- 1.3%.
  9. Reference the Chandra CLI options

    master

    The chandra CLI supports the following options for fine-tuning document processing:

    OptionDescription
    --method [hf|vllm]Inference method (default: vllm)
    --page-range TEXTPage range for PDFs (e.g., "1-5,7,9-12")
    --max-output-tokens INTEGERMax tokens per page
    --max-workers INTEGERParallel workers for vLLM
    --include-images/--no-imagesExtract and save images (default: include)
    --include-headers-footers/--no-headers-footersInclude page headers/footers (default: exclude)
    --batch-size INTEGERPages per batch (default: 28 for vllm, 1 for hf)
  10. Review Chandra 2 throughput benchmarks

    master

    Throughput benchmarks were conducted using vLLM on a single NVIDIA H100 80GB GPU with the olmOCR benchmark set (math, tables, scans, multi-column layouts).

    Performance Metrics (96 concurrent sequences):

    • Throughput: 1.44 Pages/sec
    • Average Latency: 60s
    • P95 Latency: 156s
    • Failure Rate: 0%

    Note: Real-world usage is estimated to be approximately 2 pages/s.

  11. Configure the olmOCR benchmark script

    master

    The chandra.scripts.olmocr_bench module accepts several flags to control the benchmarking process, including concurrency, API endpoints, and processing modes.

    FlagDescription
    --image-dpi <int>Set the image DPI (default: 300).
    --workers <int>Number of concurrent vLLM requests.
    --vllm-api-base <url>Override the vLLM server URL (default: http://localhost:8000/v1).
    --skip-inferenceReuse existing candidate markdown; only perform re-scoring.
    --skip-scoringOnly produce candidate markdown (prints the olmocr command).
    --stockUse stock settings without Chandra format correction.
    # Example: Running with 32 workers and a custom API base
    python -m chandra.scripts.olmocr_bench --bench-dir ./olmOCR-bench/bench_data --workers 32 --vllm-api-base http://localhost:8000/v1