Spaces:
Runtime error
Runtime error
Added app files
Browse files- src/app/.streamlit/config.toml +6 -0
- src/app/app.py +169 -0
src/app/.streamlit/config.toml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[theme]
|
2 |
+
primaryColor = "#1E8449"
|
3 |
+
backgroundColor = "#F9E79F"
|
4 |
+
secondaryBackgroundColor = "#F5B041"
|
5 |
+
textColor = "#17202A"
|
6 |
+
font = "sans serif"
|
src/app/app.py
ADDED
@@ -0,0 +1,169 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import os
|
3 |
+
import streamlit as st
|
4 |
+
import pandas as pd
|
5 |
+
import pickle
|
6 |
+
import datetime
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Add the root folder to the Python module search path
|
10 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
11 |
+
|
12 |
+
from src.utils import create_dataframe, process_data
|
13 |
+
|
14 |
+
# Set Streamlit page configuration
|
15 |
+
st.set_page_config(
|
16 |
+
page_title="CAPE TOWN ANALYTICS",
|
17 |
+
page_icon="📉",
|
18 |
+
initial_sidebar_state="expanded",
|
19 |
+
menu_items={
|
20 |
+
'About': "# This is a header. This is an *extremely* cool app!"
|
21 |
+
}
|
22 |
+
)
|
23 |
+
|
24 |
+
# Define directory paths
|
25 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
26 |
+
ml_components_1 = os.path.join(DIRPATH, "..", "assets", "ml_components", "ml_components_1.pkl")
|
27 |
+
ml_components_2 = os.path.join(DIRPATH, "..", "assets", "ml_components", "ml_components_2.pkl")
|
28 |
+
hist_df = os.path.join(DIRPATH, "..", "assets", "history.csv")
|
29 |
+
image_path = os.path.join(DIRPATH, "..", "assets", "images", "justin-lim-JKjBsuKpatU-unsplash.jpg")
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
# check if csv file exits
|
34 |
+
def check_csv(csv_file, data):
|
35 |
+
if os.path.isfile(csv_file):
|
36 |
+
data.to_csv(csv_file, mode='a', header=False, encoding='utf-8', index=False)
|
37 |
+
else:
|
38 |
+
history = data.copy()
|
39 |
+
history.to_csv(csv_file, encoding='utf-8', index=False)
|
40 |
+
|
41 |
+
# Load pickle files
|
42 |
+
def load_pickle(filename):
|
43 |
+
with open(filename, 'rb') as file:
|
44 |
+
data = pickle.load(file)
|
45 |
+
return data
|
46 |
+
|
47 |
+
ml_compos_1 = load_pickle(ml_components_1)
|
48 |
+
ml_compos_2 = load_pickle(ml_components_2)
|
49 |
+
|
50 |
+
# Extract components from ml_compos_2
|
51 |
+
categorical_pipeline = ml_compos_2['categorical_pipeline']
|
52 |
+
numerical_pipeliine = ml_compos_2['numerical_pipeline']
|
53 |
+
model = ml_compos_2['model']
|
54 |
+
|
55 |
+
# Extract columns from ml_compos_1
|
56 |
+
num_cols = ml_compos_1['num_cols']
|
57 |
+
cat_cols = ml_compos_1['cat_cols']
|
58 |
+
hol_level_list = ml_compos_1['Holiday_level'].tolist()
|
59 |
+
hol_city_list = ml_compos_1['Holiday_city'].tolist()
|
60 |
+
|
61 |
+
# Remove 'Not Holiday' from lists
|
62 |
+
hol_city_list.remove('Not Holiday')
|
63 |
+
hol_level_list.remove('Not Holiday')
|
64 |
+
|
65 |
+
# Create a container for expanding content
|
66 |
+
my_expander = st.container()
|
67 |
+
|
68 |
+
|
69 |
+
holiday_level = 'Not Holiday'
|
70 |
+
hol_city = 'Not Holiday'
|
71 |
+
# st.sidebar.selectbox('Menu', ['About', 'Model'])
|
72 |
+
|
73 |
+
# Expandable container for displaying content
|
74 |
+
with my_expander:
|
75 |
+
image = Image.open(image_path)
|
76 |
+
st.image(image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
|
77 |
+
st.markdown("""
|
78 |
+
<style>
|
79 |
+
h1 {
|
80 |
+
text-align: center;
|
81 |
+
}
|
82 |
+
</style>
|
83 |
+
""", unsafe_allow_html=True)
|
84 |
+
st.title('Demo Sales Forecasting :red[App]')
|
85 |
+
st.sidebar.markdown("""
|
86 |
+
## Demo App
|
87 |
+
|
88 |
+
This app predict sales from the parameters on the interface
|
89 |
+
""")
|
90 |
+
|
91 |
+
# create a three column layout
|
92 |
+
col1, col2, col3 = st.columns(3)
|
93 |
+
|
94 |
+
# create a date input to receive date
|
95 |
+
date = col1.date_input(
|
96 |
+
"Enter the Date",
|
97 |
+
datetime.date(2019, 7, 6))
|
98 |
+
|
99 |
+
# create a select box to select a family
|
100 |
+
item_family = col2.selectbox('What is the category of item?',
|
101 |
+
ml_compos_1['family'])
|
102 |
+
|
103 |
+
# create a select box for store city
|
104 |
+
store_city = col3.selectbox("Which city is the store located?",
|
105 |
+
ml_compos_1['Store_city'])
|
106 |
+
|
107 |
+
store_state = col1.selectbox("What state is the store located?",
|
108 |
+
ml_compos_1['Store_state'])
|
109 |
+
|
110 |
+
crude_price = col3.number_input('Price of Crude Oil', min_value=1.0, max_value=500.0, value=1.0)
|
111 |
+
|
112 |
+
day_type = col2.selectbox("Type of Day?",
|
113 |
+
ml_compos_1['Type_of_day'], index=2)
|
114 |
+
# holiday_level = col3.radio("level of Holiday?",
|
115 |
+
# ml_compos_1['Holiday_level'])
|
116 |
+
colZ, colY = st.columns(2)
|
117 |
+
store_type = colZ.radio("Type of store?",
|
118 |
+
ml_compos_1['Store_type'][::-1])
|
119 |
+
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;}</style>', unsafe_allow_html=True)
|
120 |
+
|
121 |
+
holi = colY.empty()
|
122 |
+
with holi.expander(label='Holiday', expanded=False):
|
123 |
+
if day_type == 'Additional Holiday' or day_type == 'Holiday' or day_type=='Transferred holiday':
|
124 |
+
holiday_level = st.radio("level of Holiday?",
|
125 |
+
hol_level_list)#.tolist().remove('Not Holiday'))
|
126 |
+
hol_city = st.selectbox("In which city is the holiday?",
|
127 |
+
hol_city_list)#.tolist().remove('Not Holiday'))
|
128 |
+
else:
|
129 |
+
st.markdown('Not Holiday')
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
colA, colB, colC = st.columns(3)
|
134 |
+
|
135 |
+
store_number = colA.slider("Select the Store number ",
|
136 |
+
min_value=1,
|
137 |
+
max_value=54,
|
138 |
+
value=1)
|
139 |
+
store_cluster = colB.slider("Select the Store Cluster ",
|
140 |
+
min_value=1,
|
141 |
+
max_value=17,
|
142 |
+
value=1)
|
143 |
+
item_onpromo = colC.slider("Number of items onpromo ",
|
144 |
+
min_value=0,
|
145 |
+
max_value=800,
|
146 |
+
value=1)
|
147 |
+
button = st.button(label='Predict', use_container_width=True, type='primary')
|
148 |
+
|
149 |
+
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]
|
150 |
+
|
151 |
+
data = create_dataframe(raw_data)
|
152 |
+
processed_data = process_data(data, categorical_pipeline, numerical_pipeliine, cat_cols, num_cols)
|
153 |
+
|
154 |
+
if button:
|
155 |
+
st.balloons()
|
156 |
+
|
157 |
+
st.metric('Predicted Sale', value=model.predict(processed_data))
|
158 |
+
# predictions = model.predict(process_data)
|
159 |
+
csv_file = hist_df
|
160 |
+
check_csv(csv_file, data)
|
161 |
+
history = pd.read_csv(csv_file)
|
162 |
+
with st.expander('Download Input History'):
|
163 |
+
# new_history = history.iloc[1:]
|
164 |
+
st.dataframe(history)
|
165 |
+
|
166 |
+
st.download_button('Download Data',
|
167 |
+
history.to_csv(index=False),
|
168 |
+
file_name='input_history.csv')
|
169 |
+
|