Batch-translate .txt documents into English (or another target language) using Google's TranslateGemma models via HuggingFace Transformers. Supports automatic language detection and token-aware chunking to work within the 2048-token context window.
- Each
.txtfile in an input directory is read (with automatic encoding detection). - The source language is detected via
langdetect(or supplied with--source-lang). - If the document exceeds the token budget (900 tokens by default), it is split into chunks at paragraph and/or sentence boundaries.
- Chunks are translated via the HuggingFace Transformers pipeline. Truncated output is automatically detected and retried with smaller chunks.
- Translated chunks are reassembled and written to the output directory.
Spanish, German, French, Portuguese, Czech, Danish, Finnish, Greek, Hungarian, Hebrew, Italian, Norwegian, Polish, Slovak, Swedish, Turkish, Korean, Catalan, Basque, and others supported by langdetect and TranslateGemma (see technical report).
tgemma/
__init__.py # Package exports
chunking.py # Token-aware text splitting
cli.py # Typer CLI
detection.py # Language detection
orchestration.py # Translation workflows
translator.py # HuggingFace translator
utils.py # File I/O, exceptions
- Python >= 3.13
- GPU with sufficient VRAM (~24 GB for 12B model in bfloat16)
- HuggingFace account with accepted terms for TranslateGemma
git clone <repo-url> && cd tgemmaWith uv (recommended):
pip install uv # skip if already installed
uv sync
source .venv/bin/activateWith conda:
conda create -n tgemma python=3.13
conda activate tgemma
pip install -e .Authenticate with HuggingFace (you'll be prompted to paste your token — get one after accepting the TranslateGemma license):
hf auth loginDownload the model:
hf download google/translategemma-12b-ittgemma ./inputtgemma --help
Options:
--output-dir PATH Output directory (default: input_dir/translated)
--source-lang TEXT Source language code (auto-detect if not provided)
--target-lang TEXT Target language code (default: en)
--chunk-size INT Maximum tokens per chunk (default: 900)
--batch-size INT Chunks to translate in parallel (default: auto)
--model TEXT HuggingFace model (default: google/translategemma-12b-it)
--suffix TEXT Output filename suffix (default: _translated_{target_lang})
--fetch / --no-fetch Allow downloading from HuggingFace Hub (default: no-fetch)
--force / --no-force Re-translate even if output exists (default: no-force)
Useful for inspecting how documents will be split:
tgemma chunk ./input
tgemma chunk ./input --chunk-size 500With conda:
cd /scratch/gpfs/$USER/tgemma
conda create -n tgemma python=3.13
conda activate tgemma
pip install -e .With uv:
cd /scratch/gpfs/$USER/tgemma
pip install uv # skip if already installed
uv sync
source .venv/bin/activateAuthenticate and download the model to local cache:
hf auth login
HF_HOME=./.hf hf download google/translategemma-27b-itWith conda:
#!/bin/bash
#SBATCH --job-name=translate
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=4
#SBATCH --mem=10G
#SBATCH --gres=gpu:1
#SBATCH --constraint=gpu80
#SBATCH --time=24:00:00
#SBATCH --mail-type=all
#SBATCH --mail-user=<user>@princeton.edu
#SBATCH --output=/scratch/gpfs/your-net-id/some-project-dir/translate.out
cd /scratch/gpfs/$USER/tgemma/
conda activate tgemma
export HF_HOME=./.hf
export PYTHONUNBUFFERED=1
tgemma --model google/translategemma-27b-it ./inputWith uv:
#!/bin/bash
#SBATCH --job-name=translate
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=4
#SBATCH --mem=10G
#SBATCH --gres=gpu:1
#SBATCH --constraint=gpu80
#SBATCH --time=24:00:00
#SBATCH --mail-type=all
#SBATCH --mail-user=<user>@princeton.edu
#SBATCH --output=/scratch/gpfs/your-net-id/some-project-dir/translate.out
cd /scratch/gpfs/$USER/tgemma/
source .venv/bin/activate
export HF_HOME=./.hf
export PYTHONUNBUFFERED=1
tgemma ./input --model google/translategemma-27b-itSimply submit your slurm job like so:
sbatch translate.slurm
from tgemma import (
HuggingFaceTranslator,
load_tokenizer,
translate_text,
)
# Load tokenizer and translator
tokenizer = load_tokenizer("google/translategemma-12b-it")
translator = HuggingFaceTranslator(
"google/translategemma-12b-it",
tokenizer=tokenizer,
)
# Translate text
result = translate_text(
"Hallo, wie geht es dir?",
translator,
source_lang="de",
target_lang="en",
)