AI Predictive Maintenance System with Explainable AI (XAI)

Production API SCADA GUI Model Engine XAI Attributions

An end-to-end industrial predictive maintenance application built around Clean Architecture and SOLID design principles. The system ingests raw machine telemetry, executes data pipelines, evaluates classification probabilities, analyzes SHAP feature attributions, and serves outputs via a REST gateway API and a premium SCADA-style dashboard interface.


1. Project Overview & Problem Statement

In modern manufacturing, unexpected equipment failures result in severe financial overhead and production line shutdowns. Traditional maintenance operates reactively or based on rigid scheduling intervals, both of which are highly inefficient.

This system leverages machine learning classifiers trained on industrial telemetry records to detect imminent machine failures beforehand. To gain the trust of floor operators and comply with industrial audit standards, the system embeds an Explainable AI (XAI) layer using SHAP. Instead of a black-box warning, operators receive exact probability contributions (e.g. Tool wear +31%, Torque +18%) and dynamic human-readable guidance text.


2. System Architecture

The data flow and software components follow strict separations of concern:

                  ┌───────────────────────┐
                  │   ai4i2020.csv Data   │
                  └───────────┬───────────┘
                              │
                              ▼
                  ┌───────────────────────┐
                  │ Data Preprocessing    │ ◄─── Schema check, Clean, Split
                  └───────────┬───────────┘
                              │
                              ▼
                  ┌───────────────────────┐
                  │ Model Training Grid   │ ◄─── baseline classifiers fit
                  └───────────┬───────────┘
                              │
                              ▼
                  ┌───────────────────────┐
                  │ Hyperparameter Tuning │ ◄─── CV stability, Threshold Scan
                  └───────────┬───────────┘
                              │
                              ▼
                  ┌───────────────────────┐
                  │  Production Pipeline  │ ◄─── Serialized combined binary
                  └───────────┬───────────┘
                              │
                              ▼
                  ┌───────────────────────┐
                  │      FastAPI App      │ ◄─── Single-load Startup lifespan
                  └───────────┬───────────┘
                              │
                  ┌───────────┴───────────┐
                  ▼                       ▼
       ┌────────────────────┐   ┌────────────────────┐
       │  Local SHAP Engine │   │   REST endpoints   │
       └──────────┬─────────┘   └─────────┬──────────┘
                  │                       │
                  └───────────┬───────────┘
                              │
                              ▼
                  ┌───────────────────────┐
                  │    SCADA Dashboard    │ ◄─── Gauges, Simulators, Warnings
                  └───────────────────────┘

3. Project Structure

├── api/                   # REST API Gateway application
│   ├── dependencies/      # FastAPI dependency injection modules
│   ├── exceptions/        # Custom exception handlers & mapping
│   ├── middleware/        # Timing and logging request middlewares
│   ├── routers/           # Endpoint path configurations (info, inference)
│   └── schemas/           # Pydantic validation request/response models
├── config/                # App configuration management setup
├── dashboard/             # Streamlit SCADA web dashboard
│   ├── components/        # Sub-panels (fleet grid, dials, SHAP, trends)
│   └── services/          # API consumer client wrappers
├── dataset/               # Telemetry records storage (git-ignored)
├── models/                # Local serialized preprocessor & estimators
├── reports/               # HTML figures and markdown evaluation logs
├── src/                   # Core business logic pipelines
│   ├── explainability/    # SHAP explainer & local waterfall logic
│   ├── models/            # Hyperparameter tuning & train orchestrators
│   └── preprocessing/     # Cleaners, validators, encoders
└── tests/                 # Integration and unit test suites

4. Technology Stack

  • Core Logic & Frameworks: Python 3.10, FastAPI, Streamlit.
  • Machine Learning & Mathematics: scikit-learn, XGBoost, SHAP, joblib, pandas, NumPy.
  • Visuals & Charts: Plotly, Custom HTML/CSS.
  • Testing & Quality Assurance: pytest, TestClient, Docker.

5. Dataset Description

