forked from dmnelson/sentiment-analysis-imdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentiment_analysis.py
43 lines (35 loc) · 1.13 KB
/
sentiment_analysis.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
from flask import Flask, render_template, request, jsonify
from lib.classifier import Classifier
from lib.examples import Examples
import threading
print(" - Starting up application")
lock = threading.Lock()
app = Flask(__name__)
class App:
__shared_state = {}
def __init__(self):
self.__dict__ = self.__shared_state
def classifier(self):
with lock:
if getattr(self, '_classifier', None) == None:
print(" - Building new classifier - might take a while.")
self._classifier = Classifier().build()
print(" - Done!")
return self._classifier
t = threading.Thread(target=App().classifier)
t.daemon = True
t.start()
@app.route('/')
def main():
return render_template('main.html')
@app.route('/predict')
def predict():
q = request.args.get('q')
label, prediction = App().classifier().classify(q)
return jsonify(q=q, predicted_class=label, prediction=prediction)
@app.route('/examples')
def examples():
examples = Examples(App().classifier()).load(5, 5)
return jsonify(items=examples)
if __name__ == '__main__':
app.run(debug=True)