ollama-ocr

repository·main·Indexed 25 days ago

https://github.com/imanoop7/ollama-ocr

A Python package that uses vision language models via Ollama to perform Optical Character Recognition (OCR) on images and PDFs. It features the OCRProcessor class for single and batch processing, supporting models such as llama3.2-vision:11b, granite3.2-vision, moondream, and minicpm-v. It provides multiple output formats including markdown, text, json, structured, key_value, and table, and includes a Streamlit web application and integration capabilities for AutoGen agents.

Tokens
3.1K
Snippets
13
Records
23
Agent score
82%

What's inside ollama-ocr

  1. Run the Streamlit Web Application

    main

    To run the interactive web interface with drag-and-drop support and real-time processing, follow these steps:

    1. Clone the repository and enter the directory.
    2. Install the required dependencies.
    3. Navigate to the src/ollama_ocr directory.
    4. Run the Streamlit application.
    git clone https://github.com/imanoop7/Ollama-OCR.git
    cd Ollama-OCR
    pip install -r requirements.txt
    cd src/ollama_ocr
    streamlit run app.py
  2. Set up Ollama and Vision Models

    main

    Before using the package, ensure Ollama is installed on your system and pull the desired vision models. Supported models include llama3.2-vision:11b, granite3.2-vision, moondream, and minicpm-v.

    ollama pull llama3.2-vision:11b
    ollama pull granite3.2-vision
    ollama pull moondream
    ollama pull minicpm-v
  3. Initialize the OCRProcessor

    main

    To use the library, import OCRProcessor from ollama_ocr and initialize it by specifying a model_name. This model name should correspond to a vision-capable model available in your Ollama instance (e.g., granite3.2-vision or llama3.2-vision:11b).

    from ollama_ocr import OCRProcessor
    
    # Create an instance
    ocr = OCRProcessor(model_name='granite3.2-vision')
  4. Initialize OCRProcessor

    main

    To use the package, instantiate the OCRProcessor class. You can specify the model_name and a custom base_url if you are using a custom Ollama API endpoint.

    from ollama_ocr import OCRProcessor
    
    # Initialize with a specific model and custom API URL
    ocr = OCRProcessor(model_name='llama3.2-vision:11b', base_url="http://host.docker.internal:11434/api/generate")
  5. Configure OCR output formats

    main

    When calling process_image or process_batch, you can specify the format_type to control the structure of the extracted data. Supported formats include:

    • markdown: Markdown string with headers and lists.
    • text: Plain text string.
    • json: JSON object.
    • structured: Structured object.
    • key_value: Dictionary of labeled information.
    • table: Extracted tabular data.
  6. Register OCR tool with AutoGen agents

    main

    To allow an AutoGen AssistantAgent to use the OCR function, use register_function. You must define a UserProxyAgent to act as the executor of the tool.

    from autogen import AssistantAgent, UserProxyAgent, register_function
    
    # Define agents
    user = UserProxyAgent(
        name="human",
        llm_config=False,
        is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
        human_input_mode="NEVER",
        code_execution_config=False
    )
    
    assistant = AssistantAgent(
        name="OCR_Agent",
        system_message="You are an expert OCR assistant...",
        llm_config=llm_config,
        code_execution_config=False,
    )
    
    # Register the tool
    register_function(
        doc_parser,
        caller=assistant,
        executor=user,
        name="doc_parser",
        description="Extract text from a document and returns complete extracted text.",
    )
    
    # Start the chat
    user.initiate_chat(
        assistant,
        message="Hello, I have a document that I need help extracting text from 'panel_ui.pdf' ",
    )
  7. Integrate OCRProcessor with AutoGen tools

    main

    You can wrap the OCRProcessor.process_image method inside a Python function to serve as a tool for an AutoGen AssistantAgent.

    When initializing OCRProcessor, specify a vision-capable model (e.g., granite3.2-vision). The process_image method accepts image_path, format_type (e.g., "text"), and language (e.g., "eng").

    from ollama_ocr import OCRProcessor
    
    def doc_parser(file_path:str)->str:
        ocr = OCRProcessor(model_name='granite3.2-vision')
        result = ocr.process_image(
            image_path=file_path,
            format_type="text",
            language="eng",
        )
        return result
  8. Process an image or PDF with OCRProcessor.process_image()

    main

    Use the process_image method to extract text from images or PDF files.

    Parameters:

    • image_path (str): Path to the image or PDF file.
    • format_type (str): The desired output format. Supported options: markdown, text, json, structured, key_value.
    • custom_prompt (str, optional): A specific instruction for the model to guide extraction.
    • language (str, optional): The language of the text to be extracted.
    from ollama_ocr import OCRProcessor
    
    ocr = OCRProcessor(model_name='granite3.2-vision')
    
    result = ocr.process_image(
        image_path="/content/pdf.pdf",
        format_type="markdown",
        custom_prompt="Extract all text, focusing on dates and names.",
        language="English"
    )
    print(result)