File size: 1,740 Bytes
4583e07 | 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 | 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.")
|