Add option --load-only language codes

The user will not have to install language packages from "argos-translate" that he/she has no use for.
This commit is contained in:
Philippe-Cholet 2021-03-28 16:19:19 +02:00
parent fb031b826a
commit abeee7e385
3 changed files with 27 additions and 4 deletions

View file

@ -49,7 +49,7 @@ def get_routes_limits(default_req_limit, api_keys_db):
def create_app(args): def create_app(args):
if not args.offline: if not args.offline:
from app.init import boot from app.init import boot
boot() boot(args.load_only)
from app.language import languages from app.language import languages
app = Flask(__name__) app = Flask(__name__)

View file

@ -5,11 +5,11 @@ import os, glob, shutil, zipfile
from app.language import languages from app.language import languages
import polyglot import polyglot
def boot(): def boot(load_only=None):
check_and_install_models() check_and_install_models(load_only_lang_codes=load_only)
check_and_install_transliteration() check_and_install_transliteration()
def check_and_install_models(force=False): def check_and_install_models(force=False, load_only_lang_codes=None):
if len(package.get_installed_packages()) < 2 or force: if len(package.get_installed_packages()) < 2 or force:
# Update package definitions from remote # Update package definitions from remote
print("Updating language models") print("Updating language models")
@ -19,6 +19,25 @@ def check_and_install_models(force=False):
available_packages = package.load_available_packages() available_packages = package.load_available_packages()
print("Found %s models" % len(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 # Download and install all available packages
for available_package in available_packages: for available_package in available_packages:
print("Downloading %s (%s) ..." % (available_package, available_package.package_version)) print("Downloading %s (%s) ..." % (available_package, available_package.package_version))

View file

@ -1,4 +1,5 @@
import argparse import argparse
import operator
from app.app import create_app from app.app import create_app
def main(): def main():
@ -29,6 +30,9 @@ def main():
help="Use offline") help="Use offline")
parser.add_argument('--api-keys', default=False, action="store_true", parser.add_argument('--api-keys', default=False, action="store_true",
help="Enable API keys database for per-user rate limits lookup") help="Enable API keys database for per-user rate limits lookup")
parser.add_argument('--load-only', type=operator.methodcaller('split', ','),
metavar='<comma-separated language codes>',
help='Set available languages (ar,de,en,es,fr,ga,hi,it,ja,ko,pt,ru,zh)')
args = parser.parse_args() args = parser.parse_args()