Spaces:
Sleeping
Sleeping
Upload seer.py
Browse files
seer.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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="ETA 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"
|
| 30 |
+
|
| 31 |
+
# Set up sidebar
|
| 32 |
+
st.sidebar.header('Navigation')
|
| 33 |
+
menu = ['Home', 'About']
|
| 34 |
+
choice = st.sidebar.selectbox("Select an option", menu)
|
| 35 |
+
|
| 36 |
+
def predict(sales_data):
|
| 37 |
+
sales_data = getDateFeatures(sales_data).set_index('date')
|
| 38 |
+
# print(sales_data.columns)
|
| 39 |
+
|
| 40 |
+
# Make predictions for the next 8 weeks
|
| 41 |
+
prediction_inputs = [] # Initialize the list for prediction inputs
|
| 42 |
+
|
| 43 |
+
# Encode the prediction inputs
|
| 44 |
+
# numeric_columns = sales_data.select_dtypes(include=['int64', 'float64']).columns.tolist()
|
| 45 |
+
numeric_columns = ['onpromotion', 'year', 'month', 'dayofmonth', 'dayofweek', 'dayofyear', 'weekofyear', 'quarter', 'year_weekofyear', 'sin(dayofyear)', 'cos(dayofyear)']
|
| 46 |
+
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']
|
| 47 |
+
print(categoric_columns)
|
| 48 |
+
# encoder = BinaryEncoder(drop_invariant=False, return_df=True,)
|
| 49 |
+
# encoder.fit(sales_data[categoric_columns])
|
| 50 |
+
num = sales_data[numeric_columns]
|
| 51 |
+
encoded_cat = encoder.transform(sales_data[categoric_columns])
|
| 52 |
+
sales_data = pd.concat([num, encoded_cat], axis=1)
|
| 53 |
+
|
| 54 |
+
# Make the prediction using the loaded machine learning model
|
| 55 |
+
predicted_sales = model.predict(sales_data)
|
| 56 |
+
|
| 57 |
+
return predicted_sales
|
| 58 |
+
|
| 59 |
+
# Home section
|
| 60 |
+
if choice == 'Home':
|
| 61 |
+
st.image(gif_url, use_column_width=True)
|
| 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 |
+
# Set Page Title
|
| 66 |
+
st.title('SEER- A Sales Forecasting APP')
|
| 67 |
+
st.markdown('Enter the required information to forecast sales:')
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Input form
|
| 71 |
+
col1, col2 = st.columns(2)
|
| 72 |
+
|
| 73 |
+
Stores = ['Store_' + str(i) for i in range(1, 55)]
|
| 74 |
+
Stores1 = ['Store_' + str(i) for i in range(0, 5)]
|
| 75 |
+
cities = ['city_' + str(i) for i in range(22)]
|
| 76 |
+
clusters = ['cluster_' + str(i) for i in range(17)]
|
| 77 |
+
categories = ['Category_' + str(i) for i in range(33)]
|
| 78 |
+
|
| 79 |
+
with col1:
|
| 80 |
+
date = st.date_input("Date")
|
| 81 |
+
# Convert the date to datetime format
|
| 82 |
+
date = pd.to_datetime(date)
|
| 83 |
+
onpromotion = st.number_input("How many products are on promotion?", min_value=0, step=1)
|
| 84 |
+
selected_category = st.selectbox("Category", categories)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
with col2:
|
| 88 |
+
selected_store = st.selectbox("Store_type", Stores)
|
| 89 |
+
selected_store1 = st.selectbox("Store_id", Stores1)
|
| 90 |
+
selected_city = st.selectbox("City", cities)
|
| 91 |
+
selected_cluster = st.selectbox("Cluster", clusters)
|
| 92 |
+
|
| 93 |
+
# Call getDateFeatures() function on sales_data (replace sales_data with your DataFrame)
|
| 94 |
+
sales_data = pd.DataFrame({
|
| 95 |
+
'date': [date],
|
| 96 |
+
'store_id': [selected_store],
|
| 97 |
+
'category_id': [selected_category],
|
| 98 |
+
'onpromotion': [onpromotion],
|
| 99 |
+
'city' :[selected_city],
|
| 100 |
+
'store_type': [selected_store1],
|
| 101 |
+
'cluster':[selected_cluster]
|
| 102 |
+
})
|
| 103 |
+
print(sales_data)
|
| 104 |
+
print(sales_data.info())
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
if st.button('Predict'):
|
| 108 |
+
with st.spinner('Predicting sales...'):
|
| 109 |
+
sales = predict(sales_data)
|
| 110 |
+
formatted_sales = round(sales[0], 2)
|
| 111 |
+
st.success(f"Total sales for this week is: #{formatted_sales}")
|
| 112 |
+
|
| 113 |
+
# About section
|
| 114 |
+
elif choice == 'About':
|
| 115 |
+
# Load the banner image
|
| 116 |
+
banner_image_url = "https://raw.githubusercontent.com/Gilbert-B/Forecasting-Sales/0d7b869515bdf5551672f71b6e1f62be9902e3dc/app/seer.png"
|
| 117 |
+
banner_image = Image.open(requests.get(banner_image_url, stream=True).raw)
|
| 118 |
+
|
| 119 |
+
# Display the banner image
|
| 120 |
+
st.image(banner_image, use_column_width=True)
|
| 121 |
+
st.markdown('''
|
| 122 |
+
<p style='font-size: 20px; font-style: italic;font-style: bold;'>
|
| 123 |
+
SEER is a powerful tool designed to assist businesses in making accurate
|
| 124 |
+
and data-driven sales predictions. By leveraging advanced algorithms and
|
| 125 |
+
machine learning techniques, our app provides businesses with valuable insights
|
| 126 |
+
into future sales trends. With just a few input parameters, such as distance and
|
| 127 |
+
average speed, our app generates reliable sales forecasts, enabling businesses
|
| 128 |
+
to optimize their inventory management, production planning, and resource allocation.
|
| 129 |
+
The user-friendly interface and intuitive design make it easy for users to navigate
|
| 130 |
+
and obtain actionable predictions. With our Sales Forecasting App,
|
| 131 |
+
businesses can make informed decisions, mitigate risks,
|
| 132 |
+
and maximize their revenue potential in an ever-changing market landscape.
|
| 133 |
+
</p>
|
| 134 |
+
''', unsafe_allow_html=True)
|
| 135 |
+
st.markdown("<p style='text-align: center;'>This Sales Forecasting App is developed using Streamlit and Python.</p>", unsafe_allow_html=True)
|
| 136 |
+
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)
|
| 137 |
+
|
| 138 |
+
|