1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-12-24 10:06:43 +02:00
LibreTranslate/libretranslate/init.py

61 lines
2.4 KiB
Python
Raw Normal View History

2020-12-20 18:55:56 +02:00
from pathlib import Path
from argostranslate import package, translate
import libretranslate.language
2020-12-20 00:40:37 +02:00
2021-05-18 05:41:02 +02:00
def boot(load_only=None, update_models=False):
2021-05-16 16:42:58 +02:00
try:
check_and_install_models(force=update_models, load_only_lang_codes=load_only)
2021-05-16 16:42:58 +02:00
except Exception as e:
print("Cannot update models (normal if you're offline): %s" % str(e))
2020-12-20 00:40:37 +02:00
2021-05-18 05:41:02 +02:00
def check_and_install_models(force=False, load_only_lang_codes=None):
if len(package.get_installed_packages()) < 2 or force:
2021-02-08 17:56:45 +02:00
# Update package definitions from remote
print("Updating language models")
package.update_package_index()
# Load available packages from local package index
2022-09-17 08:45:59 +02:00
available_packages = package.get_available_packages()
2021-02-08 17:56:45 +02:00
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:
2021-05-18 05:41:02 +02:00
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:
2021-05-18 05:41:02 +02:00
raise ValueError("no available package")
print("Keep %s models" % len(available_packages))
2021-02-08 17:56:45 +02:00
# Download and install all available packages
for available_package in available_packages:
2021-05-18 05:41:02 +02:00
print(
"Downloading %s (%s) ..."
% (available_package, available_package.package_version)
)
2021-02-08 17:56:45 +02:00
download_path = available_package.download()
package.install_from_path(download_path)
# reload installed languages
libretranslate.language.languages = translate.get_installed_languages()
2021-05-18 05:41:02 +02:00
print(
"Loaded support for %s languages (%s models total)!"
2022-09-17 08:45:59 +02:00
% (len(translate.get_installed_languages()), len(available_packages))
)