File size: 5,970 Bytes
20f7431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff84bf5
20f7431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sys
import os
import streamlit as st
import pandas as pd
import pickle
import datetime
from PIL import Image

# Add the root folder to the Python module search path
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))

from src.utils import create_dataframe, process_data

# Set Streamlit page configuration
st.set_page_config(
    page_title="CAPE TOWN ANALYTICS",
    page_icon="📉",
    initial_sidebar_state="expanded",
    menu_items={
        '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, "..", "assets", "ml_components", "ml_components_1.pkl")
ml_components_2 = os.path.join(DIRPATH, "..", "assets", "ml_components", "ml_components_2.pkl")
hist_df = os.path.join(DIRPATH, "..", "assets", "history.csv")
image_path = os.path.join(DIRPATH, "..", "assets", "images", "sales_images")



# check if csv file exits 
def check_csv(csv_file, data):
    if os.path.isfile(csv_file):
        data.to_csv(csv_file, mode='a', header=False, encoding='utf-8', index=False)
    else:
        history = data.copy()
        history.to_csv(csv_file, encoding='utf-8', index=False) 

# Load pickle files
def load_pickle(filename):
    with open(filename, 'rb') as file:
        data = pickle.load(file)
        return data

ml_compos_1 = load_pickle(ml_components_1)
ml_compos_2 = load_pickle(ml_components_2)

# Extract components from ml_compos_2
categorical_pipeline = ml_compos_2['categorical_pipeline']
numerical_pipeliine = ml_compos_2['numerical_pipeline']
model = ml_compos_2['model']

# Extract columns from ml_compos_1
num_cols = ml_compos_1['num_cols']
cat_cols = ml_compos_1['cat_cols']
hol_level_list = ml_compos_1['Holiday_level'].tolist()
hol_city_list = ml_compos_1['Holiday_city'].tolist()

# Remove 'Not Holiday' from lists
hol_city_list.remove('Not Holiday')
hol_level_list.remove('Not Holiday')

# Create a container for expanding content
my_expander = st.container()


holiday_level = 'Not Holiday'
hol_city = 'Not Holiday'
# st.sidebar.selectbox('Menu', ['About', 'Model'])

# Expandable container for displaying content
with my_expander:
    image = Image.open(image_path)
    st.image(image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
    st.markdown("""
        <style>
        h1 {
            text-align: center;
        }
        </style>
        """, unsafe_allow_html=True)
    st.title('Demo Sales Forecasting :red[App]')
    st.sidebar.markdown("""
    ## Demo App

    This app predict sales from the parameters on the interface
    """)
    
    # 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'])

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

    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=False):
        if day_type == 'Additional Holiday' or day_type == 'Holiday' or day_type=='Transferred holiday':
            holiday_level = st.radio("level of Holiday?",
                                hol_level_list)#.tolist().remove('Not Holiday'))
            hol_city = st.selectbox("In which city is the holiday?",
                                hol_city_list)#.tolist().remove('Not Holiday'))
        else:
            st.markdown('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')

    raw_data = [date, store_number, item_family, item_onpromo, crude_price, holiday_level, hol_city, day_type, store_city, store_state, store_type, store_cluster]

    data = create_dataframe(raw_data)
    processed_data = process_data(data, categorical_pipeline, numerical_pipeliine, cat_cols, num_cols)

if button:
    st.balloons()

    st.metric('Predicted Sale', value=model.predict(processed_data))
    # predictions = model.predict(process_data)
    csv_file = hist_df
    check_csv(csv_file, data)
    history = pd.read_csv(csv_file)
    with st.expander('Download Input History'):
        # new_history = history.iloc[1:]
        st.dataframe(history)
    
    st.download_button('Download Data', 
                history.to_csv(index=False), 
                file_name='input_history.csv')