File size: 14,486 Bytes
a7fd2fe
39440ed
47fbf7b
e2cb6ae
 
 
 
d7ac35b
e2cb6ae
008dd16
e2cb6ae
 
 
 
0921718
 
 
 
 
 
 
e2cb6ae
c49aa0f
e2cb6ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c49aa0f
e2cb6ae
 
 
 
 
 
 
 
 
 
 
 
 
c49aa0f
e2cb6ae
 
 
 
 
 
 
 
 
 
c49aa0f
e2cb6ae
 
 
 
 
 
 
 
 
 
c49aa0f
e2cb6ae
 
 
3e41dea
e2cb6ae
 
 
804d8f9
 
 
 
 
 
 
 
 
 
 
 
 
0aa2056
b470a83
804d8f9
a6370b3
804d8f9
 
 
 
 
 
e2cb6ae
c49aa0f
57b2558
e2cb6ae
 
 
 
 
 
 
 
c49aa0f
4331672
916d7d9
507c880
 
24d0d7c
507c880
 
 
24d0d7c
507c880
 
24d0d7c
e2cb6ae
 
 
 
 
 
 
 
 
 
 
c49aa0f
e2cb6ae
 
 
 
 
 
 
 
4de408f
e2cb6ae
 
 
4de408f
e2cb6ae
 
 
 
 
b54ea30
102b083
6812a18
f56609c
 
6812a18
102b083
 
6812a18
 
 
102b083
 
6812a18
b54ea30
6812a18
329b9f4
102b083
 
2af4464
21a5308
102b083
 
1d218d2
102b083
 
b54ea30
 
33ff941
4d681bd
33ff941
4d681bd
0662fc3
33ff941
4d681bd
0662fc3
33ff941
4d681bd
0662fc3
128493f
4d681bd
0662fc3
 
9441b2b
e2cb6ae
db843a0
c49aa0f
bae9232
923595a
 
 
e2cb6ae
 
 
bae9232
e2cb6ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dff3f71
 
e2cb6ae
 
 
 
a7fd2fe
be97a09
e2cb6ae
4de408f
 
8b45a7a
 
4de408f
ffeca50
 
 
596c828
ffeca50
39440ed
 
b54ea30
eed76ea
b54ea30
 
39440ed
af7be88
 
18b17ea
 
b54ea30
 
 
 
0ffc81e
 
 
b54ea30
 
 
 
 
 
 
 
 
 
 
e2cb6ae
39440ed
e2cb6ae
 
 
cc724c9
8b45a7a
804d8f9
 
 
 
 
d7ac35b
1898dec
e2cb6ae
 
 
 
a6370b3
8b45a7a
804d8f9
57b2558
b5ca234
1898dec
57b2558
0aa2056
1898dec
6812a18
b5ca234
 
 
255721c
1898dec
9010788
1898dec
 
 
a6370b3
1898dec
 
 
0aa2056
 
 
 
e40f126
0aa2056
1898dec
0aa2056
b8821cb
 
1898dec
0aa2056
6812a18
 
0751948
 
6607488
0751948
0662fc3
707f8be
116d61c
f56609c
707f8be
255721c
 
2b2d52f
43de9aa
102b083
 
a0a099c
 
 
7983b5b
a0a099c
 
d5e0d06
a0a099c
 
d5e0d06
a303ce2
d5e0d06
2b2d52f
a0a099c
255721c
43de9aa
 
255721c
 
 
 
d5e0d06
255721c
 
37b261d
 
 
707f8be
 
 
57d4b86
707f8be
57d4b86
8b45a7a
5c50e05
905a52b
51520f0
 
 
 
 
 
 
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import streamlit as st
import pandas as pd
import time
from datetime import datetime

import numpy as np
import pmdarima as pm
import matplotlib.pyplot as plt
from pmdarima import auto_arima
import plotly.graph_objects as go

import torch
from transformers import pipeline, TapasTokenizer, TapasForQuestionAnswering

st.set_page_config(
      page_title="Sales Forecasting System",
      page_icon="πŸ“ˆ",
      layout="wide",
      initial_sidebar_state="expanded",
)

# Preprocessing
@st.cache_data
def merge(B, C, A):
  i = j = k = 0

  # Convert 'Date' columns to datetime.date objects
  B['Date'] = pd.to_datetime(B['Date']).dt.date
  C['Date'] = pd.to_datetime(C['Date']).dt.date
  A['Date'] = pd.to_datetime(A['Date']).dt.date

  while i < len(B) and j < len(C):
    if B['Date'].iloc[i] <= C['Date'].iloc[j]:
      A['Date'].iloc[k] = B['Date'].iloc[i]
      A['Sales'].iloc[k] = B['Sales'].iloc[i]
      i += 1
      
    else:
      A['Date'].iloc[k] = C['Date'].iloc[j]
      A['Sales'].iloc[k] = C['Sales'].iloc[j]
      j += 1
    k += 1

  while i < len(B):
    A['Date'].iloc[k] = B['Date'].iloc[i]
    A['Sales'].iloc[k] = B['Sales'].iloc[i]
    i += 1
    k += 1

  while j < len(C):
    A['Date'].iloc[k] = C['Date'].iloc[j]
    A['Sales'].iloc[k] = C['Sales'].iloc[j]
    j += 1
    k += 1

  return A

