Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
import os
|
5 |
+
import time
|
6 |
+
import pickle
|
7 |
+
import seaborn as sns
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
import pip
|
10 |
+
|
11 |
+
|
12 |
+
try:
|
13 |
+
#insert headers
|
14 |
+
st.header(" Welcome to Sales Prediction Using Prophet ")
|
15 |
+
st.subheader("To help you know your future sales📈...")
|
16 |
+
st.image("future.png", width=500, caption="Sales Prediction")
|
17 |
+
|
18 |
+
Disp_results = pd.DataFrame() # Initialize for download
|
19 |
+
|
20 |
+
# Take input
|
21 |
+
with st.form("This form", clear_on_submit=True):
|
22 |
+
st.subheader("Enter the number of day(s)/Week(s) you want to predict, And the frequency as D for Daily or W for weekly ")
|
23 |
+
|
24 |
+
frequency = str(st.text_input("Frequency 'D' for Daily 'W' for weekly ")).upper() # convert to string and change to upper
|
25 |
+
|
26 |
+
Number_of_days = int(st.number_input("Number of day(s)/Week(s)")) # convert to int
|
27 |
+
|
28 |
+
submit = st.form_submit_button("Predict your sales")
|
29 |
+
|
30 |
+
# process the input
|
31 |
+
if submit:
|
32 |
+
# check if we have the right data type
|
33 |
+
if frequency == "D" or frequency == 'W':
|
34 |
+
st.success("Inputs received successfully ✅")
|
35 |
+
|
36 |
+
# import model
|
37 |
+
with open('prophet_model.pkl', 'rb') as f:
|
38 |
+
model = pickle.load(f)
|
39 |
+
|
40 |
+
# pass inputs to the model(To make predictions, prophet requires number of days and frequency)
|
41 |
+
future = model.make_future_dataframe(periods=Number_of_days, freq=str(frequency), include_history=False)
|
42 |
+
|
43 |
+
# Make prediction
|
44 |
+
forecast = model.predict(future)
|
45 |
+
|
46 |
+
# show results
|
47 |
+
print(f'[INFO]: The whole results {forecast}')
|
48 |
+
|
49 |
+
# pick the relevant columns from the forecast
|
50 |
+
sales_forecast = forecast[['ds', 'yhat_lower', 'yhat_upper', 'yhat']]
|
51 |
+
|
52 |
+
# rename the columns
|
53 |
+
Disp_results = sales_forecast.rename(columns={'ds': 'Date', 'yhat_lower': 'lowest Expected sales', 'yhat_upper': 'Highest Expected Sales', 'yhat': 'Expected Sales'})
|
54 |
+
|
55 |
+
# print result dataframe to terminal
|
56 |
+
print(f'[INFO]: results dataframe {Disp_results}')
|
57 |
+
|
58 |
+
# show progress
|
59 |
+
with st.spinner("Prediction in progress..."):
|
60 |
+
time.sleep(2)
|
61 |
+
st.balloons()
|
62 |
+
st.success("Great✅")
|
63 |
+
|
64 |
+
# Display results
|
65 |
+
if frequency == "W":
|
66 |
+
output_frequency = 'Week(s)'
|
67 |
+
else:
|
68 |
+
output_frequency = 'Day(s)'
|
69 |
+
|
70 |
+
# Check frequency
|
71 |
+
st.write(f"These are your predicted sales in the next {Number_of_days} {output_frequency}")
|
72 |
+
st.dataframe(Disp_results)
|
73 |
+
|
74 |
+
# Display the graph of sales
|
75 |
+
st.title(f"Line Graph Of Predicted Sales Over {Number_of_days} {output_frequency} ")
|
76 |
+
# Line Graph
|
77 |
+
st.line_chart(data=Disp_results, x='Date', y='Expected Sales')
|
78 |
+
print('[INFO]: Line Chart displayed')
|
79 |
+
|
80 |
+
else:
|
81 |
+
st.error("Input the right frequency or Days ⚠")
|
82 |
+
|
83 |
+
# Print input to the terminal
|
84 |
+
print(f'[INFO]: These are the inputs to the model {Number_of_days},{frequency}')
|
85 |
+
print(f"[INFO]: Inputs received")
|
86 |
+
|
87 |
+
|
88 |
+
# Create a function to convert df to csv
|
89 |
+
def convert_to_csv(df):
|
90 |
+
return df.to_csv()
|
91 |
+
|
92 |
+
|
93 |
+
# Create an expander
|
94 |
+
expand = st.expander('Download Results as CSV')
|
95 |
+
with expand:
|
96 |
+
st.download_button(
|
97 |
+
'Download results',
|
98 |
+
convert_to_csv(Disp_results),
|
99 |
+
'prediction_results.csv',
|
100 |
+
'text/csv',
|
101 |
+
'download'
|
102 |
+
)
|
103 |
+
|
104 |
+
|
105 |
+
# Create Sidebar for Description
|
106 |
+
sidebar = st.sidebar.title('Sales Prediction')
|
107 |
+
|
108 |
+
# first option
|
109 |
+
option1 = st.sidebar.button('About', key="About")
|
110 |
+
|
111 |
+
# second option
|
112 |
+
option2 = st.sidebar.button('About the sales prediction', key="sales prediction")
|
113 |
+
|
114 |
+
# Display text for a selected option
|
115 |
+
if option1:
|
116 |
+
st.sidebar.write('This is a Sales prediction app Using Prophet(Developed by meta), this project was done under the Azubi Africa Data Analysis Training program ')
|
117 |
+
|
118 |
+
elif option2:
|
119 |
+
st.sidebar.write('This is a time series analysis & forecasting problem. In this project, we shalll predict store sales on data from Corporation Favorita, a large Ecuadorian-based grocery retailer. Specifically, this app predicts the sales for up to weeks in advance for Corporation Favorita ')
|
120 |
+
|
121 |
+
except:
|
122 |
+
st.error('''something went wrong: Make sure you entered the correct number of days
|
123 |
+
otherwise contact admin!
|
124 |
+
'''
|
125 |
+
)
|
126 |
+
|