argostranslate package

Submodules

argostranslate.apis module

class argostranslate.apis.LibreTranslateAPI(url: Optional[str] = None, api_key: Optional[str] = None)

Bases: object

Connect to the LibreTranslate API

DEFAULT_URL = 'https://translate.argosopentech.com/'
detect(q: str)

Detect the language of a single text.

Args:

q: 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: str, source: str = 'en', target: str = 'es') → str

Translate string

Args:

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

Returns: The translated text

class argostranslate.apis.OpenAIAPI(api_key: str)

Bases: argostranslate.models.ILanguageModel

infer(prompt: str) → str | None

Connect to OpenAI API

Args:

prompt: The prompt to run inference on.

Returns: The generated text

argostranslate.argospm module

argostranslate.argospm.get_available_packages() → list[AvailablePackage]

Get available packages and update packages list if it is not done

Returns:

The list of available packages

argostranslate.argospm.install_all_packages()

Install all packages available to Argos Translate.

argostranslate.argospm.install_package(args)

Install package.

argostranslate.argospm.install_package_print_path(available_package: argostranslate.package.AvailablePackage)
argostranslate.argospm.list_packages(args)

List packages.

argostranslate.argospm.main()

Run argospm command line program

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()

Run Argos Translate command line interface

argostranslate.fewshot module

argostranslate.cli.main()

Run Argos Translate command line interface

argostranslate.gui module

argostranslate.models module

class argostranslate.models.ILanguageModel

Bases: object

infer(x: str) → str | None

Run language model on input x

Args:

x: Prompt to run inference on

Returns:

Output of inference

argostranslate.networking module

argostranslate.networking.get(url: str, retry_count: int = 3) → bytes | None

Downloads data from a url and returns it

Args:

url: The url to download (http, https) retry_count: The number of retries to attempt if the initial download fails.

If retry_count is 0 the download will only be attempted once.

Returns:

The downloaded data, None is returned if the download fails

argostranslate.networking.get_from(urls: list[str], retry_count: int = 3) → bytes | None

Downloads data from a list of urls and returns it

Args:

urls: The urls to download (http, https) retry_count: The number of retries to attempt if the initial download fails.

If retry_count is 0 the download will only be attempted once.

Returns:

The downloaded data, None is returned if the download fails

argostranslate.networking.get_protocol(url: str) → str | None

Gets the protocol of a URL string

For example if url is “https://www.argosopentech.com” “https” is returned. If the protocol can’t be determined None is returned

Args:

url: The URL to get the protocol of

Returns:

The string representation of the protocol or None

argostranslate.package module

class argostranslate.package.AvailablePackage(metadata)

Bases: argostranslate.package.IPackage

A package available for download and installation

argos_version
code
dependencies
download() → pathlib.Path

Downloads the AvailablePackage and returns its path

from_code
from_codes
from_name
get_description()
install()
languages
package_path
package_version
source_languages
target_languages
to_code
to_codes
to_name
type
class argostranslate.package.IPackage

Bases: object

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

Attributes:

package_path: The path to the installed package. None if not installed.

package_version: The version of the package.

argos_version: The version of Argos Translate the package is intended for.

from_code: The code of the language the package translates from.

from_name: Human readable name of the language the package translates from.

to_code: The code of the language the package translates to.

to_name: Human readable name of the language the package translates to.

links: 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/ is expected in the root directory along with a sentencepiece model named sentencepiece.model for tokenizing and Stanza data 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”]

}

argos_version: str
code: str
dependencies: list
from_code: str
from_codes: list
from_name: str
get_description()
get_readme() → str

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

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

if README.md can’t be read

languages: list
load_metadata_from_json(metadata)

Loads package metadata from a JSON object.

Args:

metadata: A json object from json.load

package_path: Path
package_version: str
source_languages: list
target_languages: list
to_code: str
to_codes: list
to_name: str
type: str
class argostranslate.package.Package(package_path: pathlib.Path)

Bases: argostranslate.package.IPackage

An installed package

argos_version
code
dependencies
from_code
from_codes
from_name
get_description()
get_readme() → str | None

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

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

if README.md can’t be read

languages
package_path
package_version
source_languages
target_languages
to_code
to_codes
to_name
type
update()

Update the package if a newer version is available.

argostranslate.package.argospm_package_name(pkg: argostranslate.package.IPackage) → str

Gets argospm name of an IPackage.

Args:

The package to get the name of.

Returns:

Package name for argospm

argostranslate.package.get_available_packages() → list[AvailablePackage]

Returns a list of AvailablePackages from the package index.

argostranslate.package.get_installed_packages(path: Path = None) → list[Package]

Return a list of installed Packages

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

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

