2021-05-18 05:51:33 +02:00
|
|
|
|
|
|
|
from argostranslate import package, translate
|
|
|
|
|
2022-12-30 01:44:53 +02:00
|
|
|
import libretranslate.language
|
2020-12-20 00:40:37 +02:00
|
|
|
|
2021-05-18 05:41:02 +02:00
|
|
|
|
2023-09-29 22:43:59 +02:00
|
|
|
def boot(load_only=None, update_models=False, install_models=False):
|
2021-05-16 16:42:58 +02:00
|
|
|
try:
|
2023-09-29 22:43:59 +02:00
|
|
|
if update_models:
|
|
|
|
check_and_install_models(load_only_lang_codes=load_only, update=update_models)
|
|
|
|
else:
|
|
|
|
check_and_install_models(force=install_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
|
|
|
|
2023-09-29 22:43:59 +02:00
|
|
|
def check_and_install_models(force=False, load_only_lang_codes=None,update=False):
|
|
|
|
if len(package.get_installed_packages()) < 2 or force or update:
|
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()
|
2023-09-29 22:43:59 +02:00
|
|
|
installed_packages = package.get_installed_packages()
|
2021-02-08 17:56:45 +02:00
|
|
|
print("Found %s models" % len(available_packages))
|
2021-03-28 16:19:19 +02:00
|
|
|
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))
|
|
|
|
)
|
2021-03-28 16:19:19 +02:00
|
|
|
# Keep only the packages that have both from_code and to_code in our list.
|
|
|
|
available_packages = [
|
|
|
|
pack
|
|
|
|
for pack in available_packages
|
2021-05-18 05:51:33 +02:00
|
|
|
if pack.from_code in load_only_lang_codes and pack.to_code in load_only_lang_codes
|
2021-03-28 16:19:19 +02:00
|
|
|
]
|
|
|
|
if not available_packages:
|
2021-05-18 05:41:02 +02:00
|
|
|
raise ValueError("no available package")
|
2021-03-28 16:19:19 +02:00
|
|
|
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:
|
2023-09-29 22:43:59 +02:00
|
|
|
update = False
|
|
|
|
if not force:
|
|
|
|
for pack in installed_packages:
|
|
|
|
if (
|
|
|
|
pack.from_code == available_package.from_code
|
|
|
|
and pack.to_code == available_package.to_code
|
|
|
|
):
|
|
|
|
update = True
|
|
|
|
if pack.package_version < available_package.package_version:
|
|
|
|
print(
|
|
|
|
f"Updating {available_package} ({pack.package_version}->{available_package.package_version}) ..."
|
|
|
|
)
|
|
|
|
pack.update()
|
|
|
|
if not update:
|
|
|
|
print(
|
|
|
|
f"Downloading {available_package} ({available_package.package_version}) ..."
|
|
|
|
)
|
|
|
|
available_package.install()
|
2021-02-08 17:56:45 +02:00
|
|
|
|
2021-03-11 15:38:55 +02:00
|
|
|
# reload installed languages
|
2023-01-03 14:40:05 +02:00
|
|
|
libretranslate.language.languages = translate.get_installed_languages()
|
2021-05-18 05:41:02 +02:00
|
|
|
print(
|
2023-07-10 08:11:51 +02:00
|
|
|
f"Loaded support for {len(translate.get_installed_languages())} languages ({len(available_packages)} models total)!"
|
2022-12-30 01:44:53 +02:00
|
|
|
)
|