# farseen-farseen-07/malayalam-pdf-translator architecture

> Architecture diagram of the GitHub repository farseen-farseen-07/malayalam-pdf-translator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/farseen-farseen-07/malayalam-pdf-translator
- Repository: https://github.com/farseen-farseen-07/malayalam-pdf-translator

## Overview

1. **Overall shape**
   - This repository is a **PDF translation pipeline** with two front doors: a **Streamlit web UI** (`app.py`) and a **CLI** (`translate_pdf.py`).
   - The core flow is:
     **input PDF → structure extraction → text translation → PDF regeneration**.
   - The project is not just “translate text”; it tries to preserve **layout semantics** like headings, paragraphs, lists, and tables.

2. **User-facing entry points**
   - `app.py` is the interactive web app used by Hugging Face Spaces (`sdk: streamlit` in the README).
   - `translate_pdf.py` is the command-line entry point for batch/local use.
   - Both entry points likely orchestrate the same backend modules rather than duplicating logic, which makes the backend the main architecture boundary.

3. **Extraction subsystem**
   - `pdf_extractor.py` is responsible for reading the source PDF and turning it into structured content.
   - The README indicates it uses `pdfplumber` to inspect **words, font names, font sizes, and positions**, then groups them into higher-level units:
     - words → lines
     - lines → paragraphs
     - separate handling for tables
   - This module is the “document understanding” boundary: it decides what text exists and how it is organized before translation begins.

4. **Scanned-PDF fallback path**
   - The same extraction stage can fall back to **OCR** for image-only PDFs.
   - The README references `tesseract-ocr`, `pdf2image`, and `pytesseract` as the OCR stack.
   - Architecturally, OCR is an alternate input path into the same downstream translation/rendering pipeline, not a separate app mode.

5. **Translation subsystem**
   - `translator_core.py` is the translation engine abstraction.
   - It supports multiple providers:
     - **Google** free endpoint
     - **MyMemory**
     - **LibreTranslate**
     - **Auto** fallback mode
   - This is an external-service boundary: the repository does not own translation models; it brokers requests to external services and normalizes the results.

6. **Caching and rate control**
   - Translation results are cached in a local file (`~/.pdf_translator_cache.json` per README).
   - The README also notes a **delay between calls** and chunking of long text, which means the translation layer includes protection against rate limits and oversized inputs.
   - Architecturally, caching sits between extraction and translation as a performance/availability layer, reducing repeated API calls for repeated documents.

7. **Rendering subsystem**
   - `pdf_generator_pango.py` is the primary renderer and is a major architectural piece.
   - It uses **Cairo + Pango + HarfBuzz** to generate the output PDF with correct Malayalam shaping and layout.
   - This is the key output boundary: the system converts translated structured text back into a faithful PDF rather than plain text.

8. **Fallback rendering path**
   - `pdf_generator.py` is a secondary renderer based on **ReportLab**.
   - Its presence suggests a compatibility/fallback layer when the primary Cairo/Pango stack is unavailable or unsuitable.
   - For an architecture diagram, this is best shown as an alternate output implementation behind the same “generate PDF” boundary.

9. **Font and shaping infrastructure**
   - The `fonts/` directory is part of the rendering pipeline, not just an asset folder.
   - The README highlights that Fontconfig registers fonts from this directory so Pango can find Malayalam-capable fonts such as **Noto Sans Malayalam** or bundled **FreeSans**.
   - This font subsystem materially affects correctness: without it, shaped Malayalam text can render as boxes or broken glyphs.

10. **Packaging and runtime environment**
   - `pyproject.toml`, `requirements.txt`, and `packages.txt` define the Python and system dependencies.
   - `packages.txt` is especially relevant for **Hugging Face Spaces** and Docker-based deployment because Cairo/Pango/HarfBuzz are system libraries, not pure-Python dependencies.
   - `Dockerfile` and `run.sh`/`setup.sh` show the app is intended to run in containerized or scripted environments as well as locally.

11. **Deployment and CI/CD**
   - `.github/workflows/sync_to_hf.yml` indicates an automated sync/deploy path to **Hugging Face Spaces**.
   - The README also mentions deployment on **Render** via Docker.
   - So the repo has both:
     - a hosted Streamlit deployment path
     - a container-based self-host path

12. **End-to-end data flow**
   - The conceptual pipeline is:
     1. User uploads or selects a PDF in Streamlit/CLI
     2. `pdf_extractor.py` extracts structure or OCR text
     3. `translator_core.py` translates and caches segments
     4. `pdf_generator_pango.py` rebuilds the document with proper Malayalam shaping
     5. Output PDF is returned for download
   - Tables, headings, and paragraphs are preserved as structured blocks rather than flattened text.

13. **Important architectural boundary**
   - The biggest design boundary is between **document analysis** and **document rendering**.
   - Extraction is concerned with interpreting the input PDF; rendering is concerned with producing a visually correct output PDF.
   - Translation sits in the middle as a content transformation step, with external network dependencies and caching.

