Spaces:
Sleeping
Sleeping
Uploaded files.
Browse files- app.py +86 -0
- kmeans.pkl +3 -0
- linear_model.pkl +3 -0
- logistic_model.pkl +3 -0
- poly_model.pkl +3 -0
- rf_classifier.pkl +3 -0
- rf_regressor.pkl +3 -0
- ridge_model.pkl +3 -0
- scaler.pkl +3 -0
- scaler_initial.pkl +3 -0
- scaler_with_cluster.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import seaborn as sns
|
6 |
+
|
7 |
+
# Load pre-trained models and scalers
|
8 |
+
scaler_initial = joblib.load("scaler_initial.pkl")
|
9 |
+
scaler_with_cluster = joblib.load("scaler_with_cluster.pkl")
|
10 |
+
kmeans = joblib.load("kmeans.pkl")
|
11 |
+
linear_model = joblib.load("linear_model.pkl")
|
12 |
+
poly_model = joblib.load("poly_model.pkl")
|
13 |
+
ridge_model = joblib.load("ridge_model.pkl")
|
14 |
+
rf_regressor = joblib.load("rf_regressor.pkl")
|
15 |
+
logistic_model = joblib.load("logistic_model.pkl")
|
16 |
+
rf_classifier = joblib.load("rf_classifier.pkl")
|
17 |
+
|
18 |
+
|
19 |
+
# Prediction function
|
20 |
+
def predict_aqi(pm25, pm10, no2, co, temp, humidity):
|
21 |
+
# Create input dataframe with initial features
|
22 |
+
input_data = pd.DataFrame([[pm25, pm10, no2, co, temp, humidity]],
|
23 |
+
columns=["PM2.5", "PM10", "NO2", "CO", "Temperature", "Humidity"])
|
24 |
+
|
25 |
+
# Scale initial features for clustering
|
26 |
+
input_scaled_initial = scaler_initial.transform(input_data)
|
27 |
+
|
28 |
+
# Apply K-means clustering
|
29 |
+
cluster = kmeans.predict(input_scaled_initial)[0]
|
30 |
+
input_data['Cluster'] = cluster
|
31 |
+
|
32 |
+
# Scale data with Cluster feature
|
33 |
+
input_scaled_with_cluster = scaler_with_cluster.transform(input_data)
|
34 |
+
|
35 |
+
# Regression predictions
|
36 |
+
linear_pred = linear_model.predict(input_scaled_with_cluster)[0]
|
37 |
+
poly_pred = poly_model.predict(input_scaled_with_cluster)[0]
|
38 |
+
ridge_pred = ridge_model.predict(input_scaled_with_cluster)[0]
|
39 |
+
rf_pred = rf_regressor.predict(input_scaled_with_cluster)[0]
|
40 |
+
|
41 |
+
# Classification predictions
|
42 |
+
logistic_class = logistic_model.predict(input_scaled_with_cluster)[0]
|
43 |
+
rf_class = rf_classifier.predict(input_scaled_with_cluster)[0]
|
44 |
+
|
45 |
+
# Create performance plot
|
46 |
+
models = ["Linear", "Polynomial", "Ridge", "Random Forest"]
|
47 |
+
predictions = [linear_pred, poly_pred, ridge_pred, rf_pred]
|
48 |
+
plt.figure(figsize=(8, 4))
|
49 |
+
sns.barplot(x=models, y=predictions)
|
50 |
+
plt.title("AQI Predictions by Model")
|
51 |
+
plt.ylabel("Predicted AQI")
|
52 |
+
plt.savefig("aqi_plot.png")
|
53 |
+
plt.close()
|
54 |
+
|
55 |
+
output_text = (
|
56 |
+
f"Linear Regression AQI: {linear_pred:.2f}\n"
|
57 |
+
f"Polynomial Regression AQI: {poly_pred:.2f}\n"
|
58 |
+
f"Ridge Regression AQI: {ridge_pred:.2f}\n"
|
59 |
+
f"Random Forest AQI: {rf_pred:.2f}\n"
|
60 |
+
f"Logistic Classification: {'Safe' if logistic_class == 0 else 'Unsafe'}\n"
|
61 |
+
f"Random Forest Classification: {'Safe' if rf_class == 0 else 'Unsafe'}"
|
62 |
+
)
|
63 |
+
return output_text, "aqi_plot.png"
|
64 |
+
|
65 |
+
|
66 |
+
# Gradio interface
|
67 |
+
iface = gr.Interface(
|
68 |
+
fn=predict_aqi,
|
69 |
+
inputs=[
|
70 |
+
gr.Slider(0, 200, label="PM2.5 (µg/m³)", value=50),
|
71 |
+
gr.Slider(0, 300, label="PM10 (µg/m³)", value=80),
|
72 |
+
gr.Slider(0, 100, label="NO2 (µg/m³)", value=20),
|
73 |
+
gr.Slider(0, 10, label="CO (mg/m³)", value=1),
|
74 |
+
gr.Slider(-10, 40, label="Temperature (°C)", value=20),
|
75 |
+
gr.Slider(0, 100, label="Humidity (%)", value=50)
|
76 |
+
],
|
77 |
+
outputs=[
|
78 |
+
gr.Textbox(label="Predictions"),
|
79 |
+
gr.Image(label="Model Comparison Plot")
|
80 |
+
],
|
81 |
+
title="Air Quality Prediction and Classification",
|
82 |
+
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."
|
83 |
+
)
|
84 |
+
|
85 |
+
if __name__ == "__main__":
|
86 |
+
iface.launch()
|
kmeans.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:97a6ecdb004ad35fdadcb196518cfe9567ff3e043971c8f8e87d4a04fcffa814
|
3 |
+
size 4871
|
linear_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bee2a0d250041256fb29fa8fa370bfe341f17ebb676e70b4a48d62ad845e1ec6
|
3 |
+
size 656
|
logistic_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b0d8d92f39744c7f0172577f9a85be35e6798d6322096a1dce3b55a6f65669f6
|
3 |
+
size 879
|
poly_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:967d72036668b807bdd138d7f169faa634b16ade943dfe95be075e540981e08b
|
3 |
+
size 1439
|
rf_classifier.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:558446d08687cf4ccc39a9dc31fb23842eb22f83595f687a1aa98d1bc3889002
|
3 |
+
size 2785769
|
rf_regressor.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:ad238a7f778b182d9d55d674a8ba77d309a05a2a62cd101d83d9fff346b60138
|
3 |
+
size 7299601
|
ridge_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5154a961f3f9c1c1c0604b1fc318553f78b4a00fdc96403be137c612161fd6f9
|
3 |
+
size 608
|
scaler.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:975c9d316b72c961b7dd3849a194930b59735413c7f827ff071ee7694ef8ec82
|
3 |
+
size 1127
|
scaler_initial.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:975c9d316b72c961b7dd3849a194930b59735413c7f827ff071ee7694ef8ec82
|
3 |
+
size 1127
|
scaler_with_cluster.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f2e61f76fab60f57857131c148ea8c341615bbc86c1e471041f29684ea7f5653
|
3 |
+
size 1151
|