@st.cache_data
def merge_sort(dataframe):
  if len(dataframe) > 1:
      center = len(dataframe) // 2
      left = dataframe.iloc[:center]
      right = dataframe.iloc[center:]
      merge_sort(left)
      merge_sort(right)

      return merge(left, right, dataframe)

  else:
      return dataframe

@st.cache_data
def drop (dataframe):
  def get_columns_containing(dataframe, substrings):
    return [col for col in dataframe.columns if any(substring.lower() in col.lower() for substring in substrings)]

  columns_to_keep = get_columns_containing(dataframe, ["date", "sale"])
  dataframe = dataframe.drop(columns=dataframe.columns.difference(columns_to_keep))
  dataframe = dataframe.dropna()
    
  return dataframe

@st.cache_data
def date_format(dataframe):
  for i, d, s in dataframe.itertuples():
    dataframe['Date'][i] = dataframe['Date'][i].strip()

  for i, d, s in dataframe.itertuples():
    new_date = datetime.strptime(dataframe['Date'][i], "%m/%d/%Y").date()
    dataframe['Date'][i] = new_date

  return dataframe

@st.cache_data
def group_to_three(dataframe):
  dataframe['Date'] = pd.to_datetime(dataframe['Date'])
  dataframe = dataframe.groupby([pd.Grouper(key='Date', freq='3D')])['Sales'].mean().round(2)
  dataframe = dataframe.replace(0, np.nan).dropna()

  return dataframe

@st.cache_data
def series_to_df_exogenous(series):
  dataframe = series.to_frame()
  dataframe = dataframe.reset_index()
  dataframe = dataframe.set_index('Date')
  dataframe = dataframe.dropna()
  # Create the eXogenous values
  dataframe['Sales First Difference'] = dataframe['Sales'] - dataframe['Sales'].shift(1)
  dataframe['Seasonal First Difference'] = dataframe['Sales'] - dataframe['Sales'].shift(12)
  dataframe = dataframe.dropna()
  return dataframe

@st.cache_data
def dates_df(dataframe):
  dataframe = dataframe.reset_index()
  dataframe['Date'] = dataframe['Date'].dt.strftime('%B %d, %Y')
  dataframe[dataframe.columns] = dataframe[dataframe.columns].astype(str)
  return dataframe

@st.cache_data
def get_forecast_period(period):
  return round(period / 3)

# SARIMAX Model
@st.cache_data
def train_test(dataframe, n):
  training_y = dataframe.iloc[:-n,0]
  test_y = dataframe.iloc[-n:,0]
  test_y_series = pd.Series(test_y, index=dataframe.iloc[-n:, 0].index)
  training_X = dataframe.iloc[:-n,1:]
  test_X = dataframe.iloc[-n:,1:]
  future_X = dataframe.iloc[0:,1:]
  return (training_y, test_y, test_y_series, training_X, test_X, future_X)

@st.cache_data
def test_fitting(dataframe, Exo, trainY):
  trainTestModel = auto_arima(X = Exo, y = trainY, start_p=1, start_q=1,
                         test='adf',min_p=1,min_q=1,
                         max_p=3, max_q=3, m=12,
                         start_P=2, start_Q=2, seasonal=True,
                         d=None, D=1, trace=True,
                         error_action='ignore',
                         suppress_warnings=True,
                         stepwise=True, maxiter = 50)
  model = trainTestModel
  return model
  
def forecast_accuracy(forecast, actual):
    mape = np.mean(np.abs(forecast - actual)/np.abs(actual)).round(4)  # MAPE
    rmse = (np.mean((forecast - actual)**2)**.5).round(2)  # RMSE
    corr = np.corrcoef(forecast, actual)[0,1]   # corr
    mins = np.amin(np.hstack([forecast[:,None],
                            actual[:,None]]), axis=1)
    maxs = np.amax(np.hstack([forecast[:,None],
                            actual[:,None]]), axis=1)
    minmax = 1 - np.mean(mins/maxs)             # minmax
    return({'mape':mape, 'rmse':rmse, 'corr':corr, 'min-max':minmax})