Defaults to the path in settings module.

argostranslate.package.install_from_path(path: pathlib.Path)

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

Args:

path: The path to the .argosmodel file to install.

argostranslate.package.load_available_packages() → list[Package]

Deprecated 1.2, use get_available_packages

argostranslate.package.uninstall(pkg: argostranslate.package.Package)

Uninstalls a package.

Args:

pkg: The package to uninstall

argostranslate.package.update_package_index()

Downloads remote package index

argostranslate.sbd module

argostranslate.sbd.detect_sentence(input_text: str, sbd_translation, sentence_guess_length: int = 150) → int

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

Args:

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

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

-1 if not found.

argostranslate.sbd.generate_fewshot_sbd_prompt(input_text: str, sentence_guess_length: int = 150) → str
argostranslate.sbd.get_sbd_package() → Package | None
argostranslate.sbd.parse_fewshot_response(response_text: str) → str | None
argostranslate.sbd.process_seq2seq_sbd(input_text: str, sbd_translated_guess: str) → int

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: List of ITags and strs representing

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

translateable: If translateable is False then a tag and its children

should not be translated

text() → str

The combined text of all of the children

Returns:

Combined text

translateable: bool
class argostranslate.tags.Tag(children: ITag | str, translateable: bool = True)

Bases: argostranslate.tags.ITag

text() → str

The combined text of all of the children

Returns:

Combined text

translateable: bool
argostranslate.tags.depth(tag: ITag | str) → int

Returns the depth of an ITag or str.

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

Args:

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

argostranslate.tags.inject_tags_inference(underlying_translation: ITranslation, tag: ITag) → ITag | None

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: The translation to apply to the tags. tag: A depth=2 tag tree to attempt injection on.

Returns:

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

argostranslate.tags.translate_preserve_formatting(underlying_translation: argostranslate.translate.ITranslation, input_text: str) → str

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

underlying_translation : The translation to apply input_text: The text to translate

Returns:

The translated text

argostranslate.tags.translate_tags(underlying_translation: ITranslation, tag: ITag | str) → ITag | str

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: The translation to apply tag: The tag tree to translate

Returns:

The translated tag tree

argostranslate.translate module

class argostranslate.translate.CachedTranslation(underlying: argostranslate.translate.ITranslation)

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.

cache: dict
from_lang: argostranslate.translate.Language
hypotheses(input_text: str, num_hypotheses: int = 4) → list[Hypothesis]

Translates a string from self.from_lang to self.to_lang

Args:

input_text: The text to be translated. num_hypotheses: Number of hypothetic results expected

Returns:

List of translation hypotheses

to_lang: argostranslate.translate.Language
underlying: argostranslate.translate.ITranslation
class argostranslate.translate.CompositeTranslation(t1: argostranslate.translate.ITranslation, t2: argostranslate.translate.ITranslation)

Bases: argostranslate.translate.ITranslation

A ITranslation that is performed by chaining two Translations

Attributes:

t1: The first Translation to apply. t2: The second Translation to apply.

from_lang: argostranslate.translate.Language
hypotheses(input_text: str, num_hypotheses: int = 4) → list[Hypothesis]

Translates a string from self.from_lang to self.to_lang

Args:

input_text: The text to be translated. num_hypotheses: Number of hypothetic results expected

Returns:

List of translation hypotheses

t1: argostranslate.translate.ITranslation
t2: argostranslate.translate.ITranslation
to_lang: argostranslate.translate.Language
class argostranslate.translate.FewShotTranslation(from_lang: argostranslate.translate.Language, to_lang: argostranslate.translate.Language, language_model: argostranslate.models.ILanguageModel)

Bases: argostranslate.translate.ITranslation

A translation performed with a few shot language model

from_lang: argostranslate.translate.Language
hypotheses(input_text: str, num_hypotheses: int = 1) → list[Hypothesis]

Translates a string from self.from_lang to self.to_lang

Args:

input_text: The text to be translated. num_hypotheses: Number of hypothetic results expected

Returns:

List of translation hypotheses

language_model: argostranslate.models.ILanguageModel
to_lang: argostranslate.translate.Language
class argostranslate.translate.Hypothesis(value: str, score: float)

Bases: object

Represents a translation hypothesis

Attributes:

value: The hypothetical translation value score: The score representing the quality of the translation

score: float
value: str
class argostranslate.translate.ITranslation

Bases: object

Represents a translation between two Languages

Attributes:

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

static combine_paragraphs(paragraphs: list[str]) → str

Combines a list of paragraphs together.

Args:

paragraphs: A list of paragraphs.

Returns:

list of n paragraphs combined into one string.

