-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
64 lines (49 loc) · 1.76 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
from flask import Flask, request, render_template, jsonify
import random
import text_classifier
def create_app():
"""
App Creation factor
:return: flask app ready to be deployed
"""
app = Flask(__name__)
logprior, loglikelihood = load_model()
@app.route('/', methods=['GET'])
def main():
return render_template('index.html')
@app.route('/hello')
def hello():
return 'Hello, World!'
@app.route('/predict', methods=['POST'])
def predict_review():
# Get the review from the form data
review_text = request.json.get('review')
classification = text_classifier.naive_bayes_predict(
review_text, logprior, loglikelihood)
if classification == 0:
mood = "happy"
prediction = "Awesome! Glad you enjoyed it !"
else:
# Randomly choose a negative sentiment: annoyed or sad
options = [0, 1]
weights = [0.8, 0.2]
chosen_option = random.choices(options, weights=weights, k=1)[0]
if chosen_option == 0:
mood = "annoyed"
prediction = "Sorry you didnt enjoy that movie..."
else:
mood = "angry"
prediction = "I agree. Lets demand a refund!"
# return render_template(
# 'index.html', prediction=prediction, mood=mood)
return jsonify({'prediction': prediction, 'mood': mood})
return app
def load_model():
"""
Load model and return the values needed nicely
:return: logprior and loglikelihood
"""
model = text_classifier.load_model("movie_sentiment_model_parameters.json")
logprior = model['logprior']
loglikelihood = model['loglikelihood']
return logprior, loglikelihood