Translate text between languages
Tags: ai, llm, rag, vectordatabase
Published: 2021-03-27 18:52:37

[![](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/neuml/txtai/blob/master/examples/12_Translate_text_between_languages.ipynb)

*A [Spanish language translation](https://www.ibidemgroup.com/edu/traduccion-automatica-txtai/) of this article is available. Thank you to <strong>Chema Bescós</strong> for providing this!*

This article covers machine translation backed by Hugging Face models. The quality of machine translation via cloud services has come a very long way and produces high quality results. This article shows how the models from Hugging Face give developers a reasonable alternative for local machine translation.

# Install dependencies

Install `txtai` and all dependencies. Since this article is using optional pipelines, we need to install the pipeline extras package.

```
pip install txtai[pipeline]
```

# Create a Translation instance

The Translation instance is the main entrypoint for translating text between languages. The pipeline abstracts translating text into a one line call! 

The pipeline has logic to detect the input language, load the relevant model that handles translating from source to target language and return results. The translation pipeline also has built-in logic to handle splitting large text blocks into smaller sections the models can handle.

```python
from txtai.pipeline import Translation

# Create translation model
translate = Translation()
```

# Translate text

The example below shows how to translate text from English to Spanish. This text is then translated back to English.

```python
translation = translate("This is a test translation into Spanish", "es")
translation
```

```
Esta es una traducción de prueba al español
```

```python
translate(translation, "en")
```

```
This is a test translation into Spanish
```

# Translating multiple languages in a single call

The section below translates a single English sentence into 5 different languages. The results are then passed to a single translation call to translate back into English. The pipeline detects each input language and is able to load the relevant translation models.

```python
def run():
  languages = ["fr", "es", "de", "hi", "ja"]
  translations = [translate("The sky is blue, the stars are far", language) for language in languages]
  english = translate(translations, "en")

  for x, text in enumerate(translations):
    print("Original Language: %s" % languages[x])
    print("Translation: %s" % text)
    print("Back to English: %s" % english[x])
    print()

# Run multiple translations
run()
```

```
Original Language: fr
Translation: Le ciel est bleu, les étoiles sont loin
Back to English: The sky is blue, the stars are far away

Original Language: es
Translation: El cielo es azul, las estrellas están lejos.
Back to English: The sky is blue, the stars are far away.

Original Language: de
Translation: Der Himmel ist blau, die Sterne sind weit
Back to English: The sky is blue, the stars are wide

Original Language: hi
Translation: आकाश नीला है, तारे दूर हैं
Back to English: Sky is blue, stars are away

Original Language: ja
Translation: 天は青い、星は遠い。
Back to English: The heavens are blue and the stars are far away.
```

The translation quality overall is very high!

# Additional model types

The translation pipeline is flexible and supports multiple model types. The default mode for the pipeline is to scan the Hugging Face Hub for models that best match the source-target translation pair. This often produces the best quality and is usually a smaller model than a large multi-language mode.

There is a parameter that can override this and always use the base model.

```python
translate = Translation("t5-small", findmodels=False)
translate("translate English to French: The sky is blue, the stars are far", None)
```

```
Le ciel est bleu, les étoiles sont loin
```

Translation isn't limited to spoken languages. txtai provides a text-to-sql model that converts English text into a txtai-compatible SQL statement. 

```python
translate = Translation("NeuML/t5-small-txtsql", findmodels=False)
translate("translate English to SQL: feel good story since yesterday", None)
```

```
select id, text, score from txtai where similar('feel good story') and entry >= date('now', '-1 day')
```

Last thing we'll do is run the multiple language example only using a single large language model.

```python
translate = Translation("facebook/mbart-large-50-many-to-many-mmt", findmodels=False)
run()
```

```
Original Language: fr
Translation: Le ciel est bleu, les étoiles sont loin
Back to English: The sky is blue, the stars are far away

Original Language: es
Translation: El cielo es azul, las estrellas están lejos.
Back to English: The sky is blue, the stars are far away.

Original Language: de
Translation: Der Himmel ist blau, die Sterne sind weit
Back to English: The sky is blue, the stars are far.

Original Language: hi
Translation: आकाश नीली है, तारे दूर हैं।
Back to English: The sky is blue, and the stars are far away.

Original Language: ja
Translation: 空は青い、星は遠い
Back to English: the sky is blue, the stars are far away.
```

# Wrapping up

Machine translation has made giant leaps and strides the last couple of years. These models give developers a solid, locally-hosted alternative to cloud translation services. Additionally, there are models built for low resource languages that cloud translation services don't support.

A number of different models and configurations are supported, give it a try!