Justin-J commited on
Commit
ae2e769
·
1 Parent(s): cb061dd

added my project files

Browse files
Files changed (5) hide show
  1. app.py +157 -0
  2. date_features.py +39 -0
  3. encoder.pkl +3 -0
  4. model.pkl +3 -0
  5. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+ # Set up sidebar
29
+ st.sidebar.header('Navigation')
30
+ menu = ['Home', 'About']
31
+ choice = st.sidebar.selectbox("Select an option", menu)
32
+
33
+ # predict function
34
+ def predict(sales_data):
35
+ if sales_data.empty:
36
+ raise ValueError("No sales data provided.")
37
+
38
+ else:
39
+ # Perform the necessary data processing steps
40
+ sales_data = getDateFeatures(sales_data).set_index('date')
41
+ numeric_columns = ['onpromotion', 'year', 'month', 'dayofmonth', 'dayofweek', 'dayofyear', 'weekofyear', 'quarter', 'year_weekofyear', 'sin(dayofyear)', 'cos(dayofyear)']
42
+ 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']
43
+
44
+ num = sales_data[numeric_columns]
45
+ encoded_cat = encoder.transform(sales_data[categoric_columns])
46
+ sales_data = pd.concat([num, encoded_cat], axis=1)
47
+
48
+ # Make predictions using the pre-trained model
49
+ predicted_sales = model.predict(sales_data)
50
+
51
+ return predicted_sales
52
+
53
+ # Home section
54
+ if choice == 'Home':
55
+ # Set Page Title
56
+ st.title('SEER- A Sales Forecasting APP')
57
+
58
+ # Loading GIF
59
+ gif_url = "https://i.gifer.com/7BzK.gif"
60
+ st.image(gif_url, use_column_width=True)
61
+
62
+ st.markdown("<h1 style='text-align: center;'>Welcome</h1>", unsafe_allow_html=True)
63
+ st.markdown("<p style='text-align: center;'>This is a Sales Forecasting App.</p>", unsafe_allow_html=True)
64
+
65
+ st.markdown('Enter the required information to forecast sales:')
66
+
67
+ # defining input parameters
68
+ Store_type = ["Supermarket", "Department Store", "Convenience Store", "Pharmacy", "Clothing Store", "Electronics Store", "Furniture Store", "Hardware Store", "Bookstore", "Jewelry Store", "Toy Store", "Pet Store", "Sporting Goods Store", "Shoe Store", "Dollar Store", "Liquor Store", "Beauty Supply Store", "Home Improvement Store", "Stationery Store", "Gift Shop", "Bakery", "Butcher Shop", "Fish Market", "Vegetable Market", "Farmers Market", "Coffee Shop", "Café", "Restaurant", "Fast Food Restaurant", "Pizza Place", "Burger Joint", "Ice Cream Shop", "Food Truck", "Bar", "Pub", "Nightclub", "Gas Station", "Car Dealership", "Auto Repair Shop", "Car Wash", "Bank", "ATM", "Post Office", "Laundry", "Hair Salon", "Nail Salon", "Spa", "Gym", "Yoga Studio", "Movie Theater", "Bowling Alley", "Arcade", "Museum", "Art Gallery"]
69
+ Store_id = ['Store_' + str(i) for i in range(0, 5)]
70
+ cities = ["Lagos", "Abuja", "Kano", "Ibadan", "Kaduna", "Port Harcourt", "Benin City", "Maiduguri", "Zaria", "Aba", "Jos", "Ilorin", "Oyo", "Enugu", "Abeokuta", "Onitsha", "Warri", "Sokoto", "Calabar", "Katsina", "Akure", "Bauchi"]
71
+ clusters = ["Fashion", "Electronics", "Supermarket", "Home Improvement", "Department Store","Pharmacy", "Furniture", "Sports Goods", "Jewelry", "Cosmetics", "Automotive","Bookstore", "Toy Store", "Pet Store", "Convenience Store", "Hardware Store","Outdoor Recreation"]
72
+ categories = ["Apparel", "Beauty", "Books", "Electronics", "Furniture", "Grocery", "Health", "Home", "Jewelry", "Kitchen", "Music", "Office", "Outdoors", "Pets", "Shoes", "Sports", "Toys", "Automotive", "Baby", "Computers", "Garden", "Movies", "Tools", "Watches", "Appliances", "Cameras", "Fitness", "Industrial", "Luggage", "Software", "Video Games", "Cell Phones", "Home Improvement"]
73
+
74
+ # Input form
75
+ col1, col2 = st.columns(2)
76
+
77
+ with col1:
78
+ start_date = st.date_input("Start Date")
79
+ # Convert the date to datetime format
80
+ start_date = pd.to_datetime(start_date)
81
+ end_date = st.date_input("End Date")
82
+ # Convert the date to datetime format
83
+ end_date = pd.to_datetime(end_date)
84
+ onpromotion = st.number_input("How many products are on promotion?", min_value=0, step=1)
85
+ selected_category = st.selectbox("Product_Category", categories)
86
+
87
+
88
+ with col2:
89
+ selected_store = st.selectbox("Store_type", Store_type)
90
+ selected_store1 = st.selectbox("Store_id", Store_id)
91
+ selected_city = st.selectbox("City", cities)
92
+ selected_cluster = st.selectbox("Cluster", clusters)
93
+
94
+ predicted_data = pd.DataFrame(columns=['Start Date', 'End Date', 'Store', 'Category', 'On Promotion', 'City', 'Cluster', 'Predicted Sales'])
95
+
96
+
97
+ if st.button('Predict'):
98
+ if start_date > end_date:
99
+ st.error("Start date should be earlier than the end date.")
100
+ else:
101
+ with st.spinner('Predicting sales...'):
102
+ sales_data = pd.DataFrame({
103
+ 'date': pd.date_range(start=start_date, end=end_date),
104
+ 'store_id': [selected_store] * len(pd.date_range(start=start_date, end=end_date)),
105
+ 'category_id': [selected_category] * len(pd.date_range(start=start_date, end=end_date)),
106
+ 'onpromotion': [onpromotion] * len(pd.date_range(start=start_date, end=end_date)),
107
+ 'city': [selected_city] * len(pd.date_range(start=start_date, end=end_date)),
108
+ 'store_type': [selected_store1] * len(pd.date_range(start=start_date, end=end_date)),
109
+ 'cluster': [selected_cluster] * len(pd.date_range(start=start_date, end=end_date))
110
+ })
111
+ try:
112
+ sales = predict(sales_data)
113
+ formatted_sales = round(sales[0], 2)
114
+ predicted_data = predicted_data.append({
115
+ 'Start Date': start_date,
116
+ 'End Date': end_date,
117
+ 'Store': selected_store,
118
+ 'Category': selected_category,
119
+ 'On Promotion': onpromotion,
120
+ 'City': selected_city,
121
+ 'Cluster': selected_cluster,
122
+ 'Predicted Sales': formatted_sales}, ignore_index=True)
123
+
124
+ st.success(f"Total sales for the period is: #{formatted_sales}")
125
+ except ValueError as e:
126
+ st.error(str(e))
127
+
128
+ if st.button('Clear Data'):
129
+ predicted_data = pd.DataFrame(columns=['Start Date', 'End Date', 'Store', 'Category', 'On Promotion', 'City', 'Cluster', 'Predicted Sales'])
130
+ st.success("Data cleared successfully.")
131
+
132
+
133
+
134
+ # About section
135
+ elif choice == 'About':
136
+ # Load the banner image
137
+ banner_image_url = "https://guardian.ng/wp-content/uploads/2017/03/Sales-targets.jpg"
138
+
139
+ # Display the banner image
140
+ st.image(
141
+ banner_image_url,
142
+ use_column_width=True,
143
+ width=400)
144
+ st.markdown('''
145
+ <p style='font-size: 20px; font-style: italic;font-style: bold;'>
146
+ SEER is a powerful tool designed to assist businesses in making accurate
147
+ and data-driven sales predictions. By leveraging advanced algorithms and
148
+ machine learning techniques, our app provides businesses with valuable insights
149
+ into future sales trends. With just a few input parameters, such as distance and
150
+ average speed, our app generates reliable sales forecasts, enabling businesses
151
+ to optimize their inventory management, production planning, and resource allocation.
152
+ The user-friendly interface and intuitive design make it easy for users to navigate
153
+ and obtain actionable predictions. With our Sales Forecasting App,
154
+ businesses can make informed decisions, mitigate risks,
155
+ and maximize their revenue potential in an ever-changing market landscape.
156
+ </p>
157
+ ''', 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
encoder.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:22a1fc015481505b761fab9debd936f8fcd598aefcedd76ada7cf26ad09709ef
3
+ size 16541
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:11ce0047dc890be988faa21eb1054703f2abd0afd4ce7159f9975564a943323f
3
+ size 6544838
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