-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.py
51 lines (34 loc) · 1004 Bytes
/
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
# -*- coding: utf-8 -*-
from flask import Flask, request
import json
import logging
import os
from sklearn.externals import joblib
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
MODEL = os.path.join(APP_ROOT, 'classifier.pkl')
PORT = 5000
app = Flask(__name__)
logging.basicConfig(filename='movie_classifier.log', level=logging.DEBUG)
model = joblib.load(MODEL)
label = {0:'negative', 1:'positive'}
@app.route('/')
def home():
return 'It works.'
def predict(model, text):
return label[model.predict([text])[0]]
@app.route('/review', methods=['GET'])
def extract():
"""Return the movie review sentiment score.
Returns a JSON object :
{
"sentiment": "positive"
}
"""
if request.method == 'GET':
description = request.args.get('text', '')
result = {
'sentiment': predict(model, description)
}
return json.dumps(result)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=PORT)