2021-02-15 19:30:28 +01:00
|
|
|
import os
|
2020-12-20 21:05:22 +01: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-02-09 01:24:34 +01:00
|
|
|
from pkg_resources import resource_filename
|
2021-02-15 19:30:28 +01:00
|
|
|
from .api_keys import Database
|
2021-03-11 12:32:26 +01:00
|
|
|
from app.language import detect_languages, transliterate
|
2021-02-15 19:30:28 +01:00
|
|
|
|
|
|
|
api_keys_db = None
|
|
|
|
|
2021-03-07 23:23:25 +01:00
|
|
|
def get_json_dict(request):
|
|
|
|
d = request.get_json()
|
|
|
|
if not isinstance(d, dict):
|
|
|
|
abort(400, description="Invalid JSON format")
|
|
|
|
return d
|
|
|
|
|
2020-12-21 17:16:49 +01: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 21:05:22 +01:00
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
def get_routes_limits(default_req_limit, api_keys_db):
|
|
|
|
if default_req_limit == -1:
|
|
|
|
# TODO: better way?
|
|
|
|
default_req_limit = 9999999999999
|
|
|
|
|
|
|
|
def limits():
|
|
|
|
req_limit = default_req_limit
|
|
|
|
|
|
|
|
if api_keys_db:
|
|
|
|
if request.is_json:
|
2021-03-07 23:23:25 +01:00
|
|
|
json = get_json_dict(request)
|
2021-02-15 19:30:28 +01:00
|
|
|
api_key = json.get('api_key')
|
|
|
|
else:
|
|
|
|
api_key = request.values.get("api_key")
|
|
|
|
|
|
|
|
if api_key:
|
|
|
|
db_req_limit = api_keys_db.lookup(api_key)
|
|
|
|
if db_req_limit is not None:
|
|
|
|
req_limit = db_req_limit
|
|
|
|
|
|
|
|
return "%s per minute" % req_limit
|
2021-03-31 17:57:02 +02:00
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
return [limits]
|
|
|
|
|
|
|
|
def create_app(args):
|
|
|
|
if not args.offline:
|
2021-02-10 17:52:03 +01:00
|
|
|
from app.init import boot
|
2021-03-28 16:19:19 +02:00
|
|
|
boot(args.load_only)
|
2021-01-19 17:51:10 +01:00
|
|
|
|
2020-12-20 22:02:30 +01:00
|
|
|
from app.language import languages
|
2020-12-20 21:05:22 +01:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
if args.debug:
|
2020-12-20 21:05:22 +01:00
|
|
|
app.config['TEMPLATES_AUTO_RELOAD'] = True
|
|
|
|
|
2021-01-10 09:07:56 +01:00
|
|
|
# Map userdefined frontend languages to argos language object.
|
2021-02-15 19:30:28 +01:00
|
|
|
if args.frontend_language_source == "auto":
|
2021-01-15 18:01:16 +01:00
|
|
|
frontend_argos_language_source = type('obj', (object,), {
|
|
|
|
'code': 'auto',
|
|
|
|
'name': 'Auto Detect'
|
|
|
|
})
|
|
|
|
else:
|
2021-02-15 19:30:28 +01:00
|
|
|
frontend_argos_language_source = next(iter([l for l in languages if l.code == args.frontend_language_source]), None)
|
2021-01-19 17:51:10 +01:00
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
frontend_argos_language_target = next(iter([l for l in languages if l.code == args.frontend_language_target]), None)
|
2021-01-19 17:51:10 +01:00
|
|
|
|
2021-01-10 09:07:56 +01:00
|
|
|
# Raise AttributeError to prevent app startup if user input is not valid.
|
|
|
|
if frontend_argos_language_source is None:
|
2021-02-15 19:30:28 +01:00
|
|
|
raise AttributeError(f"{args.frontend_language_source} as frontend source language is not supported.")
|
2021-01-10 09:07:56 +01:00
|
|
|
if frontend_argos_language_target is None:
|
2021-02-15 19:30:28 +01:00
|
|
|
raise AttributeError(f"{args.frontend_language_target} as frontend target language is not supported.")
|
2021-01-10 09:07:56 +01:00
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
if args.req_limit > 0 or args.api_keys:
|
2020-12-20 21:05:22 +01:00
|
|
|
from flask_limiter import Limiter
|
|
|
|
limiter = Limiter(
|
|
|
|
app,
|
|
|
|
key_func=get_remote_address,
|
2021-02-15 19:30:28 +01:00
|
|
|
default_limits=get_routes_limits(args.req_limit, Database() if args.api_keys else None)
|
2020-12-20 21:05:22 +01:00
|
|
|
)
|
2021-02-26 15:58:29 +01:00
|
|
|
else:
|
|
|
|
from .no_limiter import Limiter
|
|
|
|
limiter = Limiter()
|
2020-12-20 21:05:22 +01:00
|
|
|
|
|
|
|
@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("/")
|
2021-02-15 19:30:28 +01:00
|
|
|
@limiter.exempt
|
2020-12-20 21:05:22 +01:00
|
|
|
def index():
|
2021-02-15 19:30:28 +01:00
|
|
|
return render_template('index.html', gaId=args.ga_id, frontendTimeout=args.frontend_timeout, offline=args.offline, api_keys=args.api_keys, web_version=os.environ.get('LT_WEB') is not None)
|
2020-12-20 21:05:22 +01:00
|
|
|
|
2021-03-31 17:57:02 +02:00
|
|
|
@app.route("/javascript-licenses", methods=['GET'])
|
|
|
|
@limiter.exempt
|
|
|
|
def javascript_licenses():
|
|
|
|
return render_template('javascript-licenses.html', offline=args.offline)
|
|
|
|
|
2021-02-10 19:06:11 +01:00
|
|
|
@app.route("/languages", methods=['GET', 'POST'])
|
2021-02-15 19:30:28 +01:00
|
|
|
@limiter.exempt
|
2020-12-20 21:05:22 +01:00
|
|
|
def langs():
|
|
|
|
"""
|
|
|
|
Retrieve list of supported languages
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- translate
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: List of languages
|
2021-01-10 09:38:11 +01: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 21:05:22 +01:00
|
|
|
429:
|
|
|
|
description: Slow down
|
2021-01-10 09:38:11 +01:00
|
|
|
schema:
|
|
|
|
id: error-slow-down
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
error:
|
|
|
|
type: string
|
|
|
|
description: Reason for slow down
|
2020-12-20 21:05:22 +01:00
|
|
|
"""
|
2021-01-10 10:24:42 +01:00
|
|
|
return jsonify([{'code': l.code, 'name': l.name} for l in languages])
|
2020-12-20 21:05:22 +01: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:
|
2021-01-19 17:51:10 +01:00
|
|
|
oneOf:
|
|
|
|
- type: string
|
|
|
|
example: Hello world!
|
|
|
|
- type: array
|
|
|
|
example: ['Hello world!']
|
2020-12-20 21:05:22 +01:00
|
|
|
required: true
|
2021-01-19 17:51:10 +01:00
|
|
|
description: Text(s) to translate
|
2020-12-20 21:05:22 +01:00
|
|
|
- in: formData
|
|
|
|
name: source
|
|
|
|
schema:
|
|
|
|
type: string
|
|
|
|
example: en
|
|
|
|
required: true
|
2021-01-19 17:51:10 +01:00
|
|
|
description: Source language code
|
2020-12-20 21:05:22 +01:00
|
|
|
- in: formData
|
|
|
|
name: target
|
|
|
|
schema:
|
|
|
|
type: string
|
|
|
|
example: es
|
|
|
|
required: true
|
|
|
|
description: Target language code
|
2021-02-15 19:30:28 +01:00
|
|
|
- in: formData
|
|
|
|
name: api_key
|
|
|
|
schema:
|
|
|
|
type: string
|
|
|
|
example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
|
|
required: false
|
|
|
|
description: API key
|
2020-12-20 21:05:22 +01:00
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: Translated text
|
2021-01-10 09:38:11 +01:00
|
|
|
schema:
|
|
|
|
id: translate
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
translatedText:
|
2021-01-19 17:51:10 +01:00
|
|
|
oneOf:
|
|
|
|
- type: string
|
|
|
|
- type: array
|
|
|
|
description: Translated text(s)
|
2020-12-20 21:05:22 +01:00
|
|
|
400:
|
|
|
|
description: Invalid request
|
2021-01-10 09:38:11 +01:00
|
|
|
schema:
|
|
|
|
id: error-response
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
error:
|
|
|
|
type: string
|
|
|
|
description: Error message
|
2020-12-20 21:05:22 +01:00
|
|
|
500:
|
|
|
|
description: Translation error
|
2021-01-10 09:38:11 +01:00
|
|
|
schema:
|
|
|
|
id: error-response
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
error:
|
|
|
|
type: string
|
|
|
|
description: Error message
|
2020-12-20 21:05:22 +01:00
|
|
|
429:
|
|
|
|
description: Slow down
|
2021-01-10 09:38:11 +01:00
|
|
|
schema:
|
|
|
|
id: error-slow-down
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
error:
|
|
|
|
type: string
|
|
|
|
description: Reason for slow down
|
2020-12-20 21:05:22 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
if request.is_json:
|
2021-03-07 23:23:25 +01:00
|
|
|
json = get_json_dict(request)
|
2020-12-20 21:05:22 +01:00
|
|
|
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")
|
|
|
|
|
2021-01-19 17:51:10 +01:00
|
|
|
batch = isinstance(q, list)
|
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
if batch and args.batch_limit != -1:
|
2021-01-19 18:53:53 +01:00
|
|
|
batch_size = len(q)
|
2021-02-15 19:30:28 +01:00
|
|
|
if args.batch_limit < batch_size:
|
|
|
|
abort(400, description="Invalid request: Request (%d) exceeds text limit (%d)" % (batch_size, args.batch_limit))
|
2021-01-19 18:53:53 +01:00
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
if args.char_limit != -1:
|
2021-01-19 17:51:10 +01:00
|
|
|
if batch:
|
|
|
|
chars = sum([len(text) for text in q])
|
|
|
|
else:
|
|
|
|
chars = len(q)
|
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
if args.char_limit < chars:
|
|
|
|
abort(400, description="Invalid request: Request (%d) exceeds character limit (%d)" % (chars, args.char_limit))
|
2020-12-20 21:05:22 +01:00
|
|
|
|
2021-01-13 15:33:58 +01:00
|
|
|
if source_lang == 'auto':
|
2021-03-11 10:01:12 +01:00
|
|
|
candidate_langs = detect_languages(q)
|
2021-01-15 18:12:00 +01:00
|
|
|
|
2021-03-11 10:01:12 +01:00
|
|
|
if args.debug:
|
|
|
|
print(candidate_langs)
|
2021-01-15 18:12:00 +01:00
|
|
|
|
2021-03-11 10:01:12 +01:00
|
|
|
source_lang = candidate_langs[0]["language"]
|
2021-01-19 17:51:10 +01:00
|
|
|
|
2021-02-15 19:30:28 +01:00
|
|
|
if args.debug:
|
2021-01-15 18:01:16 +01:00
|
|
|
print("Auto detected: %s" % source_lang)
|
2021-01-13 15:33:58 +01:00
|
|
|
|
2020-12-20 21:05:22 +01: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)
|
2021-01-19 17:51:10 +01:00
|
|
|
|
2020-12-20 21:05:22 +01: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)
|
2021-01-19 17:51:10 +01:00
|
|
|
|
2020-12-20 21:05:22 +01:00
|
|
|
try:
|
2021-01-19 17:51:10 +01:00
|
|
|
if batch:
|
2021-03-11 12:32:26 +01:00
|
|
|
return jsonify({"translatedText": [translator.translate(transliterate(text, target_lang=source_lang)) for text in q] })
|
2021-01-19 17:51:10 +01:00
|
|
|
else:
|
2021-03-11 12:32:26 +01:00
|
|
|
return jsonify({"translatedText": translator.translate(transliterate(q, target_lang=source_lang)) })
|
2020-12-20 21:05:22 +01:00
|
|
|
except Exception as e:
|
|
|
|
abort(500, description="Cannot translate text: %s" % str(e))
|
|
|
|
|
2021-02-10 16:51:17 +01:00
|
|
|
@app.route("/detect", methods=['POST'])
|
|
|
|
def detect():
|
|
|
|
"""
|
|
|
|
Detect the language of a single text
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- translate
|
|
|
|
parameters:
|
|
|
|
- in: formData
|
|
|
|
name: q
|
|
|
|
schema:
|
|
|
|
type: string
|
|
|
|
example: Hello world!
|
|
|
|
required: true
|
|
|
|
description: Text to detect
|
2021-02-15 19:30:28 +01:00
|
|
|
- in: formData
|
|
|
|
name: api_key
|
|
|
|
schema:
|
|
|
|
type: string
|
|
|
|
example: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
|
|
|
|
required: false
|
|
|
|
description: API key
|
2021-02-10 16:51:17 +01:00
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: Detections
|
|
|
|
schema:
|
|
|
|
id: detections
|
|
|
|
type: array
|
|
|
|
items:
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
confidence:
|
|
|
|
type: number
|
|
|
|
format: float
|
|
|
|
minimum: 0
|
|
|
|
maximum: 1
|
|
|
|
description: Confidence value
|
|
|
|
example: 0.6
|
|
|
|
language:
|
|
|
|
type: string
|
|
|
|
description: Language code
|
|
|
|
example: en
|
|
|
|
400:
|
|
|
|
description: Invalid request
|
|
|
|
schema:
|
|
|
|
id: error-response
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
error:
|
|
|
|
type: string
|
|
|
|
description: Error message
|
|
|
|
500:
|
|
|
|
description: Detection error
|
|
|
|
schema:
|
|
|
|
id: error-response
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
error:
|
|
|
|
type: string
|
|
|
|
description: Error message
|
|
|
|
429:
|
|
|
|
description: Slow down
|
|
|
|
schema:
|
|
|
|
id: error-slow-down
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
error:
|
|
|
|
type: string
|
|
|
|
description: Reason for slow down
|
|
|
|
"""
|
|
|
|
if request.is_json:
|
2021-03-07 23:23:25 +01:00
|
|
|
json = get_json_dict(request)
|
2021-02-10 16:51:17 +01:00
|
|
|
q = json.get('q')
|
|
|
|
else:
|
|
|
|
q = request.values.get("q")
|
|
|
|
|
|
|
|
if not q:
|
|
|
|
abort(400, description="Invalid request: missing q parameter")
|
|
|
|
|
2021-03-11 10:01:12 +01:00
|
|
|
return jsonify(detect_languages(q))
|
2021-02-10 16:51:17 +01:00
|
|
|
|
|
|
|
|
2021-01-10 09:07:56 +01:00
|
|
|
@app.route("/frontend/settings")
|
2021-02-15 19:30:28 +01:00
|
|
|
@limiter.exempt
|
2021-01-10 09:07:56 +01:00
|
|
|
def frontend_settings():
|
|
|
|
"""
|
|
|
|
Retrieve frontend specific settings
|
|
|
|
---
|
|
|
|
tags:
|
|
|
|
- frontend
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: frontend settings
|
|
|
|
schema:
|
|
|
|
id: frontend-settings
|
|
|
|
type: object
|
|
|
|
properties:
|
2021-01-10 10:24:42 +01:00
|
|
|
charLimit:
|
|
|
|
type: integer
|
|
|
|
description: Character input limit for this language (-1 indicates no limit)
|
2021-01-28 17:16:55 +01:00
|
|
|
frontendTimeout:
|
|
|
|
type: integer
|
|
|
|
description: Frontend translation timeout
|
2021-01-10 09:07:56 +01:00
|
|
|
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-02-15 19:30:28 +01:00
|
|
|
return jsonify({'charLimit': args.char_limit,
|
|
|
|
'frontendTimeout': args.frontend_timeout,
|
2021-01-10 10:24:42 +01:00
|
|
|
'language': {
|
2021-01-10 09:07:56 +01:00
|
|
|
'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 21:05:22 +01:00
|
|
|
|
|
|
|
swag = swagger(app)
|
2021-02-15 19:30:28 +01:00
|
|
|
swag['info']['version'] = "1.2"
|
2020-12-20 21:05:22 +01:00
|
|
|
swag['info']['title'] = "LibreTranslate"
|
|
|
|
|
|
|
|
@app.route("/spec")
|
2021-02-15 19:30:28 +01:00
|
|
|
@limiter.exempt
|
2020-12-20 21:05:22 +01:00
|
|
|
def spec():
|
|
|
|
return jsonify(swag)
|
|
|
|
|
|
|
|
SWAGGER_URL = '/docs' # URL for exposing Swagger UI (without trailing '/')
|
2020-12-20 23:06:29 +01:00
|
|
|
API_URL = '/spec'
|
2020-12-20 23:11:06 +01:00
|
|
|
|
2020-12-20 21:05:22 +01:00
|
|
|
# Call factory function to create our blueprint
|
|
|
|
swaggerui_blueprint = get_swaggerui_blueprint(
|
|
|
|
SWAGGER_URL,
|
2020-12-21 00:17:06 +01:00
|
|
|
API_URL
|
2020-12-20 21:05:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
app.register_blueprint(swaggerui_blueprint)
|
|
|
|
|
2020-12-21 00:17:06 +01:00
|
|
|
return app
|