This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.py
69 lines (51 loc) · 2 KB
/
server.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
57
58
59
60
61
62
63
64
65
66
67
68
69
#encoding: utf-8
import json
from flask import Flask, render_template, request, send_from_directory
from datautils.bpe import BPEApplier, BPERemover
from datautils.moses import SentenceSplitter
# import Tokenizer/Detokenizer/SentenceSplitter from datautils.zh for Chinese
from datautils.pymoses import Detokenizer, Detruecaser, Normalizepunctuation, Tokenizer, Truecaser
from modules.server.transformer import Translator, TranslatorCore
import cnfg.base as cnfg
"""
slang = "de"# source language
tlang = "en"# target language
tcmodel = "path/to/truecase/model/source language"
tmodel = "path/to/translation/model"
srcvcb = "path/to/source/vocabulary"
tgtvcb = "path/to/target/vocabulary"
bpecds = "path/to/source/bpe/codes"
bpevcb = "path/to/source/bpe/vocabulary"
bpethr = 8# bpe threshold
"""
spl = SentenceSplitter(slang)
tok = Tokenizer(slang)
detok = Detokenizer(tlang)
punc_norm = Normalizepunctuation(slang)
truecaser = Truecaser(tcmodel)
detruecaser = Detruecaser()
tran_core = TranslatorCore(tmodel, srcvcb, tgtvcb, cnfg)
bpe = BPEApplier(bpecds, bpevcb, bpethr)
debpe = BPERemover()
trans = Translator(tran_core, spl, tok, detok, bpe, debpe, punc_norm, truecaser, detruecaser)
app = Flask(__name__)
@app.route("/", methods=["GET"])
def translate_form():
return render_template("translate.html")
@app.route("/", methods=["POST"])
def translate_core():
srclang = request.form["src"]
return render_template("translate.html", tgt=trans(srclang), src=srclang)
@app.route("/api", methods=["POST"])
def translate_core_api():
try:
srclang = json.loads(request.get_data())["src"]
except Exception as e:
return json.dumps({"exception": str(e)})
return json.dumps({"tgt": trans(srclang)})
# send everything from client as static content
@app.route("/favicon.ico")
def favicon():
return send_from_directory(app.root_path, "favicon.ico", mimetype="image/vnd.microsoft.icon")
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8888, debug=False, threaded=True, use_reloader=False, use_debugger=False, use_evalex=False)