Spaces:
Running
Running
Commit ·
51520f0
1
Parent(s): 8b45a7a
Update app
Browse files
app.py
CHANGED
|
@@ -1,4 +1,8 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime
|
| 4 |
|
|
@@ -18,6 +22,16 @@ st.set_page_config(
|
|
| 18 |
initial_sidebar_state="expanded",
|
| 19 |
)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
# Preprocessing
|
| 22 |
@st.cache_data
|
| 23 |
def merge(B, C, A):
|
|
@@ -261,10 +275,13 @@ with st.sidebar:
|
|
| 261 |
st.write("Your uploaded data:")
|
| 262 |
st.write(df)
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
| 268 |
st.session_state.uploaded = True
|
| 269 |
|
| 270 |
with open('sample.csv', 'rb') as f:
|
|
@@ -290,13 +307,9 @@ if (st.session_state.uploaded):
|
|
| 290 |
train = train_test(df)
|
| 291 |
training_y, test_y, test_y_series, training_X, test_X, future_X = train
|
| 292 |
train_test_model = test_fitting(df, training_X, training_y)
|
| 293 |
-
|
| 294 |
n_periods = round(len(df) * 0.2)
|
| 295 |
-
|
| 296 |
future_n_periods = forecast_period + n_periods
|
| 297 |
-
|
| 298 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
| 299 |
-
|
| 300 |
index_of_fc = test_y_series.index
|
| 301 |
|
| 302 |
# make series for plotting purpose
|
|
@@ -305,6 +318,20 @@ if (st.session_state.uploaded):
|
|
| 305 |
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
|
| 306 |
upper_series = pd.Series(confint[:, 1], index=index_of_fc)
|
| 307 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
#Future predictions
|
| 309 |
frequency = '3D'
|
| 310 |
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)
|
|
@@ -316,11 +343,23 @@ if (st.session_state.uploaded):
|
|
| 316 |
future_lower_series = pd.Series(confint[:, 0], index=future_index_of_fc)
|
| 317 |
future_upper_series = pd.Series(confint[:, 1], index=future_index_of_fc)
|
| 318 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
future_sales_growth = sales_growth(df, future_fitted_series)
|
| 320 |
future_sales_growth = future_sales_growth.iloc[n_periods:]
|
| 321 |
df = dates_df(future_sales_growth)
|
| 322 |
-
|
| 323 |
-
|
|
|
|
| 324 |
st.session_state.forecasted = True
|
| 325 |
|
| 326 |
with st.form("question_form"):
|
|
@@ -329,4 +368,12 @@ if (st.session_state.uploaded):
|
|
| 329 |
|
| 330 |
if query_button or question:
|
| 331 |
answer = get_converted_answer(df, question)
|
| 332 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from streamlit_lottie import st_lottie_spinner
|
| 3 |
+
import json
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
import pandas as pd
|
| 7 |
from datetime import datetime
|
| 8 |
|
|
|
|
| 22 |
initial_sidebar_state="expanded",
|
| 23 |
)
|
| 24 |
|
| 25 |
+
@st.cache_data
|
| 26 |
+
def load_lottieurl(url: str):
|
| 27 |
+
r = requests.get(url)
|
| 28 |
+
if r.status_code != 200:
|
| 29 |
+
return None
|
| 30 |
+
return r.json()
|
| 31 |
+
|
| 32 |
+
lottie_progress_url = "https://lottie.host/12c7a018-d6c9-4595-abab-2992e4117d95/TnBbTO5WR5.json"
|
| 33 |
+
lottie_progress = load_lottieurl(lottie_progress_url)
|
| 34 |
+
|
| 35 |
# Preprocessing
|
| 36 |
@st.cache_data
|
| 37 |
def merge(B, C, A):
|
|
|
|
| 275 |
st.write("Your uploaded data:")
|
| 276 |
st.write(df)
|
| 277 |
|
| 278 |
+
with st_lottie_spinner(lottie_progress, loop=True, key="progress"):
|
| 279 |
+
time.sleep(5)
|
| 280 |
+
df = drop(df)
|
| 281 |
+
df = date_format(df)
|
| 282 |
+
merge_sort(df)
|
| 283 |
+
series = group_to_three(df)
|
| 284 |
+
|
| 285 |
st.session_state.uploaded = True
|
| 286 |
|
| 287 |
with open('sample.csv', 'rb') as f:
|
|
|
|
| 307 |
train = train_test(df)
|
| 308 |
training_y, test_y, test_y_series, training_X, test_X, future_X = train
|
| 309 |
train_test_model = test_fitting(df, training_X, training_y)
|
|
|
|
| 310 |
n_periods = round(len(df) * 0.2)
|
|
|
|
| 311 |
future_n_periods = forecast_period + n_periods
|
|
|
|
| 312 |
fitted, confint = train_test_model.predict(X=test_X, n_periods=n_periods, return_conf_int=True)
|
|
|
|
| 313 |
index_of_fc = test_y_series.index
|
| 314 |
|
| 315 |
# make series for plotting purpose
|
|
|
|
| 318 |
lower_series = pd.Series(confint[:, 0], index=index_of_fc)
|
| 319 |
upper_series = pd.Series(confint[:, 1], index=index_of_fc)
|
| 320 |
|
| 321 |
+
#TODO Plot Test and Training
|
| 322 |
+
col1, col2 = st.columns(2)
|
| 323 |
+
with col1:
|
| 324 |
+
col1.header("Sales Forecast")
|
| 325 |
+
col1.line_chart(df['Sales'], x="Date", y="Actual Sales" color="#0000EE")
|
| 326 |
+
# plt.figure(figsize=(18,10))
|
| 327 |
+
# plt.plot(df['Sales'], color='b', label = 'Actual Sales')
|
| 328 |
+
# plt.plot(test_y, color='b')
|
| 329 |
+
# plt.plot(fitted_series, color='r', label = 'Predicted Sales')
|
| 330 |
+
# plt.title("SARIMAX - Forecast of Auto Business Retail Sales VS Actual Sales")
|
| 331 |
+
# plt.legend(loc='upper left', fontsize=8)
|
| 332 |
+
# plt.show()
|
| 333 |
+
# plt.savefig('Auto Retail Future Sales Forecast 80-20.png')
|
| 334 |
+
|
| 335 |
#Future predictions
|
| 336 |
frequency = '3D'
|
| 337 |
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)
|
|
|
|
| 343 |
future_lower_series = pd.Series(confint[:, 0], index=future_index_of_fc)
|
| 344 |
future_upper_series = pd.Series(confint[:, 1], index=future_index_of_fc)
|
| 345 |
|
| 346 |
+
# TODO Plot Future
|
| 347 |
+
# plt.plot(future_fitted_series, color='darkgreen', label ='Future Forecasted Sales')
|
| 348 |
+
# plt.fill_between(future_lower_series.index,
|
| 349 |
+
# future_lower_series,
|
| 350 |
+
# future_upper_series,
|
| 351 |
+
# color='k', alpha=.15)
|
| 352 |
+
# plt.fill_between(lower_series.index,
|
| 353 |
+
# lower_series,
|
| 354 |
+
# upper_series,
|
| 355 |
+
# color='k', alpha=.15)
|
| 356 |
+
|
| 357 |
future_sales_growth = sales_growth(df, future_fitted_series)
|
| 358 |
future_sales_growth = future_sales_growth.iloc[n_periods:]
|
| 359 |
df = dates_df(future_sales_growth)
|
| 360 |
+
with col2:
|
| 361 |
+
col2.subheader("Forecasted sales in x days")
|
| 362 |
+
col2.write(df)
|
| 363 |
st.session_state.forecasted = True
|
| 364 |
|
| 365 |
with st.form("question_form"):
|
|
|
|
| 368 |
|
| 369 |
if query_button or question:
|
| 370 |
answer = get_converted_answer(df, question)
|
| 371 |
+
st.subheader("The answer is:", answer)
|
| 372 |
+
|
| 373 |
+
# Hide Streamlit default style
|
| 374 |
+
hide_st_style = """
|
| 375 |
+
<style>
|
| 376 |
+
footer {visibility: hidden;}
|
| 377 |
+
</style>
|
| 378 |
+
"""
|
| 379 |
+
st.markdown(hide_st_style, unsafe_allow_html=True)
|