-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlask_Predict_LocalTesting.py
More file actions
53 lines (45 loc) · 1.48 KB
/
Flask_Predict_LocalTesting.py
File metadata and controls
53 lines (45 loc) · 1.48 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
# Import Library
from flask import Flask,request,jsonify
from tensorflow import keras
import numpy as np
import pickle
import re
from keras.preprocessing.sequence import pad_sequences
from sklearn.preprocessing import LabelEncoder
# Load Data
model = keras.models.load_model('capstone_model.h5')
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
with open('label_encoder.pickle', 'rb') as a:
lb = pickle.load(a)
stopwords = []
with open('stopwords.txt', encoding='utf-8') as f:
for line in f:
stopwords.append(line.strip())
# Function
def clean(a):
temp = [w for w in a.split() if w not in stopwords]
temp = " ".join(temp)
temp = re.sub(r'[.,’"\'-?:!;]', '', temp)
temp = re.sub(r'^whats|^im', '', temp)
temp = temp.strip()
return temp
inp = [0] * 1
# Request
app = Flask(__name__)
@app.route('/')
def index():
return "Hello world"
@app.route('/predict',methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
user_input = request.form.get('user')
user_input = clean(str(user_input))
inp[0] = user_input
return user_input
else:
result = model.predict(pad_sequences(tokenizer.texts_to_sequences([inp[0]]),
truncating='post', maxlen=100))[0]
return jsonify({'Answer':lb.inverse_transform([np.argmax(result)])[0]})
if __name__ == '__main__':
app.run(debug=True)