14. **What to show in an architecture diagram**
   - Two ingress paths: **Web UI** and **CLI**
   - One shared backend pipeline: **extract → translate → render**
   - One optional OCR branch from extraction
   - One external-service boundary for translation providers
   - One filesystem boundary for cache, fonts, and generated PDFs
   - One deployment boundary for Hugging Face Spaces / Docker / Render

Main entry points: [`translator_core.py`](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/translator_core.py) (Translator core), [`app.py`](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/app.py) (Streamlit app), [`pdf_extractor.py`](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_extractor.py) (PDF extractor), [`pdf_generator_pango.py`](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_generator_pango.py) (Pango renderer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entrypoints["Entry points"]
  node_app_py["Streamlit app<br/>ui entrypoint<br/>[app.py]"]
  node_translate_pdf_py["CLI tool<br/>cli entrypoint<br/>[translate_pdf.py]"]
end

subgraph group_pipeline["Translation pipeline"]
  node_pdf_extractor_py["PDF extractor<br/>analysis engine<br/>[pdf_extractor.py]"]
  node_ocr_fallback["OCR fallback<br/>image-to-text path"]
  node_structure_parser["Layout parser<br/>document structure"]
  node_translator_core_py["Translator core<br/>translation broker<br/>[translator_core.py]"]
  node_provider_google(("Google API<br/>provider"))
  node_provider_mymemory(("MyMemory<br/>provider"))
  node_provider_libre(("LibreTranslate<br/>provider"))
  node_translation_cache[("Cache file<br/>local cache")]
end

subgraph group_rendering["Rendering"]
  node_pdf_generator_pango_py["Pango renderer<br/>primary renderer"]
  node_pdf_generator_py["ReportLab fallback<br/>alternate renderer<br/>[pdf_generator.py]"]
  node_fonts_dir[("Fonts<br/>render assets")]
  node_output_pdf["Translated PDF<br/>generated artifact"]
end

subgraph group_runtime["Runtime and deployment"]
  node_requirements_txt["Python deps<br/>package manifest<br/>[requirements.txt]"]
  node_pyproject_toml["Project config<br/>build config<br/>[pyproject.toml]"]
  node_packages_txt["System deps<br/>system package list<br/>[packages.txt]"]
  node_dockerfile["Dockerfile<br/>container build"]
  node_hf_sync["HF sync<br/>deployment workflow<br/>[sync_to_hf.yml]"]
end

node_app_py -->|"submit PDF"| node_pdf_extractor_py
node_translate_pdf_py -->|"submit PDF"| node_pdf_extractor_py
node_pdf_extractor_py -->|"structure"| node_structure_parser
node_pdf_extractor_py -.->|"scan fallback"| node_ocr_fallback
node_structure_parser -->|"segments"| node_translator_core_py
node_ocr_fallback -->|"recognized text"| node_translator_core_py
node_translator_core_py -.->|"auto/provider"| node_provider_google
node_translator_core_py -.->|"auto/provider"| node_provider_mymemory
node_translator_core_py -.->|"auto/provider"| node_provider_libre
node_translator_core_py -->|"store/reuse"| node_translation_cache
node_translator_core_py -->|"translated blocks"| node_pdf_generator_pango_py
node_pdf_generator_pango_py -->|"font lookup"| node_fonts_dir
node_pdf_generator_pango_py -->|"build PDF"| node_output_pdf
node_pdf_generator_py -.->|"fallback build"| node_output_pdf
node_translator_core_py -.->|"compatibility"| node_pdf_generator_py
node_app_py -->|"download"| node_output_pdf
node_translate_pdf_py -->|"write file"| node_output_pdf
node_requirements_txt -->|"runtime deps"| node_app_py
node_requirements_txt -->|"runtime deps"| node_translate_pdf_py
node_pyproject_toml -.->|"package config"| node_requirements_txt
node_packages_txt -->|"native libs"| node_pdf_generator_pango_py
node_packages_txt -->|"ocr libs"| node_ocr_fallback
node_dockerfile -->|"install system deps"| node_packages_txt
node_dockerfile -->|"run app"| node_app_py
node_hf_sync -->|"deploy"| node_app_py

click node_app_py "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/app.py"
click node_translate_pdf_py "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/translate_pdf.py"
click node_pdf_extractor_py "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_extractor.py"
click node_translator_core_py "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/translator_core.py"
click node_pdf_generator_pango_py "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_generator_pango.py"
click node_pdf_generator_py "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_generator.py"
click node_fonts_dir "https://github.com/farseen-farseen-07/malayalam-pdf-translator/tree/main/fonts"
click node_requirements_txt "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/requirements.txt"
click node_pyproject_toml "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pyproject.toml"
click node_packages_txt "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/packages.txt"
click node_dockerfile "https://github.com/farseen-farseen-07/malayalam-pdf-translator/tree/main/Dockerfile"
click node_hf_sync "https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/.github/workflows/sync_to_hf.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app_py,node_translate_pdf_py toneBlue
class node_pdf_extractor_py,node_ocr_fallback,node_structure_parser,node_translator_core_py,node_provider_google,node_provider_mymemory,node_provider_libre,node_translation_cache toneAmber
class node_pdf_generator_pango_py,node_pdf_generator_py,node_fonts_dir,node_output_pdf toneMint
class node_requirements_txt,node_pyproject_toml,node_packages_txt,node_dockerfile,node_hf_sync toneRose
```

## Components

### Entry points

User-facing ways to start translation.

- [Streamlit app](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/app.py): ui entrypoint · Interactive web front door for PDF translation. · `app.py`
- [CLI tool](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/translate_pdf.py): cli entrypoint · Command-line entry point for batch or local translation. · `translate_pdf.py`

### Translation pipeline

Core extract-translate-render flow.

- [PDF extractor](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_extractor.py): analysis engine · Reads PDFs, infers structure, and prepares text blocks. · `pdf_extractor.py`
- **OCR fallback**: image-to-text path · Alternate path for scanned PDFs using OCR tooling.
- **Layout parser**: document structure · Converts words and lines into paragraphs, headings, lists, and tables.
- [Translator core](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/translator_core.py): translation broker · Coordinates provider selection, chunking, delays, and translation calls. · `translator_core.py`
- **Google API**: provider · External translation endpoint used by the broker.
- **MyMemory**: provider · External translation endpoint used by the broker.
- **LibreTranslate**: provider · External translation endpoint used by the broker.
- **Cache file**: local cache · Stores translated segments to reduce repeated calls.

### Rendering

Output PDF generation and shaping.

- [Pango renderer](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_generator_pango.py): primary renderer · Rebuilds the translated PDF with Cairo, Pango, and HarfBuzz. · `pdf_generator_pango.py`
- [ReportLab fallback](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pdf_generator.py): alternate renderer · Secondary PDF generation path when the primary stack is unavailable. · `pdf_generator.py`
- [Fonts](https://github.com/farseen-farseen-07/malayalam-pdf-translator/tree/main/fonts): render assets · Malayalam-capable fonts registered through Fontconfig. · `fonts`
- **Translated PDF**: generated artifact · Final translated document returned to the user.

### Runtime and deployment

Environment, packaging, and hosting.

- [Python deps](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/requirements.txt): package manifest · Python dependency specification for the app and pipeline. · `requirements.txt`
- [Project config](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/pyproject.toml): build config · Project metadata and packaging configuration. · `pyproject.toml`
- [System deps](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/packages.txt): system package list · Native libraries needed for Cairo, Pango, and OCR-related runtime support. · `packages.txt`
- [Dockerfile](https://github.com/farseen-farseen-07/malayalam-pdf-translator/tree/main/Dockerfile): container build · Container image definition for self-hosted deployment. · `Dockerfile`
- [HF sync](https://github.com/farseen-farseen-07/malayalam-pdf-translator/blob/main/.github/workflows/sync_to_hf.yml): deployment workflow · Automated synchronization to Hugging Face Spaces. · `.github/workflows/sync_to_hf.yml`

## Connections

- Streamlit app → PDF extractor: submit PDF · The web UI sends uploaded PDFs into the backend pipeline.
- CLI tool → PDF extractor: submit PDF · The CLI invokes the shared extraction pipeline.
- PDF extractor → Layout parser: structure · Extracted words and lines are organized into semantic blocks.
- PDF extractor → OCR fallback: scan fallback · Image-only PDFs can route through OCR.
- Layout parser → Translator core: segments · Structured text blocks are forwarded for translation.
- OCR fallback → Translator core: recognized text · OCR output joins the same translation path as native text.
- Translator core → Google API: auto/provider · One supported external translation backend.
- Translator core → MyMemory: auto/provider · One supported external translation backend.
- Translator core → LibreTranslate: auto/provider · One supported external translation backend.
- Translator core → Cache file: store/reuse · Translated chunks are cached locally.
- Translator core → Pango renderer: translated blocks · Translated content is handed to the primary renderer.
- Pango renderer → Fonts: font lookup · Fontconfig and Pango resolve Malayalam-capable fonts here.
- Pango renderer → Translated PDF: build PDF · Primary path emits the final translated document.
- ReportLab fallback → Translated PDF: fallback build · Alternate renderer also produces the final PDF artifact.
- Translator core → ReportLab fallback: compatibility · Fallback rendering can be used with the same translated content.
- Streamlit app → Translated PDF: download · The web UI returns the generated PDF to the user.
- CLI tool → Translated PDF: write file · The CLI writes or exposes the translated PDF artifact.
- Python deps → Streamlit app: runtime deps · Python dependencies support the web app.
- Python deps → CLI tool: runtime deps · Python dependencies support the CLI.
- Project config → Python deps: package config · Project packaging is coordinated with dependency management.
- System deps → Pango renderer: native libs · System libraries enable Cairo/Pango/HarfBuzz rendering.
- System deps → OCR fallback: ocr libs · Native OCR support depends on system packages.
- Dockerfile → System deps: install system deps · Container builds include required native libraries.
- Dockerfile → Streamlit app: run app · Containerized deployment starts the Streamlit app.
- HF sync → Streamlit app: deploy · Workflow syncs the app to Hugging Face Spaces.