@st.cache_data
def sales_growth(dataframe, fittedValues):
    sales_growth = fittedValues.to_frame()
    sales_growth = sales_growth.reset_index()
    sales_growth.columns = ("Date", "Sales")
    sales_growth = sales_growth.set_index('Date')

    sales_growth['Sales'] = (sales_growth['Sales']).round(2)

    # Calculate and create the column for sales difference and growth
    sales_growth['Forecasted Sales First Difference']=(sales_growth['Sales']-sales_growth['Sales'].shift(1)).round(2)
    sales_growth['Forecasted Sales Growth']=(((sales_growth['Sales']-sales_growth['Sales'].shift(1))/sales_growth['Sales'].shift(1))*100).round(2)

    # Calculate and create the first row for sales difference and growth
    sales_growth['Forecasted Sales First Difference'].iloc[0] = (dataframe['Sales'].iloc[-1]-dataframe['Sales'].iloc[-2]).round(2)
    sales_growth['Forecasted Sales Growth'].iloc[0]=(((dataframe['Sales'].iloc[-1]-dataframe['Sales'].iloc[-2])/dataframe['Sales'].iloc[-1])*100).round(2)

    return sales_growth

@st.cache_data
def merge_forecast_data(actual, predicted, future): # debug
    actual = actual.to_frame()
    print("BEFORE RENAME ACTUAL")
    print(actual)
    actual.rename(columns={actual.columns[0]: "Actual Sales"}, inplace=True)
    print("ACTUAL")
    print(actual)

    predicted = predicted.to_frame()
    predicted.rename(columns={predicted.columns[0]: "Predicted Sales"}, inplace=True)
    print("PREDICTED")
    print(predicted)

    future = future.to_frame()
    future = future.rename_axis('Date')
    future.rename(columns={future.columns[0]: "Forecasted Future Sales"}, inplace=True)
    print("FUTURE")
    print(future)

    merged_dataframe = pd.concat([actual, predicted, future], axis=1)
    print("MERGED DATAFRAME")
    print(merged_dataframe)
    merged_dataframe = merged_dataframe.reset_index()
    print("MERGED DATAFRAME RESET INDEX")
    print(merged_dataframe)
    return merged_dataframe

def interpret_mape(mape_score):
  score = (mape_score * 100).round(2)
  if score < 10:
    interpretation = "Great"
    color = "green"
  elif score < 20:
    interpretation = "Good"
    color = "seagreen"
  elif score < 50:
    interpretation = "Relatively good"
    color = "orange"
  else:
    interpretation = "Poor"
    color = "red"
  return score, interpretation, color

# TAPAS Model

@st.cache_resource
def load_tapas_model():
  model_name = "google/tapas-large-finetuned-wtq"
  tokenizer = TapasTokenizer.from_pretrained(model_name)
  model = TapasForQuestionAnswering.from_pretrained(model_name, local_files_only=False)
  pipe = pipeline("table-question-answering", model=model, tokenizer=tokenizer)
  return pipe

pipe = load_tapas_model()

def get_answer(table, query):
    answers = pipe(table=table, query=query)
    return answers

def convert_answer(answer):
    if answer['aggregator'] == 'SUM':
      cells = answer['cells']
      converted = sum(float(value.replace(',', '')) for value in cells)
      return converted

    if answer['aggregator'] == 'AVERAGE':
      cells = answer['cells']
      values = [float(value.replace(',', '')) for value in cells]
      converted = sum(values) / len(values)
      return converted

    if answer['aggregator'] == 'COUNT':
      cells = answer['cells']
      converted = sum(int(value.replace(',', '')) for value in cells)
      return converted

    else:

      return answer['answer']

def get_converted_answer(table, query):
    converted_answer = convert_answer(get_answer(table, query))
    return converted_answer

# Session States
if 'uploaded' not in st.session_state:
    st.session_state.uploaded = False

if 'forecasted' not in st.session_state:
    st.session_state.forecasted = False

# Web Application
st.title("Forecasting Dashboard πŸ“ˆ")
if not st.session_state.uploaded:
  st.subheader("Welcome User, get started forecasting by uploading your file in the sidebar!")

# Sidebar Menu
with st.sidebar:
    # TODO Name for product
    st.title("MLCast v1.1")
    st.subheader("An intelligent sales forecasting system")
    uploaded_file = st.file_uploader("Upload your store data here to proceed (must atleast contain Date and Sales)", type=["csv"])
    if uploaded_file is not None:
      date_found = False
      sales_found = False
      df = pd.read_csv(uploaded_file, parse_dates=True)
      for column in df.columns:
        if 'Date' in column:  
          date_found = True
        if 'Sales' in column:
          sales_found = True
      if(date_found == False or sales_found == False):
        st.error('Please upload a csv containing both Date and Sales...')
        st.stop()

      st.success("File uploaded successfully!")
      st.write("Your uploaded data:")
      st.write(df)

      df = drop(df)
      df = date_format(df)
      merge_sort(df)
      series = group_to_three(df)

      st.session_state.uploaded = True

    with open('sample.csv', 'rb') as f:
       st.download_button("Download our sample CSV", f, file_name='sample.csv')

