Surya Document Intelligence Toolkit

repository·master·Indexed 12 days ago

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

A high-performance document intelligence toolkit featuring a 650M parameter OCR model. Surya provides multilingual OCR in 90+ languages, layout analysis, reading order detection, and table recognition. It includes a Python API and CLI tools (surya_ocr, surya_detect, surya_layout, surya_table) and supports inference via vLLM for NVIDIA GPUs or llama.cpp for CPU and Apple Silicon.

Tokens
12K
Snippets
33
Records
43
Agent score
97%

What's inside Surya

  1. Surya 2 Model Architecture and Training

    master

    Surya 2 uses a unified vision-language model architecture (Qwen3.5-style, approximately 650M parameters) to handle Layout, OCR, and table recognition.

    • Layout, OCR, and Table Recognition: These tasks share the single vision-language model. The model is trained to emit either a layout JSON or a full-page HTML output based on the provided prompt.
    • Text-line Detection: This is handled by a separate, small PyTorch model. It is a modified EfficientViT segformer trained specifically on document line annotations.

    If you require assistance fine-tuning Surya on custom data or wish to use the managed training stack, contact hi@datalab.to.

  2. Manage Surya inference server lifecycle

    master

    By default, every command spawns the VLM server on startup and shuts it down on exit. To avoid the startup/model-load cost when running multiple commands, use the --keep_server flag. This allows subsequent commands to attach to the already running server.

    Alternatively, set the environment variable SURYA_INFERENCE_KEEP_ALIVE=1 to make keep-alive the default behavior.

    To stop the server manually:

    • For NVIDIA/vLLM: docker stop <container_id>
    • For CPU/llama.cpp: Kill the llama-server process.
    surya_ocr    DATA_PATH --keep_server   # spawns the server and leaves it up
    surya_layout DATA_PATH                 # attaches to the running server
    surya_table  DATA_PATH                 # attaches to the running server
  3. Migrate from Surya v1 to v2

    master

    In v2, SuryaInferenceManager replaces FoundationPredictor. A single manager instance should be shared across LayoutPredictor, RecognitionPredictor, and TableRecPredictor.

    Key Schema Changes:

    • text_lines is now blocks (which includes html).
    • Layout output: top_k is removed, count is added.
    • Table recognition: is_header, colspan, and rowspan have been removed from cells.
    # v2 migration example
    from surya.inference import SuryaInferenceManager
    from surya.recognition import RecognitionPredictor
    
    manager = SuryaInferenceManager()              # auto-spawns vllm or llama-server
    rec = RecognitionPredictor(manager)
    predictions = rec([image])
  4. Install Surya for development using uv

    master

    To develop on Surya, install it manually using uv:

    git clone https://github.com/datalab-to/surya.git
    cd surya
    uv sync --group dev      # installs runtime + dev deps
    uv run surya_ocr ...     # or source .venv/bin/activate to enter the venv
    git clone https://github.com/datalab-to/surya.git
    cd surya
    uv sync --group dev
    uv run surya_ocr ...
  5. Configure Surya inference backends

    master

    Layout, OCR, and table recognition tasks share a Vision Language Model (VLM) served via vllm (GPU) or llama.cpp (CPU/Apple Silicon). The SuryaInferenceManager can automatically spawn these, or you can connect to an existing server using environment variables.

    Environment Variables:

    VariableDefaultDescription
    SURYA_INFERENCE_BACKENDautovllm (NVIDIA), llamacpp (else), or unset (auto)
    SURYA_INFERENCE_URL(auto-spawn)URL of a running OpenAI-compatible server
    SURYA_INFERENCE_PARALLEL8Client-side concurrency to the backend
    SURYA_INFERENCE_KEEP_ALIVEfalseIf true, leaves the spawned server up after exit
    SURYA_GUIDED_LAYOUTtrueEnables JSON-schema-constrained layout decode

    Example: Attaching to an existing vLLM server:

    export SURYA_INFERENCE_BACKEND=vllm
    export SURYA_INFERENCE_URL=http://localhost:8000/v1
  6. Launch the Surya Screenshot Viewer

    master

    The surya_screenshot command launches a web-based viewer designed for creating clean screenshots of OCR results. The application displays a PDF or image page on the left and the full-page OCR output on the right side-by-side.

    To use it:

    1. Run the command surya_screenshot in your terminal.
    2. Open your web browser and navigate to http://localhost:8504.

    Features include:

    • Page scrolling and previewing before running OCR.
    • Exporting the side-by-side view as a PNG.
    • Support for .pdf, .png, .jpg, .jpeg, .gif, and .webp files.
    surya_screenshot
  7. Run the text-detection server via CLI

    master

    You can run the shared text-detection server, which uses a single EfficientViT instance with continuous batching, using the following command. The server batches forward passes across all clients and handles heatmap-to-box post-processing in a thread pool.

    python -m surya.detection.server --port <PORT>

    Arguments:

    • --port: (Required) The port number to listen on.
    • --host: The host address (defaults to settings.DETECTOR_SERVER_HOST).
    • --checkpoint: Optional path to a specific model checkpoint.
    python -m surya.detection.server --port P
  8. Troubleshoot OCR and detection issues

    master

    If you are experiencing poor OCR or detection results, consider the following:

    • Image Resolution: Try increasing the image resolution. If it is already very high, try decreasing it to a maximum width of 2048px.
    • Preprocessing: Binarizing or deskewing images can help with old or blurry scans.
    • Threshold Tuning: Adjust the following environment variables:
      • DETECTOR_BLANK_THRESHOLD: Controls space between lines. Predictions below this are considered blank. (Range: 0-1)
      • DETECTOR_TEXT_THRESHOLD: Controls how text is joined. Predictions above this are considered text. (Range: 0-1)
      • Note: DETECTOR_TEXT_THRESHOLD should always be higher than DETECTOR_BLANK_THRESHOLD.
  9. Use Surya OCR via Python API

    master

    You can integrate Surya directly into your Python workflows using SuryaInferenceManager and RecognitionPredictor.

    Full-page OCR mode: Performs one VLM call per page. Returns PageOCRResult containing .blocks and .image_bbox.

    Block mode: Pre-runs layout analysis, then performs per-block OCR. This is automatically selected if you pass layout_results to the predictor.

    from PIL import Image
    from surya.inference import SuryaInferenceManager
    from surya.recognition import RecognitionPredictor
    from surya.layout import LayoutPredictor
    
    manager = SuryaInferenceManager()
    recognition_predictor = RecognitionPredictor(manager)
    
    # 1. Full-page OCR
    predictions = recognition_predictor([Image.open(IMAGE_PATH)])
    
    # 2. Block mode (Layout + Per-block OCR)
    layout = LayoutPredictor(manager)
    layouts = layout([Image.open(IMAGE_PATH)])
    predictions = recognition_predictor([Image.open(IMAGE_PATH)], layouts)