mirror of
https://github.com/LibreTranslate/LibreTranslate.git
synced 2024-11-21 15:31:00 +00:00
Swagger docs, fix links, character limits
This commit is contained in:
parent
e9609ddd64
commit
c5863fcc84
3 changed files with 196 additions and 35 deletions
180
main.py
180
main.py
|
@ -2,12 +2,16 @@ from app.init import boot
|
||||||
import argparse
|
import argparse
|
||||||
from flask import Flask, render_template, jsonify, request, abort, send_from_directory
|
from flask import Flask, render_template, jsonify, request, abort, send_from_directory
|
||||||
from app.language import languages
|
from app.language import languages
|
||||||
|
from flask_swagger import swagger
|
||||||
|
from flask_swagger_ui import get_swaggerui_blueprint
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API')
|
parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API')
|
||||||
parser.add_argument('host', type=str,
|
parser.add_argument('host', type=str,
|
||||||
help='Hostname', default="127.0.0.1")
|
help='Hostname', default="127.0.0.1")
|
||||||
parser.add_argument('port', type=int,
|
parser.add_argument('port', type=int,
|
||||||
help='Port', default=5000)
|
help='Port', default=5000)
|
||||||
|
parser.add_argument('--char-limit', default=-1,
|
||||||
|
help='Character limit')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
boot()
|
boot()
|
||||||
|
@ -27,46 +31,166 @@ def server_error(e):
|
||||||
def index():
|
def index():
|
||||||
return send_from_directory('static', 'index.html')
|
return send_from_directory('static', 'index.html')
|
||||||
|
|
||||||
|
|
||||||
@app.route("/languages")
|
@app.route("/languages")
|
||||||
def langs():
|
def langs():
|
||||||
return jsonify([{'code': l.code, 'name': l.name } for l in languages])
|
"""
|
||||||
|
Retrieve list of supported languages
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- translate
|
||||||
|
responses:
|
||||||
|
200:
|
||||||
|
description: List of languages
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: array
|
||||||
|
items:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
code:
|
||||||
|
type: string
|
||||||
|
description: Language code
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
description: Human-readable language name (in English)
|
||||||
|
charLimit:
|
||||||
|
type: string
|
||||||
|
description: Character input limit for this language (-1 indicates no limit)
|
||||||
|
"""
|
||||||
|
return jsonify([{'code': l.code, 'name': l.name, 'charLimit': args.char_limit } for l in languages])
|
||||||
|
|
||||||
|
# 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'])
|
@app.route("/translate", methods=['POST'])
|
||||||
def translate():
|
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
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
translatedText:
|
||||||
|
type: string
|
||||||
|
description: Translated text
|
||||||
|
400:
|
||||||
|
description: Invalid request
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
error:
|
||||||
|
type: string
|
||||||
|
description: Error message
|
||||||
|
500:
|
||||||
|
description: Translation error
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
error:
|
||||||
|
type: string
|
||||||
|
description: Error message
|
||||||
|
"""
|
||||||
|
|
||||||
if request.is_json:
|
if request.is_json:
|
||||||
json = request.get_json()
|
json = request.get_json()
|
||||||
q = json.get('q')
|
q = json.get('q')
|
||||||
source_lang = json.get('source')
|
source_lang = json.get('source')
|
||||||
target_lang = json.get('target')
|
target_lang = json.get('target')
|
||||||
else:
|
else:
|
||||||
q = request.values.get("q")
|
q = request.values.get("q")
|
||||||
source_lang = request.values.get("source")
|
source_lang = request.values.get("source")
|
||||||
target_lang = request.values.get("target")
|
target_lang = request.values.get("target")
|
||||||
|
|
||||||
if not q:
|
if not q:
|
||||||
abort(400, description="Invalid request: missing q parameter")
|
abort(400, description="Invalid request: missing q parameter")
|
||||||
if not source_lang:
|
if not source_lang:
|
||||||
abort(400, description="Invalid request: missing source parameter")
|
abort(400, description="Invalid request: missing source parameter")
|
||||||
if not target_lang:
|
if not target_lang:
|
||||||
abort(400, description="Invalid request: missing target parameter")
|
abort(400, description="Invalid request: missing target parameter")
|
||||||
|
|
||||||
|
if args.char_limit != -1:
|
||||||
|
q = q[:args.char_limit]
|
||||||
|
|
||||||
|
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:
|
||||||
|
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))
|
||||||
|
|
||||||
|
|
||||||
src_lang = next(iter([l for l in languages if l.code == source_lang]), None)
|
swag = swagger(app)
|
||||||
tgt_lang = next(iter([l for l in languages if l.code == target_lang]), None)
|
swag['info']['version'] = "1.0"
|
||||||
|
swag['info']['title'] = "LibreTranslate"
|
||||||
|
|
||||||
if src_lang is None:
|
@app.route("/spec")
|
||||||
abort(400, description="%s is not supported" % source_lang)
|
def spec():
|
||||||
if tgt_lang is None:
|
return jsonify(swag)
|
||||||
abort(400, description="%s is not supported" % target_lang)
|
|
||||||
|
|
||||||
translator = src_lang.get_translation(tgt_lang)
|
SWAGGER_URL = '/docs' # URL for exposing Swagger UI (without trailing '/')
|
||||||
try:
|
API_URL = 'http://petstore.swagger.io/v2/swagger.json' # Our API url (can of course be a local resource)
|
||||||
return jsonify({"translatedText": translator.translate(q) })
|
|
||||||
except Exception as e:
|
|
||||||
abort(500, description="Cannot translate text: %s" % str(e))
|
|
||||||
|
|
||||||
|
# Call factory function to create our blueprint
|
||||||
|
swaggerui_blueprint = get_swaggerui_blueprint(
|
||||||
|
SWAGGER_URL,
|
||||||
|
"",
|
||||||
|
config={ # Swagger UI config overrides
|
||||||
|
'app_name': "LibreTranslate",
|
||||||
|
"spec": swag
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
app.register_blueprint(swaggerui_blueprint)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run(host=args.host)
|
app.run(host=args.host)
|
||||||
|
|
|
@ -1,2 +1,4 @@
|
||||||
argostranslate==1.0.3
|
argostranslate==1.0.3
|
||||||
Flask==1.1.2
|
Flask==1.1.2
|
||||||
|
flask-swagger==0.2.14
|
||||||
|
flask-swagger-ui==3.36.0
|
||||||
|
|
|
@ -21,22 +21,27 @@
|
||||||
overflow: auto;
|
overflow: auto;
|
||||||
font-family: monospace;
|
font-family: monospace;
|
||||||
min-height: 280px;
|
min-height: 280px;
|
||||||
|
width: 100%;
|
||||||
|
overflow: auto;
|
||||||
}
|
}
|
||||||
.progress.translate{
|
.progress.translate{
|
||||||
position: absolute;
|
position: absolute;
|
||||||
}
|
}
|
||||||
|
.card.horizontal .card-stacked{
|
||||||
|
overflow: auto;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<nav class="blue lighten-1" role="navigation">
|
<nav class="blue lighten-1" role="navigation">
|
||||||
<div class="nav-wrapper container"><a id="logo-container" href="#" class="brand-logo"><i class="material-icons">translate</i> LibreTranslate</a>
|
<div class="nav-wrapper container"><a id="logo-container" href="/" class="brand-logo"><i class="material-icons">translate</i> LibreTranslate</a>
|
||||||
<ul class="right hide-on-med-and-down">
|
<ul class="right hide-on-med-and-down">
|
||||||
<li><a href="https://github.com/uav4geo/LibreTranslate">Documentation</a></li>
|
<li><a href="/docs">API Docs</a></li>
|
||||||
<li><a href="https://github.com/uav4geo/LibreTranslate">GitHub</a></li>
|
<li><a href="https://github.com/uav4geo/LibreTranslate">GitHub</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<ul id="nav-mobile" class="sidenav">
|
<ul id="nav-mobile" class="sidenav">
|
||||||
<li><a href="https://github.com/uav4geo/LibreTranslate">Documentation</a></li>
|
<li><a href="/docs">API Docs</a></li>
|
||||||
<li><a href="https://github.com/uav4geo/LibreTranslate">GitHub</a></li>
|
<li><a href="https://github.com/uav4geo/LibreTranslate">GitHub</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
<a href="#" data-target="nav-mobile" class="sidenav-trigger"><i class="material-icons">menu</i></a>
|
<a href="#" data-target="nav-mobile" class="sidenav-trigger"><i class="material-icons">menu</i></a>
|
||||||
|
@ -61,7 +66,24 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div v-else-if="error">
|
<div v-else-if="error">
|
||||||
{{ error }}: TODO
|
<div class="section no-pad-bot">
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col s12 m7">
|
||||||
|
<div class="card horizontal">
|
||||||
|
<div class="card-stacked">
|
||||||
|
<div class="card-content">
|
||||||
|
<i class="material-icons">warning</i><p> {{ error }}</p>
|
||||||
|
</div>
|
||||||
|
<div class="card-action">
|
||||||
|
<a href="#" @click="dismissError">Dismiss</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<div class="section no-pad-bot">
|
<div class="section no-pad-bot">
|
||||||
|
@ -95,6 +117,9 @@
|
||||||
<div class="input-field col s6">
|
<div class="input-field col s6">
|
||||||
<textarea id="textarea1" class="materialize-textarea" v-model="inputText" @input="handleInput" ref="inputTextarea"></textarea>
|
<textarea id="textarea1" class="materialize-textarea" v-model="inputText" @input="handleInput" ref="inputTextarea"></textarea>
|
||||||
<label for="textarea1">Input Text</label>
|
<label for="textarea1">Input Text</label>
|
||||||
|
<div v-if="charactersLimit !== -1">
|
||||||
|
<label>{{ inputText.length }} / {{ charactersLimit }}</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="input-field col s6">
|
<div class="input-field col s6">
|
||||||
<div>
|
<div>
|
||||||
|
@ -121,13 +146,13 @@
|
||||||
<div class="card-stacked">
|
<div class="card-stacked">
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
<div class="row center">
|
<div class="row center">
|
||||||
<div class="col s12 m6 left-align">
|
<div class="col s12 m12 l6 left-align">
|
||||||
<p>Request</p>
|
<p>Request</p>
|
||||||
<p>
|
<p>
|
||||||
<pre class="code"><code class="language-javascript" v-html="$options.filters.highlight(requestCode)">
|
<pre class="code"><code class="language-javascript" v-html="$options.filters.highlight(requestCode)">
|
||||||
</code></pre></p>
|
</code></pre></p>
|
||||||
</div>
|
</div>
|
||||||
<div class="col s12 m6 left-align">
|
<div class="col s12 m12 l6 left-align">
|
||||||
<p>Response</p>
|
<p>Response</p>
|
||||||
<pre class="code"><code class="language-javascript" v-html="$options.filters.highlight(output)">
|
<pre class="code"><code class="language-javascript" v-html="$options.filters.highlight(output)">
|
||||||
</code></pre>
|
</code></pre>
|
||||||
|
@ -200,7 +225,8 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
loadingTranslation: false,
|
loadingTranslation: false,
|
||||||
inputText: "",
|
inputText: "",
|
||||||
translatedText: "",
|
translatedText: "",
|
||||||
output: ""
|
output: "",
|
||||||
|
charactersLimit: -1,
|
||||||
},
|
},
|
||||||
mounted: function(){
|
mounted: function(){
|
||||||
var self = this;
|
var self = this;
|
||||||
|
@ -219,6 +245,8 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
|
|
||||||
self.sourceLang = self.langs[0].code;
|
self.sourceLang = self.langs[0].code;
|
||||||
self.targetLang = self.langs[1].code;
|
self.targetLang = self.langs[1].code;
|
||||||
|
self.charactersLimit = self.langs[0].charLimit;
|
||||||
|
// TODO: update this when switching languages
|
||||||
|
|
||||||
// Set Spanish
|
// Set Spanish
|
||||||
for (var i = 1; i < self.langs.length; i++){
|
for (var i = 1; i < self.langs.length; i++){
|
||||||
|
@ -251,6 +279,10 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
this.$refs.inputTextarea.style.height = Math.max(150, this.$refs.inputTextarea.scrollHeight) + "px";
|
this.$refs.inputTextarea.style.height = Math.max(150, this.$refs.inputTextarea.scrollHeight) + "px";
|
||||||
this.$refs.translatedTextarea.style.height = Math.max(150, this.$refs.translatedTextarea.scrollHeight) + "px";
|
this.$refs.translatedTextarea.style.height = Math.max(150, this.$refs.translatedTextarea.scrollHeight) + "px";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.charactersLimit !== -1 && this.inputText.length >= this.charactersLimit){
|
||||||
|
this.inputText = this.inputText.substring(0, this.charactersLimit);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
requestCode: function(){
|
requestCode: function(){
|
||||||
|
@ -291,6 +323,9 @@ document.addEventListener('DOMContentLoaded', function(){
|
||||||
this.translatedText = "";
|
this.translatedText = "";
|
||||||
this.handleInput();
|
this.handleInput();
|
||||||
},
|
},
|
||||||
|
dismissError: function(){
|
||||||
|
this.error = '';
|
||||||
|
},
|
||||||
handleInput: function(e){
|
handleInput: function(e){
|
||||||
if (this.timeout) clearTimeout(this.timeout);
|
if (this.timeout) clearTimeout(this.timeout);
|
||||||
this.timeout = null;
|
this.timeout = null;
|
||||||
|
|
Loading…
Reference in a new issue