import streamlit as st import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.metrics import mean_squared_error from datetime import date # Set page configuration st.set_page_config(page_title="Air Quality Monitoring Dashboard", layout="wide") # Sidebar for navigation st.sidebar.title("Air Quality Monitoring") dashboard_choice = st.sidebar.radio("Select Dashboard:", ("User Dashboard", "Admin Dashboard")) # WHO Guidelines for display in both dashboards who_guidelines = { "Pollutant": ["NO2", "O3"], "Max Safe Concentration (µg/m³)": [25, 100] } # Data Simulation for Current Day and Predictions today_data = { "NO2 (µg/m³)": np.random.uniform(20, 60), # Simulated data "O3 (µg/m³)": np.random.uniform(50, 120) # Simulated data } # Simulated predictions for the next three days next_three_days = pd.DataFrame({ "Day": ["Day 1", "Day 2", "Day 3"], "NO2 (µg/m³)": np.random.uniform(20, 60, 3), "O3 (µg/m³)": np.random.uniform(50, 120, 3) }) # Simulated actual and predicted data for Admin Dashboard y_test = pd.DataFrame({ "NO2": np.random.uniform(20, 60, 100), "O3": np.random.uniform(50, 120, 100) }) y_test_pred = pd.DataFrame({ "NO2": np.random.uniform(20, 60, 100), "O3": np.random.uniform(50, 120, 100) }) ### User Dashboard ### if dashboard_choice == "User Dashboard": st.title("🌍 User Dashboard: Air Quality Monitoring") # Current Day Pollutant Concentrations st.sidebar.markdown(f"Today's Date: **{date.today().strftime('%B %d, %Y')}**") st.sidebar.markdown("### Current Pollutant Concentrations") st.sidebar.write(pd.DataFrame(today_data, index=["Concentration"])) st.sidebar.markdown("### WHO Guidelines") st.sidebar.write(pd.DataFrame(who_guidelines)) # WHO guideline alerts if today_data["NO2 (µg/m³)"] > 25: st.sidebar.error("⚠️ NO2 levels are above WHO guidelines!") else: st.sidebar.success("✅ NO2 levels are within safe limits.") if today_data["O3 (µg/m³)"] > 100: st.sidebar.error("⚠️ O3 levels are above WHO guidelines!") else: st.sidebar.success("✅ O3 levels are within safe limits.") # Predictions for the next three days st.write("### Predictions for the Next 3 Days") st.dataframe(next_three_days) # Visualization: Prediction vs WHO Guidelines st.write("### Visualization: Predictions vs WHO Guidelines") fig, ax = plt.subplots(figsize=(8, 4)) ax.plot(next_three_days["Day"], next_three_days["NO2 (µg/m³)"], label="NO2", marker='o', color="red") ax.axhline(y=25, color="red", linestyle='--', label="NO2 WHO Limit (25 µg/m³)") ax.plot(next_three_days["Day"], next_three_days["O3 (µg/m³)"], label="O3", marker='x', color="blue") ax.axhline(y=100, color="blue", linestyle='--', label="O3 WHO Limit (100 µg/m³)") ax.set_title("Predicted Pollutant Levels vs WHO Guidelines") ax.set_xlabel("Days") ax.set_ylabel("Concentration (µg/m³)") ax.legend() st.pyplot(fig) ### Admin Dashboard ### elif dashboard_choice == "Admin Dashboard": st.title("🔧 Admin Dashboard: Detailed Monitoring & Model Performance") # Model selection for monitoring (simulated) model_name = st.sidebar.selectbox( "Select model for monitoring:", ("Decision Tree", "Random Forest", "XGBoost") ) # Displaying prediction and actual comparison st.write("### Predicted vs Actual Values") col1, col2 = st.columns(2) with col1: st.write("**Actual Values**") st.dataframe(y_test) with col2: st.write("**Predicted Values**") st.dataframe(y_test_pred) # Performance Metrics mse_no2 = mean_squared_error(y_test["NO2"], y_test_pred["NO2"]) mse_o3 = mean_squared_error(y_test["O3"], y_test_pred["O3"]) rmse_no2 = np.sqrt(mse_no2) rmse_o3 = np.sqrt(mse_o3) st.markdown("### Model Performance Metrics") col3, col4 = st.columns(2) col3.metric("NO2 - Mean Squared Error (MSE)", f"{mse_no2:.2f}") col4.metric("O3 - Mean Squared Error (MSE)", f"{mse_o3:.2f}") col3.metric("NO2 - Root Mean Squared Error (RMSE)", f"{rmse_no2:.2f}") col4.metric("O3 - Root Mean Squared Error (RMSE)", f"{rmse_o3:.2f}") # Visualization of Actual vs Predicted Values st.markdown("### Visualization: Actual vs Predicted Values") fig, ax = plt.subplots(figsize=(10, 5)) ax.plot(y_test["NO2"], label="Actual NO2", color="blue", marker='o') ax.plot(y_test_pred["NO2"], label="Predicted NO2", color="red", linestyle='--') ax.set_title(f"NO2 Actual vs Predicted - {model_name}") ax.set_xlabel("Samples") ax.set_ylabel("Concentration (µg/m³)") ax.legend() st.pyplot(fig) # Feature Importance (simulated example) st.markdown("### Feature Importance") feature_importances = pd.Series([0.3, 0.2, 0.5], index=["Feature 1", "Feature 2", "Feature 3"]) fig, ax = plt.subplots(figsize=(8, 4)) feature_importances.plot(kind="barh", ax=ax) ax.set_title(f"Feature Importance - {model_name}") st.pyplot(fig) # say bye st.sidebar.write("### Thank you for using our app!")