How farseen-farseen-07/malayalam-pdf-translator works
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 Translator core · app.py Streamlit app · pdf_extractor.py PDF extractor · pdf_generator_pango.py Pango renderer
Components 19
Entry points
User-facing ways to start translation.
- Streamlit app · Interactive web front door for PDF translation.
app.py - CLI tool · Command-line entry point for batch or local translation.
translate_pdf.py
Translation pipeline
Core extract-translate-render flow.
- PDF extractor · Reads PDFs, infers structure, and prepares text blocks.
pdf_extractor.py - OCR fallback · Alternate path for scanned PDFs using OCR tooling.
- Layout parser · Converts words and lines into paragraphs, headings, lists, and tables.
- Translator core · Coordinates provider selection, chunking, delays, and translation calls.
translator_core.py - Google API · External translation endpoint used by the broker.
- MyMemory · External translation endpoint used by the broker.
- LibreTranslate · External translation endpoint used by the broker.
- Cache file · Stores translated segments to reduce repeated calls.
Rendering
Output PDF generation and shaping.
- Pango renderer · Rebuilds the translated PDF with Cairo, Pango, and HarfBuzz.
pdf_generator_pango.py - ReportLab fallback · Secondary PDF generation path when the primary stack is unavailable.
pdf_generator.py - Fonts · Malayalam-capable fonts registered through Fontconfig.
fonts - Translated PDF · Final translated document returned to the user.
Runtime and deployment
Environment, packaging, and hosting.
- Python deps · Python dependency specification for the app and pipeline.
requirements.txt - Project config · Project metadata and packaging configuration.
pyproject.toml - System deps · Native libraries needed for Cairo, Pango, and OCR-related runtime support.
packages.txt - Dockerfile · Container image definition for self-hosted deployment.
Dockerfile - HF sync · Automated synchronization to Hugging Face Spaces.
.github/workflows/sync_to_hf.yml
Connections 25
- 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.