1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-19 05:50:09 +01:00
LibreTranslate/app/init.py
P.J. Finlay ee638b407d Remove Polyglot transliteration library
The model server for the Polyglot transliteration library is currently
down and the project looks unmaintained; this currently is breaking
LibreTranslate installs. This commit removes Polyglot for
transliteration but keeps using Polyglot for language detection.

- https://github.com/LibreTranslate/LibreTranslate/issues/344
- https://community.libretranslate.com/t/improving-transliteration-in-libretranslate/400
2022-11-16 08:59:11 -06:00

60 lines
2.4 KiB
Python

from pathlib import Path
from argostranslate import package, translate
import app.language
def boot(load_only=None):
try:
check_and_install_models(load_only_lang_codes=load_only)
except Exception as e:
print("Cannot update models (normal if you're offline): %s" % str(e))
def check_and_install_models(force=False, load_only_lang_codes=None):
if len(package.get_installed_packages()) < 2 or force:
# Update package definitions from remote
print("Updating language models")
package.update_package_index()
# Load available packages from local package index
available_packages = package.get_available_packages()
print("Found %s models" % len(available_packages))
if load_only_lang_codes is not None:
# load_only_lang_codes: List[str] (codes)
# Ensure the user does not use any unavailable language code.
unavailable_lang_codes = set(load_only_lang_codes)
for pack in available_packages:
unavailable_lang_codes -= {pack.from_code, pack.to_code}
if unavailable_lang_codes:
raise ValueError(
"Unavailable language codes: %s."
% ",".join(sorted(unavailable_lang_codes))
)
# Keep only the packages that have both from_code and to_code in our list.
available_packages = [
pack
for pack in available_packages
if pack.from_code in load_only_lang_codes and pack.to_code in load_only_lang_codes
]
if not available_packages:
raise ValueError("no available package")
print("Keep %s models" % len(available_packages))
# Download and install all available packages
for available_package in available_packages:
print(
"Downloading %s (%s) ..."
% (available_package, available_package.package_version)
)
download_path = available_package.download()
package.install_from_path(download_path)
# reload installed languages
app.language.languages = translate.get_installed_languages()
print(
"Loaded support for %s languages (%s models total)!"
% (len(translate.get_installed_languages()), len(available_packages))
)