if (st.session_state.uploaded):
  st.subheader("Sales History")
  st.line_chart(series)
  
  MIN_DAYS = 30
  MAX_DAYS = 90
  period = st.slider('How many days would you like to forecast?', min_value=MIN_DAYS, max_value=MAX_DAYS)
  forecast_period = get_forecast_period(period)

  forecast_button = st.button(
    'Start Forecasting',
    key='forecast_button',
    type="primary",
  )

  if (forecast_button or st.session_state.forecasted):
    df = series_to_df_exogenous(series)
    n_periods = round(len(df) * 0.2)
    print(n_periods) # debug

    train = train_test(df, n_periods)
    training_y, test_y, test_y_series, training_X, test_X, future_X = train
    train_test_model = test_fitting(df, training_X, training_y)
    
    print(df) # debug
    print(len(df)) # debug

    future_n_periods = forecast_period
    fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
    index_of_fc = test_y_series.index

    # make series for plotting purpose
    fitted_series = pd.Series(fitted)
    fitted_series.index = index_of_fc
    lower_series = pd.Series(confint[:, 0], index=index_of_fc)
    upper_series = pd.Series(confint[:, 1], index=index_of_fc)

    #Future predictions
    frequency = '3D'
    future_fitted, confint = train_test_model.predict(X=df.iloc[-future_n_periods:,1:], n_periods=future_n_periods, return_conf_int=True, freq=frequency)
    future_index_of_fc = pd.date_range(df['Sales'].index[-1], periods = future_n_periods, freq=frequency)

    # make series for future plotting purpose
    future_fitted_series = pd.Series(future_fitted)
    future_fitted_series.index = future_index_of_fc
    # future_lower_series = pd.Series(confint[:, 0], index=future_index_of_fc)
    # future_upper_series = pd.Series(confint[:, 1], index=future_index_of_fc)

    future_sales_growth = sales_growth(df, future_fitted_series)

    test_y, predictions = np.array(test_y), np.array(fitted)
    print("Test Y:", test_y) # debug
    print("Prediction:", fitted) # debug
    score = forecast_accuracy(predictions, test_y)
    print("Score:", score) # debug
    mape, interpretation, mape_color = interpret_mape(score['mape'])
    
    print(df)
    print(df['Sales'])
    merged_data = merge_forecast_data(df['Sales'], fitted_series, future_fitted_series)

    col_charts = st.columns(2)

    print(merged_data) # debug
    print(merged_data.info)
    print(merged_data.dtypes)
    with col_charts[0]:
      fig_compare = go.Figure()
      fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
      fig_compare.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Predicted Sales'], mode='lines', name='Predicted Sales', line=dict(color='#006400')))
      fig_compare.update_layout(title='Historical Sales Data', xaxis_title='Date', yaxis_title='Sales')
      st.plotly_chart(fig_compare, use_container_width=True)

    with col_charts[1]:
      fig_forecast = go.Figure()
      fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Actual Sales'], mode='lines', name='Actual Sales'))
      fig_forecast.add_trace(go.Scatter(x=merged_data[merged_data.columns[0]], y=merged_data['Forecasted Future Sales'], mode='lines', name='Future Forecasted Sales', line=dict(color=mape_color)))
      fig_forecast.update_layout(title='Forecasted Sales Data', xaxis_title='Date', yaxis_title='Sales')
      st.plotly_chart(fig_forecast, use_container_width=True)
      st.write(f"MAPE score: {mape}% - {interpretation}")

    df = dates_df(future_sales_growth)

    col_table = st.columns(2)
    with col_table[0]:
      col_table[0].subheader(f"Forecasted sales in the next {period} days")
      col_table[0].write(df)

    with col_table[1]:
      col_table[1] = st.subheader("Question-Answering")
      with st.form("question_form"):
        question = st.text_input('Ask a Question about the Forecasted Data', placeholder="What is the total sales in the month of December?")
        query_button = st.form_submit_button(label='Generate Answer')
      if query_button or question:
          answer = get_converted_answer(df, question)
          if answer is not None:
            st.write("The answer is:", answer)
          else:
            st.write("Answer is not found in table")
    st.session_state.forecasted = True


# Hide Streamlit default style
hide_st_style = """
            <style>
            footer {visibility: hidden;}
            </style>
            """
st.markdown(hide_st_style, unsafe_allow_html=True)