Skip to main content

No project description provided

Project description

langchain_ocr_lib

langchain_ocr_lib is the OCR processing engine behind LangChain-OCR. It provides a modular, vision-LLM-powered Chain to convert image and PDF documents into clean Markdown. Designed for direct CLI usage or integration into larger applications.

Table of Contents

  1. Overview
  2. Features
  3. Installation
    1. Prerequisites
    2. Environment Setup
  4. Usage
    1. CLI
    2. Python Module
    3. Docker
  5. Architecture
  6. Testing
  7. License

1. Overview

This package offers the core functionality to extract text from documents using vision LLMs and convert it into Markdown. It is highly configurable by environment variables and its design based on dependency injection, that allows you to easily swap out components. The package is designed to be used as a library, but it also provides a command-line interface (CLI) for easy local execution.


2. Features

  • Vision-Language OCR: Supports Ollama. Other LLM providers will be added soon.
  • CLI Interface: Simple local execution via command line or container
  • Highly Configurable: Use environment variables to configure the OCR
  • Dependency Injection: Easily swap out components for custom implementations
  • LangChain: Integrates with LangChain
  • Markdown Output: Outputs well-formatted Markdown text

3. Installation

3.1 Prerequisites

  • Python: 3.11+
  • Poetry: Install Poetry
  • Docker: For containerized CLI usage (optional)
  • Ollama: Follow instructions here
  • Langfuse: Different options for self hosting, see here (optional, for observability)

3.2 Environment Setup

The package is published on PyPI, so you can install it directly with pip:

pip install langchain-ocr-lib

However, if you want to run the latest version or contribute to the project, you can clone the repository and install it locally.

git clone https://github.com/a-klos/langchain-ocr.git
cd langchain-ocr/langchain_ocr_lib
poetry install --with dev

You can configure the package by setting environment variables. Configuration options are shown in the .env.template file.


4. Usage

Remember that you need to pull the configured LLM model first. With Ollama, you can do this with:

ollama pull <model_name>

For example, to pull the gemma3:4b-it-q4_K_M model, run:

ollama pull gemma3:4b-it-q4_K_M

4.1 CLI

Run OCR locally from the terminal:

langchain-ocr <<input_file>> 

Supports:

  • .jpg, .jpeg, .png, and .pdf inputs

4.2 Python Module

Use the the library programmatically:

import inject

import configure_di
from langchain_ocr_lib.di_config import configure_di
from langchain_ocr_lib.di_binding_keys.binding_keys import PdfConverterKey
from langchain_ocr_lib.impl.converter.pdf_converter import Pdf2MarkdownConverter


configure_di() #This sets up the dependency injection

class Converter:
    _converter: Pdf2MarkdownConverter = inject.attr(PdfConverterKey)
    def convert(self, filename: str) -> str:
        return self._converter.convert2markdown(filename=filename)

converter = Converter()
markdown = converter.convert("../docs/invoice.pdf") # Adjust the file path as needed
print(markdown)

The configure_di() function sets up the dependency injection for the library. The dependencies can be easily swapped out or appended with new dependencies. See ../api/src/langchain_ocr/di_config.py for more details on how to add new dependencies.

Swapping out the dependencies can be done as follows:

import inject
from inject import Binder

from langchain_ocr_lib.di_config import lib_di_config, PdfConverterKey
from langchain_ocr_lib.impl.converter.pdf_converter import Pdf2MarkdownConverter


class MyPdfConverter(Pdf2MarkdownConverter):
    def convert(self, filename: str) -> None:
        markdown = self.convert2markdown(filename=filename)
        print(markdown)

def _api_specific_config(binder: Binder):
    binder.install(lib_di_config)  # Install all default bindings
    binder.bind(PdfConverterKey, MyPdfConverter())  # Then override PdfConverter

def configure():
    """Configure the dependency injection container."""
    inject.configure(_api_specific_config, allow_override=True, clear=True)

configure()

class Converter:
    _converter: MyPdfConverter = inject.attr(PdfConverterKey)
    def convert(self, filename: str) -> None:
        self._converter.convert(filename=filename)

converter = Converter()
converter.convert("../docs/invoice.pdf") # Adjust the file path as needed

4.3 Docker

Run OCR via Docker without local Python setup:

docker build -t ocr -f langchain_ocr_lib/Dockerfile .
docker run --net=host -it --rm -v ./docs:/app/docs:ro ocr docs/invoice.png

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

langchain_ocr_lib-0.2.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

langchain_ocr_lib-0.2.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

File details

Details for the file langchain_ocr_lib-0.2.0.tar.gz.

File metadata

  • Download URL: langchain_ocr_lib-0.2.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.2 Linux/6.8.0-57-generic

File hashes

Hashes for langchain_ocr_lib-0.2.0.tar.gz
Algorithm Hash digest
SHA256 7a5e2968125a4a2b4553bb950f11cb0b220953dea8fe6f3c70503f0fbb326fff
MD5 cae3df9653b2d35dc63ebda5402c823a
BLAKE2b-256 cf0e7b862505fa0222b0752137c2c06c69f0b2be3cfc7dae7208f26189131476

See more details on using hashes here.

File details

Details for the file langchain_ocr_lib-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: langchain_ocr_lib-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.13.2 Linux/6.8.0-57-generic

File hashes

Hashes for langchain_ocr_lib-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3dee240f25ef96910d75b47ae4807bd35b17c02f3f9d1762fc05d977daeea351
MD5 4aa8318a66c39f48a91f0609d6240dd1
BLAKE2b-256 d9a95ee80c56c1c38ccf9373663835920c70dd6bc849b5c6ced7eef825e79f1c

See more details on using hashes here.

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