File size: 3,157 Bytes
0d45c02
ece6fc5
 
 
 
 
 
0d45c02
e76690d
0d45c02
ece6fc5
0d45c02
e76690d
0d45c02
ece6fc5
0d45c02
 
ece6fc5
0d45c02
 
 
 
ece6fc5
0d45c02
 
 
 
 
 
ece6fc5
 
0d45c02
ece6fc5
0d45c02
 
ece6fc5
 
b79b363
0d45c02
ece6fc5
 
 
 
 
 
0d45c02
ece6fc5
0d45c02
 
ece6fc5
0d45c02
ece6fc5
0d45c02
 
 
 
 
ece6fc5
0d45c02
 
ece6fc5
e43d8b7
0d45c02
3001f34
0d45c02
3001f34
 
 
 
f5de927
3001f34
0d45c02
b79b363
0d45c02
b79b363
0d45c02
 
 
 
b79b363
0d45c02
 
f5de927
0d45c02
 
f5de927
0d45c02
 
 
 
 
f5de927
0d45c02
 
ece6fc5
7a6822c
 
 
 
 
0d45c02
 
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
# Libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import streamlit as st
import pickle

# Load model
with open('src/model_finpro_2.pkl', 'rb') as file:
    model = pickle.load(file)

# Load test data
with open('src/data_test_finpro_2.pkl', 'rb') as file:
    test = pickle.load(file)

# Pastikan date dalam datetime format
test.index = pd.to_datetime(test.index)

# Forecasting function
def forecast_sales(n_days, qty_sum_input):   
    # Future qty_sum sebagai eksogen variabel
    future_exog_vars = pd.DataFrame(qty_sum_input, columns=['qty_sum'])

    # Buat index tanggal untuk prediksi
    forecast_index = pd.date_range(start=test.index[-1] + pd.Timedelta(days=1), 
                                   periods=n_days, freq='D')

    # Forecast
    forecast = model.get_forecast(steps=n_days, exog=future_exog_vars)
    forecast_mean = forecast.predicted_mean

    # Visualisasi
    fig, ax = plt.subplots(figsize=(10, 6))
    ax.plot(forecast_index, forecast_mean, label='Forecasted Total Sales', color='red', linestyle='--')
    ax.set_title('Forecast of Total Sales Over the Next Days')
    ax.set_xlabel('Date')
    ax.set_ylabel('Total Sales')
    ax.legend()
    ax.grid(True)
    plt.xticks(rotation=45)
    plt.tight_layout()

    return fig, forecast_index, forecast_mean


# Streamlit app
def run():

    # --- Sidebar ---
    st.sidebar.image("src/Amazon.png", use_container_width=True)
    st.sidebar.title("SalesBoost")
    st.sidebar.markdown("""
    **Team Members**
    - πŸ‘©β€πŸ”¬ Avisa Rahma Benedicta (Data Scientist)
    - πŸ‘¨β€πŸ’» Muhammad Farhan Hendriyanto (Data Engineer)
    - πŸ‘©β€πŸ”¬ Neila Ismahunnisa (Data Analyst)
    - πŸ‘©β€πŸ”¬ Sesilia Virdha Kezia (Data Scientist)            
    """)
    st.sidebar.markdown("""
    **Batch HCK-027** """)

    st.title("πŸ›οΈ Forecast Future Sales")
    st.markdown(""" 
    
    **Guidelines:**
    - Use the slider below to select how many days you'd like to forecast.
        *Note: The larger the day selected, the higher the chance the prediction will be less accurate.*
        
    - Enter your expected daily quantity values.
    
    - Click **Generate Forecast** to visualize the results.
    """)
    
    n_days = st.slider("Number of Days to Forecast", 1, 30, 10)
    
    qty_sum_input = []
    for i in range(n_days):
        val = st.number_input(f"Day {i+1} Total Quantity", value=100, step=1)
        qty_sum_input.append(val)
    
    if st.button("Generate Forecast"):
        fig, forecast_index, forecast_mean = forecast_sales(n_days, qty_sum_input)
        
        # Tampilkan plot
        st.pyplot(fig)
        
        # Tampilkan tabel hasil
        forecast_df = pd.DataFrame({
            'Date': forecast_index.strftime('%d-%m-%Y'),  # Format jadi dd-mm-yyyy
            'Forecasted Total Sales': forecast_mean
        }).reset_index(drop=True)
        
        st.write("### Forecast Table")
        st.write(forecast_df)

    st.markdown("---")
    st.markdown("### πŸ™ Ikan hiu makan tomat \
                \
                Thank you for exploring with us! πŸ™")

if __name__ == "__main__":
    run()