Skip to main content

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

Generation overrides

Model defaults come from Hugging Face generation_config.json (typically do_sample=True, temperature=0.2, top_k=0, top_p=0.9). Override them at load time with generation_kwargs (merged onto the decoder config; unknown keys raise ValueError):

# Faster / deterministic OCR on CPU (greedy decoding)
model = LightOnOCR.from_pretrained(
    "onnx-community/LightOnOCR-2-1B-ONNX",
    preset="q4",
    generation_kwargs={
        "do_sample": False,
        "max_new_tokens": 256,
    },
)

# Sampling with a top-k cutoff (HF default top_k=0 walks the full vocab)
model = LightOnOCR.from_pretrained(
    "...",
    generation_kwargs={
        "do_sample": True,
        "temperature": 0.2,
        "top_k": 50,
        "top_p": 0.9,
        "max_new_tokens": 256,
    },
)

Supported keys: max_new_tokens, do_sample, temperature, top_k, top_p.

You can also update knobs after load:

model.generation_kwargs = {"do_sample": False}
print(model.generation_kwargs)

Bare max_new_tokens= remains supported as a shorthand:

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

On CPU, prefer do_sample=False for throughput. If you need sampling, set a modest top_k (for example 50) instead of leaving the HF default top_k=0.


🛠 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.

Release files for fast-lightonocr 0.1.3

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

Source distribution (sdist)

Source distribution for fast-lightonocr 0.1.3
File Size Uploaded
fast_lightonocr-0.1.3.tar.gz 122.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for fast-lightonocr 0.1.3
File Interpreter ABI Platform
fast_lightonocr-0.1.3-cp39-abi3-manylinux_2_28_x86_64.whl CPython 3.9 abi3 Linux glibc 2.28+ x86-64 Details
fast_lightonocr-0.1.3-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details

Total release size: 23.7 MB

Release files / fast_lightonocr-0.1.3.tar.gz

Download URL fast_lightonocr-0.1.3.tar.gz
Size 122.1 kB
Tags Source
SHA-256 checksum
How to use checksums
242c426ce8f378482163012af4d854963721e836a01f0864fcdaca8f80bdafd1
BLAKE2b-256 checksum
How to use checksums
7f71f308078f681727c82ad319039e9d32334d63e631e8520c1a553aa19e4e2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 5, 2026.

Transparency log

Release files / fast_lightonocr-0.1.3-cp39-abi3-manylinux_2_28_x86_64.whl

Download URL fast_lightonocr-0.1.3-cp39-abi3-manylinux_2_28_x86_64.whl
Size 12.3 MB
Tags CPython 3.9 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
67b77d59f6b8834eb2468cbbc6a7f75c65c565a10f405bc2627e0a222d1cf296
BLAKE2b-256 checksum
How to use checksums
b97f26a1f714e9a7114a022104716ddfbc547dc644fa8f3e7ce8422e1f3cb3b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 5, 2026.

Transparency log

Release files / fast_lightonocr-0.1.3-cp39-abi3-macosx_11_0_arm64.whl

Download URL fast_lightonocr-0.1.3-cp39-abi3-macosx_11_0_arm64.whl
Size 11.3 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
e3028a43144bf6252d50bab76ba1b840ca1058972e8f0e9702ae324ecaa1870a
BLAKE2b-256 checksum
How to use checksums
8b4137e8e1fb9d94e5d99a691b881c7c18408ac8c361d2bd5188a29055d71c20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 5, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.4

3 release files

This release

0.1.3 This release

3 release files

0.1.2

3 release files

0.1.1

3 release files

0.1.0

3 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