import sys, re from gevent.pywsgi import WSGIServer from flask import Flask, render_template, request from pingcap.pe.flash.util import ( translate_with_terms_dict, load_1gram_terms_en, file_upload_to_s3_with_ts, ) app = Flask(__name__) app.config.update(TEMPLATES_AUTO_RELOAD=True) @app.route("/") def index(): return render_template("index.html") @app.post("/api/translate") def translate_api(): source_text = request.form["source"] lang_option = request.form["lang_option"] terms = request.form["terms"] src_lang, dst_lang = lang_option.split("2") terms_name = "terms-{}-{}-{}".format(terms, src_lang, dst_lang) en_1gram_list = load_1gram_terms_en(terms_name.split("-")[1]) result = [] translation_mute_zone = False for source_line in source_text.split("\n"): line_result = "\n" if ( re.match("^ *``", source_line) and len(re.findall("``", source_line)) % 2 == 1 and translation_mute_zone ): print("A") translation_mute_zone = False line_result = source_line elif ( re.match("^ *``", source_line) and len(re.findall("``", source_line)) % 2 == 1 and not translation_mute_zone ): print("B") translation_mute_zone = True line_result = source_line elif not re.match("^ *``", source_line) and translation_mute_zone: print("C") line_result = source_line elif (not translation_mute_zone) and ( re.match("^[0-9]*$", source_line.strip()) or re.match("^[ -0123456789:,>]*$", source_line.strip()) ): print("D") line_result = source_line elif not translation_mute_zone: print("E") print(lang_option, terms_name) line_result = translate_with_terms_dict( source_line, lang_option, terms_name, en_1gram_list ) result += [line_result] translation_mute_zone = False f = open("./translator.md", "w") f.write(source_text) f.close() # file_upload_to_s3_with_ts('./translator.md', 'tidb-t', 'translator-log') return {"result": "\n".join(result)} http_server = WSGIServer(("0.0.0.0", int(sys.argv[1])), app) http_server.serve_forever()