argostranslate package
Submodules
argostranslate.apis module
- class argostranslate.apis.LibreTranslateAPI(url: str | None = None, api_key: str | None = None)
Bases:
objectConnect 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:
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[argostranslate.package.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: 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
argostranslate.networking module
- argostranslate.networking.cache_spacy() Path | None
Downloads Spacy multilingual model and saves it the cache directory for further use
- argostranslate.networking.get(url: str, retry_count: int = 3) bytes | None
Downloads data from an 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:
IPackageA package available for download and installation
- argos_version: str
- code: str
- dependencies: list
- download() Path
Downloads the AvailablePackage and returns its path
- from_code: str
- from_codes: list
- from_name: str
- get_description()
- install()
- languages: list
- links: list[str]
- 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.IPackage
Bases:
objectA 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, an OpenNMT CTranslate2 directory named model/ is expected in the root directory along with a sentencepiece model named sentencepiece.model or a bpe.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
- links: list[str]
- 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: Path)
Bases:
IPackageAn installed package
- argos_version: str
- code: str
- dependencies: list
- from_code: str
- from_codes: list
- from_name: str
- 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: list
- links: list[str]
- package_path: Path
- package_version: str
- source_languages: list
- target_languages: list
- to_code: str
- to_codes: list
- to_name: str
- type: str
- update()
Update the package if a newer version is available.
- argostranslate.package.argospm_package_name(pkg: 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[argostranslate.package.AvailablePackage]
Returns a list of AvailablePackages from the package index.
- argostranslate.package.get_installed_packages(path: Path | None = None) list[argostranslate.package.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_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: Path)
Install a package file (zip archive ending in .argosmodel).
- Args:
path: The path to the .argosmodel file to install.
- argostranslate.package.install_package_for_language_pair(from_code: str, to_code: str) bool
Installs the necessary package to translate between a pair of languages
- Args:
from_code (str): The ISO 639 code for the language being translated from to_code (str): The ISO 639 code for the language being translated to
- Returns:
True if the package was installed successfully, False if the installation failed or was not possible
- argostranslate.package.load_available_packages() list[argostranslate.package.Package]
Deprecated 1.2, use get_available_packages
- argostranslate.package.uninstall(pkg: Package)
Uninstalls a package.
- Args:
pkg: The package to uninstall
- argostranslate.package.update_package_index()
Downloads remote package index
argostranslate.sbd module
- class argostranslate.sbd.ISentenceBoundaryDetectionModel
Bases:
object- split_sentences(text: str) List[str]
- class argostranslate.sbd.SpacySentencizerSmall(pkg: Package)
Bases:
ISentenceBoundaryDetectionModel- split_sentences(text: str) List[str]
- class argostranslate.sbd.StanzaSentencizer(pkg: Package)
Bases:
ISentenceBoundaryDetectionModel- LANGUAGE_CODE_MAPPING = {'pb': 'pt', 'zt': 'zh-hant'}
- split_sentences(text: str) List[str]
- 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_stanza_processors(lang_code: str, resources: dict) str
Get appropriate processors for a language, including MWT if available.
- 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.ChunkType(value)
Bases:
EnumAn enumeration.
- ARGOSTRANSLATE = 1
- DEFAULT = 0
- NONE = 2
- SPACY = 4
- STANZA = 3
- class argostranslate.settings.ModelProvider(value)
Bases:
EnumAn enumeration.
- LIBRETRANSLATE = 1
- OPENAI = 2
- OPENNMT = 0
- argostranslate.settings.experimental_enabled = False
# Legacy environment variable for sentence boundary detection stanza_available = os.getenv(“ARGOS_STANZA_AVAILABLE”) in (TRUE_VALUES + [None])
- argostranslate.settings.get_setting(key: str, default=None)
Gets a setting from either environment variables or settings.json
Settings from environment variables take precedence over settings.json
- Args:
key (str): Key value default: The default setting value. Defaults to None.
- Returns:
The setting value
- argostranslate.settings.load_settings_dict() Dict[str, Any]
- argostranslate.settings.set_setting(key: str, value)
Sets a setting in the settings.json file.
- Args:
key (str): The key to set. value: The value to set.
argostranslate.translate module
- class argostranslate.translate.CachedTranslation(underlying: ITranslation)
Bases:
ITranslationCaches 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
- hypotheses(input_text: str, num_hypotheses: int = 4) list[argostranslate.translate.Hypothesis]
Translates a string from self.from_lang to self.to_lang
- Args:
input_text: The text to be translated. num_hypotheses: Number of hypothetical results expected
- Returns:
List of translation hypotheses
- underlying: ITranslation
- class argostranslate.translate.CompositeTranslation(t1: ITranslation, t2: ITranslation)
Bases:
ITranslationA ITranslation that is performed by chaining two Translations
- Attributes:
t1: The first Translation to apply. t2: The second Translation to apply.
- hypotheses(input_text: str, num_hypotheses: int = 4) list[argostranslate.translate.Hypothesis]
Translates a string from self.from_lang to self.to_lang
- Args:
input_text: The text to be translated. num_hypotheses: Number of hypothetical results expected
- Returns:
List of translation hypotheses
- t1: ITranslation
- t2: ITranslation
- class argostranslate.translate.FewShotTranslation(from_lang: Language, to_lang: Language, language_model: ILanguageModel)
Bases:
ITranslationA translation performed with a few shot language model
- hypotheses(input_text: str, num_hypotheses: int = 1) list[argostranslate.translate.Hypothesis]
Translates a string from self.from_lang to self.to_lang
- Args:
input_text: The text to be translated. num_hypotheses: Number of hypothetical results expected
- Returns:
List of translation hypotheses
- language_model: ILanguageModel
- class argostranslate.translate.Hypothesis(value: str, score: float)
Bases:
objectRepresents 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:
objectRepresents 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.
- hypotheses(input_text: str, num_hypotheses: int = 4) list[argostranslate.translate.Hypothesis]
Translates a string from self.from_lang to self.to_lang
- Args:
input_text: The text to be translated. num_hypotheses: Number of hypothetical 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.
- 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: Language)
Bases:
ITranslationA Translation that doesn’t modify input_text.
- 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 hypothetical results expected
- Returns:
List of translation hypotheses
- class argostranslate.translate.InstalledTranslate
Bases:
objectGlobal storage of instances of the CachedTranslation class by unique keys. To avoid creating unnecessary objects in memory.
- cached_translation: CachedTranslation
- package_key: str
- class argostranslate.translate.Language(code: str, name: str)
Bases:
objectRepresents 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[argostranslate.translate.ITranslation] = []
- translations_to: list[argostranslate.translate.ITranslation] = []
- argostranslate.translate.LibreTranslateTranslation
alias of
RemoteTranslation
- class argostranslate.translate.PackageTranslation(from_lang: Language, to_lang: Language, pkg: Package)
Bases:
ITranslationA Translation that is installed with a package
- hypotheses(input_text: str, num_hypotheses: int = 4) list[argostranslate.translate.Hypothesis]
Translates a string from self.from_lang to self.to_lang
- Args:
input_text: The text to be translated. num_hypotheses: Number of hypothetical results expected
- Returns:
List of translation hypotheses
- class argostranslate.translate.RemoteTranslation(from_lang: Language, to_lang: Language, api)
Bases:
ITranslationA translation provided by a remote LibreTranslate server
- hypotheses(input_text: str, num_hypotheses: int = 1) list[argostranslate.translate.Hypothesis]
LibreTranslate only supports single hypotheses.
A list of length num_hypotheses will be returned with identical hypotheses.
- argostranslate.translate.apply_packaged_translation(pkg: Package, input_text: str, translator: Translator, sentencizer: ISentenceBoundaryDetectionModel, num_hypotheses: int = 4) list[argostranslate.translate.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 sentencizer: The Sentence Boundary Detection package num_hypotheses: The number of hypotheses to generate
- Returns:
A list of Hypotheses objects for translated input_text.
- argostranslate.translate.get_installed_languages() list[argostranslate.translate.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) 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[argostranslate.translate.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.debug(*argv)
Debug level log
- argostranslate.utils.error(*argv)
Error level log
- argostranslate.utils.info(*argv)
Info level log
- argostranslate.utils.warning(*argv)
Warning level log