Main application where the translation web service endpoints are defined.


We are using Flask to define the web service endpoints, and we are using Hugging Face’s transformers library to load the model and perform the translation.

from flask import Flask, request, jsonify
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

Loading Models

We are loading the models from the models folder. The models are MarianMT models.



 get_model (model_path:str)

Load a Hugging Face model and tokenizer from the specified directory

# Load the models and tokenizers for each supported language
en_fr_model, en_fr_tokenizer = get_model('models/en_fr/')
fr_en_model, fr_en_tokenizer = get_model('models/fr_en/')

Web Service Endpoints

The next step is to use Flask to create the English/French and French/English translation web service endpoints.

We first check if the path to the translation web service exists. The two paths currently defined are:

  • /translate/en/fr
  • /translate/fr/en

Once the container is running, we can test the web service endpoints using curl:

curl http://localhost:6000/translate/en/fr/ POST -H "Content-Type: application/json" -d '{"en_text":"Hello World!"}'

The output should be:

  "fr_text": "Bonjour le monde!"
curl http://localhost:6000/translate/fr/en/ POST -H "Content-Type: application/json" -d '{"fr_text":"Bonjour le monde!"}'

The output should be:

  "en_text": "Hello world!"



 translate_endpoint (from_lang:str, to_lang:str)

Translate text from one language to another. This function is called when a POST request is sent to /translate/<from_lang>/<to_lang>/



 is_translation_supported (from_lang:str, to_lang:str)

Check if the specified translation is supported

assert is_translation_supported('en', 'fr')
assert is_translation_supported('fr', 'en')
assert not is_translation_supported('en', 'es')
assert not is_translation_supported('es', 'en')


Finally, we define the entry point of the application. This is the file that will be executed when the container is run. It will run the Flask application on port 6000 and enables the debug mode.

if __name__ == '__main__':'', port=6000, debug=True)