Spaces:
Running
Running
Commit
Β·
804d8f9
1
Parent(s):
9d2e2d0
Fix app
Browse files- app.py +43 -86
- requirements.txt +1 -2
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import numpy as np
|
|
| 6 |
import pmdarima as pm
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from pmdarima import auto_arima
|
| 9 |
-
import plotly.graph_objects as go
|
| 10 |
|
| 11 |
import torch
|
| 12 |
from transformers import pipeline, TapasTokenizer, TapasForQuestionAnswering
|
|
@@ -98,6 +98,31 @@ def group_to_three(dataframe):
|
|
| 98 |
|
| 99 |
return dataframe
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# SARIMAX Model
|
| 102 |
@st.cache_data
|
| 103 |
def train_test(dataframe):
|
|
@@ -120,7 +145,7 @@ def model_fitting(dataframe, Exo):
|
|
| 120 |
error_action='ignore',
|
| 121 |
suppress_warnings=True,
|
| 122 |
stepwise=True,
|
| 123 |
-
maxiter=
|
| 124 |
model = futureModel
|
| 125 |
return model
|
| 126 |
|
|
@@ -134,7 +159,7 @@ def test_fitting(dataframe, Exo, trainY):
|
|
| 134 |
error_action='ignore',
|
| 135 |
suppress_warnings=True,
|
| 136 |
stepwise=True,
|
| 137 |
-
maxiter=
|
| 138 |
model = trainTestModel
|
| 139 |
return model
|
| 140 |
|
|
@@ -212,18 +237,20 @@ def get_converted_answer(table, query):
|
|
| 212 |
|
| 213 |
|
| 214 |
# Web Application
|
| 215 |
-
st.title("
|
| 216 |
st.subheader("Welcome User, start using the application by uploading your file in the sidebar!")
|
| 217 |
|
| 218 |
# Session States
|
| 219 |
if 'uploaded' not in st.session_state:
|
| 220 |
st.session_state.uploaded = False
|
| 221 |
|
| 222 |
-
if 'preprocessed_data' not in st.session_state:
|
| 223 |
-
|
| 224 |
|
| 225 |
# Sidebar Menu
|
| 226 |
with st.sidebar:
|
|
|
|
|
|
|
| 227 |
uploaded_file = st.file_uploader("Upload your Store Data here (must atleast contain Date and Sale)", type=["csv"])
|
| 228 |
err = 0
|
| 229 |
if uploaded_file is not None:
|
|
@@ -240,35 +267,27 @@ with st.sidebar:
|
|
| 240 |
df = drop(df)
|
| 241 |
df = date_format(df)
|
| 242 |
merge_sort(df)
|
| 243 |
-
|
| 244 |
st.session_state.uploaded = True
|
| 245 |
|
| 246 |
with open('sample.csv', 'rb') as f:
|
| 247 |
st.download_button("Download our sample CSV", f, file_name='sample.csv')
|
| 248 |
|
| 249 |
if (st.session_state.uploaded):
|
| 250 |
-
st.line_chart(
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
|
|
|
| 255 |
|
| 256 |
forecast_button = st.button(
|
| 257 |
'Start Forecasting',
|
| 258 |
key='forecast_button',
|
| 259 |
type="primary",
|
| 260 |
)
|
| 261 |
-
|
| 262 |
if (forecast_button):
|
| 263 |
-
df =
|
| 264 |
-
df = df.reset_index()
|
| 265 |
-
df = df.set_index('Date')
|
| 266 |
-
df = df.dropna()
|
| 267 |
-
|
| 268 |
-
# Create the eXogenous values
|
| 269 |
-
df['Sales First Difference'] = df['Sales'] - df['Sales'].shift(1)
|
| 270 |
-
df['Seasonal First Difference'] = df['Sales'] - df['Sales'].shift(12)
|
| 271 |
-
df = df.dropna()
|
| 272 |
|
| 273 |
auto_train_test = train_test(df)
|
| 274 |
training_y, test_y, test_y_series, training_X, test_X, future_X = auto_train_test
|
|
@@ -285,7 +304,7 @@ if (st.session_state.uploaded):
|
|
| 285 |
|
| 286 |
# make series for plotting purpose
|
| 287 |
fitted_series = pd.Series(fitted)
|
| 288 |
-
fitted_series.index=index_of_fc
|
| 289 |
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
|
| 290 |
upper_series = pd.Series(confint[:, 1], index=index_of_fc)
|
| 291 |
|
|
@@ -308,39 +327,7 @@ if (st.session_state.uploaded):
|
|
| 308 |
# plt.legend(loc='upper left', fontsize=8)
|
| 309 |
# plt.show()
|
| 310 |
|
| 311 |
-
trace_actual = go.Scatter(x=range(len(training_y) - 80, len(training_y)),
|
| 312 |
-
y=training_y[-80:],
|
| 313 |
-
mode='lines',
|
| 314 |
-
name='Training Data')
|
| 315 |
-
|
| 316 |
-
trace_actual_sales = go.Scatter(x=range(len(training_y), len(training_y) + len(test_y)),
|
| 317 |
-
y=test_y,
|
| 318 |
-
mode='lines',
|
| 319 |
-
name='Actual Sales',
|
| 320 |
-
line=dict(color='red'))
|
| 321 |
-
|
| 322 |
-
trace_predicted_sales = go.Scatter(x=range(len(training_y), len(training_y) + len(fitted_series)),
|
| 323 |
-
y=fitted_series,
|
| 324 |
-
mode='lines',
|
| 325 |
-
name='Predicted Sales',
|
| 326 |
-
line=dict(color='darkgreen'))
|
| 327 |
-
|
| 328 |
-
trace_fill_between = go.Scatter(x=list(range(len(training_y), len(training_y) + len(lower_series))) +
|
| 329 |
-
list(range(len(training_y) + len(lower_series), len(training_y) + len(upper_series))),
|
| 330 |
-
y=list(lower_series) + list(upper_series)[::-1],
|
| 331 |
-
fill='toself',
|
| 332 |
-
fillcolor='rgba(0,100,80,0.2)',
|
| 333 |
-
line=dict(color='rgba(255,255,255,0)'),
|
| 334 |
-
name='Prediction Interval')
|
| 335 |
-
|
| 336 |
# Combine traces and create layout
|
| 337 |
-
data = [trace_actual, trace_actual_sales, trace_predicted_sales, trace_fill_between]
|
| 338 |
-
layout = go.Layout(title="SARIMAX - Forecast of Retail Sales VS Actual Sales",
|
| 339 |
-
legend=dict(x=0, y=1.0),
|
| 340 |
-
xaxis=dict(title='X-axis Label'),
|
| 341 |
-
yaxis=dict(title='Y-axis Label'))
|
| 342 |
-
fig_test = go.Figure(data=data, layout=layout)
|
| 343 |
-
st.plotly_chart(fig_test)
|
| 344 |
|
| 345 |
# Forecast (actual)
|
| 346 |
n_periods = forecast_period
|
|
@@ -367,40 +354,10 @@ if (st.session_state.uploaded):
|
|
| 367 |
# plt.show()
|
| 368 |
|
| 369 |
# Create traces for each line and fill_between
|
| 370 |
-
trace_sales = go.Scatter(x=df.index[-50:],
|
| 371 |
-
y=df['Sales'][-50:],
|
| 372 |
-
mode='lines',
|
| 373 |
-
name='Sales')
|
| 374 |
-
|
| 375 |
-
trace_predicted_sales = go.Scatter(x=df.index[-50:] + future_fitted_series.index,
|
| 376 |
-
y=future_fitted_series,
|
| 377 |
-
mode='lines',
|
| 378 |
-
name='Predicted Sales',
|
| 379 |
-
line=dict(color='darkgreen'))
|
| 380 |
-
|
| 381 |
-
trace_fill_between = go.Scatter(x=list(df.index[-50:] + future_lower_series.index) +
|
| 382 |
-
list(df.index[-50:] + future_upper_series.index[::-1]),
|
| 383 |
-
y=list(future_lower_series) + list(future_upper_series)[::-1],
|
| 384 |
-
fill='toself',
|
| 385 |
-
fillcolor='rgba(0,100,80,0.2)',
|
| 386 |
-
line=dict(color='rgba(255,255,255,0)'),
|
| 387 |
-
name='Prediction Interval')
|
| 388 |
-
|
| 389 |
-
# Combine traces and create layout
|
| 390 |
-
data = [trace_sales, trace_predicted_sales, trace_fill_between]
|
| 391 |
-
layout = go.Layout(title="SARIMA - Final Forecast of Retail Sales",
|
| 392 |
-
legend=dict(x=0, y=1.0),
|
| 393 |
-
xaxis=dict(title='X-axis Label'),
|
| 394 |
-
yaxis=dict(title='Y-axis Label'))
|
| 395 |
-
fig_final = go.Figure(data=data, layout=layout)
|
| 396 |
-
st.plotly_chart(fig_final)
|
| 397 |
|
| 398 |
auto_sales_growth = sales_growth(df, future_fitted_series)
|
| 399 |
-
df = auto_sales_growth
|
| 400 |
-
df = df.reset_index()
|
| 401 |
-
df['Date'] = df['Date'].dt.strftime('%B %d, %Y')
|
| 402 |
-
df[df.columns] = df[df.columns].astype(str)
|
| 403 |
|
|
|
|
| 404 |
st.write("Forecasted sales in the next 3 months")
|
| 405 |
st.write(df)
|
| 406 |
|
|
|
|
| 6 |
import pmdarima as pm
|
| 7 |
import matplotlib.pyplot as plt
|
| 8 |
from pmdarima import auto_arima
|
| 9 |
+
# import plotly.graph_objects as go
|
| 10 |
|
| 11 |
import torch
|
| 12 |
from transformers import pipeline, TapasTokenizer, TapasForQuestionAnswering
|
|
|
|
| 98 |
|
| 99 |
return dataframe
|
| 100 |
|
| 101 |
+
@st.cache_data
|
| 102 |
+
def series_to_df_exogenous(series):
|
| 103 |
+
dataframe = series.to_frame()
|
| 104 |
+
dataframe = dataframe.reset_index()
|
| 105 |
+
dataframe = dataframe.set_index('Date')
|
| 106 |
+
dataframe = dataframe.dropna()
|
| 107 |
+
|
| 108 |
+
# Create the eXogenous values
|
| 109 |
+
dataframe['Sales First Difference'] = dataframe['Sales'] - dataframe['Sales'].shift(1)
|
| 110 |
+
dataframe['Seasonal First Difference'] = dataframe['Sales'] - dataframe['Sales'].shift(12)
|
| 111 |
+
dataframe = dataframe.dropna()
|
| 112 |
+
return dataframe
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
@st.cache_data
|
| 116 |
+
def series_to_df_dates(series):
|
| 117 |
+
dataframe = series.reset_index()
|
| 118 |
+
dataframe['Date'] = dataframe['Date'].dt.strftime('%B %d, %Y')
|
| 119 |
+
dataframe[df.columns] = dataframe[df.columns].astype(str)
|
| 120 |
+
return dataframe
|
| 121 |
+
|
| 122 |
+
@st.cache_data
|
| 123 |
+
def get_forecast_period(period):
|
| 124 |
+
return round(period / 3)
|
| 125 |
+
|
| 126 |
# SARIMAX Model
|
| 127 |
@st.cache_data
|
| 128 |
def train_test(dataframe):
|
|
|
|
| 145 |
error_action='ignore',
|
| 146 |
suppress_warnings=True,
|
| 147 |
stepwise=True,
|
| 148 |
+
maxiter=5)
|
| 149 |
model = futureModel
|
| 150 |
return model
|
| 151 |
|
|
|
|
| 159 |
error_action='ignore',
|
| 160 |
suppress_warnings=True,
|
| 161 |
stepwise=True,
|
| 162 |
+
maxiter=5)
|
| 163 |
model = trainTestModel
|
| 164 |
return model
|
| 165 |
|
|
|
|
| 237 |
|
| 238 |
|
| 239 |
# Web Application
|
| 240 |
+
st.title("Forecasting Dashboard π")
|
| 241 |
st.subheader("Welcome User, start using the application by uploading your file in the sidebar!")
|
| 242 |
|
| 243 |
# Session States
|
| 244 |
if 'uploaded' not in st.session_state:
|
| 245 |
st.session_state.uploaded = False
|
| 246 |
|
| 247 |
+
# if 'preprocessed_data' not in st.session_state:
|
| 248 |
+
# st.session_state.preprocessed_data = None
|
| 249 |
|
| 250 |
# Sidebar Menu
|
| 251 |
with st.sidebar:
|
| 252 |
+
st.title("SalesCast")
|
| 253 |
+
st.subheader("version 0.1")
|
| 254 |
uploaded_file = st.file_uploader("Upload your Store Data here (must atleast contain Date and Sale)", type=["csv"])
|
| 255 |
err = 0
|
| 256 |
if uploaded_file is not None:
|
|
|
|
| 267 |
df = drop(df)
|
| 268 |
df = date_format(df)
|
| 269 |
merge_sort(df)
|
| 270 |
+
series = group_to_three(df)
|
| 271 |
st.session_state.uploaded = True
|
| 272 |
|
| 273 |
with open('sample.csv', 'rb') as f:
|
| 274 |
st.download_button("Download our sample CSV", f, file_name='sample.csv')
|
| 275 |
|
| 276 |
if (st.session_state.uploaded):
|
| 277 |
+
st.line_chart(series)
|
| 278 |
+
|
| 279 |
+
MIN_DAYS = 30
|
| 280 |
+
MAX_DAYS = 90
|
| 281 |
+
period = st.slider('How many days would you like to forecast?', min_value=MIN_DAYS, max_value=MAX_DAYS)
|
| 282 |
+
forecast_period = get_forecast_period(period)
|
| 283 |
|
| 284 |
forecast_button = st.button(
|
| 285 |
'Start Forecasting',
|
| 286 |
key='forecast_button',
|
| 287 |
type="primary",
|
| 288 |
)
|
|
|
|
| 289 |
if (forecast_button):
|
| 290 |
+
df = series_to_df_exogenous(series)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 291 |
|
| 292 |
auto_train_test = train_test(df)
|
| 293 |
training_y, test_y, test_y_series, training_X, test_X, future_X = auto_train_test
|
|
|
|
| 304 |
|
| 305 |
# make series for plotting purpose
|
| 306 |
fitted_series = pd.Series(fitted)
|
| 307 |
+
fitted_series.index = index_of_fc
|
| 308 |
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
|
| 309 |
upper_series = pd.Series(confint[:, 1], index=index_of_fc)
|
| 310 |
|
|
|
|
| 327 |
# plt.legend(loc='upper left', fontsize=8)
|
| 328 |
# plt.show()
|
| 329 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 330 |
# Combine traces and create layout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
|
| 332 |
# Forecast (actual)
|
| 333 |
n_periods = forecast_period
|
|
|
|
| 354 |
# plt.show()
|
| 355 |
|
| 356 |
# Create traces for each line and fill_between
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
auto_sales_growth = sales_growth(df, future_fitted_series)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
+
df = series_to_df_dates(auto_sales_growth)
|
| 361 |
st.write("Forecasted sales in the next 3 months")
|
| 362 |
st.write(df)
|
| 363 |
|
requirements.txt
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
pmdarima
|
| 2 |
statsmodels
|
| 3 |
transformers
|
| 4 |
-
torch
|
| 5 |
-
plotly==5.18.0
|
|
|
|
| 1 |
pmdarima
|
| 2 |
statsmodels
|
| 3 |
transformers
|
| 4 |
+
torch
|
|
|