2. Open Neural Machine Translation Models
Learning Objectives
After completing this section, you should be able to:
-
Understand the distinction between an NMT model and MTUOC-server and their respective roles in the translation workflow.
-
Identify the main characteristics of OpusMT and NLLB and select appropriate models and translation directions.
-
Test open NMT models independently using Python and Hugging Face.
-
Configure, run, and test OpusMT and NLLB models with MTUOC-server.
Suggested Reading
To gain a deeper understanding of the two open NMT approaches explored in this section, we recommend the following academic papers:
- Tiedemann, J., Aulamo, M., Bakshandaeva, D., Boggia, M., Grönroos, S. A., Nieminen, T., et al. (2024). Democratizing neural machine translation with OPUS-MT. Language Resources and Evaluation, 58(2), 713–755.
- NLLB Team (2024). Scaling neural machine translation to 200 languages. Nature, 630, 841–846.
2.0. Overview of Open NMT Models
As explained in Section 1, MTUOC-server provides the infrastructure required to connect translation engines with client applications such as CAT tools and translation software. The actual translation, however, is performed by the underlying machine translation model selected in the server configuration.
When MTUOC-server is launched, it starts a specific translation engine according to the configuration file being used. The basic server configuration identifies, among other parameters:
- the MT engine to be used;
- the model configuration file associated with that engine;
- the port through which client applications connect to the server;
- the communication protocol used by the server.
For example, the default configuration distributed with MTUOC-server uses an OpusMT English–Spanish model. When the server starts, it displays information about the active model, IP address, port, and server type. Client applications need this connection information in order to send source segments to the server and receive their translations.
MTUOC-server supports different types of translation technology, including neural machine translation systems, Large Language Models (LLMs), commercial translation APIs, and rule-based machine translation systems. In this section, however, we focus specifically on two open neural machine translation model families:
- OpusMT, a large collection of pre-trained bilingual and multilingual translation models closely associated with the OPUS parallel corpus ecosystem.
- NLLB (No Language Left Behind), a multilingual neural machine translation system designed to support translation across more than 200 languages.
These two model families illustrate two different approaches to open NMT. OpusMT provides a large collection of models, many of which are designed for specific language pairs, whereas NLLB uses a multilingual model capable of handling a large number of source and target languages through language codes.
For both OpusMT and NLLB, we will follow a similar practical workflow:
- identify and select an appropriate pre-trained model;
- test the model independently using Python and the Hugging Face
transformerslibrary; - configure the model for use with MTUOC-server;
- start the server and test the translation service;
- examine the main configuration parameters that control model behaviour.
This workflow makes it possible to distinguish clearly between the translation model, which generates the translation, and MTUOC-server, which manages deployment, communication, preprocessing, postprocessing, and integration with external applications.
Later sections will apply a similar deployment logic to Large Language Models, including models accessed through Hugging Face and other frameworks.
2.1. OpusMT
OpusMT is a project closely associated with the OPUS corpora, dedicated to the distribution of a large collection of open Neural Machine Translation models.
Many OpusMT models are designed for particular language pairs, although multilingual models are also available. The models can be downloaded from Hugging Face and deployed locally through MTUOC-server.
A large collection of available OpusMT models can be explored in the OpusMT model registry.
2.1.1. Testing OpusMT with Python
Before integrating a model into MTUOC-server, it is useful to test it independently. This allows us to verify that the model can be loaded and produces translations before introducing the server configuration.
The example script is available as translateOpusMT.py.
First, install the required Python libraries:
transformers
torch
sentencepiece
sacremoses
The following script loads an English–Spanish OpusMT model and translates a sentence:
from transformers import MarianMTModel, MarianTokenizer
def translate_text(text, model_name):
"""
Translates a given string using a specific Opus-MT model.
"""
# 1. Load the tokenizer and the model from Hugging Face
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)
# 2. Tokenize the input text
# The 'pt' return_tensors means we are using PyTorch
inputs = tokenizer(text, return_tensors="pt", padding=True)
# 3. Generate the translated tokens
translated_tokens = model.generate(**inputs)
# 4. Decode the tokens back into a human-readable string
result = tokenizer.decode(
translated_tokens[0],
skip_special_tokens=True
)
return result
if __name__ == "__main__":
# Configuration
# This way the model is automatically downloaded:
# model_id = "Helsinki-NLP/opus-mt-en-es"
# To use an already downloaded model:
model_id = "../../models/opus-mt-en-es"
input_sentence = "This is a translation test using OpusMT"
print("Loading model and performing translation...")
# Execution
translation = translate_text(input_sentence, model_id)
# Output
print("-" * 30)
print(f"Original text: {input_sentence}")
print(f"Translated text: {translation}")
print("-" * 30)
The important distinction here is between:
model_id = "Helsinki-NLP/opus-mt-en-es"
which downloads the model from Hugging Face when required, and:
model_id = "../../models/opus-mt-en-es"
which loads a model that has already been downloaded locally.
Try changing the input sentence and running the script again.
You can also select another OpusMT model and observe how model selection determines the available translation direction.
2.1.2. Configuring MTUOC-server for OpusMT
Once the model has been tested independently, we can make it available as a translation service through MTUOC-server.
Two configuration files are required:
config-server-opusmt.yaml
config-model-opusmt-eng-spa.yaml
The first configures MTUOC-server, while the second configures the OpusMT model.
This separation is important:
server configuration
↓
model configuration
↓
translation model
Server configuration
In config-server-opusmt.yaml, set the MT engine to OpusMT and indicate the model configuration file:
MTengine: OpusMT
# OpusMT, NLLB, HuggingFace, Ollama, ctranslate2,
# GoogleT, DeepL, Marian, Eole, Apertium...
system_name: OpusMT-eng-spa
model_config: config-model-opusmt-eng-spa.yaml
The rest of the configuration controls the server itself and optional processing components:
MTUOCServer:
port: 8000
type: MTUOC
# one of MTUOC, Moses, ModernMT, OpenNMT, NMTWizard
verbosity_level: 1
log_file: log.log
dopreprocess: False
preprocess_config: None
dotokenization: False
tokenization_config: None
dopostprocess: False
postprocess_config: None
TranslationMemory:
use: False
memo: None
minsim: 100
Here, for example:
portspecifies the port through which clients will communicate with MTUOC-server;typespecifies the communication protocol;- preprocessing and postprocessing are disabled;
- translation memory integration is disabled.
2.1.3. Configuring the OpusMT Model
The model itself is configured in config-model-opusmt-eng-spa.yaml:
OpusMT:
# model_path: Helsinki-NLP/opus-mt-en-es
model_path: "./opus-mt-en-es"
beam_size: 5
num_hypotheses: 5
multilingual_prefix: ""
device: cuda
# cuda or cpu
The model_path parameter identifies the model to use.
You can specify a Hugging Face model:
model_path: Helsinki-NLP/opus-mt-en-es
In this case, the model will be downloaded when required.
Alternatively, you can provide the path to a model already stored locally:
model_path: "./opus-mt-en-es"
Most OpusMT models are bilingual, meaning that the model is designed for a particular source and target language. For these models:
multilingual_prefix: ""
Some OpusMT models are multilingual. In these cases, multilingual_prefix can be used to specify the target language using the format:
>>id<<
For example:
>>ca<<
can be used when Catalan is a valid target language for the selected multilingual model.
The parameters:
beam_size: 5
num_hypotheses: 5
control aspects of translation generation and the number of translation candidates considered or produced.
Finally:
device: cuda
specifies the device on which the model will run. It can be changed to:
device: cpu
when GPU execution is not available.
2.1.4. Installing the OpusMT Requirements
If you only plan to use OpusMT, you do not necessarily need to install every dependency supported by MTUOC-server. The OpusMT-specific requirements include:
# GENERAL
pyyaml
requests
flask
waitress
websocket-client
sentencepiece
sacremoses
ftfy
protobuf
# OPUS MT
torch
2.1.5. Starting and Testing the Server
MTUOC-server can be started from the terminal:
python3 MTUOC-server.py config-server-opusmt.yaml
When the model has been successfully loaded, the terminal displays information about the running server, including the model, IP address, port, and protocol.
An example output is:
INFO: TransformersTranslator started (Auto-device: cuda)
Loading config inside class: config-model-opusmt-eng-spa.yaml
INFO: Device changed to cuda (the model will be loaded here)
Loading weights: 100%|████████████████████| 258/258 [00:00<00:00, 91550.80it/s]
Translating with OpusMT models ./opus-mt-en-es
MTUOC server started using MTUOC protocol
MTUOC server IP: 192.168.0.139
MTUOC server port: 8000
MTUOC server type: MTUOC
Alternatively, you can start the server using MTUOC-server-manager. Select the appropriate model in the Control tab and click Start.
If the server was started from the terminal, you can test it with:
python3 MTUOC-test-server.py config-server-opusmt.yaml
The test application allows you to enter a source text and send it to the running server.
If you are using MTUOC-server-manager, the same operation can be performed from the Test tab.
When you have finished working with the server, stop it from MTUOC-server-manager using the Stop button or from the terminal with:
python3 MTUOC-stop-server.py config-server-opusmt.yaml
2.1.6. Using an MTUOC Recipe
MTUOC recipes provide ready-made configurations that can be downloaded and adapted.
For a basic English–Spanish OpusMT configuration without preprocessing or postprocessing, you can use:
recipe-OpusMT-noprepost-eng-spa.yaml
The recipe can be installed with MTUOC-downloader:
python3 MTUOC-downloader.py https://huggingface.co/datasets/aoliverg/MTUOC-recipes/resolve/main/recipe-OpusMT-noprepost-eng-spa.yaml
Alternatively, use the Recipes tab in MTUOC-server-manager.
The downloaded recipe may place some configuration files in the MTUOC-server directory and others in the model directory.
2.1.7. Practical Activity
- Select an OpusMT model.
- Test it independently using the Python script.
- Configure the model in MTUOC-server.
- Start the server.
- Translate several sentences using the test application.
- Change at least one configuration parameter and observe the result.
- Stop the server when you have finished.
2.2. NLLB
NLLB (No Language Left Behind) is a multilingual neural machine translation model. Unlike a bilingual model designed for one particular translation direction, the same NLLB model can translate between a large number of languages.
NLLB supports more than 200 languages. The required source and target languages are specified using language codes.
This means that changing the translation direction does not necessarily require downloading another model. Instead, the same multilingual model can be configured with different source and target language codes.
2.2.1. Testing NLLB with Python
As with OpusMT, we will first test the model independently before integrating it into MTUOC-server.
The example script is available as translateNLLB.py.
The basic requirements are:
transformers
torch
The following example uses the nllb-200-distilled-600M model:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
def translate_nllb(text, model_name, source_lang, target_lang):
"""
Translates text using the NLLB model.
"""
# 1. Load the tokenizer and model
# NLLB uses Auto classes for easier loading
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
# 2. Prepare the input
inputs = tokenizer(text, return_tensors="pt", padding=True)
# 3. Generate the translation specifying the target language code
# forced_bos_token_id tells the model which language to translate into
forced_bos_token_id = tokenizer.convert_tokens_to_ids(target_lang)
translated_tokens = model.generate(
**inputs,
forced_bos_token_id=forced_bos_token_id
)
# 4. Decode the output
result = tokenizer.decode(
translated_tokens[0],
skip_special_tokens=True
)
return result
if __name__ == "__main__":
# Model configuration
# If you want to download from Hugging Face:
# model_id = "facebook/nllb-200-distilled-600M"
# If you want to use a local version:
model_id = "../../models/nllb-200-distilled-600M"
# Language codes:
# Spanish: spa_Latn | English: eng_Latn | Catalan: cat_Latn
src_code = "eng_Latn"
tgt_code = "spa_Latn"
input_sentence = "This is a translated sentence using NLLB."
print(f"Loading {model_id}...")
translation = translate_nllb(
input_sentence,
model_id,
src_code,
tgt_code
)
print("-" * 30)
print(f"Source ({src_code}): {input_sentence}")
print(f"Target ({tgt_code}): {translation}")
print("-" * 30)
As with OpusMT, the model can be loaded directly from Hugging Face:
model_id = "facebook/nllb-200-distilled-600M"
or from a local directory:
model_id = "../../models/nllb-200-distilled-600M"
The main difference is that we also specify the language direction:
src_code = "eng_Latn"
tgt_code = "spa_Latn"
For example:
eng_Latn = English
spa_Latn = Spanish
cat_Latn = Catalan
Try changing:
tgt_code = "cat_Latn"
and translate the same English sentence into Catalan.
2.2.2. Configuring MTUOC-server for NLLB
As with OpusMT, two configuration files are required:
config-server-NLLB.yaml
config-model-NLLB.yaml
The first configures MTUOC-server and identifies NLLB as the translation engine.
A basic configuration follows the same structure used previously:
MTengine: NLLB
system_name: NLLB
model_config: config-model-NLLB.yaml
MTUOCServer:
port: 8000
type: MTUOC
# one of MTUOC, Moses, ModernMT, OpenNMT, NMTWizard
verbosity_level: 1
log_file: log.log
dopreprocess: False
preprocess_config: None
dotokenization: False
tokenization_config: None
dopostprocess: False
postprocess_config: None
TranslationMemory:
Path: None
sl: es
tl: en
The server configuration therefore identifies which engine is being used, while the model configuration specifies which NLLB model and language direction should be loaded.
2.2.3. Configuring the NLLB Model
The model configuration is stored in config-model-NLLB.yaml:
NLLB:
# model: facebook/nllb-200-distilled-600M
model: "./nllb-200-distilled-600M"
# A Hugging Face repository or a full/relative path to a local model
src_lang: eng_Latn
tgt_lang: spa_Latn
device: gpu
beam_size: 5
num_hypotheses: 5
NLLB is distributed in several model sizes:
facebook/nllb-200-distilled-600M
facebook/nllb-200-1.3B
facebook/nllb-200-distilled-1.3B
facebook/nllb-200-3.3B
The model parameter can point either to a Hugging Face repository:
model: facebook/nllb-200-distilled-600M
or to a model already stored locally:
model: "./nllb-200-distilled-600M"
The translation direction is controlled by:
src_lang: eng_Latn
tgt_lang: spa_Latn
For example, changing the target language to Catalan requires:
src_lang: eng_Latn
tgt_lang: cat_Latn
The device parameter determines whether the model is run on the CPU or GPU:
device: gpu
The parameters:
beam_size: 5
num_hypotheses: 5
control aspects of translation generation and the number of translation candidates considered or produced.
2.2.4. NLLB Language Codes
NLLB uses language codes that identify both the language and, where relevant, the writing system.
Some useful examples are:
| Language | NLLB code |
|---|---|
| Catalan | cat_Latn |
| Chinese (Simplified) | zho_Hans |
| Chinese (Traditional) | zho_Hant |
| English | eng_Latn |
| French | fra_Latn |
| German | deu_Latn |
| Italian | ita_Latn |
| Portuguese | por_Latn |
| Spanish | spa_Latn |
The appropriate codes must be used in the model configuration when defining the source and target languages.
2.2.5. Starting and Testing the Server
Once the NLLB server and model configuration files have been prepared, start MTUOC-server using the NLLB server configuration.
You can then use the MTUOC test application to send source segments to the running server and verify that the selected NLLB model and language direction are working correctly.
As with OpusMT, you can also start and test the server through MTUOC-server-manager.
When testing different translation directions, modify the source and target language codes in the NLLB model configuration and restart the server before testing the new configuration.
2.2.6. Practical Activity
- Load
facebook/nllb-200-distilled-600Mor the corresponding local model. - Test an English–Spanish translation with the Python script.
- Change the target language from Spanish (
spa_Latn) to Catalan (cat_Latn). - Compare the two translations.
- Configure NLLB in MTUOC-server.
- Start the server and test the translation service.
- Change the source or target language in the model configuration and test another translation direction.
Key Takeaways
After completing this section, you should remember that:
-
The translation model and MTUOC-server have different roles: the model generates translations, while MTUOC-server deploys it and connects it with external applications.
-
OpusMT and NLLB represent different approaches to open NMT: OpusMT provides many models for specific language pairs as well as some multilingual models, while NLLB provides multilingual models supporting more than 200 languages.
-
Open NMT models can be loaded from Hugging Face or locally, tested independently, and then integrated into MTUOC-server through separate server and model configurations.
-
Once configured in MTUOC-server, NMT models can be run as translation services and subsequently integrated into professional translation workflows, including CAT tools.