diff --git a/.gitignore b/.gitignore index b6e4761..bb44dea 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,5 @@ dmypy.json # Pyre type checker .pyre/ +installed_models/ + diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c65b44a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "models"] + path = models + url = https://github.com/uav4geo/LibreTranslate-Models diff --git a/app/init.py b/app/init.py new file mode 100644 index 0000000..6aeb88b --- /dev/null +++ b/app/init.py @@ -0,0 +1,30 @@ +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()))) + \ No newline at end of file diff --git a/app/language.py b/app/language.py new file mode 100644 index 0000000..279f2c8 --- /dev/null +++ b/app/language.py @@ -0,0 +1,3 @@ +from argostranslate import translate + +languages = translate.load_installed_languages() \ No newline at end of file diff --git a/install_models.py b/install_models.py new file mode 100644 index 0000000..334e1fc --- /dev/null +++ b/install_models.py @@ -0,0 +1,4 @@ +from app.init import check_and_install_models + +if __name__ == "__main__": + check_and_install_models(force=True) \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..602db8a --- /dev/null +++ b/main.py @@ -0,0 +1,66 @@ +import argparse +from flask import Flask, render_template, jsonify, request, abort, send_from_directory +from app.language import languages +from app.init import boot + +parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API') +parser.add_argument('host', type=str, + help='Hostname', default="127.0.0.1") +parser.add_argument('port', type=int, + help='Port', default=5000) +args = parser.parse_args() + +boot() +app = Flask(__name__) +app.config['TEMPLATES_AUTO_RELOAD'] = True + + +@app.errorhandler(400) +def invalid_api(e): + return jsonify({"error": str(e.description)}), 400 + +@app.errorhandler(500) +def server_error(e): + return jsonify({"error": str(e.description)}), 500 + +@app.route("/") +def index(): + return send_from_directory('static', 'index.html') + + +@app.route("/languages") +def langs(): + return jsonify([{'code': l.code, 'name': l.name } for l in languages]) + +@app.route("/translate", methods=['POST']) +def translate(): + + q = request.values.get("q") + source_lang = request.values.get("source") + target_lang = request.values.get("target") + + if not q: + abort(400, description="Invalid request: missing q parameter") + if not source_lang: + abort(400, description="Invalid request: missing source parameter") + if not target_lang: + abort(400, description="Invalid request: missing target parameter") + + + src_lang = next(iter([l for l in languages if l.code == source_lang]), None) + tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None) + + if src_lang is None: + abort(400, description="%s is not supported" % source_lang) + if tgt_lang is None: + abort(400, description="%s is not supported" % target_lang) + + translator = src_lang.get_translation(tgt_lang) + try: + return jsonify({"translatedText": translator.translate(q) }) + except Exception as e: + abort(500, description="Cannot translate text: %s" % str(e)) + + +if __name__ == "__main__": + app.run(host=args.host) diff --git a/models b/models new file mode 160000 index 0000000..0492a76 --- /dev/null +++ b/models @@ -0,0 +1 @@ +Subproject commit 0492a7619847d74f3ec6e7e2b6707e00103eebd0 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e927cd3 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +argostranslate==1.0.3 + diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..22efc52 --- /dev/null +++ b/static/index.html @@ -0,0 +1,185 @@ + + +
+ + +{{ sourceLang }}+