LibreTranslate/app/init.py
Piero Toffanin 1062ef2713 First commit
2020-12-19 17:40:37 -05:00

30 lines
No EOL
882 B
Python

import os
from argostranslate import translate
import os, glob, shutil, zipfile
INSTALLED_MODELS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "installed_models"))
os.environ["ARGOS_TRANSLATE_PACKAGES_DIR"] = INSTALLED_MODELS_DIR
def boot():
check_and_install_models()
def check_and_install_models(force=False):
if os.path.exists(INSTALLED_MODELS_DIR) and not force:
return
if os.path.exists(INSTALLED_MODELS_DIR):
print("Removing old %s" % INSTALLED_MODELS_DIR)
shutil.rmtree(INSTALLED_MODELS_DIR)
print("Creating %s" % INSTALLED_MODELS_DIR)
os.makedirs(INSTALLED_MODELS_DIR, exist_ok=True)
for f in glob.glob("models/**.argosmodel"):
print("Installing %s..." % f)
with zipfile.ZipFile(f, 'r') as zip:
zip.extractall(path=INSTALLED_MODELS_DIR)
print("Installed %s language models!" % (len(translate.load_installed_languages())))