ikoghoemmanuell
commited on
Commit
•
4daf5d3
1
Parent(s):
1aa902c
Update app.py
Browse files
app.py
CHANGED
@@ -5,15 +5,25 @@ from PIL import Image
|
|
5 |
import requests
|
6 |
from bokeh.plotting import figure
|
7 |
from bokeh.models import HoverTool
|
8 |
-
import
|
|
|
9 |
from date_features import getDateFeatures
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Set Page Configurations
|
16 |
-
st.set_page_config(page_title="
|
17 |
|
18 |
# Loading GIF
|
19 |
gif_url = "https://raw.githubusercontent.com/Gilbert-B/Forecasting-Sales/main/app/salesgif.gif"
|
@@ -24,24 +34,19 @@ menu = ['Home', 'About']
|
|
24 |
choice = st.sidebar.selectbox("Select an option", menu)
|
25 |
|
26 |
def predict(sales_data):
|
|
|
|
|
|
|
|
|
27 |
sales_data = getDateFeatures(sales_data).set_index('date')
|
28 |
-
# print(sales_data.columns)
|
29 |
-
|
30 |
-
# Make predictions for the next 8 weeks
|
31 |
-
prediction_inputs = [] # Initialize the list for prediction inputs
|
32 |
-
|
33 |
-
# Encode the prediction inputs
|
34 |
-
# numeric_columns = sales_data.select_dtypes(include=['int64', 'float64']).columns.tolist()
|
35 |
numeric_columns = ['onpromotion', 'year', 'month', 'dayofmonth', 'dayofweek', 'dayofyear', 'weekofyear', 'quarter', 'year_weekofyear', 'sin(dayofyear)', 'cos(dayofyear)']
|
36 |
-
categoric_columns = ['store_id','category_id','city','store_type','cluster','holiday_type','is_holiday','is_month_start','is_month_end','is_quarter_start','is_quarter_end','is_year_start','is_year_end','is_weekend', 'season']
|
37 |
-
|
38 |
-
# encoder = BinaryEncoder(drop_invariant=False, return_df=True,)
|
39 |
-
# encoder.fit(sales_data[categoric_columns])
|
40 |
num = sales_data[numeric_columns]
|
41 |
encoded_cat = encoder.transform(sales_data[categoric_columns])
|
42 |
sales_data = pd.concat([num, encoded_cat], axis=1)
|
43 |
-
|
44 |
-
# Make
|
45 |
predicted_sales = model.predict(sales_data)
|
46 |
|
47 |
return predicted_sales
|
@@ -67,9 +72,12 @@ if choice == 'Home':
|
|
67 |
categories = ['Category_' + str(i) for i in range(33)]
|
68 |
|
69 |
with col1:
|
70 |
-
|
71 |
# Convert the date to datetime format
|
72 |
-
|
|
|
|
|
|
|
73 |
onpromotion = st.number_input("How many products are on promotion?", min_value=0, step=1)
|
74 |
selected_category = st.selectbox("Category", categories)
|
75 |
|
@@ -80,48 +88,48 @@ if choice == 'Home':
|
|
80 |
selected_city = st.selectbox("City", cities)
|
81 |
selected_cluster = st.selectbox("Cluster", clusters)
|
82 |
|
83 |
-
|
84 |
-
sales_data = pd.DataFrame({
|
85 |
-
'date': [date],
|
86 |
-
'store_id': [selected_store],
|
87 |
-
'category_id': [selected_category],
|
88 |
-
'onpromotion': [onpromotion],
|
89 |
-
'city' :[selected_city],
|
90 |
-
'store_type': [selected_store1],
|
91 |
-
'cluster':[selected_cluster]
|
92 |
-
})
|
93 |
-
print(sales_data)
|
94 |
-
print(sales_data.info())
|
95 |
|
96 |
|
97 |
if st.button('Predict'):
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
# # Display the forecast results
|
104 |
-
# st.subheader("Sales Forecast for the Next 8 Weeks:")
|
105 |
-
# for week, sales in enumerate(predicted_sales, start=1):
|
106 |
-
# st.write(f"Week {week}: {sales:.2f} units")
|
107 |
-
|
108 |
-
# # Update the line chart
|
109 |
-
# chart_data = pd.DataFrame({'Week': range(1, 9), 'Sales': predicted_sales})
|
110 |
-
# p = figure(plot_width=600, plot_height=400, title="Sales Forecast",
|
111 |
-
# x_axis_label="Week", y_axis_label="Sales")
|
112 |
-
|
113 |
-
# p.line(chart_data['Week'], chart_data['Sales'], line_width=2)
|
114 |
-
# p.circle(chart_data['Week'], chart_data['Sales'], fill_color="white", size=6)
|
115 |
-
# p.add_tools(HoverTool(tooltips=[("Week", "@x"), ("Sales", "@y")]))
|
116 |
-
# st.bokeh_chart(p)
|
117 |
|
118 |
# About section
|
119 |
elif choice == 'About':
|
120 |
# Load the banner image
|
121 |
banner_image_url = "https://raw.githubusercontent.com/Gilbert-B/Forecasting-Sales/0d7b869515bysBoi5XxNGa3hayALLn9BK1VQqD69Dc/app/seer.png"
|
122 |
banner_image = Image.open(requests.get(banner_image_url, stream=True).raw)
|
123 |
-
|
124 |
-
|
125 |
st.image(banner_image, use_column_width=True)
|
126 |
st.markdown('''
|
127 |
<p style='font-size: 20px; font-style: italic;font-style: bold;'>
|
@@ -137,5 +145,6 @@ elif choice == 'About':
|
|
137 |
and maximize their revenue potential in an ever-changing market landscape.
|
138 |
</p>
|
139 |
''', unsafe_allow_html=True)
|
140 |
-
|
141 |
-
|
|
|
|
5 |
import requests
|
6 |
from bokeh.plotting import figure
|
7 |
from bokeh.models import HoverTool
|
8 |
+
import joblib
|
9 |
+
import os
|
10 |
from date_features import getDateFeatures
|
11 |
|
12 |
+
|
13 |
+
# Get the current directory path
|
14 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
15 |
+
|
16 |
+
# Load the model from the pickle file
|
17 |
+
model_path = os.path.join(current_dir, 'model.pkl')
|
18 |
+
model = joblib.load(model_path)
|
19 |
+
|
20 |
+
# Load the scaler from the pickle file
|
21 |
+
encoder_path = os.path.join(current_dir, 'encoder.pkl')
|
22 |
+
encoder = joblib.load(encoder_path)
|
23 |
+
|
24 |
|
25 |
# Set Page Configurations
|
26 |
+
st.set_page_config(page_title="Sales Prediction App", page_icon="fas fa-chart-line", layout="wide", initial_sidebar_state="auto")
|
27 |
|
28 |
# Loading GIF
|
29 |
gif_url = "https://raw.githubusercontent.com/Gilbert-B/Forecasting-Sales/main/app/salesgif.gif"
|
|
|
34 |
choice = st.sidebar.selectbox("Select an option", menu)
|
35 |
|
36 |
def predict(sales_data):
|
37 |
+
if sales_data.empty:
|
38 |
+
raise ValueError("No sales data provided.")
|
39 |
+
|
40 |
+
# Perform the necessary data processing steps
|
41 |
sales_data = getDateFeatures(sales_data).set_index('date')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
numeric_columns = ['onpromotion', 'year', 'month', 'dayofmonth', 'dayofweek', 'dayofyear', 'weekofyear', 'quarter', 'year_weekofyear', 'sin(dayofyear)', 'cos(dayofyear)']
|
43 |
+
categoric_columns = ['store_id', 'category_id', 'city', 'store_type', 'cluster', 'holiday_type', 'is_holiday', 'is_month_start', 'is_month_end', 'is_quarter_start', 'is_quarter_end', 'is_year_start', 'is_year_end', 'is_weekend', 'season']
|
44 |
+
|
|
|
|
|
45 |
num = sales_data[numeric_columns]
|
46 |
encoded_cat = encoder.transform(sales_data[categoric_columns])
|
47 |
sales_data = pd.concat([num, encoded_cat], axis=1)
|
48 |
+
|
49 |
+
# Make predictions using the pre-trained model
|
50 |
predicted_sales = model.predict(sales_data)
|
51 |
|
52 |
return predicted_sales
|
|
|
72 |
categories = ['Category_' + str(i) for i in range(33)]
|
73 |
|
74 |
with col1:
|
75 |
+
start_date = st.date_input("Start Date")
|
76 |
# Convert the date to datetime format
|
77 |
+
start_date = pd.to_datetime(start_date)
|
78 |
+
end_date = st.date_input("End Date")
|
79 |
+
# Convert the date to datetime format
|
80 |
+
end_date = pd.to_datetime(end_date)
|
81 |
onpromotion = st.number_input("How many products are on promotion?", min_value=0, step=1)
|
82 |
selected_category = st.selectbox("Category", categories)
|
83 |
|
|
|
88 |
selected_city = st.selectbox("City", cities)
|
89 |
selected_cluster = st.selectbox("Cluster", clusters)
|
90 |
|
91 |
+
predicted_data = pd.DataFrame(columns=['Start Date', 'End Date', 'Store', 'Category', 'On Promotion', 'City', 'Cluster', 'Predicted Sales'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
|
94 |
if st.button('Predict'):
|
95 |
+
if start_date > end_date:
|
96 |
+
st.error("Start date should be earlier than the end date.")
|
97 |
+
else:
|
98 |
+
with st.spinner('Predicting sales...'):
|
99 |
+
sales_data = pd.DataFrame({
|
100 |
+
'date': pd.date_range(start=start_date, end=end_date),
|
101 |
+
'store_id': [selected_store] * len(pd.date_range(start=start_date, end=end_date)),
|
102 |
+
'category_id': [selected_category] * len(pd.date_range(start=start_date, end=end_date)),
|
103 |
+
'onpromotion': [onpromotion] * len(pd.date_range(start=start_date, end=end_date)),
|
104 |
+
'city': [selected_city] * len(pd.date_range(start=start_date, end=end_date)),
|
105 |
+
'store_type': [selected_store1] * len(pd.date_range(start=start_date, end=end_date)),
|
106 |
+
'cluster': [selected_cluster] * len(pd.date_range(start=start_date, end=end_date))
|
107 |
+
})
|
108 |
+
try:
|
109 |
+
sales = predict(sales_data)
|
110 |
+
formatted_sales = round(sales[0], 2)
|
111 |
+
predicted_data = predicted_data.append({
|
112 |
+
'Start Date': start_date,
|
113 |
+
'End Date': end_date,
|
114 |
+
'Store': selected_store,
|
115 |
+
'Category': selected_category,
|
116 |
+
'On Promotion': onpromotion,
|
117 |
+
'City': selected_city,
|
118 |
+
'Cluster': selected_cluster,
|
119 |
+
'Predicted Sales': formatted_sales}, ignore_index=True)
|
120 |
+
|
121 |
+
st.success(f"Total sales for the period is: #{formatted_sales}")
|
122 |
+
except ValueError as e:
|
123 |
+
st.error(str(e))
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
# About section
|
127 |
elif choice == 'About':
|
128 |
# Load the banner image
|
129 |
banner_image_url = "https://raw.githubusercontent.com/Gilbert-B/Forecasting-Sales/0d7b869515bysBoi5XxNGa3hayALLn9BK1VQqD69Dc/app/seer.png"
|
130 |
banner_image = Image.open(requests.get(banner_image_url, stream=True).raw)
|
131 |
+
|
132 |
+
# Display the banner image
|
133 |
st.image(banner_image, use_column_width=True)
|
134 |
st.markdown('''
|
135 |
<p style='font-size: 20px; font-style: italic;font-style: bold;'>
|
|
|
145 |
and maximize their revenue potential in an ever-changing market landscape.
|
146 |
</p>
|
147 |
''', unsafe_allow_html=True)
|
148 |
+
if st.button('Clear Data'):
|
149 |
+
predicted_data = pd.DataFrame(columns=['Start Date', 'End Date', 'Store', 'Category', 'On Promotion', 'City', 'Cluster', 'Predicted Sales'])
|
150 |
+
st.success("Data cleared successfully.")
|