Manbearpig01 commited on
Commit
9ac0ba8
1 Parent(s): d63404f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +212 -0
app.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Importing required Libraries
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import numpy as np
5
+ import os, pickle
6
+ from sklearn.tree import DecisionTreeRegressor
7
+ from sklearn import preprocessing
8
+
9
+ # Setting up page configuration and directory path
10
+ st.set_page_config(page_title="Sales Forecasting App", page_icon="🐞", layout="centered")
11
+ DIRPATH = os.path.dirname(os.path.realpath(__file__))
12
+
13
+ # Setting background image
14
+ import base64
15
+ def add_bg_from_local(image_file):
16
+ with open(image_file, "rb") as image_file:
17
+ encoded_string = base64.b64encode(image_file.read())
18
+ st.markdown(
19
+ f"""
20
+ <style>
21
+ .stApp {{
22
+ background-image: url(data:image/{"jpg"};base64,{encoded_string.decode()});
23
+ background-size: cover
24
+ }}
25
+ </style>
26
+ """,
27
+ unsafe_allow_html=True
28
+ )
29
+ add_bg_from_local('background.jpg')
30
+
31
+ # Setting up logo
32
+ left1, left2, mid,right1, right2 = st.columns(5)
33
+ with mid:
34
+ st.image("logo.jpg", use_column_width=True)
35
+
36
+ # Setting up Sidebar
37
+ social_acc = ['Data Field Description', 'EDA', 'About App']
38
+ social_acc_nav = st.sidebar.radio('**INFORMATION SECTION**', social_acc)
39
+
40
+ if social_acc_nav == 'Data Field Description':
41
+ st.sidebar.markdown("<h2 style='text-align: center;'> Data Field Description </h2> ", unsafe_allow_html=True)
42
+ st.sidebar.markdown("**Date:** The date you want to predict sales for")
43
+ st.sidebar.markdown("**Family:** identifies the type of product sold")
44
+ st.sidebar.markdown("**Onpromotion:** gives the total number of items in a product family that are being promoted at a store at a given date")
45
+ st.sidebar.markdown("**Store Number:** identifies the store at which the products are sold")
46
+ st.sidebar.markdown("**Holiday Locale:** provide information about the locale where holiday is celebrated")
47
+
48
+ elif social_acc_nav == 'EDA':
49
+ st.sidebar.markdown("<h2 style='text-align: center;'> Exploratory Data Analysis </h2> ", unsafe_allow_html=True)
50
+ st.sidebar.markdown('''---''')
51
+ st.sidebar.markdown('''The exploratory data analysis of this project can be find in a Jupyter notebook from the linl below''')
52
+ st.sidebar.markdown("[Open Notebook](https://github.com/Kyei-frank/Regression-Project-Store-Sales--Time-Series-Forecasting/blob/main/project_workflow.ipynb)")
53
+
54
+ elif social_acc_nav == 'About App':
55
+ st.sidebar.markdown("<h2 style='text-align: center;'> Sales Forecasting App </h2> ", unsafe_allow_html=True)
56
+ st.sidebar.markdown('''---''')
57
+ st.sidebar.markdown("This App predicts the sales for product families sold at Favorita stores using regression model.")
58
+ st.sidebar.markdown("")
59
+ st.sidebar.markdown("[ Visit Github Repository for more information](https://github.com/Kyei-frank/Regression-Project-Store-Sales--Time-Series-Forecasting)")
60
+
61
+ # Loading Machine Learning Objects
62
+ @st.cache()
63
+ def load_saved_objects(file_path = 'ML_items'):
64
+ # Function to load saved objects
65
+ with open('ML_items', 'rb') as file:
66
+ loaded_object = pickle.load(file)
67
+
68
+ return loaded_object
69
+
70
+ # Instantiating ML_items
71
+ Loaded_object = load_saved_objects(file_path = 'ML_items')
72
+ model, encoder, train_data, stores, holidays_event = Loaded_object['model'], Loaded_object['encoder'], Loaded_object['train_data'], Loaded_object['stores'], Loaded_object['holidays_event']
73
+
74
+ # Setting Function for extracting Calendar features
75
+ @st.cache()
76
+ def getDateFeatures(df, date):
77
+ df['date'] = pd.to_datetime(df['date'])
78
+ df['month'] = df.date.dt.month
79
+ df['day_of_month'] = df.date.dt.day
80
+ df['day_of_year'] = df.date.dt.dayofyear
81
+ df['week_of_year'] = df.date.dt.isocalendar().week
82
+ df['day_of_week'] = df.date.dt.dayofweek
83
+ df['year'] = df.date.dt.year
84
+ df['is_weekend']= np.where(df['day_of_week'] > 4, 1, 0)
85
+ df['is_month_start']= df.date.dt.is_month_start.astype(int)
86
+ df['is_month_end']= df.date.dt.is_month_end.astype(int)
87
+ df['quarter']= df.date.dt.quarter
88
+ df['is_quarter_start']= df.date.dt.is_quarter_start.astype(int)
89
+ df['is_quarter_end']= df.date.dt.is_quarter_end.astype(int)
90
+ df['is_year_start']= df.date.dt.is_year_start.astype(int)
91
+
92
+ return df
93
+
94
+ # Setting up variables for input data
95
+ @st.cache()
96
+ def setup(tmp_df_file):
97
+ "Setup the required elements like files, models, global variables, etc"
98
+ pd.DataFrame(
99
+ dict(
100
+ date=[],
101
+ store_nbr=[],
102
+ family=[],
103
+ onpromotion=[],
104
+ city=[],
105
+ state=[],
106
+ store_type=[],
107
+ cluster=[],
108
+ day_type=[],
109
+ locale=[],
110
+ locale_name=[],
111
+ )
112
+ ).to_csv(tmp_df_file, index=False)
113
+
114
+ # Setting up a file to save our input data
115
+ tmp_df_file = os.path.join(DIRPATH, "tmp", "data.csv")
116
+ setup(tmp_df_file)
117
+
118
+ # setting Title for forms
119
+ st.markdown("<h2 style='text-align: center;'> Sales Prediction </h2> ", unsafe_allow_html=True)
120
+ st.markdown("<h7 style='text-align: center;'> Fill in the details below and click on SUBMIT button to make a prediction for a specific date and item </h7> ", unsafe_allow_html=True)
121
+
122
+ # Creating columns for for input data(forms)
123
+ left_col, mid_col, right_col = st.columns(3)
124
+
125
+ # Developing forms to collect input data
126
+ with st.form(key="information", clear_on_submit=True):
127
+
128
+ # Setting up input data for 1st column
129
+ left_col.markdown("**PRODUCT DATA**")
130
+ date = left_col.date_input("Prediction Date:")
131
+ family = left_col.selectbox("Item family:", options= list(train_data["family"].unique()))
132
+ onpromotion = left_col.selectbox("Onpromotion code:", options= set(train_data["onpromotion"].unique()))
133
+ store_nbr = left_col.selectbox("Store Number:", options= set(stores["store_nbr"].unique()))
134
+
135
+ # Setting up input data for 2nd column
136
+ mid_col.markdown("**STORE DATA**")
137
+ city = mid_col.selectbox("City:", options= set(stores["city"].unique()))
138
+ state = mid_col.selectbox("State:", options= list(stores["state"].unique()))
139
+ cluster = mid_col.selectbox("Store Cluster:", options= list(stores["cluster"].unique()))
140
+ store_type = mid_col.radio("Store Type:", options= set(stores["store_type"].unique()), horizontal = True)
141
+
142
+ # Setting up input data for 3rd column
143
+ right_col.markdown("**ADDITIONAL DATA**")
144
+ check= right_col.checkbox("Is it a Holiday or weekend?")
145
+ if check:
146
+ right_col.write('Fill the following information on Day Type')
147
+ day_type = right_col.selectbox("Holiday:", options= ('Holiday','Special Day:Transfered/Additional Holiday','No Work/Weekend'))
148
+ locale= right_col.selectbox("Holiday Locale:", options= list(holidays_event["locale"].unique()))
149
+ locale_name= right_col.selectbox("Locale Name:", options= list(holidays_event["locale_name"].unique()))
150
+ else:
151
+ day_type = 'Workday'
152
+ locale = 'National'
153
+ locale_name= 'Ecuador'
154
+
155
+ submitted = st.form_submit_button(label="Submit")
156
+
157
+ # Setting up background operations after submitting forms
158
+ if submitted:
159
+ # Saving input data as csv after submission
160
+ pd.read_csv(tmp_df_file).append(
161
+ dict(
162
+ date = date,
163
+ store_nbr = store_nbr,
164
+ family=family,
165
+ onpromotion= onpromotion,
166
+ city=city,
167
+ state=state,
168
+ store_type=store_type,
169
+ cluster=cluster,
170
+ day_type=day_type,
171
+ locale=locale,
172
+ locale_name=locale_name
173
+ ),
174
+ ignore_index=True,
175
+ ).to_csv(tmp_df_file, index=False)
176
+ st.balloons()
177
+
178
+ # Converting input data to a dataframe for prediction
179
+ df = pd.read_csv(tmp_df_file)
180
+ df= df.copy()
181
+
182
+ # Getting date Features
183
+ processed_data= getDateFeatures(df, 'date')
184
+ processed_data= processed_data.drop(columns=['date'])
185
+
186
+ # Encoding Categorical Variables
187
+ encoder = preprocessing.LabelEncoder()
188
+ cols = ['family', 'city', 'state', 'store_type', 'locale', 'locale_name', 'day_type']
189
+ for col in cols:
190
+ processed_data[col] = encoder.fit_transform(processed_data[col])
191
+
192
+ # Making Predictions
193
+ def predict(X, model):
194
+ results = model.predict(X)
195
+ return results
196
+
197
+ prediction = predict(X= processed_data, model= Loaded_object['model'])
198
+ df['Sales']= prediction
199
+
200
+
201
+ # Displaying prediction results
202
+ st.markdown('''---''')
203
+ st.markdown("<h4 style='text-align: center;'> Prediction Results </h4> ", unsafe_allow_html=True)
204
+ st.success(f"Predicted Sales: {prediction[-1]}")
205
+ st.markdown('''---''')
206
+
207
+ # Making expander to view all records
208
+ expander = st.expander("See all records")
209
+ with expander:
210
+ df = pd.read_csv(tmp_df_file)
211
+ df['Sales']= prediction
212
+ st.dataframe(df)