-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
401 lines (335 loc) · 13.9 KB
/
backend.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
from flask import Flask, request, jsonify, g
from flask_pymongo import PyMongo
import numpy as np
from sklearn.metrics import mean_squared_error
import ydata_profiling
from autots import AutoTS
import pandas as pd
from flask_cors import CORS
import json
from datetime import datetime
import base64
import io
import matplotlib.pyplot as plt
import matplotlib
import jwt
from datetime import datetime, timedelta
from functools import wraps
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
matplotlib.use('Agg')
from urllib.parse import quote_plus
# -------------------------------------------------- MONGODB CONNECT FOR PROD ----------------------------------------
# uri = "mongodb+srv://Kishore_97:S@msquantch69@kishorescluster.2dup8o1.mongodb.net/?retryWrites=true&w=majority"
# username = quote_plus("Kishore_97")
# password = quote_plus("S@msquantch69")
# encoded_uri = uri.replace("Kishore_97:S@msquantch69",f"{username}:{password}")
# print(encoded_uri)
# client = MongoClient(encoded_uri, server_api=ServerApi('1'))
# try:
# client.admin.command('ping')
# print("Pinged your deployment. You successfully connected to MongoDB!")
# except Exception as e:
# print(e)
# db = client.get_database("SalesForecast")
#---------------------------------------------------------------------------------------------------------------------
app = Flask(__name__)
CORS(app)
app.config["MONGO_URI"] = "mongodb://localhost:27017/SalesForecast"
app.secret_key = "f5734a02a045495cb47483f3a88594f2"
db = PyMongo(app).db
# predictions_json, full_json, img_data, best_mod, best_par, best_trans, error_rate, EDA, pred_head, full_head, rmse
def token_required(func):
@wraps(func)
def decorated(*args, **kwargs):
# if request.method == 'OPTIONS':
# # Respond to the preflight request
# return '', 200
token = request.headers.get('Authorization')
g.normaltoken = token
#print("-----------from wrapper before trying jwt.decode:", token)
if not token:
return jsonify('token is missing')
else:
try:
decoded_token = jwt.decode(
token, app.secret_key, algorithms=['HS256'])
g.token = decoded_token
except jwt.InvalidSignatureError as e:
#print("------------"+str(e)+"--------------")
return jsonify({"message": "Invalid signature"}), 400, {'Content-Type': 'application/json'}
except jwt.InvalidAlgorithmError as e:
#print("------------"+str(e)+"--------------")
return jsonify({"message": "Invalid algorithm"}), 200, {'Content-Type': 'application/json'}
# except jwt.InvalidTokenError as e:
# #print("------------"+str(e)+"--------------")
# return jsonify({"message": "Invalid token"}), 200, {'Content-Type': 'application/json'}
except jwt.ExpiredSignatureError as e:
#print("------------"+str(e)+"--------------")
return jsonify({"message": "Token expired"}), 200, {'Content-Type': 'application/json'}
except jwt.DecodeError as e:
#print("------------"+str(e)+"--------------")
return jsonify({"message": "Error decoding token"}), 200, {'Content-Type': 'application/json'}
return func(*args, **kwargs)
return decorated
@app.route("/forecast", methods=['GET', 'POST'])
@token_required
def forecast():
# #print(g.normaltoken)
# return jsonify("predict works")
email = g.token['email']
#print(email)
forecast_data = {
'message': '',
'pred_json': {},
'full_json': {},
'img_data': {},
'best_mod': '',
'best_par': '',
'best_trans': '',
'error_rate': '',
'EDA': '',
'pred_head': [],
'full_head': [],
'rmse': ''
}
# #print(type(request.data))
input_data = request.data.decode()
input_data = json.loads(input_data)
filename = input_data['filename']
ds = input_data['df']
target_var = input_data['target']
date_var = input_data['date']
periodicity = input_data['periodicity']
n = input_data["range"]
n = n.strip()
if n == '':
n = '1'
df = []
for row in ds.split('\n'):
subl = []
for cell in row.split(','):
subl.append(cell)
df.append(subl)
#print(df[0])
df = pd.DataFrame(df, columns=df[0])
df.drop(index=0, inplace=True)
EDA = ydata_profiling.ProfileReport(df,tsmode=True).to_html()
df[date_var] = pd.to_datetime(df[date_var])
df.set_index(date_var, inplace=True)
#print(df.dtypes, df.head())
for col in df.columns:
df[col] = pd.to_numeric(df[col], errors='coerce')
#print("df length before astype:", len(df))
df[target_var] = df[target_var].astype('int64')
#print("df length after astype:", len(df))
#print(df.dtypes)
forecast = 0
if periodicity == 'Days':
forecast = int(n) * 1
elif periodicity == 'Weeks':
forecast = int(n) * 7
elif periodicity == 'Months':
forecast = int(n) * 30
elif periodicity == 'Years':
forecast = int(n) * 365
best_mod,best_par,best_trans,error_rate,predictions,back_forecast,rmse,full = predict(df,forecast,target_var)
full_img_mongo = plot_for_mongo(full)
backf_img_mongo = plot_for_mongo(back_forecast)
target_df = pd.DataFrame(predictions[target_var+'_predicted'])
pred_img_mongo = plot_for_mongo(target_df)
mongo_img_data = {
'pred': pred_img_mongo,
'full': full_img_mongo,
'back_forecast': backf_img_mongo
}
predictions.index = predictions.index.map(lambda x: x.strftime("%Y-%m-%d"))
full.index = full.index.map(lambda x: x.strftime("%Y-%m-%d"))
predictions.reset_index(inplace=True)
full.reset_index(inplace=True)
full.rename(columns={'index': date_var}, inplace=True)
predictions.rename(columns={'index': date_var}, inplace=True)
predictions_json = predictions.to_json()
full_json = full.to_json()
pred_head = [i for i in predictions.columns]
full_head = [i for i in full.columns]
#print(pred_head, full_head)
now = datetime.now()
formatted_now = now.strftime("%Y-%m-%d %H:%M")
prediction_length = n + " " + periodicity
db.Forecasts.insert_one({'user': email, 'date and time': formatted_now, 'pred_img': pred_img_mongo, 'full_img': full_img_mongo,
'backf_img': backf_img_mongo, 'pred_json': predictions_json, 'full_json': full_json, 'eda': EDA,
'best_mod': best_mod, 'best_par': best_par, 'best_trans': best_trans, 'error_rate': error_rate,
'rmse': rmse, 'pred_head': pred_head, 'full_head': full_head, 'filename': filename,
'prediction_length': prediction_length, 'target': target_var})
forecast_data['pred_json'] = predictions_json
forecast_data['full_json'] = full_json
forecast_data['img_data'] = mongo_img_data
forecast_data['best_mod'] = best_mod
forecast_data['best_par'] = best_par
forecast_data['best_trans'] = best_trans
forecast_data['error_rate'] = error_rate
forecast_data['EDA'] = EDA
forecast_data['pred_head'] = pred_head
forecast_data['full_head'] = full_head
forecast_data['rmse'] = rmse
forecast_data['message'] = 'token present'
return jsonify(forecast_data), 200, {'Content-Type': 'application/json'}
def predict(df,forecast,target_var):
model = AutoTS(forecast_length=forecast,
frequency='infer',
prediction_interval=1,
ensemble=None,
model_list="fast",
transformer_list="fast",
drop_most_recent=1,
max_generations=0,
num_validations=0,
validation_method="backwards")
if len(df.columns) == 1:
model = model.import_template('uni_var.csv', method='only')
#print('univar init')
elif len(df.columns) > 1:
model = model.import_template('multi_var.csv', method='only')
#print('multivar init')
fitted_model = model.fit(df)
best_mod = fitted_model.best_model_name
best_par = fitted_model.best_model_params
best_trans = fitted_model.best_model_transformation_params
#print(best_mod, best_par, best_trans)
# #print(fitted_model)
error_rate = str(fitted_model.failure_rate)
pred = fitted_model.predict(forecast_length=forecast)
#print(pred.forecast.head())
predictions = pred.forecast
# #print("pred length before astype:", len(predictions))
predictions[target_var] = predictions[target_var].astype('int64')
# #print("pred length after astype:", len(predictions))
for col in predictions.columns:
predictions.rename(columns={col: col+'_predicted'}, inplace=True)
back_f = fitted_model.back_forecast(column=target_var).forecast
#print('back forecast:', len(back_f))
back_f.rename(columns={target_var: target_var +
'_back_forecasted'}, inplace=True)
back_forecast = pd.concat([df[target_var], back_f], axis=1)
if len(df) == len(back_f):
rmse = np.sqrt(mean_squared_error(df[target_var], back_f))
elif len(df) < len(back_f):
rmse = np.sqrt(mean_squared_error(df[target_var], back_f[:(len(df))]))
else:
rmse = np.sqrt(mean_squared_error(
df[target_var][:(len(back_f))], back_f))
# #print("\n" ,predictions)
full = pd.concat([df, predictions], axis=1)
#print(full.tail())
return (best_mod,best_par,best_trans,error_rate,predictions,back_forecast,rmse,full)
def plot_for_mongo(ds):
ds.plot(figsize=(20, 5), legend=True)
buffer = io.BytesIO()
plt.savefig(buffer, format='png')
buffer.seek(0)
res = base64.b64encode(buffer.read()).decode('utf-8')
plt.close()
return res
@app.route("/signup", methods=['GET', 'POST'])
def signup():
data = request.data.decode()
data = json.loads(data)
username = data['username']
password = data['password']
email = data['email']
if (db.Users.find_one({"email": email}) is not None):
return jsonify("User Already exists")
else:
db.Users.insert_one(
{"username": username, "password": password, "email": email})
return jsonify("User Successfully registered")
@app.route("/login", methods=['GET', 'POST'])
def login():
data = request.data.decode()
#print("-------------------"+str(data))
data = json.loads(data)
email = data['email']
password = data['password']
#print(email, password)
user = db.Users.find_one({"email": email})
if (user is not None):
if (user['password'] == password):
token = jwt.encode({
'email': email,
'exp': datetime.utcnow() + timedelta(hours=1)
}, app.secret_key, algorithm="HS256")
username = user['username']
return jsonify({"message": "authenticated", "Authorization": token,'username':username})
else:
return jsonify("incorrect password")
else:
return jsonify("User not found")
@app.route("/decode", methods=['GET', 'POST'])
@token_required
def decode():
#print("---------From decode : g----:", g.normaltoken)
return jsonify({"message":"token valid"})
@app.route("/history", methods=['GET'])
@token_required
def history():
email = g.token['email']
records = db.Forecasts.find({'user': email}, {
'_id': False, 'date and time': True, 'target': True, 'filename': True, 'prediction_length': True})
records_json_str = json.dumps(list(records), default=str)
records_json = json.loads(records_json_str)
return jsonify({"message": "token present", "records": records_json})
@app.route("/record", methods=['POST'])
@token_required
def getrecord():
input_data = request.data.decode()
input_data = json.loads(input_data)
date_and_time = input_data['date and time']
email = g.token['email']
record = db.Forecasts.find_one(
{'user': email, 'date and time': date_and_time},
{'_id': False, 'user': False, 'date and time': False,
'filename': False, 'target': False,'prediction_length':False})
record_json_str = json.dumps(record, default=str)
record_json = json.loads(record_json_str)
img_data = {
'pred': record_json['pred_img'],
'full': record_json['full_img'],
'back_forecast': record_json['backf_img']
}
record_json['img_data'] = img_data
record_json['message'] = 'token present'
record_json['EDA'] = record_json['eda']
record_json.pop('pred_img')
record_json.pop('full_img')
record_json.pop('backf_img')
record_json.pop('eda')
return jsonify(record_json)
@app.route("/profile",methods = ['GET','POST'])
@token_required
def profile():
email = g.token['email']
if request.method == 'GET':
profile = db.Users.find_one({'email':email},{'_id':False})
profile_json_str = json.dumps(profile, default=str)
profile_json = json.loads(profile_json_str)
return jsonify({"message": "token present", "profile": profile_json})
elif request.method == 'POST':
data = request.data.decode()
data = json.loads(data)
db.Users.update_one({"email":email},{ "$set" : {"username":data['username'],"password":data['password']}})
return jsonify({'message':'profile updated successfully'})
@app.route("/deleteRecord",methods=['POST'])
@token_required
def delete_record():
input_data = request.data.decode()
input_data = json.loads(input_data)
date_and_time = input_data['date and time']
email = g.token['email']
try:
db.Forecasts.delete_one({'user': email, 'date and time': date_and_time})
return jsonify({'message':'deletion successful'})
except Exception as e:
return jsonify({'message':e})
if __name__ == "__main__":
app.run()