Forecast / main.py
muddasser's picture
Upload 4 files
4583e07 verified
import streamlit as st
import pandas as pd
import pickle
import matplotlib.pyplot as plt
# --------------------
# LOAD TRAINED MODEL
# --------------------
MODEL_PATH = "prophet_model.pkl" # updated to match your file
try:
with open(MODEL_PATH, "rb") as f:
model = pickle.load(f)
st.success("βœ… Loaded saved Prophet model.")
except FileNotFoundError:
model = None
st.warning("⚠ No saved model found. You can still upload CSV for chart display.")
# --------------------
# UPLOAD CSV FILE
# --------------------
st.title("πŸ“Š Prophet Forecast Viewer")
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
st.subheader("Data Preview")
st.write(df.head())
# Prepare data for Prophet if model exists
if model is not None:
try:
# Ensure Prophet format
df = df.rename(columns={df.columns[0]: 'ds'})
df['ds'] = pd.to_datetime(df['ds'])
predictions = model.predict(df)
st.subheader("Predictions")
st.write(predictions[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].head())
# Plot
fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(df['ds'], predictions['yhat'], label="Forecast", color="blue")
ax.fill_between(df['ds'], predictions['yhat_lower'], predictions['yhat_upper'], alpha=0.2)
ax.set_title("Forecast Chart")
ax.set_xlabel("Date")
ax.set_ylabel("Value")
ax.legend()
st.pyplot(fig)
except Exception as e:
st.error(f"Prediction failed: {e}")
else:
st.info("πŸ“‚ Please upload a CSV file to begin.")