ikoghoemmanuell
commited on
Commit
•
982c0a1
1
Parent(s):
20b45de
Upload 3 files
Browse files- app.py +152 -0
- date_features.py +39 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
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 |
+
# Get the current directory path
|
13 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
14 |
+
|
15 |
+
# Define path for the model and encoder from the pickle file
|
16 |
+
assets_dir = os.path.abspath(os.path.join(current_dir, "../../assets/ML components"))
|
17 |
+
model_path = os.path.join(assets_dir, 'model.pkl')
|
18 |
+
encoder_path = os.path.join(assets_dir, 'encoder.pkl')
|
19 |
+
# model_path = os.path.join(current_dir, 'model.pkl')
|
20 |
+
# encoder_path = os.path.join(current_dir, 'encoder.pkl')
|
21 |
+
|
22 |
+
# Load the model and encoder from the pickle file
|
23 |
+
model = joblib.load(model_path)
|
24 |
+
encoder = joblib.load(encoder_path)
|
25 |
+
|
26 |
+
# Set Page Configurations
|
27 |
+
st.set_page_config(page_title="ETA Prediction App", page_icon="fas fa-chart-line", layout="wide", initial_sidebar_state="auto")
|
28 |
+
|
29 |
+
# Loading GIF
|
30 |
+
gif_url = "https://raw.githubusercontent.com/Gilbert-B/Forecasting-Sales/main/app/salesgif.gif"
|
31 |
+
|
32 |
+
# Set up sidebar
|
33 |
+
st.sidebar.header('Navigation')
|
34 |
+
menu = ['Home', 'About']
|
35 |
+
choice = st.sidebar.selectbox("Select an option", menu)
|
36 |
+
|
37 |
+
def predict(sales_data):
|
38 |
+
sales_data = getDateFeatures(sales_data).set_index('date')
|
39 |
+
# print(sales_data.columns)
|
40 |
+
|
41 |
+
# Make predictions for the next 8 weeks
|
42 |
+
prediction_inputs = [] # Initialize the list for prediction inputs
|
43 |
+
|
44 |
+
# Encode the prediction inputs
|
45 |
+
# numeric_columns = sales_data.select_dtypes(include=['int64', 'float64']).columns.tolist()
|
46 |
+
numeric_columns = ['onpromotion', 'year', 'month', 'dayofmonth', 'dayofweek', 'dayofyear', 'weekofyear', 'quarter', 'year_weekofyear', 'sin(dayofyear)', 'cos(dayofyear)']
|
47 |
+
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']
|
48 |
+
print(categoric_columns)
|
49 |
+
# encoder = BinaryEncoder(drop_invariant=False, return_df=True,)
|
50 |
+
# encoder.fit(sales_data[categoric_columns])
|
51 |
+
num = sales_data[numeric_columns]
|
52 |
+
encoded_cat = encoder.transform(sales_data[categoric_columns])
|
53 |
+
sales_data = pd.concat([num, encoded_cat], axis=1)
|
54 |
+
|
55 |
+
# Make the prediction using the loaded machine learning model
|
56 |
+
predicted_sales = model.predict(sales_data)
|
57 |
+
|
58 |
+
return predicted_sales
|
59 |
+
|
60 |
+
# Home section
|
61 |
+
if choice == 'Home':
|
62 |
+
st.image(gif_url, use_column_width=True)
|
63 |
+
st.markdown("<h1 style='text-align: center;'>Welcome</h1>", unsafe_allow_html=True)
|
64 |
+
st.markdown("<p style='text-align: center;'>This is a Sales Forecasting App.</p>", unsafe_allow_html=True)
|
65 |
+
|
66 |
+
# Set Page Title
|
67 |
+
st.title('SEER- A Sales Forecasting APP')
|
68 |
+
st.markdown('Enter the required information to forecast sales:')
|
69 |
+
|
70 |
+
|
71 |
+
# Input form
|
72 |
+
col1, col2 = st.columns(2)
|
73 |
+
|
74 |
+
Stores = ['Store_' + str(i) for i in range(1, 55)]
|
75 |
+
Stores1 = ['Store_' + str(i) for i in range(0, 5)]
|
76 |
+
cities = ['city_' + str(i) for i in range(22)]
|
77 |
+
clusters = ['cluster_' + str(i) for i in range(17)]
|
78 |
+
categories = ['Category_' + str(i) for i in range(33)]
|
79 |
+
|
80 |
+
with col1:
|
81 |
+
date = st.date_input("Date")
|
82 |
+
# Convert the date to datetime format
|
83 |
+
date = pd.to_datetime(date)
|
84 |
+
onpromotion = st.number_input("How many products are on promotion?", min_value=0, step=1)
|
85 |
+
selected_category = st.selectbox("Category", categories)
|
86 |
+
|
87 |
+
|
88 |
+
with col2:
|
89 |
+
selected_store = st.selectbox("Store_type", Stores)
|
90 |
+
selected_store1 = st.selectbox("Store_id", Stores1)
|
91 |
+
selected_city = st.selectbox("City", cities)
|
92 |
+
selected_cluster = st.selectbox("Cluster", clusters)
|
93 |
+
|
94 |
+
# Call getDateFeatures() function on sales_data (replace sales_data with your DataFrame)
|
95 |
+
sales_data = pd.DataFrame({
|
96 |
+
'date': [date],
|
97 |
+
'store_id': [selected_store],
|
98 |
+
'category_id': [selected_category],
|
99 |
+
'onpromotion': [onpromotion],
|
100 |
+
'city' :[selected_city],
|
101 |
+
'store_type': [selected_store1],
|
102 |
+
'cluster':[selected_cluster]
|
103 |
+
})
|
104 |
+
print(sales_data)
|
105 |
+
print(sales_data.info())
|
106 |
+
|
107 |
+
|
108 |
+
if st.button('Predict'):
|
109 |
+
sales = predict(sales_data)
|
110 |
+
formatted_sales = round(sales[0], 2)
|
111 |
+
st.write(f"Total sales for this week is: #{formatted_sales}")
|
112 |
+
|
113 |
+
|
114 |
+
# # Display the forecast results
|
115 |
+
# st.subheader("Sales Forecast for the Next 8 Weeks:")
|
116 |
+
# for week, sales in enumerate(predicted_sales, start=1):
|
117 |
+
# st.write(f"Week {week}: {sales:.2f} units")
|
118 |
+
|
119 |
+
# # Update the line chart
|
120 |
+
# chart_data = pd.DataFrame({'Week': range(1, 9), 'Sales': predicted_sales})
|
121 |
+
# p = figure(plot_width=600, plot_height=400, title="Sales Forecast",
|
122 |
+
# x_axis_label="Week", y_axis_label="Sales")
|
123 |
+
|
124 |
+
# p.line(chart_data['Week'], chart_data['Sales'], line_width=2)
|
125 |
+
# p.circle(chart_data['Week'], chart_data['Sales'], fill_color="white", size=6)
|
126 |
+
# p.add_tools(HoverTool(tooltips=[("Week", "@x"), ("Sales", "@y")]))
|
127 |
+
# st.bokeh_chart(p)
|
128 |
+
|
129 |
+
# About section
|
130 |
+
elif choice == 'About':
|
131 |
+
# Load the banner image
|
132 |
+
banner_image_url = "https://raw.githubusercontent.com/Gilbert-B/Forecasting-Sales/0d7b869515bysBoi5XxNGa3hayALLn9BK1VQqD69Dc/app/seer.png"
|
133 |
+
banner_image = Image.open(requests.get(banner_image_url, stream=True).raw)
|
134 |
+
|
135 |
+
# Display the banner image
|
136 |
+
st.image(banner_image, use_column_width=True)
|
137 |
+
st.markdown('''
|
138 |
+
<p style='font-size: 20px; font-style: italic;font-style: bold;'>
|
139 |
+
SEER is a powerful tool designed to assist businesses in making accurate
|
140 |
+
and data-driven sales predictions. By leveraging advanced algorithms and
|
141 |
+
machine learning techniques, our app provides businesses with valuable insights
|
142 |
+
into future sales trends. With just a few input parameters, such as distance and
|
143 |
+
average speed, our app generates reliable sales forecasts, enabling businesses
|
144 |
+
to optimize their inventory management, production planning, and resource allocation.
|
145 |
+
The user-friendly interface and intuitive design make it easy for users to navigate
|
146 |
+
and obtain actionable predictions. With our Sales Forecasting App,
|
147 |
+
businesses can make informed decisions, mitigate risks,
|
148 |
+
and maximize their revenue potential in an ever-changing market landscape.
|
149 |
+
</p>
|
150 |
+
''', unsafe_allow_html=True)
|
151 |
+
st.markdown("<p style='text-align: center;'>This Sales Forecasting App is developed using Streamlit and Python.</p>", unsafe_allow_html=True)
|
152 |
+
st.markdown("<p style='text-align: center;'>It demonstrates how machine learning can be used to predict sales for the next 8 weeks based on historical data.</p>", unsafe_allow_html=True)
|
date_features.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
|
3 |
+
# Define the getDateFeatures() function
|
4 |
+
def getDateFeatures(df):
|
5 |
+
df['holiday_type'] = 'Workday'
|
6 |
+
df['is_holiday'] = False
|
7 |
+
|
8 |
+
df['year'] = df['date'].dt.year
|
9 |
+
df['month'] = df['date'].dt.month
|
10 |
+
df['dayofmonth'] = df['date'].dt.day
|
11 |
+
df['dayofweek'] = df['date'].dt.dayofweek
|
12 |
+
df['weekofyear'] = df['date'].dt.weekofyear
|
13 |
+
|
14 |
+
df['quarter'] = df['date'].dt.quarter
|
15 |
+
df['is_month_start'] = df['date'].dt.is_month_start.astype(int)
|
16 |
+
df['is_month_end'] = df['date'].dt.is_month_end.astype(int)
|
17 |
+
df['is_quarter_start'] = df['date'].dt.is_quarter_start.astype(int)
|
18 |
+
|
19 |
+
df['is_quarter_end'] = df['date'].dt.is_quarter_end.astype(int)
|
20 |
+
df['is_year_start'] = df['date'].dt.is_year_start.astype(int)
|
21 |
+
df['is_year_end'] = df['date'].dt.is_year_end.astype(int)
|
22 |
+
# Extract the 'year' and 'weekofyear' components from the 'date' column
|
23 |
+
df['year_weekofyear'] = df['date'].dt.year * 100 + df['date'].dt.weekofyear
|
24 |
+
|
25 |
+
# create new coolumns to represent the cyclic nature of a year
|
26 |
+
df['dayofyear'] = df['date'].dt.dayofyear
|
27 |
+
df["sin(dayofyear)"] = np.sin(df["dayofyear"])
|
28 |
+
df["cos(dayofyear)"] = np.cos(df["dayofyear"])
|
29 |
+
|
30 |
+
df["is_weekend"] = np.where(df['dayofweek'] > 4, 1, 0)
|
31 |
+
|
32 |
+
# Define the criteria for each season
|
33 |
+
seasons = {'Winter': [12, 1, 2], 'Spring': [3, 4, 5], 'Summer': [6, 7, 8], 'Autumn': [9, 10, 11]}
|
34 |
+
|
35 |
+
# Create the 'season' column based on the 'date' column
|
36 |
+
df['season'] = df['month'].map({month: season for season, months in seasons.items() for month in months})
|
37 |
+
|
38 |
+
|
39 |
+
return df
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
pandas==1.5.3
|
3 |
+
numpy==1.24.2
|
4 |
+
pillow
|
5 |
+
requests
|
6 |
+
bokeh
|
7 |
+
scikit-learn==1.2.2
|
8 |
+
category_encoders
|