--- 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 1. **User Uploads**: Users upload a PDF or image files via a Gradio interface. 2. **PDF to Image Conversion**: PDFs are converted into individual page images at 300–400 DPI. 3. **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. 4. **PDF Assembly**: An invisible text layer is meticulously assembled on top of the original image for each page. 5. **Final PDF Generation**: A multi-page searchable PDF is generated. 6. **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 1. Clone the repository: ```bash git clone cd ``` 2. Create a virtual environment (recommended): ```bash python -m venv venv source venv/bin/activate # On Windows: `venv\Scripts\activate` ``` 3. Install the required dependencies: ```bash pip install -r requirements.txt ``` ### Running the Space To launch the Gradio application locally: ```bash 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 ```