From 55d3c32b40e60702ea5bcb86a1b6490cf2d37a19 Mon Sep 17 00:00:00 2001 From: Vincent Emonet Date: Wed, 13 Jan 2021 15:33:58 +0100 Subject: [PATCH] add support for auto language. Not perfect, it picks a language not supported sometimes --- app/app.py | 14 +++++++++++--- app/templates/index.html | 1 + requirements.txt | 1 + 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/app.py b/app/app.py index 0f098bc..f837325 100644 --- a/app/app.py +++ b/app/app.py @@ -1,6 +1,7 @@ from flask import Flask, render_template, jsonify, request, abort, send_from_directory from flask_swagger import swagger from flask_swagger_ui import get_swaggerui_blueprint +from langdetect import detect def get_remote_address(): if request.headers.getlist("X-Forwarded-For"): @@ -10,7 +11,7 @@ def get_remote_address(): return ip -def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="en", frontend_language_target="es"): +def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="auto", frontend_language_target="en"): from app.init import boot boot() @@ -21,7 +22,7 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la app.config['TEMPLATES_AUTO_RELOAD'] = True # Map userdefined frontend languages to argos language object. - frontend_argos_language_source = next(iter([l for l in languages if l.code == frontend_language_source]), None) + frontend_argos_language_source = next(iter([l for l in languages if l.code == frontend_language_source or l.code == 'auto']), None) frontend_argos_language_target = next(iter([l for l in languages if l.code == frontend_language_target]), None) # Raise AttributeError to prevent app startup if user input is not valid. if frontend_argos_language_source is None: @@ -187,9 +188,16 @@ def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_la if char_limit != -1: q = q[:char_limit] + original_source_lang = source_lang + if source_lang == 'auto': + source_lang = detect(q) + 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 and original_source_lang == 'auto': + return jsonify({"translatedText": "Detected language not supported (" + source_lang + ")" }) + if src_lang is None: abort(400, description="%s is not supported" % source_lang) if tgt_lang is None: diff --git a/app/templates/index.html b/app/templates/index.html index d3a9bd9..f2e9c9f 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -299,6 +299,7 @@ document.addEventListener('DOMContentLoaded', function(){ if (this.status >= 200 && this.status < 400) { // Success! self.langs = JSON.parse(this.response); + self.langs.push({ name: 'Auto (experimental)', code: 'auto' }) if (self.langs.length === 0){ self.loading = false; self.error = "No languages available. Did you install the models correctly?" diff --git a/requirements.txt b/requirements.txt index 500c95e..cf30471 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ flask-swagger==0.2.14 flask-swagger-ui==3.36.0 Flask-Limiter==1.4 waitress==1.4.4 +langdetect