The system processes the AI4I 2020 Predictive Maintenance Dataset containing 10,000 observations of machine statuses:

  • Type: Quality grade (L: Low, M: Medium, H: High).
  • Air Temperature [K]: Spindle cooling air heat level.
  • Process Temperature [K]: Internal tool processing heat level.
  • Rotational Speed [rpm]: Spindle rotation speed.
  • Torque [Nm]: Load spindle torque.
  • Tool Wear [min]: Cutting tool wear elapsed minutes.
  • Machine failure: Binary target label (True/False).

6. Implementation Features

  • Ingestion & Validation: Schema validation, boundary range validation, missing value scans, and clean split configurations.
  • Tuned Model Engine: Stratified 5-Fold Cross Validation comparing baseline models vs hyperparameter-tuned Random Forest and XGBoost candidates.
  • Reliability Calibration: Probability curve calibration plotting to optimize model confidence intervals.
  • Boundary Optimization: Scans thresholds to find the F1-maximizing decision threshold (0.35), avoiding default 0.50 assumptions.
  • Inference Packaging: Preprocessor transformations and model parameters packaged together inside src/models/production_pipeline.pkl.
  • API Middleware & Routing: Custom trace ID stamping, Timing latency middlewares, global domain error maps, and dependency injection patterns.
  • SCADA UI: Streamlit widgets showing live gauges, What-If simulation slider sandboxes, active grid selections, and Alert Center scroll logs.

7. Model Performance Summary

Architecture Backend Accuracy ROC-AUC F1 Score Status
Logistic Regression 0.8350 0.9124 0.2624 Baseline
Decision Tree 0.9760 0.8812 0.6928 Baseline
Random Forest (Tuned) 0.9850 0.9745 0.7912 Candidate
XGBoost (Tuned) 0.9862 0.9845 0.8142 Champion
  • Optimal threshold: 0.35 (exposes optimal detection recall while managing false warning alerts).

8. REST API Reference

The FastAPI gateway hosts interactive document panels at http://localhost:8000/docs.

Key Endpoints:

  • GET /health: Readiness diagnostics reporting resource loading status.
  • GET /model-info: Active model parameter details and cross-validation scores.
  • POST /predict: Classifies machinery failure risk and recommends actions.
  • POST /explain: Generates local SHAP attributions and operator logs.
  • POST /predict-and-explain: Consolidated payload packaging predictions, risk levels, and SHAP attributions in a single call.

Sample Ingestion Request:

{
  "machine_type": "L",
  "air_temperature": 300.5,
  "process_temperature": 311.2,
  "rotational_speed": 1450.0,
  "torque": 55.0,
  "tool_wear": 220.0
}

Sample Consolidated Response:

{
  "prediction": "Failure Imminent",
  "probability": 0.76,
  "risk_level": "Critical",
  "top_features": [
    {
      "feature": "Tool wear [min]",
      "impact": "+31.0%"
    },
    {
      "feature": "Torque [Nm]",
      "impact": "+18.0%"
    }
  ],
  "business_explanation": "Critical failure warnings triggered. The primary drivers are high Tool Wear and Load Spindle Torque levels.",
  "maintenance_recommendation": "STOP MACHINE: Failure risk exceeds production thresholds. Inspect hardware immediately."
}

9. Installation & Execution Guide

Prerequisites

  • Python 3.10+
  • Port 8000 available for the FastAPI REST gateway

Environment Setup

# Clone the repository
git clone https://github.com/Malik7343/AI-Predictive-Maintenance-System.git
cd AI-Predictive-Maintenance-System

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install required dependencies
pip install -r requirements.txt

Running the System Pipelines

# Step 1: Execute Preprocessing Pipeline
python src/preprocessing/run_pipeline.py

# Step 2: Run Model Training & Evaluations
python src/models/run_training.py

# Step 3: Run Optimization and Tuner
python src/models/run_optimization.py

# Step 4: Start the FastAPI Server Gateway
uvicorn api.main:app --reload

# Step 5: Start the SCADA stream Dashboard UI
streamlit run dashboard/app.py

Running Test Suites

# Run Preprocessing and API Validation Test Suites
pytest -v

10. SCADA Dashboard Previews

Below are visual layouts of the industrial SCADA operator dashboard:

  • Factory Fleet Grid: Machinery Fleet Grid
  • Live Dial Gauges & SHAP Explainer: Diagnostics and SHAP Waterfall
  • Time-Series Trends & What-If Simulator: Trends and Simulator

Built With

Share this project:

Updates