Skip to content

Translation

Bases: TextAPI

A class for serving a Hugging Face-based translation model as a web API. This API allows users to submit text for translation and receive translated text in the specified target language using advanced machine learning models.

Parameters:

Name Type Description Default
input BatchInput

Configurations and data inputs for the batch process.

required
output BatchOutput

Configurations for output data handling.

required
state State

State management for the translation task.

required
**kwargs Any

Additional keyword arguments for extended configurations.

{}

Example CLI Usage for interacting with the API:

To start the API server:

genius TranslationAPI rise \
    batch \
        --input_folder ./input \
    batch \
        --output_folder ./output \
    none \
    --id facebook/mbart-large-50-many-to-many-mmt-lol \
    listen \
        --args \
            model_name="facebook/mbart-large-50-many-to-many-mmt" \
            model_class="AutoModelForSeq2SeqLM" \
            tokenizer_class="AutoTokenizer" \
            use_cuda=True \
            precision="float" \
            quantization=0 \
            device_map="cuda:0" \
            max_memory=None \
            torchscript=False \
            endpoint="*" \
            port=3000 \
            cors_domain="http://localhost:3000" \
            username="user" \
            password="password"

To translate text using the API:

curl -X POST localhost:8080/translate \
    -H "Content-Type: application/json" \
    -d '{
        "text": "Hello, world!",
        "source_lang": "en",
        "target_lang": "fr",
        "decoding_strategy": "beam_search",
        "num_beams": 5
    }'

initialize_pipeline()

Lazy initialization of the translation Hugging Face pipeline.

translate(**kwargs)

Translates text to a specified target language using the underlying Hugging Face model.

This endpoint accepts JSON data with the text and language details, processes it through the machine learning model, and returns the translated text.

Parameters:

Name Type Description Default
**kwargs Any

Arbitrary keyword arguments, usually empty as parameters are in the POST body.

{}
POST body parameters

text (str): The text to be translated. decoding_strategy (str): Strategy to use for decoding text; e.g., 'beam_search', 'greedy'. Default is 'generate'. source_lang (str): Source language code. target_lang (str): Target language code. Default is 'en'. additional_params (dict): Other model-specific parameters for translation.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary with the original text, target language, and translated text.

Example CURL requests:

To translate text from English to French:

curl -X POST localhost:8080/translate \
    -H "Content-Type: application/json" \
    -d '{
        "text": "Hello, world!",
        "source_lang": "en",
        "target_lang": "fr",
        "decoding_strategy": "beam_search",
        "num_beams": 5
    }'

To translate text from Spanish to German:

/usr/bin/curl -X POST localhost:3000/api/v1/translate \
    -H "Content-Type: application/json" \
    -d '{
        "text": "संयुक्त राष्ट्र के प्रमुख का कहना है कि सीरिया में कोई सैन्य समाधान नहीं है",
        "source_lang": "hi_IN",
        "target_lang": "en_XX",
        "decoding_strategy": "generate",
        "decoder_start_token_id": 2,
        "early_stopping": true,
        "eos_token_id": 2,
        "forced_eos_token_id": 2,
        "max_length": 200,
        "num_beams": 5,
        "pad_token_id": 1
    }' | jq

translate_pipeline(**kwargs)

Endpoint for translating text using a pre-initialized Hugging Face translation pipeline. This method is designed to handle translation requests more efficiently by utilizing a preloaded model and tokenizer, reducing the overhead of loading these components for each request.

Returns:

Type Description
Dict[str, Any]

Dict[str, Any]: A dictionary containing the original text, source language, target language, and the translated text.

Example CURL Request for translation:

curl -X POST localhost:8080/translate_pipeline             -H "Content-Type: application/json"             -d '{
        "text": "Hello, world!",
        "source_lang": "en",
        "target_lang": "fr"
    }'