-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (44 loc) · 1.92 KB
/
Copy pathapp.py
File metadata and controls
64 lines (44 loc) · 1.92 KB
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
import pickle
from config import HOST, PORT, DEBUG, MODEL_PATH, VOCAB_FILE
from flask import Flask, render_template, request
from utils import to_tensor, generate_attention_map, save_attention, beam_search
from models import Seq2Seq
from vocab import Vocab, Vocabularies
from nltk.tokenize import RegexpTokenizer
puncts_except_apostrophe = '!"#$%&\()*+,-./:;<=>?@[\\]^_`{|}~'
TOKENIZE_PATTERN = fr"[{puncts_except_apostrophe}]|\w+|['\w]+"
tokenizer = RegexpTokenizer(pattern=TOKENIZE_PATTERN)
app = Flask(__name__)
@app.route("/")
def main():
return render_template("index.html")
@app.route("/translate", methods=["GET", "POST"])
def translate():
if request.method == "GET":
return render_template("index.html")
elif request.method == "POST":
args = request.form
print(args)
text_input = args["textarea"]
print("Input: ", text_input)
tokenized_sent = tokenizer.tokenize(text_input)
print("Tokenized input: ", tokenized_sent)
with open(VOCAB_FILE, "rb") as f:
vocabs = pickle.load(f)
model = Seq2Seq.load(MODEL_PATH)
model.device = "cpu"
hypothesis = beam_search(model, [tokenized_sent], beam_size=3, max_decoding_time_step=70)[0]
print("Hypothesis")
print(hypothesis)
for i in range(3):
new_target = [['<sos>'] + hypothesis[i].value + ['<eos>']]
a_ts = generate_attention_map(model, vocabs, [tokenized_sent], new_target)
save_attention(tokenized_sent, hypothesis[i].value,
[a[0].detach().cpu().numpy() for a in a_ts[:len(hypothesis[i].value)]],
save_path="static/list_{}.png".format(i))
result_hypothesis = []
for idx, hyp in enumerate(hypothesis):
result_hypothesis.append((idx, " ".join(hyp.value)))
return render_template("index.html", hypothesis=result_hypothesis, sentence=text_input)
if __name__ == "__main__":
app.run(host=HOST, port=PORT, debug=DEBUG)