-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
339 lines (316 loc) · 11.4 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from flask import Flask, jsonify, request
from flasgger import Swagger, swag_from, LazyString, LazyJSONEncoder
import numpy as np
import pickle, re
import sqlite3
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from keras.models import load_model
app = Flask(__name__)
app.json_encoder = LazyJSONEncoder
DATABASE = 'data.db'
def create_table():
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS sentiment_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT,
sentiment TEXT,
binary_data BLOB
)
''')
conn.commit()
conn.close()
create_table()
def recreate_table():
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute('''DROP TABLE IF EXISTS sentiment_data''')
cursor.execute('''
CREATE TABLE sentiment_data (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT,
sentiment TEXT,
binary_data BLOB
)
''')
conn.commit()
conn.close()
'''
recreate_table() call this only if the view_database endpoint breaks,
after that comment it again and recall the create_table() function to make a new table
'''
swagger_template = dict(
info={
'title': "API Documentation for Deep Learning",
'version': "1.0.0",
'description': "Neural Network and LSTM"
}
)
swagger_config = {
"headers": [],
"specs": [
{
"endpoint": 'docs',
"route": '/docs.json',
}
],
"static_url_path": "/flasgger_static",
"swagger_ui": True,
"specs_route": "/docs/"
}
swagger = Swagger(app, template=swagger_template, config=swagger_config)
# parameters for feature extraction
max_features = 100000
tokenizer = Tokenizer(num_words=max_features, split=' ', lower=True)
# sentiment labels
sentiment = ['negative', 'neutral', 'positive']
# cleansing
def cleansing(sent):
string = sent.lower()
string = re.sub(r'[^a-zA-Z0-9]', ' ', string)
return string
file = open("lstm_x_pad_sequences.pickle", "rb")
feature_file_from_lstm = pickle.load(file)
file.close()
model_file_from_lstm = load_model("model_lstm.h5")
file = open("rnn_x_pad_sequences.pickle", "rb")
feature_file_from_rnn = pickle.load(file)
file.close()
model_file_from_rnn = load_model("model_rnn.h5")
count_vect = pickle.load(open("feature.p", "rb"))
model = pickle.load(open("model.p", "rb"))
# lstm text input
@swag_from("lstm.yml", methods=['POST'])
@app.route('/lstm', methods=['POST'])
def lstm():
try:
original_text = request.form.get('text')
text = cleansing(original_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 = sentiment[np.argmax(prediction[0])]
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("INSERT INTO sentiment_data (text, sentiment) VALUES (?, ?)", (text, get_sentiment))
conn.commit()
conn.close()
json_response = {
"status_code": 200,
"description": "Result of Sentiment Analysis using LSTM",
"data": {
"text": original_text,
"sentiment": get_sentiment
},
}
response_data = jsonify(json_response)
return response_data
except Exception as e:
error_response = {
"status_code": 500,
"error": str(e)
}
response_data = jsonify(error_response)
return response_data, 500
# lstm file input
@swag_from("lstmfile.yml", methods=['POST'])
@app.route('/lstmfile', methods=['POST'])
def lstmfile():
try:
file = request.files.get('file')
if not file:
return jsonify({'error': 'No file uploaded'}), 400
pretext = file.read().decode('utf-8')
text = cleansing(pretext)
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 = sentiment[np.argmax(prediction[0])]
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("INSERT INTO sentiment_data (text, sentiment) VALUES (?, ?)", (text, get_sentiment))
conn.commit()
conn.close()
json_response = {
"status_code": 200,
"description": "Result of Sentiment Analysis using LSTM",
"data": {
"text": pretext,
"sentiment": get_sentiment
},
}
response_data = jsonify(json_response)
return response_data
except Exception as e:
error_response = {
"status_code": 500,
"error": str(e)
}
response_data = jsonify(error_response)
return response_data, 500
# rnn text input
@swag_from("rnn.yml", methods=['POST'])
@app.route('/rnn', methods=['POST'])
def rnn():
try:
original_text = request.form.get('text')
text = cleansing(original_text)
feature = tokenizer.texts_to_sequences(text)
feature = pad_sequences(feature, maxlen=feature_file_from_rnn.shape[1])
prediction = model_file_from_rnn.predict(feature)
get_sentiment = sentiment[np.argmax(prediction[0])]
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("INSERT INTO sentiment_data (text, sentiment) VALUES (?, ?)", (text, get_sentiment))
conn.commit()
conn.close()
json_response = {
"status_code": 200,
"description": "Result of Sentiment Analysis using RNN",
"data": {
"text": original_text,
"sentiment": get_sentiment
},
}
response_data = jsonify(json_response)
return response_data
except Exception as e:
error_response = {
"status_code": 500,
"error": str(e)
}
response_data = jsonify(error_response)
return response_data, 500
# rnn file input
@swag_from("rnnfile.yml", methods=['POST'])
@app.route('/rnnfile', methods=['POST'])
def rnnfile():
try:
file = request.files.get('file')
if not file:
return jsonify({'error': 'No file uploaded'}), 400
pretext = file.read().decode('utf-8')
text = cleansing(pretext)
feature = tokenizer.texts_to_sequences(text)
feature = pad_sequences(feature, maxlen=feature_file_from_rnn.shape[1])
prediction = model_file_from_rnn.predict(feature)
get_sentiment = sentiment[np.argmax(prediction[0])]
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("INSERT INTO sentiment_data (text, sentiment) VALUES (?, ?)", (text, get_sentiment))
conn.commit()
conn.close()
json_response = {
"status_code": 200,
"description": "Result of Sentiment Analysis using RNN",
"data": {
"text": pretext,
"sentiment": get_sentiment
},
}
response_data = jsonify(json_response)
return response_data
except Exception as e:
error_response = {
"status_code": 500,
"error": str(e)
}
response_data = jsonify(error_response)
return response_data, 500
@swag_from('nn.yml', methods=['POST'])
@app.route('/nn', methods=['POST'])
def nn():
try:
# Get the original text from the request
original_text = request.form.get('text')
# Clean and preprocess the text data
text = cleansing(original_text)
# Transform the text data using the CountVectorizer and convert to dense array
text_features = count_vect.transform([text]).toarray()
# Make predictions using the pre-trained neural network model
prediction = model.predict(text_features)[0]
get_sentiment = sentiment[np.argmax(prediction)]
# Store the result in the database
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("INSERT INTO sentiment_data (text, sentiment) VALUES (?, ?)", (text, get_sentiment))
conn.commit()
conn.close()
# Prepare the response JSON
json_response = {
"status_code": 200,
"description": "Result of Sentiment Analysis using MLPClassifier",
"data": {
"text": original_text,
"sentiment": get_sentiment
},
}
response_data = jsonify(json_response)
return response_data
except Exception as e:
error_response = {
"status_code": 500,
"error": str(e)
}
response_data = jsonify(error_response)
return response_data, 500
@swag_from('nnfile.yml', methods=['POST'])
@app.route('/nnfile', methods=['POST'])
def nnfile():
try:
file = request.files.get('file')
if not file:
return jsonify({'error': 'No file uploaded'}), 400
pretext = file.read().decode('utf-8')
text = cleansing(pretext)
text = count_vect.transform([text]).toarray()
prediction = model.predict(text)[0]
get_sentiment = sentiment[np.argmax(prediction)]
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("INSERT INTO sentiment_data (text, sentiment) VALUES (?, ?)", (text, get_sentiment))
conn.commit()
conn.close()
json_response = {
"status_code": 200,
"description": "Result of Sentiment Analysis using MLPClassifier",
"data": {
"text": pretext,
"sentiment": get_sentiment
},
}
response_data = jsonify(json_response)
return response_data
except Exception as e:
error_response = {
"status_code": 500,
"error": str(e)
}
response_data = jsonify(error_response)
return response_data, 500
@swag_from('view_database.yml', methods=['GET'])
@app.route('/view_database', methods=['GET'])
def view_database():
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("SELECT text, sentiment, binary_data FROM sentiment_data")
data = cursor.fetchall()
conn.close()
database_data = [{'text': row[0], 'sentiment': row[1]} for row in data]
for item in database_data:
if len(item) > 2:
item['binary_data'] = base64.b64encode(item[2]).decode('utf-8')
return jsonify({'data': database_data})
@swag_from('clear_database.yml', methods=['POST'])
@app.route('/clear_database', methods=['POST'])
def clear_database():
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("DELETE FROM sentiment_data")
conn.commit()
conn.close()
return jsonify({'message': 'Database cleared successfully'})
if __name__ == '__main__':
app.run()