File size: 2,274 Bytes
83a1f22
60cde03
 
83a1f22
60cde03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import joblib
import pandas as pd

MODEL_PATH = 'src/prophet_birth_model.joblib' 

@st.cache_resource
def load_prophet_model():
    try:
        model = joblib.load(MODEL_PATH)
        return model
    except Exception as e:
        st.error(f"Error loading the Prophet model. Check if '{MODEL_PATH}' is uploaded and if the 'prophet' library is installed. Error: {e}")
        return None

# --- Streamlit Interface ---
st.set_page_config(page_title="Births Forecast App", layout="centered")
st.title("👶 Daily Births Forecasting (Prophet Model)")
st.markdown("Enter the number of future days you want to predict.")

model = load_prophet_model()

if model is not None:
    st.sidebar.header("Prediction Settings")

    n_periods = st.sidebar.slider("Future Days to Forecast:", min_value=1, max_value=365, value=30)
    
    if st.button(f"Generate Forecast for {n_periods} Days"):
        with st.spinner(f'Generating forecast...'):
            try:
                # 1. Generate future dates (using Prophet's built-in method)
                future = model.make_future_dataframe(periods=n_periods)
                
                # 2. Predict the values
                forecast = model.predict(future)
                
                # Filter to show only the new forecasted data (the last N rows)
                forecasted_data = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(n_periods)
                
                st.success("Forecast Successful!")

                # --- Visualization ---
                st.subheader("Predicted Daily Births")
                
                # Prepare data for Streamlit's simple line chart
                plot_data = forecasted_data[['ds', 'yhat']].rename(columns={'ds': 'Date', 'yhat': 'Predicted Births'})
                plot_data = plot_data.set_index('Date')
                
                st.line_chart(plot_data)

                # --- Data Display ---
                st.subheader("Raw Forecast Data (Next 5 Days)")
                st.dataframe(forecasted_data[['ds', 'yhat', 'yhat_upper']].head(), hide_index=True)
                
            except Exception as e:
                st.error(f"Error during forecasting process. Check the model input requirements. Error: {e}")