Translate text between languages
Streamline machine translation and language detection
A Spanish language translation of this article is available. Thank you to Chema Bescós 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.
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.
translation = translate("This is a test translation into Spanish", "es")
translation
Esta es una traducción de prueba al español
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.
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.
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.
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.
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!