Skip to main content

Firefox Translations for Python

License: MPL 2.0 Python 3.10+

Fast, private, local neural machine translation in Python powered by official Firefox Translations models.


Features

  • In-Memory Model Persistence: Preload models once in memory with zero per-request reloading latency.
  • 🚀 GPU & CPU Acceleration: Automatic hardware detection (cuda / cpu), configurable thread counts, and precision quantization (int8, float16, bfloat16).
  • 🌊 Synchronous & Asynchronous Streaming: First-class streaming iterators (translate_stream & translate_stream_async) for real-time translation pipelines and web frameworks.
  • 📦 Automated Model Management: Automatically fetches, verifies, and converts official Mozilla Marian models to high-efficiency CTranslate2 format with smart caching.
  • 🔒 100% Private & Offline Capable: Zero cloud dependencies or API keys required. Models run completely on your local machine.
  • 🛠️ Full-Featured CLI: Command-line tool firefox-translate for interactive usage, stdin pipelines, and batch model pre-downloads.

Installation

Install using uv (recommended) or pip:

# Using uv
uv add firefox-translations

# Using standard pip
pip install firefox-translations

For development and running the test suite:

git clone https://github.com/mozilla/translations.git
cd translations/firefox_translations
uv sync --extra dev
uv run pytest -v

Quickstart

1. Basic Single & Batch Translation

from firefox_translations import Translator

# Initialize translator (models are downloaded and cached automatically on first run)
translator = Translator(src_lang="en", trg_lang="es", device="auto")

# Single string translation
result = translator.translate("Hello world! Machine translation runs completely on device.")
print(result)
# Output: ¡Hola mundo! La traducción automática se ejecuta completamente en el dispositivo.

# Batch translation with optimized batching
texts = [
    "Good morning!",
    "Privacy-preserving machine translation is essential.",
    "Firefox Translations runs locally in Python."
]
results = translator.translate_batch(texts, batch_size=16)
for src, trg in zip(texts, results):
    print(f"{src} -> {trg}")

2. Listing Available Language Pairs

from firefox_translations import ModelRegistry

registry = ModelRegistry()
pairs = registry.list_available_pairs()

print(f"Supported language pairs ({len(pairs)}):")
for src, trg in sorted(pairs):
    print(f"  {src} -> {trg}")

In-Memory Retention & Performance

firefox-translations keeps models resident in memory to ensure microsecond-level invocation overhead for real-time web services, bots, and high-throughput pipelines.

from firefox_translations import Translator

# Explicitly manage memory lifecycle
translator = Translator(src_lang="en", trg_lang="fr", device="auto")

print(translator.is_loaded)  # True

# Unload from memory if needed (e.g. idle timeout or freeing GPU VRAM)
translator.unload()
print(translator.is_loaded)  # False

# Preload back into memory before high-traffic bursts
translator.preload()

Threading & Concurrency

Tune CPU thread utilization with inter_threads (parallel batch workers) and intra_threads (threads per computation):

translator = Translator(
    src_lang="en",
    trg_lang="de",
    inter_threads=2,  # Number of concurrent workers
    intra_threads=4,  # CPU cores per worker
)

GPU & CPU Acceleration

Configure computation device and quantization parameters:

# Auto-detect CUDA GPU, falling back to CPU
translator = Translator(src_lang="en", trg_lang="es", device="auto")

# Force CUDA on GPU device index 0 with INT8 quantization for minimal VRAM footprint
translator_gpu = Translator(
    src_lang="en",
    trg_lang="es",
    device="cuda",
    device_index=0,
    compute_type="int8_float16"  # Options: default, int8, int8_float16, float16, bfloat16
)

# Multi-GPU inference
translator_multi = Translator(
    src_lang="en",
    trg_lang="es",
    device="cuda",
    device_index=[0, 1]
)

Streaming & Async API

Synchronous Streaming

Process large files, line-by-line generators, or token streams without loading all data into memory:

def text_stream():
    yield "First paragraph to translate."
    yield "Second paragraph arriving in the stream."
    yield "Final closing thoughts."

