1
0
mirror of https://github.com/LibreTranslate/LibreTranslate.git synced 2024-11-17 21:10:12 +01:00
LibreTranslate/main.py

73 lines
2.1 KiB
Python
Raw Normal View History

2020-12-20 17:55:56 +01:00
from app.init import boot
2020-12-19 23:40:37 +01:00
import argparse
from flask import Flask, render_template, jsonify, request, abort, send_from_directory
from app.language import languages
parser = argparse.ArgumentParser(description='LibreTranslate - Free and Open Source Translation API')
parser.add_argument('host', type=str,
help='Hostname', default="127.0.0.1")
parser.add_argument('port', type=int,
help='Port', default=5000)
args = parser.parse_args()
boot()
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
@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.route("/")
def index():
return send_from_directory('static', 'index.html')
@app.route("/languages")
def langs():
return jsonify([{'code': l.code, 'name': l.name } for l in languages])
@app.route("/translate", methods=['POST'])
def translate():
2020-12-20 17:55:56 +01: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")
2020-12-19 23:40:37 +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")
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))
if __name__ == "__main__":
app.run(host=args.host)