Skip to content

PythonToGo/energy-forecasting

Repository files navigation

πŸ”‹ Energy Forecasting Dashboard

An end-to-end MLOps pipeline for predicting household energy consumption using time-based features, built with FastAPI, Streamlit, MLflow, DVC, and Prefect.
This project is fully containerized and reproducible via Docker.

πŸš€ Current setup: Local deployment via Docker on 0.0.0.0.


πŸ“Œ Project Overview

This project forecasts household hourly electricity usage based on:

  • Hour of the day
  • Day of the week
  • Month

It features:

  • XGBoost regression model
  • A real-time FastAPI inference service
  • A user-friendly Streamlit dashboard
  • End-to-end experiment tracking with MLflow
  • DVC for data/model versioning
  • Prefect for pipeline automation
  • Packaged & deployable with Docker

🧠 Model

  • Model type: XGBoostRegressor
  • 17 features built by the shared pipeline in src/features.py (guarantees train/serve parity):
    • Calendar (10): hour, dayofweek, month, is_weekend + cyclical (sin/cos) encodings
    • Lags (3): consumption 1h / 24h / 168h ago
    • Rolling (4): mean & std over the past 24h and 168h
  • Target: Global_active_power (kW)
  • Because the model uses recent consumption, the API forecasts recursively: each predicted hour is fed back in to build the next hour's features.

πŸ—‚οΈ Project Structure

energy-forecasting/
β”œβ”€β”€ api/                    # FastAPI inference server
β”‚   └── main.py
β”œβ”€β”€ dashboard/              # Streamlit dashboard
β”‚   └── app.py
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/                # Original dataset (from UCI)
β”‚   └── processed/          # Cleaned + resampled data
β”œβ”€β”€ models/                 # Trained model files (via DVC)
β”‚   └── latest_model_path.txt
β”œβ”€β”€ mlops/
β”‚   β”œβ”€β”€ mlflow_config.yaml
β”‚   └── register_model.py
β”œβ”€β”€ pipelines/              # Prefect automation
β”‚   └── prefect_flow.py
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ features.py         # Shared feature pipeline (train/serve parity)
β”‚   β”œβ”€β”€ data_loader.py      # Preprocessing script
β”‚   └── train_model.py      # Model training & logging
β”œβ”€β”€ tests/                  # pytest unit tests
β”œβ”€β”€ pyproject.toml          # Deps + ruff / mypy / pytest config
β”œβ”€β”€ Dockerfile              # Container setup
β”œβ”€β”€ docker-compose.yaml     # Service orchestration
β”œβ”€β”€ dvc.yaml                # Pipeline stages
β”œβ”€β”€ dvc.lock
β”œβ”€β”€ requirements.txt
└── README.md

βš™οΈ MLOps Pipeline Overview

Stage Tool Description
Data versioning DVC Tracks data and models (e.g. energy_clean.csv)
Training XGBoost 17 features: calendar + lags + rolling stats
Experiment tracking MLflow Logs parameters, metrics, model artifacts
Automation Prefect Defines retraining pipeline (data β†’ train)
Serving FastAPI Recursive multi-step forecast on /forecast
Monitoring UI Streamlit Frontend to submit inputs & visualize results
Packaging Docker Full stack in one container

πŸ“¦ How to Run Locally (Docker)

# 1. Build and start
docker-compose up --build

# 2. Access:
FastAPI     β†’ http://localhost:8000
Streamlit   β†’ http://localhost:8502
MLflow UI   β†’ http://localhost:5050

πŸš€ Forecasting API (FastAPI)

The model is stateful: it forecasts forward from the latest observed data.

GET /forecast?horizon=24 β€” recursive multi-step forecast (1–168 hours):

{
  "from_timestamp": "2010-11-26T20:00:00",
  "horizon_hours": 24,
  "forecast": [
    {"timestamp": "2010-11-26T21:00:00", "predicted_energy_kW": 1.234}
  ]
}

Also: GET /predict (single next hour), GET /model/info (features, metrics, baseline skill), GET /health (liveness).


πŸ–₯️ Dashboard (Streamlit)

Access: http://localhost:8502

  • Pick a forecast horizon (1–168 h) and run a recursive forward forecast
  • View the predicted consumption curve
  • See model metrics & baseline skill in the sidebar
  • Compare against recent actual consumption

image

πŸ”„ Reproducible Training (DVC)

# Run full pipeline
dvc repro

# Push data + model versions to remote (optional)
dvc push

πŸ” Model Retraining (Prefect)

python pipelines/prefect_flow.py

Runs:

  • data_loader.py β†’ preprocessing
  • train_model.py β†’ model training + MLflow logging

πŸ§ͺ Track Experiments (MLflow)

image

Visit: http://localhost:5050
Browse runs, parameters, metrics, models.


πŸ“„ Dataset Info

  • Source: UCI - Individual household electric power consumption
  • Resampled to hourly intervals
  • Target: Global_active_power

πŸ› οΈ Tech Stack

  • Python 3.10
  • FastAPI, Streamlit
  • xgboost, scikit-learn, pandas
  • MLflow, Prefect, DVC
  • Docker, docker-compose

🧠 Author

πŸ‘¨β€πŸ’» Taey Kim
πŸ“« GitHub πŸ’‘ Passionate about MLOps, system automation, and real-time inference!


πŸ“Œ To-Do

  • Add CI/CD via GitHub Actions
  • Deploy to Heroku / Fly.io
  • Batch forecasting + scheduling
  • User login for dashboard

MIT License | 2025

About

Real-time household energy consumption forecasting system powered by XGBoost and FastAPI. Includes MLOps automation with DVC, MLflow, Prefect, and Streamlit dashboard.

Topics

Resources

License

Stars

3 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors