Spaces:
Runtime error
A newer version of the Gradio SDK is available: 6.25.0
title: Searchable PDF Generator with OCR (Hunyuan-DiT)
emoji: π
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 4.36.1
app_file: app.py
pinned: false
Searchable PDF Generator with OCR + Layout Preservation (Hunyuan-DiT)
This Hugging Face Space converts image-based PDFs and images into layout-preserving, searchable PDFs using the Hunyuan-DiT model for high-quality visual understanding and text extraction.
Objective
To develop a robust system that accepts PDFs or images, processes each page using Hunyuan-DiT for advanced OCR, extracts text with precise positional data, and reconstructs a searchable PDF that exactly mirrors the original visual layout, including page size, coordinates, fonts, bounding boxes, multi-column structures, tables, and embedded graphical elements.
Features
- Input Flexibility: Accepts PDF documents and various image formats (PNG, JPG, TIFF).
- High-Quality OCR: Utilizes Hunyuan-DiT for superior visual understanding and text extraction.
- Layout Preservation: Ensures the output PDF maintains the original document's layout, including:
- Page size
- Layout and coordinates
- Approximate fonts
- Bounding boxes for text
- Multi-column structures
- Tables and embedded graphical elements
- Searchable Output: Generates a multi-page searchable PDF with invisible text layers placed at exact coordinates over the original scanned image.
- Preview Functionality: Provides a preview of extracted OCR, highlighting bounding boxes.
- Downloadable Results: Offers a direct download link for the final searchable PDF.
Architecture
- User Uploads: Users upload a PDF or image files via a Gradio interface.
- PDF to Image Conversion: PDFs are converted into individual page images at 300β400 DPI.
- Hunyuan-DiT OCR: Each page image is fed into the Hunyuan-DiT model for OCR and layout extraction, producing structured JSON output including words, bounding boxes, line grouping, reading order, block segmentation, and confidence scores.
- PDF Assembly: An invisible text layer is meticulously assembled on top of the original image for each page.
- Final PDF Generation: A multi-page searchable PDF is generated.
- Download: The user can download the final searchable PDF.
Error Handling
The system includes robust error handling for:
- Corrupted PDF inputs.
- Multi-language input (handled by Hunyuan-DiT).
- Pages exceeding maximum size (>10,000 pixels in any dimension).
Setup and Usage
Prerequisites
Ensure you have Python 3.8+ installed.
Installation
- Clone the repository:
git clone <repository_url> cd <repository_name> - Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On Windows: `venv\Scripts\activate` - Install the required dependencies:
pip install -r requirements.txt
Running the Space
To launch the Gradio application locally:
python app.py
Then, open your web browser and navigate to the address provided by Gradio (usually http://127.0.0.1:7860).
Folder Structure
/
βββ app.py
βββ requirements.txt
βββ utils/
β βββ pdf_to_images.py
β βββ hunyuan_ocr.py
β βββ pdf_builder.py
βββ README.md