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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pickle
|
| 6 |
+
import datetime
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
# from utils import payday, date_extracts
|
| 10 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
| 11 |
+
|
| 12 |
+
from utils import payday, date_extracts
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
st.set_page_config(
|
| 16 |
+
page_title="Ex-stream-ly Cool App",
|
| 17 |
+
page_icon="🧊",
|
| 18 |
+
initial_sidebar_state="expanded",
|
| 19 |
+
menu_items={
|
| 20 |
+
'Get Help': 'https://www.extremelycoolapp.com/help',
|
| 21 |
+
'Report a bug': "https://www.extremelycoolapp.com/bug",
|
| 22 |
+
'About': "# This is a header. This is an *extremely* cool app!"
|
| 23 |
+
}
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
# Define directory paths
|
| 27 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
| 28 |
+
ml_components_1 = os.path.join(DIRPATH, "..", "src", "assets", "ml_components", "ml_components_1.pkl")
|
| 29 |
+
ml_components_2 = os.path.join(DIRPATH, "..", "src", "assets", "ml_components", "ml_components_2.pkl")
|
| 30 |
+
image_path = os.path.join(DIRPATH, "..", "src", "assets", "images", "sales.png")
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
# create a functions to load pickle file.
|
| 34 |
+
def load_pickle(filename):
|
| 35 |
+
with open(filename, 'rb') as file:
|
| 36 |
+
data = pickle.load(file)
|
| 37 |
+
return data
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
#load all pickle files
|
| 41 |
+
ml_compos_1 = load_pickle(ml_components_1)
|
| 42 |
+
ml_compos_2 = load_pickle(ml_components_2)
|
| 43 |
+
|
| 44 |
+
# components in ml_compos_2
|
| 45 |
+
categorical_pipeline = ml_compos_2['categorical_pipeline']
|
| 46 |
+
numerical_pipeliine = ml_compos_2['numerical_pipeline']
|
| 47 |
+
model = ml_compos_2['model']
|
| 48 |
+
|
| 49 |
+
num_cols = ml_compos_1['num_cols']
|
| 50 |
+
cat_cols = ml_compos_1['cat_cols']
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# the title for the app
|
| 54 |
+
st.title('✨SALES FORECASTING APP✨')
|
| 55 |
+
|
| 56 |
+
# adding image
|
| 57 |
+
image=Image.open(image_path)
|
| 58 |
+
st.image(image, width=600)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
st.subheader("Hi there! 👋 Let's start predicting sales 🙂")
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# create an expander to contain the app
|
| 67 |
+
my_expander = st.container()
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
holiday_level = 'No Holiday'
|
| 71 |
+
hol_city = 'No Holiday'
|
| 72 |
+
st.sidebar.selectbox('Menu', ['About', 'Model'])
|
| 73 |
+
with my_expander:
|
| 74 |
+
# create a three column layout
|
| 75 |
+
col1, col2, col3 = st.columns(3)
|
| 76 |
+
|
| 77 |
+
# create a date input to receive date
|
| 78 |
+
date = col1.date_input(
|
| 79 |
+
"Enter the Date",
|
| 80 |
+
datetime.date(2019, 7, 6))
|
| 81 |
+
|
| 82 |
+
# create a select box to select a family
|
| 83 |
+
item_family = col2.selectbox('What is the category of item?',
|
| 84 |
+
ml_compos_1['family'])
|
| 85 |
+
|
| 86 |
+
# create a select box for store city
|
| 87 |
+
store_city = col3.selectbox("Which city is the store located?",
|
| 88 |
+
ml_compos_1['Store_city'])
|
| 89 |
+
|
| 90 |
+
store_state = col1.selectbox("What state is the store located?",
|
| 91 |
+
ml_compos_1['Store_state'])
|
| 92 |
+
# hol_city = col2.selectbox("In which city is the holiday?",
|
| 93 |
+
# ml_compos_1['Holiday_city'])
|
| 94 |
+
|
| 95 |
+
crude_price = col3.number_input('Price of Crude Oil', min_value=0.0, max_value=500.0, value=0.01)
|
| 96 |
+
|
| 97 |
+
day_type = col2.selectbox("Type of Day?",
|
| 98 |
+
ml_compos_1['Type_of_day'], index=2)
|
| 99 |
+
# holiday_level = col3.radio("level of Holiday?",
|
| 100 |
+
# ml_compos_1['Holiday_level'])
|
| 101 |
+
colZ, colY = st.columns(2)
|
| 102 |
+
store_type = colZ.radio("Type of store?",
|
| 103 |
+
ml_compos_1['Store_type'][::-1])
|
| 104 |
+
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
|
| 105 |
+
|
| 106 |
+
holi = colY.empty()
|
| 107 |
+
with holi.expander(label='Holiday', expanded=True):
|
| 108 |
+
if day_type == 'Additional Holiday' or day_type == 'Holiday' or day_type=='Transferred holiday':
|
| 109 |
+
|
| 110 |
+
holiday_level = st.radio("level of Holiday?",
|
| 111 |
+
ml_compos_1['Holiday_level'])#.tolist().remove('Not Holiday'))
|
| 112 |
+
hol_city = st.selectbox("In which city is the holiday?",
|
| 113 |
+
ml_compos_1['Holiday_city'])#.tolist().remove('Not Holiday'))
|
| 114 |
+
else:
|
| 115 |
+
st.markdown('Not Holiday')
|
| 116 |
+
holiday_level = 'Not Holiday'
|
| 117 |
+
hol_city = 'Not Holiday'
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
colA, colB, colC = st.columns(3)
|
| 121 |
+
|
| 122 |
+
store_number = colA.slider("Select the Store number ",
|
| 123 |
+
min_value=1,
|
| 124 |
+
max_value=54,
|
| 125 |
+
value=1)
|
| 126 |
+
store_cluster = colB.slider("Select the Store Cluster ",
|
| 127 |
+
min_value=1,
|
| 128 |
+
max_value=17,
|
| 129 |
+
value=1)
|
| 130 |
+
item_onpromo = colC.slider("Number of items onpromo ",
|
| 131 |
+
min_value=0,
|
| 132 |
+
max_value=800,
|
| 133 |
+
value=1)
|
| 134 |
+
button = st.button(label='Predict', use_container_width=True, type='primary')
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
X = np.array([[date, store_number, item_family, item_onpromo, crude_price, holiday_level, hol_city, day_type,
|
| 141 |
+
store_city, store_state, store_type, store_cluster]])
|
| 142 |
+
df = pd.DataFrame(X, columns=['date', 'Store_number', 'Family', 'Item_onpromo', 'Oil_prices', 'Holiday_level', 'Holiday_city',
|
| 143 |
+
'TypeOfDay', 'Store_city', 'Store_state', 'Store_type', 'Cluster'])
|
| 144 |
+
|
| 145 |
+
df_raw = df.copy()
|
| 146 |
+
|
| 147 |
+
df[['Store_number', 'Item_onpromo', 'Cluster']] = df[['Store_number', 'Item_onpromo', 'Cluster']].apply(lambda x: x.astype(int))
|
| 148 |
+
df['date'] = pd.to_datetime(df['date'])
|
| 149 |
+
df = df.set_index('date')
|
| 150 |
+
|
| 151 |
+
date_extracts(df)
|
| 152 |
+
df['Is_payday']= df[['DayOfMonth', 'Is_month_end']].apply(payday, axis=1)
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
if button:
|
| 159 |
+
st.balloons()
|
| 160 |
+
df[cat_cols] = categorical_pipeline.transform(df[cat_cols])
|
| 161 |
+
df[num_cols] = numerical_pipeliine.transform(df[num_cols])
|
| 162 |
+
# predicted_sale = model.predict(df)
|
| 163 |
+
st.metric('Predicted Sale', value=model.predict(df))
|
| 164 |
+
|
| 165 |
+
st.write(df_raw)
|
| 166 |
+
|
| 167 |
+
st.download_button('Download Data',
|
| 168 |
+
df.to_csv(index=False),
|
| 169 |
+
file_name='data.csv')
|
| 170 |
+
|
| 171 |
+
print(df.shape)
|
| 172 |
+
|