LibreTranslate/app/init.py

33 lines
996 B
Python
Raw Normal View History

2020-12-19 22:40:37 +00:00
import os
2020-12-20 16:55:56 +00:00
from pathlib import Path
from argostranslate import settings
2020-12-19 22:40:37 +00:00
INSTALLED_MODELS_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "installed_models"))
os.environ["ARGOS_TRANSLATE_PACKAGES_DIR"] = INSTALLED_MODELS_DIR
2020-12-20 16:55:56 +00:00
settings.package_dirs = [Path(INSTALLED_MODELS_DIR)]
from argostranslate import translate
import os, glob, shutil, zipfile
2020-12-19 22:40:37 +00:00
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())))