argostranslate package

Submodules

argostranslate.apis module

class argostranslate.apis.LibreTranslateAPI(url=None, api_key=None)

Bases: object

Connect to the LibreTranslate API

DEFAULT_URL = 'https://translate.astian.org/'
detect(q)

Detect the language of a single text.

Args:

q (str): Text to detect

Returns: The detected languages ex. [{“confidence”: 0.6, “language”: “en”}]

languages()

Retrieve list of supported languages.

Returns: A list of available languages ex. [{“code”:”en”, “name”:”English”}]

translate(q, source='en', target='es')

Translate string

Args:

q (str): The text to translate source (str): The source language code (ISO 639) target (str): The target language code (ISO 639)

Returns: The translated text

class argostranslate.apis.OpenAIAPI(api_key)

Bases: argostranslate.models.ILanguageModel

infer(prompt)

Connect to OpenAI API

Args:

prompt (str): The prompt to run inference on. api_key (str): OpenAI API key

Returns: The generated text

argostranslate.argospm module

argostranslate.argospm.install_package(args)

Install package.

argostranslate.argospm.list_packages(args)

List packages.

argostranslate.argospm.main()
argostranslate.argospm.remove_package(args)

Remove installed package.

argostranslate.argospm.search_packages(args)

Display packages from remote index.

argostranslate.argospm.update_index(args)

Update the package index.

argostranslate.cli module

argostranslate.cli.main()

argostranslate.fewshot module

argostranslate.cli.main()

argostranslate.gui module

class argostranslate.gui.DownloadPackagesWindow

Bases: PyQt5.QtWidgets.QWidget

packages_changed
class argostranslate.gui.GUIApplication

Bases: object

class argostranslate.gui.GUIWindow

Bases: PyQt5.QtWidgets.QMainWindow

SHOW_LOADING_THRESHOLD = 300
about_action_triggered()
handle_worker_thread_finished()
load_languages()
manage_packages_action_triggered()
swap_languages_button_clicked()
translate()

Try to translate based on languages selected.

update_right_textEdit(text)
class argostranslate.gui.ManagePackagesWindow

Bases: PyQt5.QtWidgets.QWidget

add_packages()
packages_changed
class argostranslate.gui.PackagesTable(table_content, available_actions)

Bases: PyQt5.QtWidgets.QTableWidget

class AvailableActions(value)

Bases: enum.Enum

An enumeration.

INSTALL = 1
UNINSTALL = 0
class TableContent(value)

Bases: enum.Enum

An enumeration.

AVAILABLE = 1
INSTALLED = 0
get_packages()
install_package(pkg)
packages_changed
populate()
uninstall_package(pkg)
view_package_readme(pkg)
class argostranslate.gui.TranslationThread(translation_function, show_loading_message)

Bases: PyQt5.QtCore.QThread

run(self)
send_text_update
class argostranslate.gui.WorkerStatusButton(text, bound_worker_function)

Bases: PyQt5.QtWidgets.QPushButton

class Status(value)

Bases: enum.Enum

An enumeration.

DONE = 2
NOT_STARTED = 0
RUNNING = 1
clicked_handler()
finished_handler()
set_status(status)
argostranslate.gui.main()

argostranslate.models module

class argostranslate.models.ILanguageModel

Bases: object

infer()

Run language model on input x

Args:

x (str): Prompt to run inference on

Returns: (str) Output of inference

argostranslate.package module

class argostranslate.package.AvailablePackage(metadata)

Bases: argostranslate.package.IPackage

A package available for download

download()

Downloads the AvailablePackage and returns its path

get_description()
class argostranslate.package.IPackage

Bases: object

A package, can be either installed locally or available from a remote package index.

Attributes:

package_path (Path): The path to the installed package. None if not installed. package_version (str): The version of the package. argos_version (str): The version of Argos Translate the package is intended for. from_code (str): The code of the language the package translates from. from_name (str): Human readable name of the language the package translates from. to_code (str): The code of the language the package translates to. to_name (str): Human readable name of the language the package translates to. links [list(str)]: A list of links to download the package

Packages are a zip archive of a directory with metadata.json in its root the .argosmodel file extension. By default a OpenNMT CTranslate2 directory named model/ created using ct2-opennmt-tf-converter is expected in the root directory along with a sentencepiece model named sentencepiece.model for tokenizing and Stanza for sentence boundary detection. Packages may also optionally have a README.md in the root.

from_code and to_code should be ISO 639 codes if applicable.

