-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask-app.py
56 lines (46 loc) · 1.72 KB
/
flask-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
from flask import Flask, request, jsonify, render_template
import pickle
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
import numpy as np
app = Flask(__name__)
# Load the model
model = load_model("model/")
# Load the tokenizer using pickle
with open('tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
labels = {'anger': 0,
'hate': 6,
'empty': 2,
'sadness': 10,
'neutral': 8,
'worry': 12,
'relief': 9,
'happiness': 5,
'surprise': 11,
'boredom': 1,
'enthusiasm': 3,
'fun': 4,
'love': 7}
def predict_emotion(sentence, model):
maxlen = 178
sequences = tokenizer.texts_to_sequences([sentence])
padded = pad_sequences(sequences, maxlen=maxlen, padding='post', truncating='post')
probabilities = model.predict(padded, verbose=0)
predicted_class_index = np.argmax(probabilities)
predicted_class_probability = probabilities[0][predicted_class_index]
formatted_probability = "{:.2f}".format(predicted_class_probability*100)
predicted_emotion = list(labels.keys())[list(labels.values()).index(predicted_class_index)]
return predicted_emotion, formatted_probability
@app.route('/')
def index():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
sentence = data['sentence']
predicted_emotion, probability = predict_emotion(sentence, model)
return jsonify({"emotion": predicted_emotion, "probability": probability})
if __name__ == '__main__':
app.run(debug=True)