# hj-lh/arabic-file-renamer architecture

> Architecture diagram of the GitHub repository hj-lh/arabic-file-renamer, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hj-lh/arabic-file-renamer
- Repository: https://github.com/hj-lh/arabic-file-renamer

## Overview

##### Project type and purpose
- This is an open-source, command-line Python utility (“AI Arabic File Renamer”) whose goal is to read scanned documents (PDF/PNG/JPG), extract their contents via OCR, generate descriptive Arabic file names using an LLM, and write the renamed files to an output folder.

##### File structure
- Top-level files:
- Readme.md – project overview, installation and configuration instructions
- requirements.txt – Python dependencies (e.g., pytesseract, pdf2image, ollama-client)
- namer.py – the single Python script orchestrating the pipeline
- No subdirectories; all logic lives in a single module. Configuration is via variables in namer.py.

##### README for additional insights
- Stack and external dependencies:
- Python 3.6+
- Tesseract OCR (system-installed executable + Arabic trained data)
- Poppler (pdf2image conversion library)
- Ollama LLM (local model server)
- The script’s core function:
query_ollama(prompt) → returns an Arabic file name based on OCR’ed text and a customizable prompt.

a. Main components
1. CLI Launcher (namer.py)
2. Input Folder (scanned files)
3. File Ingestion & Dispatcher (detects file type)

4a. PDF → pdf2image (Poppler) → image pages
4b. Image (PNG/JPG) → direct
5. Preprocessing (optional resizing, grayscale)
6. OCR Engine (Tesseract) → raw text
7. Text Post-processing (cleanup)
8. LLM Service (Ollama API) ← naming prompt + text
9. Renamer Module (applies returned filename)
10. Output Folder (renamed files)

b. Relationships & interactions
- Files flow left to right: Input Folder → Ingestion → (PDF/Image branches) → OCR → LLM → Renamer → Output Folder
- CLI Launcher triggers the pipeline and handles error/reporting.
- OCR Engine and LLM Service are external processes/services invoked by the Python script.

c. Architectural patterns & design principles
- Pipeline pattern: sequential stages, each with a single responsibility.
- Single-source orchestrator (namer.py) coordinating external tools.
- Extensibility points:
- New file types/convertors
- Swap LLM backends or prompts
- Insert preprocessing steps or postprocessing filters

d. Key technologies & libraries
- Python 3.6+
- pytesseract (wrapper for Tesseract CLI)
- pdf2image (Poppler-based PDF→PNG)
- Ollama client or HTTP API for LLM queries
- Standard libs (os, shutil, logging)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Storage layer
    subgraph "Storage" 
        direction TB
        InputFolder["Input Folder"]:::storage
        OutputFolder["Output Folder"]:::storage
    end

    %% Core Python components
    subgraph "Core Pipeline (Python)" 
        direction TB
        CLI["CLI Launcher\n(namer.py)"]:::core
        Dispatcher["File Ingestion & Dispatcher"]:::core
        PDFConverter["PDF→Image Converter\n(pdf2image/Poppler)"]:::core
        ImageHandler["Image Handler\n(PNG/JPG)"]:::core
        Preprocessor["Preprocessing\n(resize, grayscale)"]:::core
        OCR["OCR Engine Invocation\n(pytesseract)"]:::core
        PostProcessor["Text Post-processing"]:::core
        LLMClient["LLM Service Client\n(Ollama API)"]:::core
        Renamer["Renamer Module"]:::core
    end

    %% External tools/services
    subgraph "External Services/Tools" 
        direction TB
        Poppler["Poppler"]:::external
        Tesseract["Tesseract OCR Engine"]:::external
        Ollama["Ollama LLM Server"]:::external
    end

    %% Documentation & Dependencies
    subgraph "Documentation & Config" 
        direction TB
        Readme["Project Overview & Usage\n(Readme.md)"]:::storage
        Requirements["Python Dependencies\n(requirements.txt)"]:::storage
    end

    %% Flow connections
    CLI -->|loads paths| InputFolder
    CLI -->|loads paths| OutputFolder
    CLI --> Dispatcher
    Dispatcher -->|PDF file| PDFConverter
    Dispatcher -->|Image file| ImageHandler
    PDFConverter -->|image pages| Preprocessor
    ImageHandler -->|image file| Preprocessor
    Preprocessor -->|preprocessed image| OCR
    OCR -->|raw text| PostProcessor
    PostProcessor -->|clean text| LLMClient
    LLMClient -->|"prompt + text"| Ollama
    Ollama -->|Arabic filename| LLMClient
    LLMClient -->|filename| Renamer
    Renamer -->|renamed file| OutputFolder

    %% External tool integrations
    PDFConverter -.->|uses| Poppler
    OCR -.->|invokes| Tesseract

    %% Click events
    click CLI "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click Dispatcher "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click PDFConverter "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click ImageHandler "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click Preprocessor "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click OCR "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click PostProcessor "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click LLMClient "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click Renamer "https://github.com/hj-lh/arabic-file-renamer/blob/main/namer.py"
    click Readme "https://github.com/hj-lh/arabic-file-renamer/blob/main/Readme.md"
    click Requirements "https://github.com/hj-lh/arabic-file-renamer/blob/main/requirements.txt"

    %% Styles
    classDef core fill:#ccffcc,stroke:#2d572c,color:#000
    classDef external fill:#ffcc99,stroke:#c66,color:#000
    classDef storage fill:#cce5ff,stroke:#2f5d8a,color:#000
```
