mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-22 07:51:00 +00:00
Add file translation
This commit is contained in:
parent
82c2f4396d
commit
0b14600199
2 changed files with 195 additions and 36 deletions
147
app/app.py
147
app/app.py
|
@ -1,19 +1,22 @@
|
|||
import os
|
||||
import tempfile
|
||||
import uuid
|
||||
from functools import wraps
|
||||
|
||||
import argostranslatefiles
|
||||
import pkg_resources
|
||||
from argostranslatefiles import get_supported_formats
|
||||
from flask import Flask, abort, jsonify, render_template, request
|
||||
from flask_swagger import swagger
|
||||
from flask_swagger_ui import get_swaggerui_blueprint
|
||||
from translatehtml import translate_html
|
||||
from werkzeug.utils import secure_filename
|
||||
|
||||
from app import flood
|
||||
from app.language import detect_languages, transliterate
|
||||
|
||||
from .api_keys import Database
|
||||
from .suggestions import Database as SuggestionsDatabase
|
||||
|
||||
from argostranslatefiles import get_supported_formats
|
||||
from translatehtml import translate_html
|
||||
|
||||
def get_json_dict(request):
|
||||
d = request.get_json()
|
||||
|
@ -427,7 +430,6 @@ def create_app(args):
|
|||
if text_format not in ["text", "html"]:
|
||||
abort(400, description="%s format is not supported" % text_format)
|
||||
|
||||
|
||||
try:
|
||||
if batch:
|
||||
results = []
|
||||
|
@ -460,6 +462,143 @@ def create_app(args):
|
|||
except Exception as e:
|
||||
abort(500, description="Cannot translate text: %s" % str(e))
|
||||
|
||||
@app.route("/translate_file", methods=["POST"])
|
||||
@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
|
||||
description: file 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
|
||||
- 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:
|
||||
id: translate
|
||||
type: object
|
||||
properties:
|
||||
translatedText:
|
||||
oneOf:
|
||||
- type: string
|
||||
- type: array
|
||||
description: Translated text(s)
|
||||
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
|
||||
"""
|
||||
if request.is_json:
|
||||
json = get_json_dict(request)
|
||||
source_lang = json.get("source")
|
||||
target_lang = json.get("target")
|
||||
else:
|
||||
source_lang = request.values.get("source")
|
||||
target_lang = request.values.get("target")
|
||||
|
||||
file = request.files['file']
|
||||
|
||||
if not file:
|
||||
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 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)
|
||||
filepath = os.path.join(tempfile.gettempdir(), filename)
|
||||
|
||||
file.save(filepath)
|
||||
|
||||
translated_file_path = argostranslatefiles.translate_file(src_langs[0].get_translation(tgt_lang), filepath)
|
||||
|
||||
return jsonify(
|
||||
{
|
||||
"translatedFileUrl": translated_file_path
|
||||
}
|
||||
)
|
||||
except Exception as e:
|
||||
abort(500, description=e)
|
||||
|
||||
@app.route("/detect", methods=["POST"])
|
||||
@access_check
|
||||
def detect():
|
||||
|
|
|
@ -33,7 +33,8 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||
|
||||
supportedFilesFormat : [],
|
||||
translationType: "text",
|
||||
inputFile: false
|
||||
inputFile: false,
|
||||
loadingFileTranslation: false,
|
||||
},
|
||||
mounted: function(){
|
||||
var self = this;
|
||||
|
@ -319,6 +320,25 @@ document.addEventListener('DOMContentLoaded', function(){
|
|||
},
|
||||
translateFile: function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
let translateFileRequest = new XMLHttpRequest();
|
||||
|
||||
translateFileRequest.open("POST", BaseUrl + "/translate_file", true);
|
||||
translateFileRequest.setRequestHeader("Content-type", "multipart/form-data");
|
||||
|
||||
let formdata = new FormData();
|
||||
formdata.append("file", this.inputFile);
|
||||
|
||||
translateFileRequest.send(formdata);
|
||||
|
||||
this.loadingFileTranslation = true
|
||||
|
||||
translateFileRequest.onreadystatechange = function () {
|
||||
if (xhr.readyState == 4 && xhr.status == 200) {
|
||||
this.loadingFileTranslation = false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue