Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,231 @@
|
|
| 1 |
-
import pickle
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
with open("classification_model.pkl", "rb") as f:
|
| 14 |
-
classification_model = pickle.load(f)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Streamlit UI
|
| 19 |
st.title("🌍 Air Pollution Prediction System")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
so2
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
features = np.array([[temperature, humidity, pm10, no2, so2, co, industrial_proximity, population_density]])
|
| 33 |
-
|
| 34 |
-
# Predict PM2.5 Level (Regression)
|
| 35 |
-
if st.sidebar.button("Predict PM2.5 Level"):
|
| 36 |
-
pm25_pred = regression_model.predict(features)
|
| 37 |
-
st.write(f"**Predicted PM2.5 Level:** {pm25_pred[0]:.2f}")
|
| 38 |
-
|
| 39 |
-
# Predict Air Quality (Classification)
|
| 40 |
-
if st.sidebar.button("Predict Air Quality Level"):
|
| 41 |
-
air_quality_pred = classification_model.predict(features)
|
| 42 |
-
quality_map = {0: "Good", 1: "Moderate", 2: "Unhealthy", 3: "Hazardous"}
|
| 43 |
-
st.write(f"**Predicted Air Quality Level:** {quality_map[air_quality_pred[0]]}")
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from air_pollution_predictor import AirPollutionPredictor
|
| 4 |
|
| 5 |
+
# Configure page
|
| 6 |
+
st.set_page_config(
|
| 7 |
+
page_title="🌍 Air Pollution Prediction System",
|
| 8 |
+
page_icon="🌍",
|
| 9 |
+
layout="wide",
|
| 10 |
+
initial_sidebar_state="collapsed"
|
| 11 |
+
)
|
| 12 |
|
| 13 |
+
# Initialize the predictor
|
| 14 |
+
@st.cache_resource
|
| 15 |
+
def load_predictor():
|
| 16 |
+
"""Load the air pollution predictor with caching"""
|
| 17 |
+
try:
|
| 18 |
+
return AirPollutionPredictor()
|
| 19 |
+
except Exception as e:
|
| 20 |
+
st.error(f"Error loading models: {str(e)}")
|
| 21 |
+
st.stop()
|
| 22 |
|
| 23 |
+
predictor = load_predictor()
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Main title and description
|
|
|
|
|
|
|
| 26 |
st.title("🌍 Air Pollution Prediction System")
|
| 27 |
+
st.markdown("""
|
| 28 |
+
This system predicts **PM2.5 levels** and **Air Quality** based on various environmental factors.
|
| 29 |
+
Enter the environmental parameters below to get predictions.
|
| 30 |
+
""")
|
| 31 |
+
|
| 32 |
+
# Create columns for better layout
|
| 33 |
+
col1, col2 = st.columns([2, 1])
|
| 34 |
+
|
| 35 |
+
with col1:
|
| 36 |
+
st.header("📊 Environmental Parameters")
|
| 37 |
+
|
| 38 |
+
# Create sub-columns for input fields
|
| 39 |
+
input_col1, input_col2 = st.columns(2)
|
| 40 |
+
|
| 41 |
+
with input_col1:
|
| 42 |
+
st.subheader("🌡️ Weather Conditions")
|
| 43 |
+
temperature = st.number_input(
|
| 44 |
+
"Temperature (°C)",
|
| 45 |
+
min_value=-10.0,
|
| 46 |
+
max_value=50.0,
|
| 47 |
+
value=25.0,
|
| 48 |
+
help="Ambient temperature in Celsius"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
humidity = st.number_input(
|
| 52 |
+
"Humidity (%)",
|
| 53 |
+
min_value=0.0,
|
| 54 |
+
max_value=100.0,
|
| 55 |
+
value=50.0,
|
| 56 |
+
help="Relative humidity percentage"
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
st.subheader("🏭 Pollutant Levels")
|
| 60 |
+
pm10 = st.number_input(
|
| 61 |
+
"PM10 Level (μg/m³)",
|
| 62 |
+
min_value=0.0,
|
| 63 |
+
max_value=500.0,
|
| 64 |
+
value=20.0,
|
| 65 |
+
help="Particulate Matter 10 micrometers"
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
no2 = st.number_input(
|
| 69 |
+
"NO2 Level (μg/m³)",
|
| 70 |
+
min_value=0.0,
|
| 71 |
+
max_value=500.0,
|
| 72 |
+
value=15.0,
|
| 73 |
+
help="Nitrogen Dioxide level"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
with input_col2:
|
| 77 |
+
st.subheader("💨 Gas Concentrations")
|
| 78 |
+
so2 = st.number_input(
|
| 79 |
+
"SO2 Level (μg/m³)",
|
| 80 |
+
min_value=0.0,
|
| 81 |
+
max_value=500.0,
|
| 82 |
+
value=10.0,
|
| 83 |
+
help="Sulfur Dioxide level"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
co = st.number_input(
|
| 87 |
+
"CO Level (mg/m³)",
|
| 88 |
+
min_value=0.0,
|
| 89 |
+
max_value=10.0,
|
| 90 |
+
value=1.0,
|
| 91 |
+
help="Carbon Monoxide level"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
st.subheader("🏘️ Location Factors")
|
| 95 |
+
industrial_proximity = st.slider(
|
| 96 |
+
"Proximity to Industrial Areas",
|
| 97 |
+
0.0,
|
| 98 |
+
10.0,
|
| 99 |
+
5.0,
|
| 100 |
+
help="Scale: 0 (far) to 10 (very close)"
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
population_density = st.number_input(
|
| 104 |
+
"Population Density (people/km²)",
|
| 105 |
+
min_value=0,
|
| 106 |
+
max_value=10000,
|
| 107 |
+
value=500,
|
| 108 |
+
help="Number of people per square kilometer"
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# Prediction buttons
|
| 112 |
+
st.header("🔮 Predictions")
|
| 113 |
+
|
| 114 |
+
prediction_col1, prediction_col2, prediction_col3 = st.columns(3)
|
| 115 |
+
|
| 116 |
+
with prediction_col1:
|
| 117 |
+
predict_pm25 = st.button("🔍 Predict PM2.5 Level", type="primary", use_container_width=True)
|
| 118 |
+
|
| 119 |
+
with prediction_col2:
|
| 120 |
+
predict_quality = st.button("🌬️ Predict Air Quality", type="primary", use_container_width=True)
|
| 121 |
+
|
| 122 |
+
with prediction_col3:
|
| 123 |
+
predict_both = st.button("📈 Predict Both", type="secondary", use_container_width=True)
|
| 124 |
+
|
| 125 |
+
with col2:
|
| 126 |
+
st.header("📈 Results")
|
| 127 |
+
|
| 128 |
+
# Handle predictions
|
| 129 |
+
if predict_pm25 or predict_both:
|
| 130 |
+
try:
|
| 131 |
+
features = predictor.prepare_features(
|
| 132 |
+
temperature, humidity, pm10, no2, so2, co,
|
| 133 |
+
industrial_proximity, population_density
|
| 134 |
+
)
|
| 135 |
+
pm25_result = predictor.predict_pm25(features)
|
| 136 |
+
|
| 137 |
+
st.success("PM2.5 Prediction Complete!")
|
| 138 |
+
st.metric(
|
| 139 |
+
label="Predicted PM2.5 Level",
|
| 140 |
+
value=f"{pm25_result:.2f} μg/m³",
|
| 141 |
+
delta=None
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
# PM2.5 health information
|
| 145 |
+
if pm25_result <= 12:
|
| 146 |
+
st.success("🟢 Good air quality")
|
| 147 |
+
elif pm25_result <= 35:
|
| 148 |
+
st.warning("🟡 Moderate air quality")
|
| 149 |
+
elif pm25_result <= 55:
|
| 150 |
+
st.warning("🟠 Unhealthy for sensitive groups")
|
| 151 |
+
else:
|
| 152 |
+
st.error("🔴 Unhealthy air quality")
|
| 153 |
+
|
| 154 |
+
except Exception as e:
|
| 155 |
+
st.error(f"Error in PM2.5 prediction: {str(e)}")
|
| 156 |
+
|
| 157 |
+
if predict_quality or predict_both:
|
| 158 |
+
try:
|
| 159 |
+
features = predictor.prepare_features(
|
| 160 |
+
temperature, humidity, pm10, no2, so2, co,
|
| 161 |
+
industrial_proximity, population_density
|
| 162 |
+
)
|
| 163 |
+
quality_index, quality_label = predictor.predict_air_quality(features)
|
| 164 |
+
|
| 165 |
+
st.success("Air Quality Prediction Complete!")
|
| 166 |
+
st.metric(
|
| 167 |
+
label="Predicted Air Quality",
|
| 168 |
+
value=quality_label,
|
| 169 |
+
delta=None
|
| 170 |
+
)
|
| 171 |
+
|
| 172 |
+
# Get and display description
|
| 173 |
+
description = predictor.get_quality_description(quality_label)
|
| 174 |
+
st.info(description)
|
| 175 |
+
|
| 176 |
+
# Color coding for quality levels
|
| 177 |
+
if quality_label == "Good":
|
| 178 |
+
st.success("🟢 " + quality_label)
|
| 179 |
+
elif quality_label == "Moderate":
|
| 180 |
+
st.warning("🟡 " + quality_label)
|
| 181 |
+
elif quality_label == "Unhealthy":
|
| 182 |
+
st.warning("🟠 " + quality_label)
|
| 183 |
+
else:
|
| 184 |
+
st.error("🔴 " + quality_label)
|
| 185 |
+
|
| 186 |
+
except Exception as e:
|
| 187 |
+
st.error(f"Error in air quality prediction: {str(e)}")
|
| 188 |
+
|
| 189 |
+
# Additional information section
|
| 190 |
+
st.markdown("---")
|
| 191 |
+
st.header("ℹ️ About the System")
|
| 192 |
+
|
| 193 |
+
info_col1, info_col2 = st.columns(2)
|
| 194 |
+
|
| 195 |
+
with info_col1:
|
| 196 |
+
st.subheader("📋 Input Parameters")
|
| 197 |
+
st.markdown("""
|
| 198 |
+
- **Temperature**: Ambient air temperature
|
| 199 |
+
- **Humidity**: Relative humidity percentage
|
| 200 |
+
- **PM10**: Particulate matter ≤ 10μm
|
| 201 |
+
- **NO2**: Nitrogen dioxide concentration
|
| 202 |
+
- **SO2**: Sulfur dioxide concentration
|
| 203 |
+
- **CO**: Carbon monoxide concentration
|
| 204 |
+
- **Industrial Proximity**: Distance to industrial areas
|
| 205 |
+
- **Population Density**: People per square kilometer
|
| 206 |
+
""")
|
| 207 |
+
|
| 208 |
+
with info_col2:
|
| 209 |
+
st.subheader("🎯 Prediction Outputs")
|
| 210 |
+
st.markdown("""
|
| 211 |
+
- **PM2.5 Level**: Predicted particulate matter ≤ 2.5μm concentration
|
| 212 |
+
- **Air Quality**: Classification into Good, Moderate, Unhealthy, or Hazardous
|
| 213 |
+
|
| 214 |
+
**Air Quality Standards:**
|
| 215 |
+
- 🟢 Good: Minimal health impact
|
| 216 |
+
- 🟡 Moderate: Acceptable for most people
|
| 217 |
+
- 🟠 Unhealthy: Risk for sensitive groups
|
| 218 |
+
- 🔴 Hazardous: Health risk for everyone
|
| 219 |
+
""")
|
| 220 |
|
| 221 |
+
# Display input summary
|
| 222 |
+
with st.expander("📊 Current Input Summary"):
|
| 223 |
+
input_data = {
|
| 224 |
+
'Parameter': ['Temperature', 'Humidity', 'PM10', 'NO2', 'SO2', 'CO', 'Industrial Proximity', 'Population Density'],
|
| 225 |
+
'Value': [f"{temperature}°C", f"{humidity}%", f"{pm10} μg/m³", f"{no2} μg/m³",
|
| 226 |
+
f"{so2} μg/m³", f"{co} mg/m³", f"{industrial_proximity}/10", f"{population_density} people/km²"],
|
| 227 |
+
'Unit': ['Celsius', 'Percentage', 'μg/m³', 'μg/m³', 'μg/m³', 'mg/m³', 'Scale 0-10', 'people/km²']
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
df = pd.DataFrame(input_data)
|
| 231 |
+
st.dataframe(df, use_container_width=True, hide_index=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|