from_lang: argostranslate.translate.Language
hypotheses(input_text: str, num_hypotheses: int = 4) → list[Hypothesis]

Translates a string from self.from_lang to self.to_lang

Args:

input_text: The text to be translated. num_hypotheses: Number of hypothetic results expected

Returns:

List of translation hypotheses

static split_into_paragraphs(input_text: str) → list[str]

Splits input_text into paragraphs and returns a list of paragraphs.

Args:

input_text: The text to be split.

Returns:

A list of paragraphs.

to_lang: argostranslate.translate.Language
translate(input_text: str) → str

Translates a string from self.from_lang to self.to_lang

Args:

input_text: The text to be translated.

Returns:

input_text translated.

class argostranslate.translate.IdentityTranslation(lang: argostranslate.translate.Language)

Bases: argostranslate.translate.ITranslation

A Translation that doesn’t modify input_text.

from_lang: argostranslate.translate.Language
hypotheses(input_text: str, num_hypotheses: int = 4)

Translates a string from self.from_lang to self.to_lang

Args:

input_text: The text to be translated. num_hypotheses: Number of hypothetic results expected

Returns:

List of translation hypotheses

to_lang: argostranslate.translate.Language
class argostranslate.translate.InstalledTranslate

Bases: object

Global storage of instances of the CachedTranslation class by unique keys. To avoid creating unnecessary objects in memory.

cached_translation: argostranslate.translate.CachedTranslation
package_key: str
class argostranslate.translate.Language(code: str, name: str)

Bases: object

Represents a language that can be translated from/to.

Attributes:

code: The code representing the language. name: The human readable name of the language. translations_from: A list of the translations

that translate from this language.

translations_to: A list of the translations

that translate to this language

get_translation(to: Language) → ITranslation | None

Gets a translation from this Language to another Language.

Args:

to: The Language to look for a Translation to.

Returns:
A valid Translation if there is one in translations_from

else None.

translations_from: list[ITranslation] = []
translations_to: list[ITranslation] = []
argostranslate.translate.LibreTranslateTranslation

alias of argostranslate.translate.RemoteTranslation

class argostranslate.translate.PackageTranslation(from_lang: argostranslate.translate.Language, to_lang: argostranslate.translate.Language, pkg: argostranslate.package.Package)

Bases: argostranslate.translate.ITranslation

A Translation that is installed with a package

from_lang: argostranslate.translate.Language
hypotheses(input_text: str, num_hypotheses: int = 4) → list[Hypothesis]

Translates a string from self.from_lang to self.to_lang

Args:

input_text: The text to be translated. num_hypotheses: Number of hypothetic results expected

Returns:

List of translation hypotheses

to_lang: argostranslate.translate.Language
class argostranslate.translate.RemoteTranslation(from_lang: argostranslate.translate.Language, to_lang: argostranslate.translate.Language, api)

Bases: argostranslate.translate.ITranslation

A translation provided by a remote LibreTranslate server

from_lang: argostranslate.translate.Language
hypotheses(input_text: str, num_hypotheses: int = 1) → list[Hypothesis]

LibreTranslate only supports single hypotheses.

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

to_lang: argostranslate.translate.Language
argostranslate.translate.apply_packaged_translation(pkg: Package, input_text: str, translator: Translator, num_hypotheses: int = 4) → list[Hypothesis]

Applies the translation in pkg to translate input_text.

Args:

pkg: The package that provides the translation. input_text: The text to be translated. translator: The CTranslate2 Translator num_hypotheses: The number of hypotheses to generate

Returns:

A list of Hypothesis’s for translating input_text

argostranslate.translate.get_installed_languages() → list[Language]

Returns a list of Languages installed from packages

argostranslate.translate.get_language_from_code(code: str) → Language | None

Gets a language object from a code

An exception will be thrown if an installed language with this code can not be found.

Args:

code: The ISO 639 code of the language

Returns:

The language object

argostranslate.translate.get_translation_from_codes(from_code: str, to_code: str)argostranslate.translate.ITranslation

Gets a translation object from codes for from and to languages

An exception will be thrown if an installed translation between the from lang and to lang can not be found.

Args:

from_code: The ISO 639 code of the source language to_code: The ISO 639 code of the target language

Returns:

The translation object

argostranslate.translate.load_installed_languages() → list[Language]

Deprecated 1.2, use get_installed_languages

argostranslate.translate.translate(q: str, from_code: str, to_code: str) → str

Translate a string of text

Args:

q: The text to translate from_code: The ISO 639 code of the source language to_code: The ISO 639 code of the target language

Returns:

The translated text

argostranslate.utils module

argostranslate.utils.error(*argv)

Error level log

argostranslate.utils.info(*argv)

Info level log

Module contents