Skip to main content



Sinapsis GLM OCR

GLM-OCR-based Optical Character Recognition (OCR) for images

🐍 Installation • 🚀 Features • 📚 Usage example • 📙 Documentation • 🔍 License

Sinapsis GLM OCR provides a powerful implementation for extracting text from images using Zhipu AI's GLM-OCR model. Built on the GLM-V encoder-decoder architecture, it supports document parsing (text, formula, table recognition) and structured information extraction via JSON schema prompts. It also supports batch inference for faster processing of multiple images.

🐍 Installation

Install using your package manager of choice. We encourage the use of uv

Example with uv:

  uv pip install sinapsis-glm-ocr --extra-index-url https://pypi.sinapsis.tech

or with raw pip:

  pip install sinapsis-glm-ocr --extra-index-url https://pypi.sinapsis.tech

with uv:

  uv pip install sinapsis-glm-ocr[all] --extra-index-url https://pypi.sinapsis.tech

or with raw pip:

  pip install sinapsis-glm-ocr[all] --extra-index-url https://pypi.sinapsis.tech

🚀 Features

Templates Supported

This module includes templates tailored for the GLM-OCR engine:

  • GLMOCRInference: Uses GLM-OCR model to extract text from images. Supports document parsing (text, formula, table) and structured information extraction.
  • GLMOCRBatchInference: Batch inference version for processing multiple images efficiently.
GLMOCRInference Attributes
  • prompt (str): The prompt to send to the model. Defaults to "Text Recognition:". Other options include "Formula Recognition:" and "Table Recognition:".
  • init_args (GLMOCRInitArgs): Initialization arguments for the model including:
    • pretrained_model_name_or_path: Model identifier. Defaults to "zai-org/GLM-OCR".
    • torch_dtype: Model precision ("float16", "bfloat16", "auto"). Defaults to "auto".
    • attn_implementation: Attention implementation ("kernels-community/flash-attn2", "kernels-community/paged-attention"). Defaults to "kernels-community/flash-attn2".
    • device_map: Device mapping ("auto", "balanced", "balanced_low_0", "sequential", or specific device like "cuda:0"). Defaults to "auto".
    • Note: This model requires CUDA. CPU inference is not supported.
  • generation_config (GLMOCRGenerationConfig): Generation configuration including:
    • max_new_tokens: Maximum tokens to generate. Defaults to 8192.
    • min_new_tokens: Minimum tokens to generate. Defaults to 1.
    • do_sample: Whether to use sampling. Defaults to False.
    • repetition_penalty: Penalty for repeating tokens. Defaults to 1.0.
    • length_penalty: Penalty for sequence length. Defaults to 1.0.

📚 Usage example

Text Recognition
agent:
  name: glm_ocr_agent
  description: Agent to run inference with GLM OCR for text recognition

templates:
- template_name: InputTemplate
  class_name: InputTemplate
  attributes: {}

- template_name: FolderImageDatasetCV2
  class_name: FolderImageDatasetCV2
  template_input: InputTemplate
  attributes:
    data_dir: dataset/input

- template_name: GLMOCRInference
  class_name: GLMOCRInference
  template_input: FolderImageDatasetCV2
  attributes:
    prompt: "Text Recognition:"
    init_args:
      pretrained_model_name_or_path: zai-org/GLM-OCR
      torch_dtype: auto
      attn_implementation: kernels-community/flash-attn2
      device_map: auto
    generation_config:
      max_new_tokens: 8192
      do_sample: false
Table Recognition
agent:
  name: glm_ocr_table_agent
  description: Agent to run inference with GLM OCR for table recognition

templates:
- template_name: InputTemplate
  class_name: InputTemplate
  attributes: {}

- template_name: FolderImageDatasetCV2
  class_name: FolderImageDatasetCV2
  template_input: InputTemplate
  attributes:
    data_dir: dataset/input

