|
@@ -0,0 +1,571 @@
|
|
1
|
+from flask import Flask, render_template, send_file, request, redirect, Response
|
|
2
|
+import os
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+import pandas as pd
|
|
6
|
+import warnings
|
|
7
|
+import json
|
|
8
|
+import requests
|
|
9
|
+from urllib.request import urlopen
|
|
10
|
+warnings.filterwarnings("ignore")
|
|
11
|
+
|
|
12
|
+app = Flask(__name__)
|
|
13
|
+
|
|
14
|
+@app.route("/", methods=["GET"])
|
|
15
|
+def home():
|
|
16
|
+ return 'forcasting app running'
|
|
17
|
+
|
|
18
|
+######################################################################################################################
|
|
19
|
+list_output=[]
|
|
20
|
+
|
|
21
|
+def day(Num,df):
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+ #url='https://qa.bizgaze.app/apis/v4/bizgaze/integrations/demandforecast/getitemdata'
|
|
25
|
+ url= get_url
|
|
26
|
+ response = urlopen(url)
|
|
27
|
+ data_json = json.loads(response.read())
|
|
28
|
+ headers = {
|
|
29
|
+ 'Authorization':get_url_token,
|
|
30
|
+ #'Authorization':'stat 873f2e6f70b3483e983972f96fbf5ea4',#qa
|
|
31
|
+ 'Content-Type': 'application/json'
|
|
32
|
+ }
|
|
33
|
+ response = requests.request("GET", url, headers=headers, data=data_json)
|
|
34
|
+ #print("##############################################################")
|
|
35
|
+ a=response.text
|
|
36
|
+ # print(response.text)
|
|
37
|
+
|
|
38
|
+ import pandas as pd
|
|
39
|
+
|
|
40
|
+ df2 = pd.read_json(response.text, orient ='index')
|
|
41
|
+ df2=df2.reset_index()
|
|
42
|
+ df2.columns = ['key','value']
|
|
43
|
+ #print(df2)
|
|
44
|
+ a=df2['value'][0]
|
|
45
|
+
|
|
46
|
+ j=json.loads(a)
|
|
47
|
+ userdata = pd.DataFrame(j)
|
|
48
|
+ #df1
|
|
49
|
+ itemid=userdata[['itemname','itemid']]
|
|
50
|
+ itemid.columns = ['ItemName', 'ItemId']
|
|
51
|
+
|
|
52
|
+ #df1=pd.read_csv(r'./upload/' + name)
|
|
53
|
+ #df1=df1[df1['obdate']!='01/01/0001']
|
|
54
|
+ userdata.columns = ['journaldate','sum','itemname','itemid']
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+ # import pandas as pd
|
|
58
|
+ # userdata = pd.read_csv(r'C:\Users\Bizga\Desktop\forcast\5yearsitems.csv')
|
|
59
|
+ # itemid = userdata[['itemname', 'itemid']]
|
|
60
|
+ #userdata['journaldate'] = pd.to_datetime(userdata['journaldate'])
|
|
61
|
+ userdata["journaldate"] = userdata["journaldate"].astype(str)
|
|
62
|
+ userdata[["year", "month", "day"]] = userdata["journaldate"].str.split("/", expand = True)
|
|
63
|
+ #userdata['Month-Year']=userdata['year'].astype(str)+'-'+userdata['month'].astype(str)
|
|
64
|
+ item_unique_name = userdata['itemname'].unique()
|
|
65
|
+
|
|
66
|
+ #df=pd.read_csv("C:\\Users\\Bizgaze\\2021_2022.csv")
|
|
67
|
+ # Group the DataFrame by the 'item' column
|
|
68
|
+ grouped = userdata.groupby('itemname')
|
|
69
|
+
|
|
70
|
+ # Print the unique items in the 'item' column
|
|
71
|
+ #print(grouped.groups.keys())
|
|
72
|
+
|
|
73
|
+ # Iterate over the unique items and print the group data
|
|
74
|
+ for item, userdata in grouped:
|
|
75
|
+ print("itemname: ", item)
|
|
76
|
+
|
|
77
|
+ item_id = userdata.iloc[-1]['itemid']
|
|
78
|
+
|
|
79
|
+ print(item_id)
|
|
80
|
+
|
|
81
|
+ userdata= userdata.groupby('journaldate').sum()
|
|
82
|
+ userdata= userdata.reset_index()
|
|
83
|
+ #print(userdata)
|
|
84
|
+ fulldata=userdata[['journaldate','sum']]
|
|
85
|
+ fulldata.columns = ["Dates","SALES"]
|
|
86
|
+ #************************************************************************************************************************
|
|
87
|
+
|
|
88
|
+ ## Use Techniques Differencing
|
|
89
|
+ import pandas as pd
|
|
90
|
+ from pandas import DataFrame
|
|
91
|
+
|
|
92
|
+ # userdata=pd.read_csv(r"C:\Users\Bizgaze\ipynb files\TS forcasting\working\139470.csv")
|
|
93
|
+
|
|
94
|
+ userdata.columns = ['Date', 'sales','sku']
|
|
95
|
+ from statsmodels.tsa.stattools import adfuller
|
|
96
|
+
|
|
97
|
+ DATE=[]
|
|
98
|
+ SALES=[]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+ def adf_test(series,userdata):
|
|
102
|
+ result=adfuller(series)
|
|
103
|
+ print('ADF Statistics: {}'.format(result[0]))
|
|
104
|
+ print('p- value: {}'.format(result[1]))
|
|
105
|
+ if result[1] <= 0.05:
|
|
106
|
+ print("strong evidence against the null hypothesis, reject the null hypothesis. Data has no unit root and is stationary")
|
|
107
|
+ else:
|
|
108
|
+ #print(userdata)
|
|
109
|
+ print(stationary_test(userdata))
|
|
110
|
+ print("weak evidence against null hypothesis, time series has a unit root, indicating it is non-stationary ")
|
|
111
|
+
|
|
112
|
+ #********************************************* stationary or non-stationary **********************************************************
|
|
113
|
+ def stationary_test(userdata):
|
|
114
|
+ data=pd.DataFrame(userdata)
|
|
115
|
+ for i in range(1,13):
|
|
116
|
+ print(i)
|
|
117
|
+ sales_data=DataFrame()
|
|
118
|
+ data['sales']=data['sales'].shift(i)
|
|
119
|
+ data.dropna(inplace=True)
|
|
120
|
+ #print( userdata['sales'])
|
|
121
|
+ try:
|
|
122
|
+ X=adf_test(data["sales"],userdata="nothing")
|
|
123
|
+ if "non-stationary" in str(X):
|
|
124
|
+ print("non-stationary")
|
|
125
|
+ else:
|
|
126
|
+ print("stationary")
|
|
127
|
+ #print(userdata[["Date","sales"]])
|
|
128
|
+ #df_sale=pd.DataFrame(userdata)
|
|
129
|
+ DATE.append(data["Date"])
|
|
130
|
+ SALES.append(data["sales"])
|
|
131
|
+ #df4 = pd.concat([data, sales_data], axis=1)
|
|
132
|
+ return "done"
|
|
133
|
+ break
|
|
134
|
+ except ValueError:
|
|
135
|
+ pass
|
|
136
|
+
|
|
137
|
+ try:
|
|
138
|
+ adf_test(userdata["sales"],userdata)
|
|
139
|
+ except ValueError:
|
|
140
|
+ pass
|
|
141
|
+ sales=pd.DataFrame(SALES).T
|
|
142
|
+ dates=pd.DataFrame(DATE).T
|
|
143
|
+ try:
|
|
144
|
+ df4 = pd.concat([dates["Date"],sales["sales"]], axis=1)
|
|
145
|
+ df4=df4.dropna()
|
|
146
|
+ print(df4)
|
|
147
|
+ except KeyError:
|
|
148
|
+ df4=userdata[['Date','sales']]
|
|
149
|
+ df4=df4.dropna()
|
|
150
|
+ print(df4)
|
|
151
|
+ pass
|
|
152
|
+
|
|
153
|
+ #####################################################################################################################
|
|
154
|
+ userdata=df4
|
|
155
|
+ a = userdata.iloc[-1]['Date']
|
|
156
|
+ userdata['Date'] = pd.to_datetime(userdata['Date'])
|
|
157
|
+ userdata["Date"] = userdata["Date"].astype(str)
|
|
158
|
+ userdata[["year", "month", "day"]] = userdata["Date"].str.split("-", expand = True)
|
|
159
|
+ #userdata[["year", "month"]] = userdata["Month"].str.split("-", expand=True)
|
|
160
|
+ #userdata = userdata[["year","month",'sum']]
|
|
161
|
+ userdata["year"] = userdata["year"].astype(int)
|
|
162
|
+ userdata["month"] = userdata["month"].astype(int)
|
|
163
|
+ userdata["day"] = userdata["day"].astype(int)
|
|
164
|
+ #####################################################################################################################
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+ list_dates=[]
|
|
168
|
+ import datetime
|
|
169
|
+
|
|
170
|
+ days=int(Num)+1
|
|
171
|
+ import pandas as pd
|
|
172
|
+ base_date=pd.to_datetime(a)
|
|
173
|
+ for x in range(1,days):
|
|
174
|
+ dates=(base_date + datetime.timedelta(days=x))
|
|
175
|
+ dates=str(dates).replace(" 00:00:00","")
|
|
176
|
+ #print(dates)
|
|
177
|
+ list_dates.append(dates)
|
|
178
|
+ fut_date = pd.DataFrame(list_dates)
|
|
179
|
+ fut_date.columns = ["Dates"]
|
|
180
|
+
|
|
181
|
+ future_dates=pd.DataFrame(list_dates)
|
|
182
|
+
|
|
183
|
+ future_dates.columns=["Dates"]
|
|
184
|
+ future_dates[["year", "month", "day"]] = future_dates["Dates"].str.split("-", expand=True)
|
|
185
|
+ future_dates.drop(['Dates'], axis=1, inplace=True)
|
|
186
|
+ future_dates["year"] = future_dates["year"].astype(int)
|
|
187
|
+ future_dates["month"] = future_dates["month"].astype(int)
|
|
188
|
+ future_dates["day"] = future_dates["day"].astype(int)
|
|
189
|
+ #print(future_dates)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+ ###############################################################################
|
|
194
|
+ userdata['sales']=userdata["sales"].astype(float)
|
|
195
|
+ dependent = userdata[['year','month','day']]
|
|
196
|
+ independent = userdata['sales']
|
|
197
|
+
|
|
198
|
+ import numpy as np
|
|
199
|
+ import pandas as pd
|
|
200
|
+
|
|
201
|
+ import xgboost
|
|
202
|
+ from sklearn.model_selection import train_test_split
|
|
203
|
+ from sklearn.model_selection import GridSearchCV
|
|
204
|
+ from sklearn.metrics import roc_auc_score
|
|
205
|
+
|
|
206
|
+ import matplotlib.pyplot as plt
|
|
207
|
+
|
|
208
|
+ #model = xgboost.XGBRegressor()
|
|
209
|
+ from sklearn.ensemble import RandomForestRegressor
|
|
210
|
+ model = RandomForestRegressor(random_state=1,n_jobs=-1)
|
|
211
|
+ #model.fit(dependent, independent)
|
|
212
|
+ model.fit(dependent, independent)
|
|
213
|
+ #future=pd.read_csv('future_dates.csv')
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+ future_prediction = model.predict(future_dates)
|
|
217
|
+ #print(future_prediction)
|
|
218
|
+ df=pd.DataFrame(future_prediction)
|
|
219
|
+ df.columns = ["SALES"]
|
|
220
|
+ frames = [fut_date, df]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+ result = pd.concat(frames,axis=1)
|
|
224
|
+ result['itemname'] = item
|
|
225
|
+ result['itemid'] =item_id
|
|
226
|
+ result.columns = ['Date','Predict','ItemName','ItemId']
|
|
227
|
+ #result['Predict']=result["Predict"].astype(int)
|
|
228
|
+ result['UpperLimit']=result["Predict"].mean()+result['Predict'].std()*3
|
|
229
|
+ result['LowerLimit']=result['Predict'].mean()-result['Predict'].std()*3
|
|
230
|
+ print(result)
|
|
231
|
+ result.to_json('forcast.json', orient="records")
|
|
232
|
+
|
|
233
|
+ with open('forcast.json', 'r') as json_file:
|
|
234
|
+ json_load = json.load(json_file)
|
|
235
|
+ #url = "https://demo.bizgaze.app/apis/v4/bizgaze/integrations/demandforecast/saveforecast/List"
|
|
236
|
+ url=post_url
|
|
237
|
+
|
|
238
|
+ payload = json.dumps(json_load)#.replace("]", "").replace("[", "")
|
|
239
|
+ print(payload)
|
|
240
|
+ headers = {
|
|
241
|
+ #'Authorization': 'stat 263162e61f084d3392f162eb7ec39b2c',#demo
|
|
242
|
+ 'Authorization': post_url_token,#test
|
|
243
|
+ 'Content-Type': 'application/json'
|
|
244
|
+ }
|
|
245
|
+ response = requests.request("POST", url, headers=headers, data=payload)
|
|
246
|
+ print("##############################################################")
|
|
247
|
+ print(response.text)
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+ return 'done'
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
|
|
257
|
+#############################################################################################################################################################
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+def month(Num,df):
|
|
261
|
+ #url='https://qa.bizgaze.app/apis/v4/bizgaze/integrations/demandforecast/getitemdata'
|
|
262
|
+ url= get_url
|
|
263
|
+ response = urlopen(url)
|
|
264
|
+ data_json = json.loads(response.read())
|
|
265
|
+ headers = {
|
|
266
|
+ 'Authorization':get_url_token,
|
|
267
|
+ #'Authorization':'stat 873f2e6f70b3483e983972f96fbf5ea4',#qa
|
|
268
|
+ 'Content-Type': 'application/json'
|
|
269
|
+ }
|
|
270
|
+ response = requests.request("GET", url, headers=headers, data=data_json)
|
|
271
|
+ #print("##############################################################")
|
|
272
|
+ a=response.text
|
|
273
|
+ # print(response.text)
|
|
274
|
+
|
|
275
|
+ import pandas as pd
|
|
276
|
+
|
|
277
|
+ df2 = pd.read_json(response.text, orient ='index')
|
|
278
|
+ df2=df2.reset_index()
|
|
279
|
+ df2.columns = ['key','value']
|
|
280
|
+ #print(df2)
|
|
281
|
+ a=df2['value'][0]
|
|
282
|
+
|
|
283
|
+ j=json.loads(a)
|
|
284
|
+ userdata = pd.DataFrame(j)
|
|
285
|
+
|
|
286
|
+ #filePath='path.csv'
|
|
287
|
+ # if os.path.exists(filePath):
|
|
288
|
+ # print('file exist')
|
|
289
|
+ # os.remove('path.csv')
|
|
290
|
+ # else:
|
|
291
|
+ # print("file doesn't exists")
|
|
292
|
+ # pass
|
|
293
|
+ #userdata=df
|
|
294
|
+ itemid=userdata[['itemname','itemid']]
|
|
295
|
+ itemid.columns = ['ItemName', 'ItemId']
|
|
296
|
+
|
|
297
|
+ #df1=pd.read_csv(r'./upload/' + name)
|
|
298
|
+ #df1=df1[df1['obdate']!='01/01/0001']
|
|
299
|
+ userdata.columns = ['journaldate','sum','itemname','itemid']
|
|
300
|
+ # import pandas as pd
|
|
301
|
+ # userdata = pd.read_csv(r'C:\Users\Bizga\Desktop\forcast\5yearsitems.csv')
|
|
302
|
+ # itemid = userdata[['itemname', 'itemid']]
|
|
303
|
+ #userdata['journaldate'] = pd.to_datetime(userdata['journaldate'])
|
|
304
|
+ userdata["journaldate"] = userdata["journaldate"].astype(str)
|
|
305
|
+ userdata[["year", "month", "day"]] = userdata["journaldate"].str.split("-", expand = True)
|
|
306
|
+ userdata['Month-Year']=userdata['year'].astype(str)+'-'+userdata['month'].astype(str)
|
|
307
|
+ item_unique_name = userdata['itemname'].unique()
|
|
308
|
+
|
|
309
|
+ #df=pd.read_csv("C:\\Users\\Bizgaze\\2021_2022.csv")
|
|
310
|
+ # Group the DataFrame by the 'item' column
|
|
311
|
+ grouped = userdata.groupby('itemname')
|
|
312
|
+
|
|
313
|
+ # Print the unique items in the 'item' column
|
|
314
|
+ #print(grouped.groups.keys())
|
|
315
|
+
|
|
316
|
+ # Iterate over the unique items and print the group data
|
|
317
|
+ for item, userdata in grouped:
|
|
318
|
+ print("itemname: ", item)
|
|
319
|
+
|
|
320
|
+ item_id = userdata.iloc[-1]['itemid']
|
|
321
|
+
|
|
322
|
+ print(item_id)
|
|
323
|
+
|
|
324
|
+ userdata= userdata.groupby('Month-Year').sum()
|
|
325
|
+ userdata= userdata.reset_index()
|
|
326
|
+
|
|
327
|
+ fulldata=userdata[['Month-Year','sum']]
|
|
328
|
+ fulldata.columns = ["Dates","SALES"]
|
|
329
|
+ #************************************************************************************************************************
|
|
330
|
+
|
|
331
|
+ ## Use Techniques Differencing
|
|
332
|
+ import pandas as pd
|
|
333
|
+ from pandas import DataFrame
|
|
334
|
+
|
|
335
|
+ # userdata=pd.read_csv(r"C:\Users\Bizgaze\ipynb files\TS forcasting\working\139470.csv")
|
|
336
|
+ userdata=userdata[['Month-Year','sum','itemid']]
|
|
337
|
+ userdata.columns = ['Month', 'sales','sku']
|
|
338
|
+ from statsmodels.tsa.stattools import adfuller
|
|
339
|
+
|
|
340
|
+ DATE=[]
|
|
341
|
+ SALES=[]
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+ def adf_test(series,userdata):
|
|
345
|
+ result=adfuller(series)
|
|
346
|
+ print('ADF Statistics: {}'.format(result[0]))
|
|
347
|
+ print('p- value: {}'.format(result[1]))
|
|
348
|
+ if result[1] <= 0.05:
|
|
349
|
+ print("strong evidence against the null hypothesis, reject the null hypothesis. Data has no unit root and is stationary")
|
|
350
|
+ else:
|
|
351
|
+ #print(userdata)
|
|
352
|
+ print(stationary_test(userdata))
|
|
353
|
+ print("weak evidence against null hypothesis, time series has a unit root, indicating it is non-stationary ")
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+ #********************************************* stationary or non-stationary **********************************************************
|
|
357
|
+ def stationary_test(userdata):
|
|
358
|
+ data=pd.DataFrame(userdata)
|
|
359
|
+ for i in range(1,13):
|
|
360
|
+ print(i)
|
|
361
|
+ sales_data=DataFrame()
|
|
362
|
+ data['sales']=data['sales'].shift(i)
|
|
363
|
+ data.dropna(inplace=True)
|
|
364
|
+ #print( userdata['sales'])
|
|
365
|
+ try:
|
|
366
|
+ X=adf_test(data["sales"],userdata="nothing")
|
|
367
|
+ if "non-stationary" in str(X):
|
|
368
|
+ print("non-stationary")
|
|
369
|
+ else:
|
|
370
|
+ print("stationary")
|
|
371
|
+ #print(userdata[["Month","sales"]])
|
|
372
|
+ #df_sale=pd.DataFrame(userdata)
|
|
373
|
+ DATE.append(data["Month"])
|
|
374
|
+ SALES.append(data["sales"])
|
|
375
|
+ #df4 = pd.concat([data, sales_data], axis=1)
|
|
376
|
+ return "done"
|
|
377
|
+ break
|
|
378
|
+ except ValueError:
|
|
379
|
+ pass
|
|
380
|
+
|
|
381
|
+ try:
|
|
382
|
+ adf_test(userdata["sales"],userdata)
|
|
383
|
+ except ValueError:
|
|
384
|
+ pass
|
|
385
|
+ sales=pd.DataFrame(SALES).T
|
|
386
|
+ dates=pd.DataFrame(DATE).T
|
|
387
|
+ try:
|
|
388
|
+ df4 = pd.concat([dates["Month"],sales["sales"]], axis=1)
|
|
389
|
+ df4=df4.dropna()
|
|
390
|
+ print(df4)
|
|
391
|
+ except KeyError:
|
|
392
|
+ df4=userdata[['Month','sales']]
|
|
393
|
+ df4=df4.dropna()
|
|
394
|
+ print(df4)
|
|
395
|
+ pass
|
|
396
|
+
|
|
397
|
+ #####################################################################################################################
|
|
398
|
+ userdata=df4
|
|
399
|
+ a = userdata.iloc[-1]['Month']
|
|
400
|
+ userdata[["year", "month"]] = userdata["Month"].str.split("-", expand=True)
|
|
401
|
+ #userdata = userdata[["year","month",'sum']]
|
|
402
|
+ userdata["year"] = userdata["year"].astype(int)
|
|
403
|
+ userdata["month"] = userdata["month"].astype(int)
|
|
404
|
+ #####################################################################################################################
|
|
405
|
+
|
|
406
|
+ #a = userdata.iloc[-1]['Month-Year']
|
|
407
|
+ from datetime import datetime
|
|
408
|
+ from dateutil.relativedelta import relativedelta
|
|
409
|
+ import pandas as pd
|
|
410
|
+ months_value = int(Num)+1
|
|
411
|
+ base_month = pd.to_datetime(a)
|
|
412
|
+ list_months = []
|
|
413
|
+
|
|
414
|
+ def months(MD):
|
|
415
|
+ date_after_month = ((base_month + relativedelta(months=MD)).strftime('%Y-%m'))
|
|
416
|
+ # print
|
|
417
|
+ list_months.append(date_after_month)
|
|
418
|
+
|
|
419
|
+ for i in range(1, months_value):
|
|
420
|
+ months(i)
|
|
421
|
+
|
|
422
|
+ future_dates = pd.DataFrame(list_months)
|
|
423
|
+
|
|
424
|
+ future_dates.columns = ["Dates"]
|
|
425
|
+
|
|
426
|
+ fut_date = pd.DataFrame(list_months)
|
|
427
|
+ fut_date.columns = ["Dates"]
|
|
428
|
+
|
|
429
|
+ future_dates[["year", "month"]] = future_dates["Dates"].str.split("-", expand=True)
|
|
430
|
+ future_dates.drop(['Dates'], axis=1, inplace=True)
|
|
431
|
+ future_dates["year"] = future_dates["year"].astype(int)
|
|
432
|
+ future_dates["month"] = future_dates["month"].astype(int)
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
|
|
437
|
+ ###############################################################################
|
|
438
|
+ userdata['sales']=userdata["sales"].astype(float)
|
|
439
|
+ dependent = userdata[['year','month']]
|
|
440
|
+ independent = userdata['sales']
|
|
441
|
+
|
|
442
|
+ import numpy as np
|
|
443
|
+ import pandas as pd
|
|
444
|
+
|
|
445
|
+ import xgboost
|
|
446
|
+ from sklearn.model_selection import train_test_split
|
|
447
|
+ from sklearn.model_selection import GridSearchCV
|
|
448
|
+ from sklearn.metrics import roc_auc_score
|
|
449
|
+
|
|
450
|
+ import matplotlib.pyplot as plt
|
|
451
|
+
|
|
452
|
+ #model = xgboost.XGBRegressor()
|
|
453
|
+ from sklearn.ensemble import RandomForestRegressor
|
|
454
|
+ model = RandomForestRegressor(random_state=1,n_jobs=-1)
|
|
455
|
+ model.fit(dependent, independent)
|
|
456
|
+ #future=pd.read_csv('future_dates.csv')
|
|
457
|
+
|
|
458
|
+
|
|
459
|
+ future_prediction = model.predict(future_dates)
|
|
460
|
+ #print(future_prediction)
|
|
461
|
+ df=pd.DataFrame(future_prediction)
|
|
462
|
+ df.columns = ["SALES"]
|
|
463
|
+ frames = [fut_date, df]
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+ result = pd.concat(frames,axis=1)
|
|
467
|
+ result['itemname'] = item
|
|
468
|
+ result['itemid'] =item_id
|
|
469
|
+ result.columns = ['Date','Predict','ItemName','ItemId']
|
|
470
|
+ #result['Predict']=result["Predict"].astype(int)
|
|
471
|
+ result['UpperLimit']=result["Predict"].mean()+result['Predict'].std()*3
|
|
472
|
+ result['LowerLimit']=result['Predict'].mean()-result['Predict'].std()*3
|
|
473
|
+ result["LowerLimit"][result["LowerLimit"] < 0] = 0
|
|
474
|
+ print(result)
|
|
475
|
+ result.to_json('forcast.json', orient="records")
|
|
476
|
+
|
|
477
|
+ with open('forcast.json', 'r') as json_file:
|
|
478
|
+ json_load = json.load(json_file)
|
|
479
|
+ #url = "https://demo.bizgaze.app/apis/v4/bizgaze/integrations/demandforecast/saveforecast/List"
|
|
480
|
+ url=post_url
|
|
481
|
+
|
|
482
|
+ payload = json.dumps(json_load)#.replace("]", "").replace("[", "")
|
|
483
|
+ print(payload)
|
|
484
|
+ headers = {
|
|
485
|
+ #'Authorization': 'stat 263162e61f084d3392f162eb7ec39b2c',#demo
|
|
486
|
+ 'Authorization': post_url_token,#test
|
|
487
|
+ 'Content-Type': 'application/json'
|
|
488
|
+ }
|
|
489
|
+ response = requests.request("POST", url, headers=headers, data=payload)
|
|
490
|
+ print("##############################################################")
|
|
491
|
+ print(response.text)
|
|
492
|
+
|
|
493
|
+ # filePath='path.csv'
|
|
494
|
+
|
|
495
|
+
|
|
496
|
+ # if os.path.exists(filePath):
|
|
497
|
+ # print('file exist')
|
|
498
|
+ # #userdata = pd.DataFrame(data)
|
|
499
|
+ # result.to_csv('path.csv', mode='a',index=False, header=False)
|
|
500
|
+
|
|
501
|
+ # else:
|
|
502
|
+ # print("file as it doesn't exists")
|
|
503
|
+ # #result = pd.DataFrame(data)
|
|
504
|
+ # result.to_csv('path.csv', index=False)
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+ # result=pd.read_csv('path.csv')
|
|
509
|
+ # result.to_json('forcast.json', orient="records")
|
|
510
|
+ # import json
|
|
511
|
+
|
|
512
|
+ # # open the JSON file and read its contents
|
|
513
|
+ # with open(r'forcast.json', 'r') as f:
|
|
514
|
+ # json_data = json.load(f)
|
|
515
|
+
|
|
516
|
+ # print the JSON data
|
|
517
|
+ #print(json_data)
|
|
518
|
+
|
|
519
|
+ #output={"response":"success","result":json_data}
|
|
520
|
+ #print(output)
|
|
521
|
+ return 'done'
|
|
522
|
+
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+###############################################################################################################################################################
|
|
526
|
+
|
|
527
|
+#####################################################################################################################
|
|
528
|
+
|
|
529
|
+@app.route("/sales_forcast", methods=["GET", "POST"])
|
|
530
|
+def sales_forcast():
|
|
531
|
+ #wise= request.args.get('wise').replace('{','').replace('}','')
|
|
532
|
+ #Num= request.args.get('value').replace('{','').replace('}','')
|
|
533
|
+ #print(wise)
|
|
534
|
+ #print(Num)
|
|
535
|
+ Dataset = request.get_json()
|
|
536
|
+ a = url_list
|
|
537
|
+ wise = a['wise']
|
|
538
|
+ # print(x)
|
|
539
|
+ Num = a['future_dates']
|
|
540
|
+ get_url = a['get_url']
|
|
541
|
+ get_url_token = a['get_url_token']
|
|
542
|
+ post_url = a['post_url']
|
|
543
|
+ post_url_token = a['post_url_token']
|
|
544
|
+
|
|
545
|
+ #print(Dataset)
|
|
546
|
+ import pandas as pd
|
|
547
|
+ df=pd.DataFrame(Dataset)
|
|
548
|
+ print(df)
|
|
549
|
+ # a = Dataset
|
|
550
|
+ #x = a['wise']
|
|
551
|
+ # cmd = "python C:\\Users\\Bizga\\Desktop\\forcast\\XGdaywise.py"
|
|
552
|
+ # os.system(cmd)
|
|
553
|
+
|
|
554
|
+ #split=wise
|
|
555
|
+ # wise='month'
|
|
556
|
+ # Num=5
|
|
557
|
+ if wise=='days':
|
|
558
|
+ print('daywise groupby')
|
|
559
|
+ output=day(Num)
|
|
560
|
+ # cmd = "python C:\\Users\\Bizga\\Desktop\\forcast\\XGdaywise.py"+" "+ Num
|
|
561
|
+ # os.system(cmd)
|
|
562
|
+ else:
|
|
563
|
+ print('monthwise groupby')
|
|
564
|
+ output=month(Num)
|
|
565
|
+ # payload = json.dumps(output)
|
|
566
|
+ # payload_list="["+payload+"]"
|
|
567
|
+
|
|
568
|
+ return output
|
|
569
|
+
|
|
570
|
+if __name__ == "__main__":
|
|
571
|
+ app.run(host='0.0.0.0', port=8082)
|