-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
141 lines (120 loc) · 4.92 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from flask import Flask, request, jsonify
from query_handler import query_handler
from util_handler import util_handler
from sentiment_analysis import sentiment_analysis
from multiprocessing import Process
app = Flask(__name__)
# Make the WSGI interface available at the top level so wfastcgi can get it.
wsgi_app = app.wsgi_app
@app.route('/authtoken', methods=[ 'POST' ])
def AuthToken():
obj = {}
try:
body = request.get_json(force=True)
username = str(body.get('username', ''))
password = str(body.get('password', ''))
valid_for = int(body.get('validfor', 60))
if (username == '' or password == ''):
return("Credentials are empty or missing")
else:
obj_query = query_handler()
query = obj_query.auth(username,password)
obj_util = util_handler()
rows = obj_util.execute(query,False)
result = obj_util.convert_data_to_json(rows)
if (int(result[0]['COUNT']) == 0):
return("Invalid Credentials")
else:
token = obj_util.GetAuthToken(username, password, valid_for)
obj["result"] = {"AccessToken" : token}
except Exception as e:
return(str(e))
return jsonify(obj)
@app.route('/trainsentiments', methods=[ 'POST' ])
def TrainSentiments():
obj = {}
try:
token = request.headers.get('Token','')
if (token != ''):
obj_util = util_handler()
obj_query = query_handler()
cred = obj_util.ExtractAuthToken(token)
if (obj_util.DateValidation(cred[3]) == False):
return("Token Expired ")
query = obj_query.auth(cred[0],cred[1])
rows = obj_util.execute(query,False)
result = obj_util.convert_data_to_json(rows)
if (int(result[0]['COUNT']) == 0):
return("Access Token is Invalid. Please pass {Token: '<Valid Token>'} ")
else:
sa = sentiment_analysis()
training_task = Process(target=sa.process_review_train)
training_task.start()
obj["result"] = "Process for traing sentiments has started."
else:
return("Access Token is missing is the header. Please pass Token: '<Valid Token>' ")
except Exception as e:
return(str(e))
return jsonify(obj)
@app.route('/predictsentiment', methods=[ 'POST' ])
def PredictSentiment():
obj = {}
try:
token = request.headers.get('Token','')
if (token != ''):
obj_util = util_handler()
obj_query = query_handler()
cred = obj_util.ExtractAuthToken(token)
if (obj_util.DateValidation(cred[3]) == False):
return("Token Expired ")
query = obj_query.auth(cred[0],cred[1])
rows = obj_util.execute(query,False)
result = obj_util.convert_data_to_json(rows)
if (int(result[0]['COUNT']) == 0):
return("Access Token is Invalid. Please pass {Token: '<Valid Token>'} ")
else:
body = request.get_json(force=True)
review = str(body.get('review', ''))
if(review != ''):
sa = sentiment_analysis()
result = sa.predict_classification(review)
obj["result"] = result
else:
return("Access Token is missing is the header. Please pass Token: '<Valid Token>' ")
except Exception as e:
return(str(e))
return jsonify(obj)
@app.route('/marksentiments', methods=[ 'POST' ])
def MarkSentiments():
obj = {}
try:
token = request.headers.get('Token','')
if (token != ''):
obj_util = util_handler()
obj_query = query_handler()
cred = obj_util.ExtractAuthToken(token)
if (obj_util.DateValidation(cred[3]) == False):
return("Token Expired ")
query = obj_query.auth(cred[0],cred[1])
rows = obj_util.execute(query,False)
result = obj_util.convert_data_to_json(rows)
if (int(result[0]['COUNT']) == 0):
return("Access Token is Invalid. Please pass {Token: '<Valid Token>'} ")
else:
sa = sentiment_analysis()
training_task = Process(target=sa.mark_sentiment)
training_task.start()
obj["result"] = "Process to mark sentiments has started."
else:
return("Access Token is missing is the header. Please pass Token: '<Valid Token>' ")
except Exception as e:
return(str(e))
return jsonify(obj)
if __name__ == '__main__':
import os
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)