-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
89 lines (60 loc) · 2.54 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from flask import Flask, render_template, url_for, request, redirect, send_from_directory
import os
import tensorflow as tf
from lstm import EncoderDecoder, Encoder, Decoder, inference
import pickle
import numpy as np
char2idx = pickle.load(open('char2idx.pkl', 'rb'))
idx2char = pickle.load(open('idx2char.pkl', 'rb'))
VOCAB_SIZE = len(char2idx)
ANSWER_MAX_LENGTH = 30
EMBEDDING_SIZE = 512
LSTM_HIDDEN_SIZE = 512
model = EncoderDecoder(input_dim=VOCAB_SIZE, embedding_dim=EMBEDDING_SIZE, hidden_dim=LSTM_HIDDEN_SIZE,
output_dim=VOCAB_SIZE, max_len=ANSWER_MAX_LENGTH)
model.load_weights('20191215_21_03-helen_all_add_subtract/model_weights')
app = Flask(__name__)
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['GET'])
def predict_redirect():
return redirect(url_for('home'))
@app.route('/predict', methods=['POST'])
def predict():
def output_to_tensor(tokens):
tensor_tokens = tf.squeeze(tf.convert_to_tensor(tokens), axis=2)
return tf.transpose(tensor_tokens)
def token_to_text(batch_tensor):
batch_array = batch_tensor.numpy()
text_outputs = []
for sequence_pred in batch_array:
text = ''.join([idx2char[pred] for pred in sequence_pred])
text_outputs.append(text)
return text_outputs
def inference(question):
questions_encoded = [char2idx[q] for q in question]
questions_encoded = np.expand_dims(np.array(questions_encoded), axis=0)
print(questions_encoded)
outputs, output_tokens = model(questions_encoded)
predicted_text = token_to_text(output_to_tensor(output_tokens))[0]
first_stop_token = predicted_text.index('~')
predicted_text = predicted_text[0:first_stop_token]
return predicted_text
message = request.form['message']
message = message.strip()
for letter in message: # remove disallowed characters
if letter not in char2idx.keys():
message = message.replace(letter, '')
data = message
my_prediction = inference(data)
print("MY PREDICTION IS: {}".format(my_prediction))
return render_template('result.html', prediction=my_prediction, input=message)
if __name__ == '__main__':
# server side:
app.run_server(host='0.0.0.0', port=5000, debug=False)
# local:
# app.run(host='0.0.0.0', port=5000, debug=False)