- template_name: GLMOCRInference
  class_name: GLMOCRInference
  template_input: FolderImageDatasetCV2
  attributes:
    prompt: "Table Recognition:"
    init_args:
      pretrained_model_name_or_path: zai-org/GLM-OCR
      torch_dtype: auto
      attn_implementation: kernels-community/flash-attn2
      device_map: auto
    generation_config:
      max_new_tokens: 8192
      do_sample: false
Information Extraction (JSON Schema)
agent:
  name: glm_ocr_json_agent
  description: Agent to extract structured information using JSON schema

templates:
- template_name: InputTemplate
  class_name: InputTemplate
  attributes: {}

- template_name: FolderImageDatasetCV2
  class_name: FolderImageDatasetCV2
  template_input: InputTemplate
  attributes:
    data_dir: dataset/input

- template_name: GLMOCRInference
  class_name: GLMOCRInference
  template_input: FolderImageDatasetCV2
  attributes:
    prompt: |
      Extract the information from the provided image and output
      it strictly as a valid JSON object matching the following schema.
      {
        "name": "string",
        "date": "string",
        "amount": "number"
      }
    init_args:
      pretrained_model_name_or_path: zai-org/GLM-OCR
      torch_dtype: auto
      attn_implementation: kernels-community/flash-attn2
      device_map: auto
    generation_config:
      max_new_tokens: 8192
      do_sample: false
Batch Inference
agent:
  name: glm_ocr_batch_agent
  description: Agent to run batch inference with GLM OCR for faster processing

templates:
- template_name: InputTemplate
  class_name: InputTemplate
  attributes: {}

- template_name: FolderImageDatasetCV2
  class_name: FolderImageDatasetCV2
  template_input: InputTemplate
  attributes:
    data_dir: dataset/input
    batch_size: 4

- template_name: GLMOCRBatchInference
  class_name: GLMOCRBatchInference
  template_input: FolderImageDatasetCV2
  attributes:
    prompt: "Text Recognition:"
    init_args:
      pretrained_model_name_or_path: zai-org/GLM-OCR
      torch_dtype: auto
      attn_implementation: kernels-community/flash-attn2
      device_map: auto
    generation_config:
      max_new_tokens: 8192
      do_sample: false

To run, simply use:

sinapsis run name_of_the_config.yml

📙 Documentation

Documentation for this and other sinapsis packages is available on the sinapsis website

Tutorials for different projects within sinapsis are available at sinapsis tutorials page

🔍 License

This project is licensed under the AGPLv3 license, which encourages open collaboration and sharing. For more details, please refer to the LICENSE file.

For commercial use, please refer to our official Sinapsis website for information on obtaining a commercial license.

Metadata

Release files for sinapsis-glm-ocr 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sinapsis-glm-ocr 0.1.2
File Size Uploaded
sinapsis_glm_ocr-0.1.2.tar.gz 22.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sinapsis-glm-ocr 0.1.2
File Interpreter ABI Platform
sinapsis_glm_ocr-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 45.1 kB

Release files / sinapsis_glm_ocr-0.1.2.tar.gz

Download URL sinapsis_glm_ocr-0.1.2.tar.gz
Size 22.4 kB
Tags Source
SHA-256 checksum
How to use checksums
729091aaa93f6ca189898fc65083b0f79060db0fe2df7d48214489d80ba09f82
BLAKE2b-256 checksum
How to use checksums
81277cee1376135acaf82e943d23cafff99b98b11e1a118e047c9042b4291a65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.16

Release files / sinapsis_glm_ocr-0.1.2-py3-none-any.whl

Download URL sinapsis_glm_ocr-0.1.2-py3-none-any.whl
Size 22.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
80da9b710c759fdf555e762a8b805846883c8d8a19122ffc7cfc17b8333c0321
BLAKE2b-256 checksum
How to use checksums
6d6f4bf950f7ed1016cbf0cb0c35e75fd4723252333d80dc3eac6c53b74e8333
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.5.16

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page