Tips for faster translation using Argos Translate Python

About Argos Translate

Argos Translate is an open-source, cross-platform language translation software developed by the Argos Open Technologies, LLC. The software uses that uses TensorFlow for translations and can be used as either a Python library, command-line, or GUI application.

Installation

Simply run the following command in your environment

pip install argostranslate

Usage

A typical first run code like this:

import argostranslate.package
import argostranslate.translate

from_code = "en"
to_code = "es"

# Download and install Argos Translate package
argostranslate.package.update_package_index()
available_packages = argostranslate.package.get_available_packages()
package_to_install = next(
    filter(
        lambda x: x.from_code == from_code and x.to_code == to_code, available_packages
    )
)
argostranslate.package.install_from_path(package_to_install.download())

# Translate
translatedText = argostranslate.translate.translate("Hello World", from_code, to_code)
print(translatedText)
# '¡Hola Mundo!'

This API is simple and it automatically downloads the necessary files for translation. However, it’s impractical to use it in this form in a web service or a public application because this API is too redundant and takes a lot of time.

Faster Translation in a Loop

import argostranslate.package
import argostranslate.translate
from argostranslate.translate import translate, get_installed_languages

from_code = "en"
to_code = "es"

en, es = get_installed_languages() # [LanguageModel_English, LanguageModel_Spanish]
translator = es.get_translation(en)

# use only this part in loop
translatedText = translator.translate(text)

Explanation

Line 1–3 : import necessary packages, it can be simplified.

Line 5–6: Selects translation languages. In arrow representation this would be en->es or English → Spanish

Line 8: Get a list of installed languages. These languages are installed using the code given in the earlier section. In this case I installed only these two languages so I am unpacking them int vars.

Line 9: Load the translation model in memory.

Line 12: Since everything is saved in memory already, if this line is used in for loop it would not take as much time as earlier code.

So Why is it faster?

Well, when using the argostranslate.translate.translate the module first performs a few filters and not to mention it reloads the language model on every call so it is inefficient.

Why isn't it the default method?

Well from what I can read from the docs it appears the developer is working on making it available. There are methods described in code that can do what I wrote but it is not implemented yet.

Thank you.