SmolDocling OCR App

repository·main·Indexed 42 days ago

https://github.com/aianytime/smoldocling-ocr-app

A Streamlit-based application using the SmolDocling-256M-preview model to perform high-accuracy OCR on document images and PDFs. It converts documents into structured DocTags and Markdown formats, with specialized capabilities for table extraction (OTSL), code extraction, formula conversion to LaTeX, and chart data extraction.

Tokens
1.4K
Snippets
6
Records
9
Agent score
4%

What's inside SmolDocling OCR App

  1. SmolDocling OCR App features and processing capabilities

    main

    The app provides advanced OCR capabilities for various document elements. Supported processing tasks include:

    • General document conversion
    • Table extraction: Outputs in OTSL format.
    • Code extraction
    • Formula conversion: Converts formulas to LaTeX.
    • Chart data extraction
    • Section header extraction

    Results are provided in two structured formats: DocTags and Markdown.

  2. Install the SmolDocling OCR App

    main

    To install the SmolDocling OCR App, clone the repository and install the required dependencies using either uv (recommended) or pip.

    Prerequisites:

    • Python 3.12+
    • A Hugging Face account with an API token for model access.
    # 1. Clone the repository
    git clone https://github.com/AIAnytime/SmolDocling-OCR-App
    cd smoldocling
    
    # 2. Install dependencies (using uv)
    uv pip install -r requirements.txt
    
    # OR using pip
    pip install -r requirements.txt
  3. Use the SmolDocling OCR App interface

    main

    Once the Streamlit app is running in your browser, follow these steps to process documents:

    1. Configure Mode: Use the sidebar to select between single or multiple image upload modes.
    2. Select Task: Choose the specific processing task type (e.g., Table extraction, Code extraction, Formula conversion, etc.).
    3. Upload: Upload your document image(s).
    4. Process: Click the "Process Image(s)" button to begin the OCR.
    5. Review/Download: View the results in DocTags and Markdown formats, and download them for use.
  4. Available OCR task prompts

    main

    The process_single_image and process_pdf functions accept a prompt_text parameter that determines the specific OCR task. The application supports the following task types:

    • General OCR: "Convert this page to docling." or "Convert this PDF to docling."
    • Table Extraction: "Convert this table to OTSL."
    • Code Extraction: "Convert code to text."
    • Formula Extraction: "Convert formula to latex."
    • Chart Extraction: "Convert chart to OTSL."
    • Header Extraction: "Extract all section header elements on the page."
  5. Check required dependencies with `check_dependencies`

    main

    Call check_dependencies to verify if the necessary libraries for the OCR engine and document processing are installed. It checks for the availability of transformers, huggingface_hub, and docling-core.

    Returns:

    • missing (list of str): A list of package names that are missing. If the list is empty, all dependencies are met.
    missing = check_dependencies()
    if missing:
        print(f"Please install: {', '.join(missing)}")
  6. Process a single image with `process_single_image`

    main

    Use process_single_image to perform OCR on a single PIL Image object. This function authenticates with Hugging Face (using HF_TOKEN from environment variables), loads the ds4sd/SmolDocling-256M-preview model and processor, and generates both DocTags and Markdown content.

    Parameters:

    • image: A PIL.Image.Image object.
    • prompt_text: A string defining the task (e.g., "Convert this page to docling."). Defaults to "Convert this page to docling.".

    Returns:

    • doctags (str): The extracted DocTags string.
    • md_content (str): The converted Markdown content.
    • processing_time (float): The time taken to process the image in seconds.
    from PIL import Image
    
    # Assuming image is a loaded PIL Image
    doctags, md_content, processing_time = process_single_image(image, prompt_text="Convert this page to docling.")
    print(f"Markdown: {md_content}")
  7. Process a PDF file with `process_pdf`

    main

    Use process_pdf to perform OCR on an entire PDF document. The function uses PyMuPDF (fitz) to convert each PDF page into an image, which is then processed individually via process_single_image. The results from all pages are combined into a single output.

    Parameters:

    • pdf_file: A file-like object containing the PDF data (e.g., from streamlit.file_uploader).
    • prompt_text: A string defining the task. Defaults to "Convert this PDF to docling.".

    Returns:

    • combined_doctags (str): A single string containing all extracted DocTags from all pages, joined by newlines.
    • combined_md_content (str): A single string containing all extracted Markdown content from all pages, joined by newlines.
    • total_processing_time (float): The total time taken to process the entire PDF in seconds.
    # Assuming uploaded_pdf is a file-like object from a file uploader
    combined_doctags, combined_md_content, total_time = process_pdf(uploaded_pdf, prompt_text="Convert this PDF to docling.")