Skip to content
notashwiniiPublic

About

Trilingual file translation tool

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Trilekha — त्रिलेखा

Trilekha (त्रिलेखा) is a multilingual document translation tool built for the Google TMT Hackathon 2026, Track 2: File Translation Tool.

The name comes from two Nepali roots: tri (त्रि) meaning three, and lekha (लेखा) meaning writing or script. Three languages. One tool.

Trilekha translates PDF, DOCX, CSV, and TSV documents between English, Nepali, and Tamang using the Google Trilingual Machine Translation (TMT) API developed by the Information and Language Processing Lab at Kathmandu University.


Try It

Deployed: trilekha.streamlit.app

No setup needed. Upload a file, choose your language pair, download the translation.


watch demo

https://youtu.be/corp57wPo6s

Key Features

  • Translates .pdf, .docx, .csv, and .tsv files while returning the same file format.
  • Preserves document structure and layout as far as possible, including PDF page geometry, DOCX paragraphs/tables, and CSV/TSV cell positions.
  • Sends text to TMT sentence by sentence, matching the API's sentence-level translation requirement.
  • Supports English, Nepali, and Tamang language pairs.
  • Handles Nepali PDFs that use legacy fonts by repairing extracted text with Likhit before translation.

System Design

Architecture

┌─────────────────────────────────────────────────────┐
│                  Streamlit Frontend                  │
│  app.py                                              │
│  • File upload, language selection, progress, download│
└───────────────────────┬─────────────────────────────┘
                        │
          ┌─────────────▼──────────────┐
          │        File Parser          │
          │  backend/parsers/*.py       │
          │                             │
          │  Extracts text blocks with  │
          │  layout/position metadata   │
          └─────────────┬──────────────┘
                        │
          ┌─────────────▼──────────────┐
          │      Sentence Splitter      │
          │  backend/utils/sentence_splitter.py │
          │                             │
          │  NLTK for English           │
          │  Punctuation for Ne/Tmg     │
          └─────────────┬──────────────┘
                        │
          ┌─────────────▼──────────────┐
          │       TMT API Client        │
          │  backend/translators/*.py   │
          │                             │
          │  One sentence per request   │
          │  Retry + rate limit aware   │
          └─────────────┬──────────────┘
                        │
          ┌─────────────▼──────────────┐
          │      File Reconstructor     │
          │  backend/reconstructors/*.py │
          │                             │
          │  Rebuilds output in same    │
          │  format as original         │
          └─────────────────────────────┘

Pipeline

  1. Parse — extract text blocks with layout metadata (coordinates, font size, style)
  2. Split — break each block into individual sentences using NLTK (English) or punctuation rules (Nepali/Tamang)
  3. Translate — send each sentence to the TMT API with rate limiting and retry logic
  4. Reconstruct — reassemble translated sentences into the original file format

Format-Specific Behaviour

PDF Uses PyMuPDF (fitz) for both parsing and writing. Original text areas are redacted with a white fill, then translated text is drawn over the original page — preserving all non-text content (images, backgrounds, lines, columns). Devanagari text uses TextWriter for proper conjunct shaping via the font's GSUB/GPOS tables.

DOCX Uses python-docx. Iterates all paragraphs and table cells. Replaces text in-place while preserving run-level formatting (bold, italic, font size, heading styles).

CSV/TSV Uses Python's built-in csv module. Reads the full grid including empty cells, translates non-empty cells, writes back to the same row/column positions with the same delimiter.

Folder Structure

trilekha/
├── app.py                              # Streamlit entrypoint and orchestration
│
├── frontend/
│   ├── __init__.py
│   ├── constants.py                    # UI strings, language options, limits, theme tokens
│   └── ui.py                           # Streamlit UI components
│
├── backend/
│   ├── parsers/
│   │   ├── __init__.py
│   │   ├── pdf_parser.py               # PyMuPDF-based PDF text extraction
│   │   ├── pdf_text_repair.py          # Likhit-backed legacy Nepali PDF text repair
│   │   ├── docx_parser.py              # python-docx paragraph + table parser
│   │   └── csv_parser.py               # CSV/TSV cell extractor
│   │
│   ├── reconstructors/
│   │   ├── __init__.py
│   │   ├── pdf_writer.py               # Redact + overlay translated text on original PDF
│   │   ├── docx_writer.py              # In-place paragraph/cell replacement
│   │   └── csv_writer.py               # Grid reconstruction with translations
│   │
│   ├── translators/
│   │   ├── __init__.py
│   │   ├── tmt_client.py               # Single sentence API call with retry
│   │   ├── batch.py                    # Generator loop over sentence list
│   │   ├── config.py                   # Settings from environment variables
│   │   └── models.py                   # TranslationRequest / TranslationResponse
│   │
│   ├── utils/
│   │   └── sentence_splitter.py        # Shared NLTK + punctuation sentence splitter
│   │
│   └── __init__.py
│
├── fonts/
│   └── NotoSansDevanagari-Regular.ttf  # Required for Devanagari PDF rendering
│
├── .env.example                        # Template for environment variables
├── .gitignore
├── logo.webp                           # Light-mode app logo and page icon
├── logo-light.webp                     # Dark-mode app logo and page icon
├── logo.ico                            # Additional icon asset
├── pyproject.toml                      # Package definition for editable install
├── requirements.txt
├── LICENSE                             # MIT License
└── README.md

Local Setup

Prerequisites

  • Python 3.12 or newer
  • pip
  • A TMT API key

1. Clone the repository

git clone https://github.com/notashwinii/trilekha.git
cd trilekha

2. Create and activate a virtual environment

python3.12 -m venv venv

# Linux / macOS
source venv/bin/activate

# Windows
venv\Scripts\activate

3. Install dependencies

pip install -r requirements.txt
pip install -e .

4. Set up environment variables

cp .env.example .env

Open .env and fill in your values:

TMT_API_URL=https://tmt.ilprl.ku.edu.np/lang-translate
TMT_API_KEY=team_xxxxxxxxxxxxxxxx

# Optional tuning
TMT_REQUEST_TIMEOUT_SECONDS=10
TMT_RETRY_COUNT=3
TMT_RETRY_BACKOFF_SECONDS=0.5
TMT_REQUEST_SPACING_SECONDS=0.35
TMT_RATE_LIMIT_RETRY_SECONDS=60
TRILEKHA_DEV_LOGS=false

5. Check the Devanagari font

The repository includes fonts/NotoSansDevanagari-Regular.ttf, which is required for correct Nepali and Tamang rendering in translated PDFs. If the file is missing, download it:

mkdir -p fonts
wget "https://github.com/googlefonts/noto-fonts/raw/main/hinted/ttf/NotoSansDevanagari/NotoSansDevanagari-Regular.ttf" \
     -O fonts/NotoSansDevanagari-Regular.ttf

6. Run the app

streamlit run app.py

Open http://localhost:8501 in your browser.


Supported Language Pairs

From To API codes
English Nepali en → ne
English Tamang en → tmg
Nepali English ne → en
Nepali Tamang ne → tmg
Tamang English tmg → en
Tamang Nepali tmg → ne

Dependencies

Package Purpose
streamlit Web frontend
pymupdf PDF parsing and writing
python-docx DOCX parsing and writing
nltk English sentence tokenisation
python-dotenv Environment variable loading
requests TMT API HTTP calls
likhit Legacy Nepali PDF text repair

License

MIT License. See LICENSE for details.

All submitted work was developed specifically for the Google TMT Hackathon 2026.

About

Trilingual file translation tool

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages