LibreTranslate/app/app.py

303 lines
9.9 KiB
Python
Raw Normal View History

2020-12-20 20:05:22 +00:00
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
2021-01-15 17:01:16 +00:00
from langdetect import detect_langs
from langdetect import DetectorFactory
DetectorFactory.seed = 0 # deterministic
2020-12-20 20:05:22 +00:00
2020-12-21 16:16:49 +00:00
def get_remote_address():
if request.headers.getlist("X-Forwarded-For"):
ip = request.headers.getlist("X-Forwarded-For")[0]
else:
ip = request.remote_addr or '127.0.0.1'
return ip
2020-12-20 20:05:22 +00:00
2021-01-15 17:01:16 +00:00
def create_app(char_limit=-1, req_limit=-1, ga_id=None, debug=False, frontend_language_source="en", frontend_language_target="en"):
2020-12-20 21:01:24 +00:00
from app.init import boot
2020-12-20 20:05:22 +00:00
boot()
2020-12-20 21:02:30 +00:00
from app.language import languages
2020-12-20 20:05:22 +00:00
app = Flask(__name__)
# For faster access
language_map = {}
for l in languages:
language_map[l.code] = l.name
2020-12-20 20:05:22 +00:00
if debug:
app.config['TEMPLATES_AUTO_RELOAD'] = True
# Map userdefined frontend languages to argos language object.
2021-01-15 17:01:16 +00:00
if frontend_language_source == "auto":
frontend_argos_language_source = type('obj', (object,), {
'code': 'auto',
'name': 'Auto Detect'
})
else:
frontend_argos_language_source = next(iter([l for l in languages if l.code == frontend_language_source]), None)
frontend_argos_language_target = next(iter([l for l in languages if l.code == frontend_language_target]), None)
2021-01-15 17:01:16 +00:00
# Raise AttributeError to prevent app startup if user input is not valid.
if frontend_argos_language_source is None:
raise AttributeError(f"{frontend_language_source} as frontend source language is not supported.")
if frontend_argos_language_target is None:
raise AttributeError(f"{frontend_language_target} as frontend target language is not supported.")
2020-12-20 20:05:22 +00:00
if req_limit > 0:
from flask_limiter import Limiter
limiter = Limiter(
app,
key_func=get_remote_address,
default_limits=["%s per minute" % req_limit]
)
@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.errorhandler(429)
def slow_down_error(e):
return jsonify({"error": "Slowdown: " + str(e.description)}), 429
@app.route("/")
def index():
2020-12-20 20:52:02 +00:00
return render_template('index.html', gaId=ga_id)
2020-12-20 20:05:22 +00:00
@app.route("/languages")
def langs():
"""
Retrieve list of supported languages
---
tags:
- translate
responses:
200:
description: List of languages
2021-01-10 08:38:11 +00:00
schema:
id: languages
type: array
items:
type: object
properties:
code:
type: string
description: Language code
name:
type: string
description: Human-readable language name (in English)
2020-12-20 20:05:22 +00:00
429:
description: Slow down
2021-01-10 08:38:11 +00:00
schema:
id: error-slow-down
type: object
properties:
error:
type: string
description: Reason for slow down
2020-12-20 20:05:22 +00:00
"""
2021-01-10 09:24:42 +00:00
return jsonify([{'code': l.code, 'name': l.name} for l in languages])
2020-12-20 20:05:22 +00:00
# Add cors
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin','*')
response.headers.add('Access-Control-Allow-Headers', "Authorization, Content-Type")
response.headers.add('Access-Control-Expose-Headers', "Authorization")
response.headers.add('Access-Control-Allow-Methods', "GET, POST")
response.headers.add('Access-Control-Allow-Credentials', "true")
response.headers.add('Access-Control-Max-Age', 60 * 60 * 24 * 20)
return response
@app.route("/translate", methods=['POST'])
def translate():
"""
Translate text from a language to another
---
tags:
- translate
parameters:
- in: formData
name: q
schema:
type: string
example: Hello world!
required: true
description: Text to translate
- in: formData
name: source
schema:
type: string
example: en
required: true
description: Source language code
- in: formData
name: target
schema:
type: string
example: es
required: true
description: Target language code
responses:
200:
description: Translated text
2021-01-10 08:38:11 +00:00
schema:
id: translate
type: object
properties:
translatedText:
type: string
description: Translated text
2020-12-20 20:05:22 +00:00
400:
description: Invalid request
2021-01-10 08:38:11 +00:00
schema:
id: error-response
type: object
properties:
error:
type: string
description: Error message
2020-12-20 20:05:22 +00:00
500:
description: Translation error
2021-01-10 08:38:11 +00:00
schema:
id: error-response
type: object
properties:
error:
type: string
description: Error message
2020-12-20 20:05:22 +00:00
429:
description: Slow down
2021-01-10 08:38:11 +00:00
schema:
id: error-slow-down
type: object
properties:
error:
type: string
description: Reason for slow down
2020-12-20 20:05:22 +00:00
"""
if request.is_json:
json = request.get_json()
q = json.get('q')
source_lang = json.get('source')
target_lang = json.get('target')
else:
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")
if char_limit != -1:
q = q[:char_limit]
if source_lang == 'auto':
candidate_langs = list(filter(lambda l: l.lang in language_map, detect_langs(q)))
2021-01-15 17:01:16 +00:00
if len(candidate_langs) > 0:
candidate_langs.sort(key=lambda l: l.prob, reverse=True)
2021-01-15 17:01:16 +00:00
if debug:
print(candidate_langs)
2021-01-15 17:01:16 +00:00
source_lang = next(iter([l.code for l in languages if l.code == candidate_langs[0].lang]), None)
if not source_lang:
source_lang = 'en'
else:
source_lang = 'en'
if debug:
print("Auto detected: %s" % source_lang)
2020-12-20 20:05:22 +00:00
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)
2020-12-20 20:05:22 +00:00
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))
@app.route("/frontend/settings")
def frontend_settings():
"""
Retrieve frontend specific settings
---
tags:
- frontend
responses:
200:
description: frontend settings
schema:
id: frontend-settings
type: object
properties:
2021-01-10 09:24:42 +00:00
charLimit:
type: integer
description: Character input limit for this language (-1 indicates no limit)
language:
type: object
properties:
source:
type: object
properties:
code:
type: string
description: Language code
name:
type: string
description: Human-readable language name (in English)
target:
type: object
properties:
code:
type: string
description: Language code
name:
type: string
description: Human-readable language name (in English)
"""
2021-01-10 09:24:42 +00:00
return jsonify({'charLimit': char_limit,
'language': {
'source': {'code': frontend_argos_language_source.code, 'name': frontend_argos_language_source.name},
'target': {'code': frontend_argos_language_target.code, 'name': frontend_argos_language_target.name}}
})
2020-12-20 20:05:22 +00:00
swag = swagger(app)
swag['info']['version'] = "1.0"
swag['info']['title'] = "LibreTranslate"
@app.route("/spec")
def spec():
return jsonify(swag)
SWAGGER_URL = '/docs' # URL for exposing Swagger UI (without trailing '/')
2020-12-20 22:06:29 +00:00
API_URL = '/spec'
2020-12-20 22:11:06 +00:00
2020-12-20 20:05:22 +00:00
# Call factory function to create our blueprint
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
2020-12-20 23:17:06 +00:00
API_URL
2020-12-20 20:05:22 +00:00
)
app.register_blueprint(swaggerui_blueprint)
2020-12-20 23:17:06 +00:00
return app