import numpy as np import matplotlib.pyplot as plt import seaborn as sns import gradio as gr import joblib # Dummy models for demonstration (replace with actual ones) class DummyLSTM: def predict(self, x): # Simulate next-day pollution by averaging inputs and adding noise return np.mean(x, axis=1) + np.random.rand(1, 5) * 5 class DummyRF: def predict(self, x): # Health score based on average pollution avg = np.mean(x) return [100 - min(avg, 100)] # Load or simulate models lstm_model = DummyLSTM() rf_model = DummyRF() pollutants = ['Ozone', 'SO2', 'NO2', 'PM10', 'PM2.5'] def predict_pollution(*inputs): try: input_data = np.array([inputs[i:i+5] for i in range(0, 15, 5)], dtype=np.float32) # shape (3, 5) reshaped = input_data.reshape(1, 3, 5) # batch size 1 pred = lstm_model.predict(reshaped) pred = np.clip(pred, 0, 500) # limit extreme values health_score = rf_model.predict(pred)[0] # Visualization plt.figure(figsize=(8, 4)) sns.barplot(x=pollutants, y=pred[0]) plt.title("Predicted Pollutant Levels") plt.ylabel("µg/m³") plot_path = "plot.png" plt.savefig(plot_path) plt.close() # Health risk evaluation if health_score > 80: risk = "🔴 High Risk" color = "#ff6b6b" advice = "Avoid outdoor activities." elif health_score > 60: risk = "🟠 Moderate Risk" color = "#ffd166" advice = "Limit outdoor exposure." else: risk = "🟢 Low Risk" color = "#06d6a0" advice = "Air quality is acceptable." health_html = f"""
Health Score: {health_score:.1f}/100
{advice}