File size: 3,404 Bytes
8fb7712
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import gradio as gr
import pandas as pd
import joblib
import matplotlib.pyplot as plt
import seaborn as sns

# Load pre-trained models and scalers
scaler_initial = joblib.load("scaler_initial.pkl")
scaler_with_cluster = joblib.load("scaler_with_cluster.pkl")
kmeans = joblib.load("kmeans.pkl")
linear_model = joblib.load("linear_model.pkl")
poly_model = joblib.load("poly_model.pkl")
ridge_model = joblib.load("ridge_model.pkl")
rf_regressor = joblib.load("rf_regressor.pkl")
logistic_model = joblib.load("logistic_model.pkl")
rf_classifier = joblib.load("rf_classifier.pkl")


# Prediction function
def predict_aqi(pm25, pm10, no2, co, temp, humidity):
    # Create input dataframe with initial features
    input_data = pd.DataFrame([[pm25, pm10, no2, co, temp, humidity]],
                              columns=["PM2.5", "PM10", "NO2", "CO", "Temperature", "Humidity"])

    # Scale initial features for clustering
    input_scaled_initial = scaler_initial.transform(input_data)

    # Apply K-means clustering
    cluster = kmeans.predict(input_scaled_initial)[0]
    input_data['Cluster'] = cluster

    # Scale data with Cluster feature
    input_scaled_with_cluster = scaler_with_cluster.transform(input_data)

    # Regression predictions
    linear_pred = linear_model.predict(input_scaled_with_cluster)[0]
    poly_pred = poly_model.predict(input_scaled_with_cluster)[0]
    ridge_pred = ridge_model.predict(input_scaled_with_cluster)[0]
    rf_pred = rf_regressor.predict(input_scaled_with_cluster)[0]

    # Classification predictions
    logistic_class = logistic_model.predict(input_scaled_with_cluster)[0]
    rf_class = rf_classifier.predict(input_scaled_with_cluster)[0]

    # Create performance plot
    models = ["Linear", "Polynomial", "Ridge", "Random Forest"]
    predictions = [linear_pred, poly_pred, ridge_pred, rf_pred]
    plt.figure(figsize=(8, 4))
    sns.barplot(x=models, y=predictions)
    plt.title("AQI Predictions by Model")
    plt.ylabel("Predicted AQI")
    plt.savefig("aqi_plot.png")
    plt.close()

    output_text = (
        f"Linear Regression AQI: {linear_pred:.2f}\n"
        f"Polynomial Regression AQI: {poly_pred:.2f}\n"
        f"Ridge Regression AQI: {ridge_pred:.2f}\n"
        f"Random Forest AQI: {rf_pred:.2f}\n"
        f"Logistic Classification: {'Safe' if logistic_class == 0 else 'Unsafe'}\n"
        f"Random Forest Classification: {'Safe' if rf_class == 0 else 'Unsafe'}"
    )
    return output_text, "aqi_plot.png"


# Gradio interface
iface = gr.Interface(
    fn=predict_aqi,
    inputs=[
        gr.Slider(0, 200, label="PM2.5 (µg/m³)", value=50),
        gr.Slider(0, 300, label="PM10 (µg/m³)", value=80),
        gr.Slider(0, 100, label="NO2 (µg/m³)", value=20),
        gr.Slider(0, 10, label="CO (mg/m³)", value=1),
        gr.Slider(-10, 40, label="Temperature (°C)", value=20),
        gr.Slider(0, 100, label="Humidity (%)", value=50)
    ],
    outputs=[
        gr.Textbox(label="Predictions"),
        gr.Image(label="Model Comparison Plot")
    ],
    title="Air Quality Prediction and Classification",
    description="Enter pollutant levels and weather conditions to predict AQI and classify air quality. Built with multiple machine learning models to address urban air pollution."
)

if __name__ == "__main__":
    iface.launch()