DATA ENGINEERING · MACHINE LEARNING · 2026

Real-Time AQI Prediction

Real-Time Air Quality Monitoring & Prediction Pipeline

An end-to-end real-time air quality prediction pipeline combining streaming data, distributed processing, machine learning, and interactive visualization.

ROLEData & ML Engineer
RESPONSIBILITIESOpenWeather Streaming · PySpark · ML Training
TECH STACKApache Spark · PySpark · Streamlit · Scikit-Learn
DATASET & SOURCEOpenWeatherMap API · Parquet
AUTO REFRESH · 10s
Predicted AQI
78
Forecast AQI
82
Current AQI
75
01 / OVERVIEW

From Live Data to Prediction

The end-to-end pipeline continuously fetches live weather and pollutant data, processes it via PySpark streaming, predicts the Air Quality Index using a Random Forest model, and updates a dashboard in real-time.

REAL-TIME
15-second intervals
10
Prediction features
RANDOM FOREST
Regression model
+10 MIN
Forecast horizon
02 / SYSTEM ARCHITECTURE

Pipeline Architecture

OpenWeatherMap API
producer.py
JSON Stream Files
collect_stream.py
Apache Spark
aqi_data.parquet
train_model.py
Random Forest Regressor
Streamlit Dashboard
Data Source
Streaming
Processing
Storage
ML + Viz
03 / DATA INGESTION

01 / Data Ingestion

Data is gathered from the OpenWeatherMap API every 15 seconds. The custom `producer.py` script orchestrates the collection of both weather metrics and air pollution statistics, writing them as JSON objects into a designated streaming directory.

WEATHER
Temperature
Humidity
Pressure
Wind Speed
Clouds
AIR POLLUTION
AQI
PM2.5
PM10
CO
NO₂
O₃
producer.py
while True:
    weather_data = fetch_weather()
    pollution_data = fetch_pollution()
    
    record = {
        "timestamp": time.time(),
        "weather": weather_data,
        "pollution": pollution_data
    }
    
    write_to_stream(record)
    time.sleep(15)
04 / STREAM PROCESSING

02 / Stream Processing with Spark

Apache Spark Structured Streaming reads the incoming JSON files, flattens nested structures, validates schemas, and handles null values before appending the clean records to a Parquet storage layer for ML training.

JSON Files
Schema Validation
Nested Transformation
Null Handling
Structured Records
Parquet Append
05 / MACHINE LEARNING

03 / AQI Prediction

Input Features (10)
  • Temperature
  • Humidity
  • Wind Speed
  • Pressure
  • Clouds
  • PM2.5
  • PM10
  • CO
  • NO₂
  • O₃

Random Forest Regressor
Predicted AQI
78.5
AlgorithmRandom Forest Regressor
Estimators100 trees
Max Depth7
TargetAQI
06 / REAL-TIME MONITORING

04 / Real-Time Monitoring

localhost:8501/aqi-dashboard

Real-Time AQI Predictions

AUTO REFRESH 10s
Predicted AQI
78
Forecast AQI +10min
82
Current AQI
75
Temp 28.5°C
Humidity 78%
Wind 3.2 m/s
Pressure 1012 hPa
Clouds 65%
PM2.5 18.3
PM10 32.1
CO 0.45
NO₂ 12.8
O₃ 48.2
AQI Trend
Weather Trend (Temp)
TimeAQIPM2.5Temp
10:45:307818.328.5
10:45:157718.128.4
10:45:007517.928.4
07 / PIPELINE MODULES

Behind the Pipeline

producer.py

Fetches data via API

collect_stream.py

Spark stream ingestion

aqi_data.parquet

Storage layer

train_model.py

Model training

aqi_rf_model

Serialized model

stream_final.py

Live inference & dash

08 / TECHNICAL CHALLENGES

Engineering Challenges

Continuous Data Ingestion

Handling rate limits and connection drops while fetching from OpenWeatherMap every 15s continuously.

Structured Streaming Data

Transforming complex nested JSON arrays into flat schemas suitable for model inference using PySpark.

Real-Time Visualization

Updating the Streamlit dashboard asynchronously as new predictions arrive without causing page reloads.

09 / KEY TAKEAWAYS

What This Project Demonstrates

  • End-to-end data pipeline construction
  • Micro-batch streaming with Spark
  • Real-time API integration
  • Handling nested JSON schemas
  • Parquet file optimization
  • Machine learning regression
  • Feature engineering from streams
  • Model serialization and inference
  • Streamlit real-time dashboarding
  • Resilient error handling
10 / TECH STACK

Technology Stack

PythonOpenWeatherMap APIApache SparkPySparkParquet
Scikit-learnRandom ForestStreamlitMatplotlibBash

Explore the Real-Time AQI Pipeline Codebase

View the PySpark streaming data pipeline, Random Forest prediction model, and Streamlit dashboard code on GitHub.