-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(machinery): add Argos offline translation backend #18572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
01799ea
1954957
6380c0e
4d177a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| # Copyright © Michal Čihař <michal@weblate.org> | ||
| # | ||
| # SPDX-License-Identifier: GPL-3.0-or-later | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from .base import MachineTranslation | ||
|
|
||
| if TYPE_CHECKING: | ||
| from weblate.auth.models import User | ||
| from weblate.trans.models import Unit | ||
|
|
||
| from .base import DownloadTranslations | ||
|
|
||
| try: | ||
| import argostranslate.package | ||
| import argostranslate.translate | ||
| except ImportError: | ||
| argostranslate = None | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic should not be needed. When the module cannot be imported, the engine is skipped by Weblate. |
||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class ArgosTranslation(MachineTranslation): | ||
| """Argos offline machine translation.""" | ||
|
|
||
| name = "Argos Translate" | ||
| settings_form = None | ||
| is_available = bool(argostranslate) | ||
|
|
||
| def is_supported(self, source_language, target_language): | ||
| """Supported if the language model package is installed in argostranslate.""" | ||
| if not self.is_available: | ||
| return False | ||
|
|
||
| src = source_language.split("-")[0].lower() | ||
| tgt = target_language.split("-")[0].lower() | ||
|
|
||
| installed_languages = argostranslate.translate.get_installed_languages() | ||
|
|
||
| src_lang = next( | ||
| (lang for lang in installed_languages if lang.code == src), None | ||
| ) | ||
| tgt_lang = next( | ||
| (lang for lang in installed_languages if lang.code == tgt), None | ||
| ) | ||
|
Comment on lines
+39
to
+49
|
||
|
|
||
| if src_lang and tgt_lang: | ||
| return src_lang.get_translation(tgt_lang) is not None | ||
|
|
||
| return False | ||
|
|
||
| def download_translations( | ||
| self, | ||
| source_language, | ||
| target_language, | ||
| text: str, | ||
| unit: Unit | None, | ||
| user: User | None, | ||
| threshold: int = 75, | ||
| ) -> DownloadTranslations: | ||
| """Translate using argostranslate.""" | ||
| if not self.is_available: | ||
| return | ||
|
|
||
| src = source_language.split("-")[0].lower() | ||
| tgt = target_language.split("-")[0].lower() | ||
|
|
||
| try: | ||
| translation = argostranslate.translate.translate(text, src, tgt) | ||
| if translation: | ||
| yield { | ||
| "text": translation, | ||
| "quality": self.max_score, | ||
| "service": self.name, | ||
| "source": text, | ||
| } | ||
| except Exception as e: | ||
| logger.debug( | ||
| "Argos translation failed for %s to %s: %s", | ||
| source_language, | ||
| target_language, | ||
| e, | ||
| ) | ||
| return | ||
|
Comment on lines
+72
to
+88
|
||
Uh oh!
There was an error while loading. Please reload this page.