File size: 5,940 Bytes
f872bf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from PIL import Image
import pandas as pd
import numpy as np
import pickle
import datetime
import os
import sys
# from utils import payday, date_extracts
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from utils import payday, date_extracts


st.set_page_config(
    page_title="Ex-stream-ly Cool App",
    page_icon="🧊",
    initial_sidebar_state="expanded",
    menu_items={
        'Get Help': 'https://www.extremelycoolapp.com/help',
        'Report a bug': "https://www.extremelycoolapp.com/bug",
        'About': "# This is a header. This is an *extremely* cool app!"
    }
)

# Define directory paths
DIRPATH = os.path.dirname(os.path.realpath(__file__))
ml_components_1 = os.path.join(DIRPATH, "..", "src", "assets", "ml_components", "ml_components_1.pkl")
ml_components_2 = os.path.join(DIRPATH, "..", "src", "assets", "ml_components", "ml_components_2.pkl")
image_path = os.path.join(DIRPATH, "..", "src", "assets", "images", "sales.png")


# create a functions to load pickle file.
def load_pickle(filename):
    with open(filename, 'rb') as file:
        data = pickle.load(file)
        return data


#load all pickle files
ml_compos_1 = load_pickle(ml_components_1)
ml_compos_2 = load_pickle(ml_components_2)

# components in ml_compos_2  
categorical_pipeline = ml_compos_2['categorical_pipeline']
numerical_pipeliine = ml_compos_2['numerical_pipeline']
model = ml_compos_2['model']

num_cols = ml_compos_1['num_cols']
cat_cols = ml_compos_1['cat_cols'] 
     

# the title for the app
st.title('✨SALES FORECASTING APP✨')

# adding image
image=Image.open(image_path)
st.image(image, width=600)

     
st.subheader("Hi there! 👋 Let's start predicting sales 🙂")

    


# create an  expander to contain the app
my_expander = st.container()


holiday_level = 'No Holiday'
hol_city = 'No Holiday'
st.sidebar.selectbox('Menu', ['About', 'Model'])
with my_expander:
# create a three column layout
    col1, col2, col3 = st.columns(3)

# create a date input to receive date
    date = col1.date_input(
            "Enter the Date",
            datetime.date(2019, 7, 6))
        
# create a select box to select a family
item_family = col2.selectbox('What is the category of item?',
                                    ml_compos_1['family'])

# create a select box for store city
store_city = col3.selectbox("Which city is the store located?",
                                    ml_compos_1['Store_city'])

store_state = col1.selectbox("What state is the store located?",
                                    ml_compos_1['Store_state'])
        # hol_city = col2.selectbox("In which city is the holiday?",
        #                           ml_compos_1['Holiday_city'])

crude_price = col3.number_input('Price of Crude Oil', min_value=0.0, max_value=500.0, value=0.01)

day_type = col2.selectbox("Type of Day?",
                                ml_compos_1['Type_of_day'], index=2)
        # holiday_level = col3.radio("level of Holiday?",
        #                            ml_compos_1['Holiday_level'])
colZ, colY = st.columns(2)
store_type = colZ.radio("Type of store?",
                                ml_compos_1['Store_type'][::-1])
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)

holi = colY.empty()
with holi.expander(label='Holiday', expanded=True):
        if day_type == 'Additional Holiday' or day_type == 'Holiday' or day_type=='Transferred holiday':
        
                holiday_level = st.radio("level of Holiday?",
                                    ml_compos_1['Holiday_level'])#.tolist().remove('Not Holiday'))
                hol_city = st.selectbox("In which city is the holiday?",
                                    ml_compos_1['Holiday_city'])#.tolist().remove('Not Holiday'))
        else:
                st.markdown('Not Holiday')
                holiday_level = 'Not Holiday'
                hol_city = 'Not Holiday'


        colA, colB, colC = st.columns(3)

        store_number = colA.slider("Select the Store number ",
                                min_value=1,
                                max_value=54,
                                value=1)
        store_cluster = colB.slider("Select the Store Cluster ",
                                    min_value=1,
                                    max_value=17,
                                    value=1)
        item_onpromo = colC.slider("Number of items onpromo ",
                                min_value=0,
                                max_value=800,
                                value=1)
        button = st.button(label='Predict', use_container_width=True, type='primary')
        




X = np.array([[date, store_number, item_family, item_onpromo, crude_price, holiday_level, hol_city, day_type,
                    store_city, store_state, store_type, store_cluster]])
df = pd.DataFrame(X, columns=['date', 'Store_number', 'Family', 'Item_onpromo', 'Oil_prices', 'Holiday_level', 'Holiday_city',
                                    'TypeOfDay', 'Store_city', 'Store_state', 'Store_type', 'Cluster'])

df_raw = df.copy()

df[['Store_number', 'Item_onpromo', 'Cluster']] = df[['Store_number', 'Item_onpromo', 'Cluster']].apply(lambda x: x.astype(int))
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')

date_extracts(df)
df['Is_payday']= df[['DayOfMonth', 'Is_month_end']].apply(payday, axis=1)

    



if button:
        st.balloons()
        df[cat_cols] = categorical_pipeline.transform(df[cat_cols])
        df[num_cols] = numerical_pipeliine.transform(df[num_cols])
        # predicted_sale = model.predict(df)
        st.metric('Predicted Sale', value=model.predict(df))

        st.write(df_raw)
        
        st.download_button('Download Data', 
                    df.to_csv(index=False), 
                    file_name='data.csv')

        print(df.shape)