translator = Translator(src_lang="en", trg_lang="it")

for translated_chunk in translator.translate_stream(text_stream(), batch_size=2):
    print(translated_chunk)

Asynchronous Streaming (FastAPI / Quart / aiohttp)

import asyncio
from firefox_translations import Translator

async def async_token_source():
    messages = [
        "Welcome to our real-time service.",
        "Your translations are computed asynchronously.",
        "Enjoy fast inference!"
    ]
    for msg in messages:
        await asyncio.sleep(0.05)
        yield msg

async def main():
    translator = Translator(src_lang="en", trg_lang="es")
    
    async for translated in translator.translate_stream_async(async_token_source(), batch_size=2):
        print("Received async translation:", translated)

asyncio.run(main())

Command Line Interface (CLI)

The package provides the firefox-translate command:

Translate Text Directly

firefox-translate --from en --to es "Local machine translation is fast and private."

Stream via Standard Input (Piping)

cat article.txt | firefox-translate -f en -t fr > article_fr.txt

List Available Models

firefox-translate --list-models

Pre-download & Cache Model

# Download and convert model ahead of time for offline use
firefox-translate --from en --to uk --download

Specify Inference Device & Quantization

firefox-translate --from en --to de --device cuda --compute-type float16 "Translate using CUDA GPU."

API Reference

Translator

Translator(
    src_lang: str,
    trg_lang: str,
    model_dir: Optional[Union[str, Path]] = None,
    device: str = "auto",                  # "auto", "cuda", or "cpu"
    device_index: Union[int, List[int]] = 0,
    compute_type: str = "default",        # "default", "int8", "float16", "int8_float16", "bfloat16"
    inter_threads: int = 1,
    intra_threads: int = 0,
    cache_dir: Optional[Union[str, Path]] = None,
    beam_size: int = 1
)
  • translate(text: str, beam_size: Optional[int] = None) -> str
  • translate_batch(texts: List[str], batch_size: int = 32, beam_size: Optional[int] = None) -> List[str]
  • translate_stream(stream: Iterable[str], batch_size: int = 16, beam_size: Optional[int] = None) -> Iterator[str]
  • translate_stream_async(async_stream: AsyncIterable[str], batch_size: int = 16, beam_size: Optional[int] = None) -> AsyncIterator[str]
  • preload() -> None
  • unload() -> None
  • is_loaded: bool

ModelRegistry & ModelManager

  • ModelRegistry.list_available_pairs() -> List[Tuple[str, str]]: Returns available source and target language pairs.
  • ModelRegistry.get_model_metadata(src_lang: str, trg_lang: str) -> Dict[str, Any]: Retrieves model metadata from registry.
  • ModelManager.ensure_model(src_lang: str, trg_lang: str) -> Path: Downloads, caches, and returns path to converted model.

License

This project is licensed under the Mozilla Public License 2.0 (MPL-2.0). Model weights are provided under their respective Mozilla and Bergamot open-source licenses.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

firefox_translations-0.1.2.tar.gz (109.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

firefox_translations-0.1.2-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file firefox_translations-0.1.2.tar.gz.

File metadata

  • Download URL: firefox_translations-0.1.2.tar.gz
  • Upload date:
  • Size: 109.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for firefox_translations-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5f69bf7f0d486ba1a74aa280ec804779219c8b5f5de7b280484e74e31accad33
MD5 cc9eae2f9c55f549524cd810eb9aa471
BLAKE2b-256 dd8fb7a6109dcfd2c426b7cbb243aa71a8c3eb8f29d62d18b0b411c2ba0a8197

See more details on using hashes here.

File details

Details for the file firefox_translations-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: firefox_translations-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for firefox_translations-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 52b44e5f4240b2be7176ea48b4098b666fd1bd9e64a93083afd7a8e54c4685fe
MD5 20f08e9371ff134a25f08f5144323cf6
BLAKE2b-256 69a3910d05185c10b382c9c4329621eebfa20977a73112b6815745ff3100489c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page