Argos Translate

1.11.0 · active · verified Thu Apr 16

Argos Translate is an open-source neural machine translation (NMT) library for Python, based on OpenNMT's CTranslate2. It enables offline translation, supporting a wide array of languages through downloadable '.argosmodel' packages. The library can be utilized as a Python API, a command-line tool, or a GUI application. The current version is 1.11.0, with new releases typically published every three months.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to programmatically download and install a language model, then use it to translate text. The library requires explicit installation of language models (e.g., 'en' to 'es') after the main library itself is installed.

import argostranslate.package
import argostranslate.translate

# Define source and target languages using ISO 639-1 codes
from_code = "en"
to_code = "es"

# Update package index and download/install the desired language model
print("Updating package index...")
argostranslate.package.update_package_index()

available_packages = argostranslate.package.get_available_packages()
package_to_install = next(
    filter(
        lambda x: x.from_code == from_code and x.to_code == to_code,
        available_packages
    ),
    None
)

if package_to_install:
    print(f"Downloading and installing {from_code} -> {to_code} package...")
    download_path = package_to_install.download()
    argostranslate.package.install_from_path(download_path)
    print("Package installed.")
else:
    print(f"No package found for {from_code} -> {to_code}. Please check available packages or codes.")

# Perform translation
installed_languages = argostranslate.translate.get_installed_languages()
from_lang = next(filter(lambda x: x.code == from_code, installed_languages), None)
to_lang = next(filter(lambda x: x.code == to_code, installed_languages), None)

if from_lang and to_lang:
    print(f"Translating 'Hello World' from {from_code} to {to_code}...")
    translatedText = from_lang.get_translation(to_lang).translate("Hello World")
    print(f"Translated text: {translatedText}")
else:
    print("Source or target language not found among installed languages.")

view raw JSON →