Merge pull request #12 from vemonet/main

add support for auto language
This commit is contained in:
Piero Toffanin 2021-01-15 11:36:43 -05:00 committed by GitHub
commit 06b3c12ff6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 3 deletions

View file

@ -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:

View file

@ -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?"

View file

@ -4,3 +4,4 @@ flask-swagger==0.2.14
flask-swagger-ui==3.36.0
Flask-Limiter==1.4
waitress==1.4.4
langdetect