Skip to main content

Python bindings for the native Rust LightOnOCR2-1B inference engine

Project description

fast-lightonocr

⚡ Native Python bindings for the Rust Fast LightOnOCR inference engine.

fast-lightonocr provides high-performance OCR for documents and images using Baidu's LightOnOCR model. Model inference runs entirely in native Rust, while the Python package adds automatic Hugging Face downloads and structured document parsing.


✨ Features

  • 🚀 Native Rust inference engine
  • 🧠 ONNX Runtime backend
  • 📄 OCR for documents and images
  • 📝 Structured Markdown output
  • 📊 Structured HTML table extraction
  • 🎨 Configurable table rendering
  • 🎛️ Multiple model presets (default, fp16, q4)

📦 Installation

Install the package with the desired ONNX Runtime backend.

CPU

pip install "fast-lightonocr[cpu]"

CUDA

pip install "fast-lightonocr[cuda]"

Note

CUDA packaging is available through a dedicated build profile, although CUDA execution is not yet fully supported.

Prebuilt wheels are currently published for Linux x86_64 and macOS arm64. These wheels bundle the required ONNX Runtime shared library, so no additional runtime installation or environment configuration is required.

macOS x86_64 (Intel) wheels are not published because ONNX Runtime 1.28 does not provide a compatible Python wheel for that platform.

Building from source

When installing from source, the build backend automatically discovers a compatible ONNX Runtime for the selected build profile.

If ORT_DYLIB_PATH is set, it is used directly. Otherwise, the build backend installs the appropriate ONNX Runtime build dependency into the isolated build environment, validates compatibility with ONNX Runtime 1.28.x (C API level 27), configures Cargo automatically, and bundles the required native runtime library into the resulting wheel.


🚀 Quick Start

from fast_lightonocr import LightOnOCR

model = LightOnOCR.from_pretrained(
    "onnx-community/LightOnOCR-2-1B-ONNX",
)

result = model.process("receipt.jpg")

The first call downloads the required model files from Hugging Face and caches them locally.


📄 OCR Results

The raw model output is available through result.text.

print(result.text)

The Python bindings also expose a parsed document representation that extracts embedded HTML tables while preserving the original document structure.

print(result.document)

Tables can be accessed directly:

for table in result.tables:
    print(table.text_rows)

📋 Table Rendering

By default, tables are rendered using ASCII borders.

result = model.process(
    "receipt.jpg",
    table_format="grid",
)

Markdown tables are also supported.

result = model.process(
    "receipt.jpg",
    table_format="github",
)

Any table format supported by tabulate may be used.


⚙️ Model Presets

from_pretrained() supports three ONNX model presets.

model = LightOnOCR.from_pretrained(
    "onnx-community/LightOnOCR-2-1B-ONNX",
    preset="q4",
)

Available presets:

  • default
  • fp16
  • q4

The generation length can be overridden:

model = LightOnOCR.from_pretrained(
    "...",
    max_new_tokens=1024,
)

🛠 Development

Install the project and development dependencies:

poetry install --with dev

Editable development

For local development, install the extension in editable mode with dynamic ONNX Runtime loading:

export ORT_DYLIB_PATH=/path/to/libonnxruntime
poetry run maturin develop --release --features load-dynamic

For example, when using the Python onnxruntime package on macOS:

export ORT_DYLIB_PATH="$(python -c \
'import onnxruntime, pathlib; print(next((pathlib.Path(onnxruntime.__file__).parent / "capi").glob("libonnxruntime*.dylib")))')"

Building a wheel

To build a distributable wheel, use the project's Python build backend:

poetry run pip wheel . --wheel-dir dist

The default build profile targets CPU execution and does not enable any Cargo features. During source builds, the build backend automatically discovers a compatible ONNX Runtime from ORT_DYLIB_PATH or from the selected build profile's Python runtime package, validates compatibility with ONNX Runtime 1.28.x (C API level 27), configures Cargo, and produces a wheel containing the required native runtime libraries.

To build using the CUDA profile:

BUILD_PROFILE=cuda poetry run pip wheel . --wheel-dir dist

Note

Running maturin develop without --features load-dynamic is not supported. The custom build backend is responsible for configuring ONNX Runtime linking during production builds, whereas editable development uses the load-dynamic feature together with ORT_DYLIB_PATH.


🙏 Acknowledgements

This package wraps the native Rust Fast LightOnOCR inference engine and uses the open-weight LightOnOCR model released by Baidu.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fast_lightonocr-0.1.1.tar.gz (112.6 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

fast_lightonocr-0.1.1-cp39-abi3-manylinux_2_28_x86_64.whl (12.3 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

fast_lightonocr-0.1.1-cp39-abi3-macosx_11_0_arm64.whl (11.3 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file fast_lightonocr-0.1.1.tar.gz.

File metadata

  • Download URL: fast_lightonocr-0.1.1.tar.gz
  • Upload date:
  • Size: 112.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for fast_lightonocr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aeb6ace25e09d411df1910ff07a6605635ba7213e32de4fdb2418d9559c8983d
MD5 97ad3b2ebdcb59aef9860e4a02ead56d
BLAKE2b-256 52e546479b762f57b9fb155c727cf1f196d9c0a1bd128c18fe1c98a3c9e85d01

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_lightonocr-0.1.1.tar.gz:

Publisher: release.yml on talmago/fast-lightonocr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_lightonocr-0.1.1-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for fast_lightonocr-0.1.1-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5d023c301c2f258af7a2d7d9fb7f6b662f4245706a2825fa0b3698f411b6c5cf
MD5 a5739b5a25a693c2708418824cac9959
BLAKE2b-256 6e616238974079f3dd49343db4ea267444da55fb2099db6f1387ab91080750c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_lightonocr-0.1.1-cp39-abi3-manylinux_2_28_x86_64.whl:

Publisher: release.yml on talmago/fast-lightonocr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_lightonocr-0.1.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_lightonocr-0.1.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 182259c489628b1e71248165495c097d67013d802be22a628eb59fe5206da771
MD5 4b6804caf45254d7f7369b25c9e0f95b
BLAKE2b-256 388b61f0e1140ea30c695df98af890d54320313ed8593d983ff8ba146748c58e

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_lightonocr-0.1.1-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on talmago/fast-lightonocr

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page