-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
246 lines (196 loc) · 7.89 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from flask_swagger_ui import get_swaggerui_blueprint
from flask import Flask, request, jsonify, make_response, render_template
from datacleansing import upload_file, cleansing_text
import sqlite3
import pandas as pd
import pickle, re
import numpy as np
from keras.models import load_model
from keras.preprocessing.text import Tokenizer
from keras.utils import pad_sequences
import joblib
# instantiate flask object
app = Flask(__name__)
# set app configs
app.config['JSON_SORT_KEYS'] = False
# flask swagger configs
SWAGGER_URL = '/swagger'
API_URL = '/static/swag.json'
SWAGGERUI_BLUEPRINT = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Data Analyst Sentiment!"
}
)
app.register_blueprint(SWAGGERUI_BLUEPRINT, url_prefix=SWAGGER_URL)
# Database
db = sqlite3.connect('database.db', check_same_thread=False)
db.row_factory = sqlite3.Row
mycursor = db.cursor()
### =================================== HOME PAGE =================================== ###
@app.route("/", methods=['GET','POST'])
def home():
return render_template("index.html", content="Guys")
### ================================= ANALYST SENTIMENT ================================= ###
#CNN
file = open("model/cnn/x_pad_sequences.pickle", 'rb')
feature_file_from_cnn = pickle.load(file)
file.close()
tokenizer = joblib.load('model/cnn/tokenizer.pickle')
model_file_from_cnn = load_model("model/cnn/modelcnn.h5")
def predict_sentiment_cnn(text):
sentiment = ['negative', 'neutral', 'positive']
text = [cleansing_text(text)]
feature = tokenizer.texts_to_sequences(text)
feature = pad_sequences(feature, maxlen=feature_file_from_cnn.shape[1])
prediction = model_file_from_cnn.predict(feature)
get_sentiment = np.argmax(prediction[0])
return sentiment[get_sentiment]
def sentiment_cnn_csv(input_file):
column = input_file.iloc[:, 0]
print(column)
for data_file in column: # Define and execute query for insert cleaned text and sentiment to sqlite database
data_clean = cleansing_text(data_file)
sent = predict_sentiment_cnn(data_clean)
query = "insert into sentiment_cnn (original_text, clean_text, analyst_sentiment) values (?,?,?)"
val = (data_file,data_clean,sent)
db.execute(query, val)
db.commit()
print(data_file)
### INPUT TEXT ###
@app.route("/cnn", methods=['POST'])
def cnn():
original_text = str(request.form["text"]) #get text from user
text = cleansing_text(original_text) #cleaning text
text_sentiment = predict_sentiment_cnn(text)
query = "insert into sentiment_cnn (original_text, clean_text, analyst_sentiment) values (?,?,?)"
variable = (original_text, text, str(text_sentiment))
mycursor.execute(query, variable)
db.commit()
# Define API response
json_response = {
'description': "Analysis Sentiment Success!",
'original_text' : original_text,
'text' : text,
'sentiment' : text_sentiment
}
response_data = jsonify(json_response)
return response_data
@app.route("/cnn", methods = ["GET"])
def get_cnn():
data_query = "select * from sentiment_cnn"
#execute data_query
select_text_from_data_query = mycursor.execute(data_query)
text_sentiment = [dict(cnn_id=row[0], original_text=row[1], clean_text=row[2], analyst_sentiment=row[3])
for row in select_text_from_data_query.fetchall()]
return jsonify(text_sentiment)
### UPLOAD CSV FILE ###
@app.route("/cnn/csv", methods=['POST'])
def cnn_csv():
# Get file
file = request.files['file']
try:
datacsv = pd.read_csv(file, encoding='iso-8859-1')
except:
datacsv = pd.read_csv(file, encoding='utf-8')
# Cleaning file
sentiment_cnn_csv(datacsv)
# Define API response
select_data = db.execute("SELECT * FROM sentiment_cnn")
db.commit
data = [
dict(cnn_id=row[0], original_text=row[1], clean_text=row[2], analyst_sentiment=row[3])
for row in select_data.fetchall()
]
return jsonify(data)
#-----------------------LSTM---------------------------#
file = open("model/lstm/x_pad_sequences.pickle", 'rb')
feature_file_from_lstm = pickle.load(file)
file.close()
tokenizer = joblib.load('model/lstm/tokenizer.pickle')
model_file_from_lstm = load_model("model/lstm/modellstm.h5")
def predict_sentiment_lstm(text):
sentiment = ['negative', 'neutral', 'positive']
text = [cleansing_text(text)]
feature = tokenizer.texts_to_sequences(text)
feature = pad_sequences(feature, maxlen=feature_file_from_lstm.shape[1])
prediction = model_file_from_lstm.predict(feature)
get_sentiment = np.argmax(prediction[0])
return sentiment[get_sentiment]
def sentiment_lstm_csv(input_file):
column = input_file.iloc[:, 0]
print(column)
for data_file in column: # Define and execute query for insert cleaned text and sentiment to sqlite database
data_clean = cleansing_text(data_file)
sent = predict_sentiment_lstm(data_clean)
query = "insert into sentiment_lstm (original_text, clean_text, analyst_sentiment) values (?,?,?)"
val = (data_file,data_clean,sent)
db.execute(query, val)
db.commit()
print(data_file)
@app.route("/lstm", methods=['POST'])
def lstm():
original_text = str(request.form["text"]) #get text from user
text = cleansing_text(original_text) #cleaning text
text_sentiment = predict_sentiment_lstm(text)
query = "insert into sentiment_lstm (original_text, clean_text, analyst_sentiment) values (?,?,?)"
variable = (original_text, text, str(text_sentiment))
mycursor.execute(query, variable)
db.commit()
# Define API response
json_response = {
'description': "Analysis Sentiment Success!",
'original_text' : original_text,
'text' : text,
'sentiment' : text_sentiment
}
response_data = jsonify(json_response)
return response_data
@app.route("/lstm", methods = ["GET"])
def get_lstm():
data_query = "select * from sentiment_lstm"
#execute data_query
select_text_from_data_query = mycursor.execute(data_query)
text_sentiment = [dict(lstm_id=row[0], original_text=row[1], clean_text=row[2], analyst_sentiment=row[3])
for row in select_text_from_data_query.fetchall()]
return jsonify(text_sentiment)
### UPLOAD CSV FILE ###
@app.route("/lstm/csv", methods=['POST'])
def lstm_csv():
# Get file
file = request.files['file']
try:
datacsv = pd.read_csv(file, encoding='iso-8859-1')
except:
datacsv = pd.read_csv(file, encoding='utf-8')
# Cleaning file
sentiment_lstm_csv(datacsv)
# Define API response
select_data = db.execute("SELECT * FROM sentiment_lstm")
db.commit
data = [
dict(lstm_id=row[0], original_text=row[1], clean_text=row[2], analyst_sentiment=row[3])
for row in select_data.fetchall()
]
return jsonify(data)
### ================================= ERROR HANDLING ================================= ###
@app.errorhandler(400)
def handle_400_error(_error):
"Return a http 400 error to client"
return make_response(jsonify({'error': 'Misunderstood'}), 400)
@app.errorhandler(401)
def handle_401_error(_error):
"Return a http 401 error to client"
return make_response(jsonify({'error': 'Unauthorised'}), 401)
@app.errorhandler(404)
def handle_404_error(_error):
"Return a http 404 error to client"
return make_response(jsonify({'error': 'Not found'}), 404)
@app.errorhandler(500)
def handle_500_error(_error):
"Return a http 500 error to client"
return make_response(jsonify({'error': 'Server error'}), 500)
#Run Server
if __name__ == '__main__':
app.run(debug=True)