Example metadata.json {

“package_version”: “1.0”, “argos_version”: “1.0”, “from_code”: “en”, “from_name”: “English”, “to_code”: “es”, “to_name”: “Spanish”, “links”: [”https://example.com/en_es.argosmodel”]

}

get_description()
get_readme()

Returns the text of the README.md in this package.

Returns:
(str): The text of the package README.md, None

if README.md can’t be read

load_metadata_from_json(metadata)

Loads package metadata from a JSON object.

Args:

metadata: A json object from json.load

class argostranslate.package.Package(package_path)

Bases: argostranslate.package.IPackage

An installed package

get_description()
get_readme()

Returns the text of the README.md in this package.

Returns:
(str): The text of the package README.md, None

if README.md can’t be read

argostranslate.package.argospm_package_name(pkg)

Gets argospm name of an IPackage.

Args:

pkg (IPackage): The package to get the name of.

Returns:

str: Package name for argospm

argostranslate.package.get_available_packages()

Returns a list of AvailablePackages from the package index.

argostranslate.package.get_installed_packages(path=None)

Return a list of installed Packages

Looks for packages in <home>/.argos-translate/packages by default. Will also look in the directory specified in the ARGOS_TRANSLATE_PACKAGE_DIR environment variable if it is set.

Args:
path (pathlib.Path): Path to look for installed package directories in.

Defaults to the path in settings module.

argostranslate.package.install_from_path(path)

Install a package file (zip archive ending in .argosmodel).

Args:

path (pathlib): The path to the .argosmodel file to install.

argostranslate.package.load_available_packages()

Deprecated 1.2, use get_available_packages

argostranslate.package.uninstall(pkg)

Uninstalls a package.

Args:

pkg (Package): The package to uninstall

argostranslate.package.update_package_index()

Downloads remote package index

argostranslate.sbd module

argostranslate.sbd.detect_sentence(input_text, sbd_translation, sentence_guess_length=150)

Given input text, return the index after the end of the first sentence.

Args:

input_text (str): The text to detect the first sentence of. sbd_translation (translate.ITranslation): An ITranslation for detecting sentences. sentence_guess_length (int): Estimated number of chars > than most sentences.

Returns:
int: The index of the character after the end of the sentence.

-1 if not found.

argostranslate.sbd.generate_fewshot_sbd_prompt(input_text, sentence_guess_length=150)
argostranslate.sbd.get_sbd_package()
argostranslate.sbd.parse_fewshot_response(response_text)
argostranslate.sbd.process_seq2seq_sbd(input_text, sbd_translated_guess)

argostranslate.settings module

class argostranslate.settings.ModelProvider(value)

Bases: enum.Enum

An enumeration.

LIBRETRANSLATE = 1
OPENAI = 2
OPENNMT = 0

argostranslate.tags module

class argostranslate.tags.ITag

Bases: object

Represents a tag tree

Attributes:
children (ITag or str): List of ITags and strs representing

the children of the tag (empty list if no children)

translateable (bool): If translateable is False then a tag and its children

should not be translated

text()

The combined text of all of the children

Returns:

str: Combined text

class argostranslate.tags.Tag(children, translateable=True)

Bases: argostranslate.tags.ITag

text()

The combined text of all of the children

Returns:

str: Combined text

argostranslate.tags.depth(tag)

Returns the depth of an ITag or str.

A str has depth 0, ITag([]) has depth 0, ITag([‘str’]) has depth 1.

Args:

tag (ITag or str): The ITag or string to get the depth of.

argostranslate.tags.inject_tags_inference(underlying_translation, tag)

Returns translated tag tree with injection tags, None if not possible

tag is only modified in place if tag injection is successful.

Args:

underlying_translation(translate.ITranslation): The translation to apply to the tags. tag (ITag): A depth=2 tag tree to attempt injection on.

Returns:

ITag: A translated version of tag, None if not possible to tag inject

argostranslate.tags.translate_preserve_formatting(underlying_translation, input_text)

Translates but preserves a space if it exists on either end of translation. Args:

underlying_translation (translate.ITranslation): The translation to apply input_text (str): The text to translate

Returns:

str: The translated text

argostranslate.tags.translate_tags(underlying_translation, tag)

Translate an ITag or str

Recursively takes either an ITag or a str, modifies it in place, and returns the translated tag tree

Args:

underlying_translation (translate.ITranslation): The translation to apply tag (ITag or str): The tag tree to translate

Returns:

ITag or str: The translated tag tree

argostranslate.translate module

class argostranslate.translate.CachedTranslation(underlying)

Bases: argostranslate.translate.ITranslation

Caches a translation to improve performance.

This is done by splitting up the text passed for translation into paragraphs and translating each paragraph individually. A hash of the paragraphs and their corresponding translations are saved from the previous translation and used to improve performance on the next one. This is especially useful if you are repeatedly translating nearly identical text with a small change at the end of it.

hypotheses(input_text, num_hypotheses=4)

Translates a string from self.from_lang to self.to_lang

Args:

input_text (str): The text to be translated. num_hypotheses (int): Number of hypothetic results expected

Returns:

[Hypothesis]: List of translation hypotheses

class argostranslate.translate.CompositeTranslation(t1, t2)

Bases: argostranslate.translate.ITranslation

A ITranslation that is performed by chaining two Translations

Attributes:

t1 (ITranslation): The first Translation to apply. t2 (ITranslation): The second Translation to apply.

hypotheses(input_text, num_hypotheses)

Translates a string from self.from_lang to self.to_lang

Args:

input_text (str): The text to be translated. num_hypotheses (int): Number of hypothetic results expected

Returns:

[Hypothesis]: List of translation hypotheses

class argostranslate.translate.FewShotTranslation(from_lang, to_lang, language_model)

Bases: argostranslate.translate.ITranslation

hypotheses(input_text, num_hypotheses=1)

Translates a string from self.from_lang to self.to_lang

Args:

input_text (str): The text to be translated. num_hypotheses (int): Number of hypothetic results expected

Returns:

[Hypothesis]: List of translation hypotheses

class argostranslate.translate.Hypothesis(value, score)

Bases: object

Represents a translation hypothesis

Attributes:

value (str): The hypothetical translation value score (float): The score representing the quality of the translation

class argostranslate.translate.ITranslation

Bases: object

Respresents a translation between two Languages

Attributes:

from_lang (Language): The Language this Translation translates from. to_lang (Language): The Language this Translation translates to.

static combine_paragraphs(paragraphs)

Combines a list of paragraphs together.

Args:

paragraphs ([str]): A list of paragraphs.

Returns:

[str]: list of n paragraphs combined into one string.

hypotheses(input_text, num_hypotheses=4)

Translates a string from self.from_lang to self.to_lang

Args:

input_text (str): The text to be translated. num_hypotheses (int): Number of hypothetic results expected

Returns:

[Hypothesis]: List of translation hypotheses

static split_into_paragraphs(input_text)

Splits input_text into paragraphs and returns a list of paragraphs.

Args:

input_text (str): The text to be split.

Returns:

[str]: A list of paragraphs.

translate(input_text)

Translates a string from self.from_lang to self.to_lang

Args:

input_text (str): The text to be translated.

Returns:

str: input_text translated.

class argostranslate.translate.IdentityTranslation(lang)

Bases: argostranslate.translate.ITranslation

A Translation that doesn’t modify input_text.

hypotheses(input_text, num_hypotheses)

Translates a string from self.from_lang to self.to_lang

Args:

input_text (str): The text to be translated. num_hypotheses (int): Number of hypothetic results expected

Returns:

[Hypothesis]: List of translation hypotheses

class argostranslate.translate.Language(code, name)

Bases: object

Represents a language that can be translated from/to.

Attributes:

code (str): The code representing the language. name (str): The human readable name of the language. translations_from ([ITranslation]): A list of the translations

that translate from this language.

translations_to ([ITranslation]): A list of the translations

that translate to this language

get_translation(to)

Gets a translation from this Language to another Language.

Args:

to (Language): The Language to look for a Translation to.

Returns:
ITranslation: A valid Translation if there is one in translations_from

else None.

class argostranslate.translate.LibreTranslateTranslation(from_lang, to_lang, api)

Bases: argostranslate.translate.ITranslation

hypotheses(input_text, num_hypotheses=1)

LibreTranslate only supports single hypotheses.

A list of length num_hypotheses will be returned with identical hypotheses.

class argostranslate.translate.PackageTranslation(from_lang, to_lang, pkg)

Bases: argostranslate.translate.ITranslation

Translation from a package

hypotheses(input_text, num_hypotheses)

Translates a string from self.from_lang to self.to_lang

Args:

input_text (str): The text to be translated. num_hypotheses (int): Number of hypothetic results expected

Returns:

[Hypothesis]: List of translation hypotheses

argostranslate.translate.apply_packaged_translation(pkg, input_text, translator, num_hypotheses=4)

Applies the translation in pkg to translate input_text.

Args:

pkg (Package): The package that provides the translation. input_text (str): The text to be translated. translator (ctranslate2.Translator): The CTranslate2 Translator num_hypotheses (int): The number of hypotheses to generate

Returns:

[Hypothesis]: A list of Hypothesis’s for translating input_text

argostranslate.translate.get_installed_languages()

Returns a list of Languages installed from packages

argostranslate.translate.load_installed_languages()

Deprecated 1.2, use get_installed_languages

argostranslate.utils module

class argostranslate.utils.WorkerThread(bound_worker_function)

Bases: PyQt5.QtCore.QThread

Runs a bound function on a thread

run(self)
argostranslate.utils.error(*argv)
argostranslate.utils.info(*argv)

Module contents