-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
56 lines (43 loc) · 1.32 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from flask import Flask, request, jsonify
from translate import Translator
from config import *
app = Flask(__name__)
translator = Translator(MODEL_PATH)
app.config["DEBUG"] = False
def return_success(data):
return jsonify({
"data": data
})
def return_error(err_code, err_msg):
return jsonify({
"error": {
"code": err_code,
"message": err_msg,
}
})
@app.route('/', methods=["GET"])
def health_check():
return return_success("Service is up and running")
@app.route('/lang_routes', methods = ["GET"])
def get_lang_route():
lang = request.args['lang']
all_langs = translator.get_supported_langs()
lang_routes = [l for l in all_langs if l[0] == lang]
return return_success(lang_routes)
@app.route('/supported_languages', methods=["GET"])
def get_supported_languages():
langs = translator.get_supported_langs()
return return_success(langs)
@app.route('/translate', methods=["POST"])
def get_prediction():
source = request.json['source']
target = request.json['target']
text = request.json['text']
chunk_num = request.json['chunk_num']
return return_success({
"text": translator.translate(source, target, text),
"chunk_num": chunk_num,
})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000)