2021-10-24 19:14:09 +02:00
import io
2021-02-15 19:30:28 +01:00
import os
2021-10-24 18:27:37 +02:00
import tempfile
import uuid
2021-05-18 05:51:33 +02:00
from functools import wraps
2022-02-20 09:09:02 +01:00
from html import unescape
2021-05-18 05:51:33 +02:00
2021-10-24 18:27:37 +02:00
import argostranslatefiles
from argostranslatefiles import get_supported_formats
2022-02-20 09:09:02 +01:00
from flask import ( Flask , abort , jsonify , render_template , request , send_file ,
url_for )
2020-12-20 21:05:22 +01:00
from flask_swagger import swagger
from flask_swagger_ui import get_swaggerui_blueprint
2021-10-24 18:27:37 +02:00
from translatehtml import translate_html
from werkzeug . utils import secure_filename
2021-05-18 05:51:33 +02:00
2021-10-26 21:41:14 +02:00
from app import flood , remove_translated_files , security
2022-11-16 15:31:25 +01:00
from app . language import detect_languages , improve_translation_formatting
2022-02-20 09:09:02 +01:00
2022-06-21 20:57:32 +02:00
from . api_keys import Database , RemoteDatabase
2021-10-09 11:44:00 +02:00
from . suggestions import Database as SuggestionsDatabase
2021-02-15 19:30:28 +01:00
2021-10-26 21:32:06 +02:00
def get_version ( ) :
try :
with open ( " VERSION " ) as f :
return f . read ( ) . strip ( )
except :
return " ? "
2021-11-05 14:55:45 +01:00
2021-10-25 11:06:39 +02:00
def get_upload_dir ( ) :
2021-10-25 11:46:49 +02:00
upload_dir = os . path . join ( tempfile . gettempdir ( ) , " libretranslate-files-translate " )
if not os . path . isdir ( upload_dir ) :
os . mkdir ( upload_dir )
return upload_dir
2021-11-05 14:55:45 +01:00
2021-10-26 23:13:51 +02:00
def get_req_api_key ( ) :
if request . is_json :
json = get_json_dict ( request )
ak = json . get ( " api_key " )
else :
ak = request . values . get ( " api_key " )
return ak
2021-10-25 11:46:49 +02:00
2021-11-05 14:55:45 +01:00
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
2021-05-18 05:41:02 +02:00
2020-12-21 17:16:49 +01:00
def get_remote_address ( ) :
if request . headers . getlist ( " X-Forwarded-For " ) :
2021-09-08 21:41:12 +02:00
ip = request . headers . getlist ( " X-Forwarded-For " ) [ 0 ] . split ( " , " ) [ 0 ]
2020-12-21 17:16:49 +01:00
else :
2021-05-18 05:41:02 +02:00
ip = request . remote_addr or " 127.0.0.1 "
2020-12-21 17:16:49 +01:00
return ip
2020-12-20 21:05:22 +01:00
2021-06-03 16:36:25 +02:00
2021-10-24 18:27:37 +02:00
def get_req_limits ( default_limit , api_keys_db , multiplier = 1 ) :
2021-06-03 16:36:25 +02:00
req_limit = default_limit
if api_keys_db :
2021-10-26 23:13:51 +02:00
api_key = get_req_api_key ( )
2021-06-03 16:36:25 +02:00
if api_key :
db_req_limit = api_keys_db . lookup ( api_key )
if db_req_limit is not None :
2021-07-04 18:36:13 +02:00
req_limit = db_req_limit * multiplier
2021-10-24 18:27:37 +02:00
2021-06-03 16:36:25 +02:00
return req_limit
2021-05-16 16:57:19 +02:00
def get_routes_limits ( default_req_limit , daily_req_limit , api_keys_db ) :
2021-02-15 19:30:28 +01:00
if default_req_limit == - 1 :
# TODO: better way?
default_req_limit = 9999999999999
2021-06-03 16:36:25 +02:00
def minute_limits ( ) :
return " %s per minute " % get_req_limits ( default_req_limit , api_keys_db )
2021-02-15 19:30:28 +01:00
2021-06-03 16:36:25 +02:00
def daily_limits ( ) :
return " %s per day " % get_req_limits ( daily_req_limit , api_keys_db , 1440 )
2021-03-31 17:57:02 +02:00
2021-06-03 16:36:25 +02:00
res = [ minute_limits ]
2021-05-16 16:57:19 +02:00
if daily_req_limit > 0 :
2021-06-03 16:36:25 +02:00
res . append ( daily_limits )
2021-05-16 16:57:19 +02:00
return res
2021-02-15 19:30:28 +01:00
2021-05-17 17:41:15 +02:00
2021-02-15 19:30:28 +01:00
def create_app ( args ) :
2021-05-16 16:42:58 +02:00
from app . init import boot
2021-05-18 05:41:02 +02:00
2021-05-16 16:42:58 +02:00
boot ( args . load_only )
2021-01-19 17:51:10 +01:00
2022-03-04 09:23:11 +01:00
from app . language import load_languages
2021-05-18 05:41:02 +02:00
2020-12-20 21:05:22 +01:00
app = Flask ( __name__ )
2021-02-15 19:30:28 +01:00
if args . debug :
2021-05-18 05:41:02 +02:00
app . config [ " TEMPLATES_AUTO_RELOAD " ] = True
2020-12-20 21:05:22 +01:00
2021-10-25 17:12:09 +02:00
if not args . disable_files_translation :
remove_translated_files . setup ( get_upload_dir ( ) )
2022-03-04 09:23:11 +01:00
languages = load_languages ( )
2022-12-09 22:36:12 +01:00
language_pairs = { }
for lang in languages :
2022-12-10 06:03:21 +01:00
language_pairs [ lang . code ] = sorted ( [ l . to_lang . code for l in lang . translations_from ] )
2021-10-25 17:12:09 +02:00
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-05-18 05:41:02 +02:00
frontend_argos_language_source = type (
" obj " , ( object , ) , { " code " : " auto " , " name " : " Auto Detect " }
)
2021-01-15 18:01:16 +01:00
else :
2021-05-18 05:41:02 +02: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-05-18 05:41:02 +02: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-10-24 16:57:45 +02:00
frontend_argos_supported_files_format = [ ]
for file_format in get_supported_formats ( ) :
for ff in file_format . supported_file_extensions :
frontend_argos_supported_files_format . append ( ff )
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 :
2022-02-07 10:45:31 +01:00
frontend_argos_language_source = languages [ 0 ]
2021-01-10 09:07:56 +01:00
if frontend_argos_language_target is None :
2022-02-07 17:22:08 +01:00
if len ( languages ) > = 2 :
2022-02-07 10:45:31 +01:00
frontend_argos_language_target = languages [ 1 ]
else :
frontend_argos_language_target = languages [ 0 ]
2021-05-17 17:41:15 +02:00
api_keys_db = None
2021-01-10 09:07:56 +01:00
2021-05-16 16:57:19 +02:00
if args . req_limit > 0 or args . api_keys or args . daily_req_limit > 0 :
2022-06-21 20:57:32 +02:00
api_keys_db = None
if args . api_keys :
if args . api_keys_remote :
api_keys_db = RemoteDatabase ( args . api_keys_remote )
else :
2022-07-15 13:22:04 +02:00
api_keys_db = Database ( args . api_keys_db_path )
2021-05-17 17:41:15 +02:00
2020-12-20 21:05:22 +01:00
from flask_limiter import Limiter
2021-05-18 05:41:02 +02:00
2020-12-20 21:05:22 +01:00
limiter = Limiter (
app ,
key_func = get_remote_address ,
2021-05-18 05:41:02 +02:00
default_limits = get_routes_limits (
args . req_limit , args . daily_req_limit , api_keys_db
) ,
2022-10-14 19:27:34 +02:00
storage_uri = args . req_limit_storage ,
2020-12-20 21:05:22 +01:00
)
2021-02-26 15:58:29 +01:00
else :
2021-05-18 05:41:02 +02:00
from . no_limiter import Limiter
limiter = Limiter ( )
2021-05-16 17:50:22 +02:00
if args . req_flood_threshold > 0 :
flood . setup ( args . req_flood_threshold )
2020-12-20 21:05:22 +01:00
2021-05-17 17:41:15 +02:00
def access_check ( f ) :
@wraps ( f )
def func ( * a , * * kw ) :
2021-11-05 14:55:45 +01:00
ip = get_remote_address ( )
if flood . is_banned ( ip ) :
2021-05-17 17:41:15 +02:00
abort ( 403 , description = " Too many request limits violations " )
2021-05-18 05:41:02 +02:00
2022-02-20 09:06:29 +01:00
if args . api_keys :
2021-10-26 23:13:51 +02:00
ak = get_req_api_key ( )
2021-05-18 05:41:02 +02:00
if (
2022-02-20 09:06:29 +01:00
ak and api_keys_db . lookup ( ak ) is None
) :
abort (
403 ,
description = " Invalid API key " ,
)
elif (
args . require_api_key_origin
and api_keys_db . lookup ( ak ) is None
and request . headers . get ( " Origin " ) != args . require_api_key_origin
2021-05-18 05:41:02 +02:00
) :
2022-06-21 22:39:08 +02:00
description = " Please contact the server operator to get an API key "
if args . get_api_key_link :
description = " Visit %s to get an API key " % args . get_api_key_link
2021-05-18 05:41:02 +02:00
abort (
403 ,
2022-06-21 22:39:08 +02:00
description = description ,
2021-05-18 05:41:02 +02:00
)
2021-05-17 17:41:15 +02:00
return f ( * a , * * kw )
2021-05-18 05:41:02 +02:00
2021-05-17 17:41:15 +02:00
return func
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 ) :
2021-05-16 17:50:22 +02:00
flood . report ( get_remote_address ( ) )
2020-12-20 21:05:22 +01:00
return jsonify ( { " error " : " Slowdown: " + str ( e . description ) } ) , 429
2021-05-16 17:50:22 +02:00
@app.errorhandler ( 403 )
def denied ( e ) :
return jsonify ( { " error " : str ( e . description ) } ) , 403
2020-12-20 21:05:22 +01:00
@app.route ( " / " )
2021-02-15 19:30:28 +01:00
@limiter.exempt
2020-12-20 21:05:22 +01:00
def index ( ) :
2022-02-07 11:02:32 +01:00
if args . disable_web_ui :
abort ( 404 )
2021-05-18 05:41:02 +02:00
return render_template (
" index.html " ,
gaId = args . ga_id ,
frontendTimeout = args . frontend_timeout ,
api_keys = args . api_keys ,
2022-06-21 21:17:42 +02:00
get_api_key_link = args . get_api_key_link ,
2021-05-18 05:41:02 +02:00
web_version = os . environ . get ( " LT_WEB " ) is not None ,
2021-10-26 21:32:06 +02:00
version = get_version ( )
2021-05-18 05:41:02 +02:00
)
2020-12-20 21:05:22 +01:00
2022-03-26 09:02:54 +01:00
@app.get ( " /javascript-licenses " )
2021-03-31 17:57:02 +02:00
@limiter.exempt
def javascript_licenses ( ) :
2022-02-07 11:02:32 +01:00
if args . disable_web_ui :
abort ( 404 )
2021-05-18 05:41:02 +02:00
return render_template ( " javascript-licenses.html " )
2021-03-31 17:57:02 +02:00
2022-04-03 13:17:13 +02:00
@app.get ( " /languages " )
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 )
2022-12-10 06:21:38 +01:00
targets :
type : array
items :
type : string
description : Supported target language codes
2020-12-20 21:05:22 +01:00
"""
2022-12-09 22:38:58 +01:00
return jsonify ( [ { " code " : l . code , " name " : l . name , " targets " : language_pairs . get ( l . code , [ ] ) } for l in languages ] )
2020-12-20 21:05:22 +01:00
# Add cors
@app.after_request
def after_request ( response ) :
2021-05-18 05:41:02 +02:00
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 )
2020-12-20 21:05:22 +01:00
return response
2022-03-26 09:02:54 +01:00
@app.post ( " /translate " )
2021-05-17 17:41:15 +02:00
@access_check
2020-12-20 21:05:22 +01:00
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-09-11 15:08:57 +02:00
- in : formData
name : format
schema :
type : string
2021-09-11 22:02:10 +02:00
enum : [ text , html ]
default : text
example : text
2021-09-11 15:08:57 +02:00
required : false
2021-09-11 22:02:10 +02:00
description : >
Format of source text :
* ` text ` - Plain text
* ` html ` - HTML markup
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
2021-05-16 17:50:22 +02:00
403 :
description : Banned
schema :
id : error - response
type : object
properties :
error :
type : string
description : Error message
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 )
2021-05-18 05:41:02 +02:00
q = json . get ( " q " )
source_lang = json . get ( " source " )
target_lang = json . get ( " target " )
2021-09-11 15:08:57 +02:00
text_format = json . get ( " format " )
2020-12-20 21:05:22 +01:00
else :
q = request . values . get ( " q " )
source_lang = request . values . get ( " source " )
target_lang = request . values . get ( " target " )
2021-09-11 15:08:57 +02:00
text_format = request . values . get ( " format " )
2020-12-20 21:05:22 +01:00
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-05-18 05:41:02 +02:00
batch_size = len ( q )
if args . batch_limit < batch_size :
abort (
400 ,
description = " Invalid request: Request ( %d ) exceeds text limit ( %d ) "
2021-10-24 18:27:37 +02:00
% ( batch_size , args . batch_limit ) ,
2021-05-18 05:41:02 +02:00
)
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 :
2021-05-18 05:41:02 +02:00
chars = sum ( [ len ( text ) for text in q ] )
2021-01-19 17:51:10 +01:00
else :
2021-05-18 05:41:02 +02:00
chars = len ( q )
2021-01-19 17:51:10 +01:00
2021-02-15 19:30:28 +01:00
if args . char_limit < chars :
2021-05-18 05:41:02 +02:00
abort (
400 ,
description = " Invalid request: Request ( %d ) exceeds character limit ( %d ) "
2021-10-24 18:27:37 +02:00
% ( chars , args . char_limit ) ,
2021-05-18 05:41:02 +02:00
)
2020-12-20 21:05:22 +01:00
2021-05-18 05:41:02 +02:00
if source_lang == " auto " :
2021-08-02 07:06:56 +02:00
source_langs = [ ]
if batch :
2021-10-24 18:27:37 +02:00
auto_detect_texts = q
2021-08-02 07:06:56 +02:00
else :
2021-10-24 18:27:37 +02:00
auto_detect_texts = [ q ]
2021-08-02 07:06:56 +02:00
overall_candidates = detect_languages ( q )
2021-10-24 18:27:37 +02:00
2021-08-02 07:06:56 +02:00
for text_to_check in auto_detect_texts :
2021-10-24 18:27:37 +02:00
if len ( text_to_check ) > 40 :
candidate_langs = detect_languages ( text_to_check )
else :
# Unable to accurately detect languages for short texts
candidate_langs = overall_candidates
2022-03-25 07:19:56 +01:00
source_langs . append ( candidate_langs [ 0 ] )
2021-10-24 18:27:37 +02:00
if args . debug :
print ( text_to_check , candidate_langs )
print ( " Auto detected: %s " % candidate_langs [ 0 ] [ " language " ] )
2021-08-02 07:06:56 +02:00
else :
2021-10-24 18:27:37 +02:00
if batch :
2022-03-25 07:19:56 +01:00
source_langs = [ { " confidence " : 100.0 , " language " : source_lang } for text in q ]
2021-10-24 18:27:37 +02:00
else :
2022-03-25 07:19:56 +01:00
source_langs = [ { " confidence " : 100.0 , " language " : source_lang } ]
2021-01-19 17:51:10 +01:00
2022-03-25 07:19:56 +01:00
src_langs = [ next ( iter ( [ l for l in languages if l . code == source_lang [ " language " ] ] ) , None ) for source_lang in source_langs ]
2021-10-24 18:27:37 +02:00
2021-08-02 07:06:56 +02:00
for idx , lang in enumerate ( src_langs ) :
2021-10-24 18:27:37 +02:00
if lang is None :
abort ( 400 , description = " %s is not supported " % source_langs [ idx ] )
2021-01-13 15:33:58 +01:00
2020-12-20 21:05:22 +01:00
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 tgt_lang is None :
abort ( 400 , description = " %s is not supported " % target_lang )
2021-09-11 15:08:57 +02:00
if not text_format :
text_format = " text "
if text_format not in [ " text " , " html " ] :
abort ( 400 , description = " %s format is not supported " % text_format )
2020-12-20 21:05:22 +01:00
try :
2021-05-18 05:41:02 +02:00
if batch :
2021-08-02 07:06:56 +02:00
results = [ ]
for idx , text in enumerate ( q ) :
2021-10-24 18:27:37 +02:00
translator = src_langs [ idx ] . get_translation ( tgt_lang )
2022-12-10 06:03:21 +01:00
if translator is None :
abort ( 400 , description = " %s ( %s ) is not available as a target language from %s ( %s ) " % ( tgt_lang . name , tgt_lang . code , src_langs [ idx ] . name , src_langs [ idx ] . code ) )
2021-10-24 18:27:37 +02:00
if text_format == " html " :
translated_text = str ( translate_html ( translator , text ) )
else :
2022-11-16 15:31:25 +01:00
translated_text = improve_translation_formatting ( text , translator . translate ( text ) )
2021-09-11 15:08:57 +02:00
2022-02-06 14:54:52 +01:00
results . append ( unescape ( translated_text ) )
2022-03-25 07:19:56 +01:00
if source_lang == " auto " :
return jsonify (
{
" translatedText " : results ,
" detectedLanguage " : source_langs
}
)
else :
return jsonify (
2022-12-09 20:35:39 +01:00
{
2022-03-25 07:19:56 +01:00
" translatedText " : results
2022-12-09 20:35:39 +01:00
}
2022-03-25 07:19:56 +01:00
)
2021-05-18 05:41:02 +02:00
else :
2021-08-02 07:06:56 +02:00
translator = src_langs [ 0 ] . get_translation ( tgt_lang )
2022-12-10 06:03:21 +01:00
if translator is None :
abort ( 400 , description = " %s ( %s ) is not available as a target language from %s ( %s ) " % ( tgt_lang . name , tgt_lang . code , src_langs [ 0 ] . name , src_langs [ 0 ] . code ) )
2021-09-11 15:08:57 +02:00
if text_format == " html " :
2021-09-24 17:30:19 +02:00
translated_text = str ( translate_html ( translator , q ) )
2021-09-11 15:08:57 +02:00
else :
2022-11-16 15:31:25 +01:00
translated_text = improve_translation_formatting ( q , translator . translate ( q ) )
2022-03-25 07:19:56 +01:00
if source_lang == " auto " :
return jsonify (
{
" translatedText " : unescape ( translated_text ) ,
2022-12-09 20:29:44 +01:00
" detectedLanguage " : source_langs [ 0 ]
2022-03-25 07:19:56 +01:00
}
)
else :
return jsonify (
{
" translatedText " : unescape ( translated_text )
}
)
2020-12-20 21:05:22 +01:00
except Exception as e :
abort ( 500 , description = " Cannot translate text: %s " % str ( e ) )
2022-03-26 09:02:54 +01:00
@app.post ( " /translate_file " )
2021-10-24 18:27:37 +02:00
@access_check
def translate_file ( ) :
"""
Translate file from a language to another
- - -
tags :
- translate
consumes :
- multipart / form - data
parameters :
- in : formData
name : file
type : file
required : true
2021-10-26 22:04:50 +02:00
description : File to translate
2021-10-24 18:27:37 +02:00
- 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
- in : formData
name : api_key
schema :
type : string
example : xxxxxxxx - xxxx - xxxx - xxxx - xxxxxxxxxxxx
required : false
description : API key
responses :
200 :
description : Translated file
schema :
2022-10-18 17:41:36 +02:00
id : translate - file
2021-10-24 18:27:37 +02:00
type : object
properties :
2021-10-25 10:50:55 +02:00
translatedFileUrl :
type : string
description : Translated file url
2021-10-24 18:27:37 +02:00
400 :
description : Invalid request
schema :
id : error - response
type : object
properties :
error :
type : string
description : Error message
500 :
description : Translation 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
403 :
description : Banned
schema :
id : error - response
type : object
properties :
error :
type : string
description : Error message
"""
2021-10-25 17:09:23 +02:00
if args . disable_files_translation :
abort ( 403 , description = " Files translation are disabled on this server. " )
2021-10-24 19:14:09 +02:00
source_lang = request . form . get ( " source " )
target_lang = request . form . get ( " target " )
2021-10-24 18:27:37 +02:00
file = request . files [ ' file ' ]
if not file :
2021-10-26 22:04:50 +02:00
abort ( 400 , description = " Invalid request: missing file parameter " )
2021-10-24 18:27:37 +02:00
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 file . filename == ' ' :
abort ( 400 , description = " Invalid request: empty file " )
if os . path . splitext ( file . filename ) [ 1 ] not in frontend_argos_supported_files_format :
abort ( 400 , description = " Invalid request: file format not supported " )
source_langs = [ source_lang ]
src_langs = [ next ( iter ( [ l for l in languages if l . code == source_lang ] ) , None ) for source_lang in source_langs ]
for idx , lang in enumerate ( src_langs ) :
if lang is None :
abort ( 400 , description = " %s is not supported " % source_langs [ idx ] )
tgt_lang = next ( iter ( [ l for l in languages if l . code == target_lang ] ) , None )
if tgt_lang is None :
abort ( 400 , description = " %s is not supported " % target_lang )
try :
filename = str ( uuid . uuid4 ( ) ) + ' . ' + secure_filename ( file . filename )
2021-10-25 11:06:39 +02:00
filepath = os . path . join ( get_upload_dir ( ) , filename )
2021-10-24 18:27:37 +02:00
file . save ( filepath )
translated_file_path = argostranslatefiles . translate_file ( src_langs [ 0 ] . get_translation ( tgt_lang ) , filepath )
2021-10-24 18:38:35 +02:00
translated_filename = os . path . basename ( translated_file_path )
2021-10-25 10:56:17 +02:00
2021-10-24 18:27:37 +02:00
return jsonify (
{
2021-10-26 23:28:59 +02:00
" translatedFileUrl " : url_for ( ' download_file ' , filename = translated_filename , _external = True )
2021-10-24 18:27:37 +02:00
}
)
except Exception as e :
abort ( 500 , description = e )
2022-03-26 09:02:54 +01:00
@app.get ( " /download_file/<string:filename> " )
2021-10-24 18:44:35 +02:00
def download_file ( filename : str ) :
2021-10-24 18:38:35 +02:00
"""
Download a translated file
"""
2021-10-25 17:09:23 +02:00
if args . disable_files_translation :
2021-10-26 21:41:14 +02:00
abort ( 400 , description = " Files translation are disabled on this server. " )
2021-11-05 14:55:45 +01:00
2021-10-25 11:06:39 +02:00
filepath = os . path . join ( get_upload_dir ( ) , filename )
2021-10-26 21:41:14 +02:00
try :
checked_filepath = security . path_traversal_check ( filepath , get_upload_dir ( ) )
if os . path . isfile ( checked_filepath ) :
filepath = checked_filepath
except security . SuspiciousFileOperation :
abort ( 400 , description = " Invalid filename " )
2021-10-24 19:14:09 +02:00
return_data = io . BytesIO ( )
with open ( filepath , ' rb ' ) as fo :
return_data . write ( fo . read ( ) )
return_data . seek ( 0 )
2021-10-25 12:05:39 +02:00
download_filename = filename . split ( ' . ' )
download_filename . pop ( 0 )
download_filename = ' . ' . join ( download_filename )
2021-10-24 18:44:35 +02:00
2022-04-07 15:56:57 +02:00
return send_file ( return_data , as_attachment = True , download_name = download_filename )
2021-10-24 18:38:35 +02:00
2022-03-26 09:02:54 +01:00
@app.post ( " /detect " )
2021-05-17 17:41:15 +02:00
@access_check
2021-02-10 16:51:17 +01:00
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
2021-05-18 05:41:02 +02:00
description : Error message
2021-02-10 16:51:17 +01:00
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
2021-05-16 17:50:22 +02:00
403 :
description : Banned
schema :
id : error - response
type : object
properties :
error :
type : string
description : Error message
2021-02-10 16:51:17 +01:00
"""
2021-05-16 17:50:22 +02:00
if flood . is_banned ( get_remote_address ( ) ) :
abort ( 403 , description = " Too many request limits violations " )
2021-02-10 16:51:17 +01:00
if request . is_json :
2021-03-07 23:23:25 +01:00
json = get_json_dict ( request )
2021-05-18 05:41:02 +02:00
q = json . get ( " q " )
2021-02-10 16:51:17 +01:00
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-11-03 15:39:59 +01:00
apiKeys :
type : boolean
description : Whether the API key database is enabled .
2021-10-30 17:31:50 +02:00
keyRequired :
type : boolean
description : Whether an API key is required .
2021-10-09 16:04:16 +02:00
suggestions :
type : boolean
description : Whether submitting suggestions is enabled .
2021-10-24 16:57:45 +02:00
supportedFilesFormat :
type : array
items :
type : string
description : Supported files format
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-05-18 05:41:02 +02:00
return jsonify (
{
" charLimit " : args . char_limit ,
" frontendTimeout " : args . frontend_timeout ,
2021-11-03 15:39:59 +01:00
" apiKeys " : args . api_keys ,
2021-10-30 17:31:50 +02:00
" keyRequired " : bool ( args . api_keys and args . require_api_key_origin ) ,
2021-10-09 15:45:58 +02:00
" suggestions " : args . suggestions ,
2021-10-25 17:09:23 +02:00
" filesTranslation " : not args . disable_files_translation ,
" supportedFilesFormat " : [ ] if args . disable_files_translation else frontend_argos_supported_files_format ,
2021-05-18 05:41:02 +02:00
" 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 21:05:22 +01:00
2022-03-26 09:02:54 +01:00
@app.post ( " /suggest " )
2021-10-31 06:53:25 +01:00
@access_check
2021-10-09 11:25:56 +02:00
def suggest ( ) :
2021-10-09 16:04:16 +02:00
"""
Submit a suggestion to improve a translation
- - -
tags :
- feedback
parameters :
- in : formData
name : q
schema :
type : string
example : Hello world !
required : true
description : Original text
- in : formData
name : s
schema :
type : string
example : ¡ Hola mundo !
required : true
description : Suggested translation
- in : formData
name : source
schema :
type : string
example : en
required : true
description : Language of original text
- in : formData
name : target
schema :
type : string
example : es
required : true
description : Language of suggested translation
responses :
200 :
description : Success
schema :
id : suggest - response
type : object
properties :
success :
type : boolean
description : Whether submission was successful
403 :
description : Not authorized
schema :
id : error - response
type : object
properties :
error :
type : string
description : Error message
"""
if not args . suggestions :
abort ( 403 , description = " Suggestions are disabled on this server. " )
2021-10-09 15:45:58 +02:00
2021-10-09 11:44:00 +02:00
q = request . values . get ( " q " )
s = request . values . get ( " s " )
source_lang = request . values . get ( " source " )
target_lang = request . values . get ( " target " )
2022-02-20 18:22:12 +01:00
if not q :
abort ( 400 , description = " Invalid request: missing q parameter " )
if not s :
abort ( 400 , description = " Invalid request: missing s 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-10-09 11:44:00 +02:00
SuggestionsDatabase ( ) . add ( q , s , source_lang , target_lang )
2021-10-09 11:25:56 +02:00
return jsonify ( { " success " : True } )
2021-10-09 16:04:16 +02:00
swag = swagger ( app )
2022-12-09 22:36:12 +01:00
swag [ " info " ] [ " version " ] = " 1.3.1 "
2021-10-09 16:04:16 +02:00
swag [ " info " ] [ " title " ] = " LibreTranslate "
@app.route ( " /spec " )
@limiter.exempt
def spec ( ) :
return jsonify ( swag )
2021-05-18 05:41:02 +02:00
SWAGGER_URL = " /docs " # URL for exposing Swagger UI (without trailing '/')
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
2021-05-18 05:41:02 +02:00
swaggerui_blueprint = get_swaggerui_blueprint ( SWAGGER_URL , 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