File size: 4,708 Bytes
89b8178
 
6fd2099
89b8178
 
 
729dd0c
89b8178
 
727692a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# import pandas as pd
# import numpy as np
import streamlit as st
# import os
# import time
# import pickle
# import seaborn as sns
# import matplotlib.pyplot as plt
# import pip


try:
    #insert headers
    st.header(" Welcome to Sales Prediction Using Prophet ")
    st.subheader("To help you know your future sales📈...")
    st.image("future.png", width=500, caption="Sales Prediction")

    Disp_results = pd.DataFrame()  # Initialize for download

    # Take input
    with st.form("This form", clear_on_submit=True):
        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 ")

        frequency = str(st.text_input("Frequency 'D' for Daily 'W' for weekly ")).upper()  # convert to string and change to upper

        Number_of_days = int(st.number_input("Number of day(s)/Week(s)"))  # convert to int

        submit = st.form_submit_button("Predict your sales")

        # process the input
        if submit:
            # check if we have the right data type
            if frequency == "D" or frequency == 'W':
                st.success("Inputs received successfully ✅")

                # import model
                with open('prophet_model.pkl', 'rb') as f:
                    model = pickle.load(f)

                # pass inputs to the model(To make predictions, prophet requires number of days and frequency)
                future = model.make_future_dataframe(periods=Number_of_days, freq=str(frequency), include_history=False)

                # Make prediction
                forecast = model.predict(future)

                # show results
                print(f'[INFO]: The whole results {forecast}')

                # pick the relevant columns from the forecast
                sales_forecast = forecast[['ds', 'yhat_lower', 'yhat_upper', 'yhat']]

                # rename the columns
                Disp_results = sales_forecast.rename(columns={'ds': 'Date', 'yhat_lower': 'lowest Expected sales', 'yhat_upper': 'Highest Expected Sales', 'yhat': 'Expected Sales'})

                # print result dataframe to terminal
                print(f'[INFO]: results dataframe {Disp_results}')

                # show progress
                with st.spinner("Prediction in progress..."):
                    time.sleep(2)
                    st.balloons()
                    st.success("Great✅")

                # Display results
                if frequency == "W":
                    output_frequency = 'Week(s)'
                else:
                    output_frequency = 'Day(s)'

                # Check frequency
                st.write(f"These are your predicted sales in the next {Number_of_days} {output_frequency}")
                st.dataframe(Disp_results)

                # Display the graph of sales
                st.title(f"Line Graph Of Predicted Sales Over {Number_of_days} {output_frequency} ")
                # Line Graph
                st.line_chart(data=Disp_results, x='Date', y='Expected Sales')
                print('[INFO]: Line Chart displayed')

            else:
                st.error("Input the right frequency or Days ⚠")

        # Print input to the terminal
        print(f'[INFO]: These are the inputs to the model {Number_of_days},{frequency}')
        print(f"[INFO]: Inputs received")


    # Create a function to convert df to csv
    def convert_to_csv(df):
        return df.to_csv()


    # Create an expander
    expand = st.expander('Download Results as CSV')
    with expand:
        st.download_button(
            'Download results',
            convert_to_csv(Disp_results),
            'prediction_results.csv',
            'text/csv',
            'download'
        )


    # Create Sidebar for Description
    sidebar = st.sidebar.title('Sales Prediction')

    # first option
    option1 = st.sidebar.button('About', key="About")

    # second option
    option2 = st.sidebar.button('About the sales prediction', key="sales prediction")

    # Display text for a selected option
    if option1:
        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 ')

    elif option2:
        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 ')

except:
    st.error('''something went wrong: Make sure you entered the correct number of days
    otherwise contact admin!
    '''
             )