-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py.old
41 lines (32 loc) · 1.04 KB
/
app.py.old
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
from flask import Flask, render_template, request, jsonify
from lstm_model import LSTMModel
import os
from flask_socketio import SocketIO
socketio = SocketIO(app)
app = Flask(__name__)
model_path = "./model.h5"
tokenizer_path = "./tokenizer.json"
if os.path.exists(model_path) and os.path.exists(tokenizer_path):
model = LSTMModel(model_path=model_path, tokenizer_path=tokenizer_path)
else:
model = LSTMModel()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
input_text = request.form['input_text']
prediction = model.predict_next_word(input_text)
return jsonify(prediction=prediction)
@app.route('/train', methods=['POST'])
def train():
text_data = request.form['training_data'].split("\n")
model.train(text_data)
return jsonify(success=True)
@app.route('/retrain', methods=['POST'])
def retrain():
new_text = request.form['new_text']
model.train([new_text])
return jsonify(success=True)
if __name__ == '__main__':
app.run()