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.2.tar.gz (114.4 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.2-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.2-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.2.tar.gz.

File metadata

  • Download URL: fast_lightonocr-0.1.2.tar.gz
  • Upload date:
  • Size: 114.4 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.2.tar.gz
Algorithm Hash digest
SHA256 579c2e053009a80d54cae5242837b9beaa11123f08c18ecdbb0580b7e3ebc4df
MD5 6a097532d8662f9e33d140ef7f0a6c01
BLAKE2b-256 8c14baf03c1ab5e93691c13a13cfb173370ed35d184b6763b1055cce6102d1e0

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_lightonocr-0.1.2.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.2-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for fast_lightonocr-0.1.2-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 72b31dd282eaba5118580cf0b7d506f02f6e2192bb6ce85b3b64c10af5e86c81
MD5 df651f23dccb2d7f761cd98dfd193518
BLAKE2b-256 eefc966344c8faf1017d44eff9574fb6598d585597326948ada054aac3c8234d

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_lightonocr-0.1.2-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.2-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for fast_lightonocr-0.1.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 ac69f2599b7fa6a0c63007b6a4d2a57958e22bf33f35ebee21ef6e2668d754fc
MD5 f00737a610fdf8f6ea641e1ce4ba8e6c
BLAKE2b-256 f3dbc644d565d56505952f37b278d0f61906b6c140262f1f963980e476ad81ea

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_lightonocr-0.1.2-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