Skip to content

General Instructions on How to Use the Package

This package allows you to create translator objects for translating text according to your needs.

Available Translators

The following translators are available, each identified by a unique ID:

Translator ID (translator_name) Description
MBart50 mbart A multilingual translation model supporting 50 languages. It can also run locally without external dependencies. This is also the best machine translator out of the evaluation we did.
Gemini gemini Gemini is a state-of-the-art translation model from Google, available in various versions. It requires an API key for access.
GPT gpt GPT models from OpenAI can be used for translation tasks, leveraging their advanced language understanding capabilities. Requires an API key.
Claude claude Claude models from Anthropic are designed for complex language tasks, including translation. Requires an API key.
Ollama ollama Ollama is a platform for running large language models locally. It supports various models for translation tasks. Requires Ollama to be installed and running.

Basic Usage

To use a translator, you first need to initialize it using its translator_name ID.

Initialize the Translator
from easy_nlp_translate import initialize_translator

# Initialize the translator using its ID
translator = initialize_translator(
    translator_name="mbart", 
    source_lang="en", 
    target_lang="de"
)

Initialization Parameters

For more details on source_lang, target_lang, and any other parameters specific to each translator (like API keys or model paths), please refer to the detailed documentation for the respective translator classes.

Once the translator object is created, you can use its translate method to translate your text.

Translating Text
translated_text = translator.translate("My dog is beautiful.")

print(translated_text)
# Expected output (will vary based on the model and target language):
# Mein Hund ist wunderschön.

You can also translate multiple texts in a batch if the translator supports it (e.g., the translate_batch method). Please check the specific translator's documentation for availability and usage of batch translation.

LLM Translator Initialization

There is also the option to initialize the translator using LLMs like Gemini, GPT, or Claude. This is useful for more complex translation tasks that require understanding context or nuances in the text and to use prompts based on the prompt library we offer.

Initialize LLM Translator
from easy_nlp_translate import initialize_translator

# Initialize the LLM translator using its ID
translator = initialize_translator(
    translator_name="gemini", 
    model_name="gemini-1.5-pro",
    source_lang="en", 
    target_lang="de",
    prompt_type="formal",
)

LLM Translator Parameters

The parameters for LLM translators may include model_name, prompt_type, and others specific to the LLM being used. Refer to the documentation for each LLM translator for more details.

These are the available LLM translators:

Translator ID (translator_name) Models API_KEY env var
Gemini gemini gemini-2.5-flash-preview-05-20, gemini-2.5-pro-preview-05-06, gemini-2.0-flash, gemini-2.0-flash-lite, gemini-1.5-flash, gemini-1.5-flash-8b,gemini-1.5-pro GEMINI_API_KEY
GPT gpt gpt-4.1, gpt-4.1-mini, gpt-4.1-nano, gpt-4.5-preview, gpt-4o, gpt-4o-mini OPENAI_API_KEY
Claude claude claude-opus-4-0, claude-sonnet-4-0, claude-3-7-sonnet-latest, claude-3-5-sonnet-latest, claude-3-5-haiku-latest, claude-3-opus-latest ANTHROPIC_API_KEY
Ollama ollama your downloaded models ;) not needed

Prompt Types for LLM Translators

When using LLM translators, you can specify different prompt types to tailor the translation style and output. The available prompt types are designed to suit various translation needs, from formal translations to more creative styles like romantic or poetic translations.

These are the available prompt types:

Prompt Type Description
default Basic Translation
formal Formal Translation
translate_and_summarize Summarize and Translate
formal_translate_and_summarize Formal Summarize and Translate
romantic Romantic Translation
poetic Poetic Translation
custom Custom Prompt

If you choose custom, you can provide your own rule definition as a string in the custom_prompt parameter when initializing the translator.

Custom Prompt Example
from easy_nlp_translate import initialize_translator

# Initialize the translator with a custom prompt
translator = initialize_translator(
    translator_name="gemini", 
    model_name="gemini-1.5-pro",
    source_lang="en", 
    target_lang="de",
    prompt_type="custom",
    custom_prompt="Translate the text into German with a friendly tone."
)

Docker Deployment

Keep in mind if you want to use the package in a docker container while using mbart, you need to use CMD instead of ENTRYPOINT in your Dockerfile to avoid issues with the mbart model. This is due to the reason that huggingface transformers need to be run in a subprocess for downloading the model files correctly.

Dockerfile Example
FROM python:3.11-slim

WORKDIR /app

RUN pip install easy-nlp-translate

COPY your_script.py .

CMD ["python", "your_script.py"]