Spaces:
Sleeping
Sleeping
Uploaded the app files
Browse files- app.py +68 -0
- linear_model.pkl +3 -0
- logistic_model.pkl +3 -0
- poly_model.pkl +3 -0
- requirements.txt +7 -0
- rf_classifier.pkl +3 -0
- scaler.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import os
|
| 7 |
+
|
| 8 |
+
SAVED_MODEL_DIR = ""
|
| 9 |
+
|
| 10 |
+
# Load the pre-trained models and scalers
|
| 11 |
+
scalar = joblib.load(os.path.join(SAVED_MODEL_DIR, "scaler.pkl"))
|
| 12 |
+
linear_model = joblib.load(os.path.join(SAVED_MODEL_DIR, "linear_model.pkl"))
|
| 13 |
+
poly_model = joblib.load(os.path.join(SAVED_MODEL_DIR, "poly_model.pkl"))
|
| 14 |
+
logistic_model = joblib.load(os.path.join(SAVED_MODEL_DIR, "logistic_model.pkl"))
|
| 15 |
+
rf_classifier = joblib.load(os.path.join(SAVED_MODEL_DIR, "rf_classifier.pkl"))
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def predict_aqi(pm25, pm10, no2, co, temp, humidity):
|
| 19 |
+
input_data = pd.DataFrame([[pm25, pm10, no2, co, temp, humidity]], columns=["PM2.5", "PM10", "NO2", "CO", "Temperature", "Humidity"])
|
| 20 |
+
input_scaled = scalar.transform(input_data)
|
| 21 |
+
|
| 22 |
+
linear_pred = linear_model.predict(input_scaled)[0]
|
| 23 |
+
poly_pred = poly_model.predict(input_scaled)[0]
|
| 24 |
+
|
| 25 |
+
logistic_class = logistic_model.predict(input_scaled)[0]
|
| 26 |
+
rf_class = rf_classifier.predict(input_scaled)[0]
|
| 27 |
+
|
| 28 |
+
# Create performance plot
|
| 29 |
+
models = ["Linear", "Polynomial"]
|
| 30 |
+
predictions = [linear_pred, poly_pred]
|
| 31 |
+
plt.figure(figsize=(8, 4))
|
| 32 |
+
sns.barplot(x=models, y=predictions)
|
| 33 |
+
plt.title("AQI Predictions by Model")
|
| 34 |
+
plt.ylabel("Predicted AQI")
|
| 35 |
+
plt.savefig("aqi_plot.png")
|
| 36 |
+
plt.close()
|
| 37 |
+
|
| 38 |
+
output_text = (
|
| 39 |
+
f"Linear Regression AQI: {linear_pred:.2f}\n"
|
| 40 |
+
f"Polynomial Regression AQI: {poly_pred:.2f}\n"
|
| 41 |
+
f"Logistic Classification: {'Safe' if logistic_class == 0 else 'Unsafe'}\n"
|
| 42 |
+
f"Random Forest Classification: {'Safe' if rf_class == 0 else 'Unsafe'}"
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
return output_text, "aqi_plot.png"
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
if __name__ == '__main__':
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=predict_aqi,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Slider(0, 200, label="PM2.5(µg/m³)", value=50),
|
| 54 |
+
gr.Slider(0, 300, label="PM10 (µg/m³)", value=80),
|
| 55 |
+
gr.Slider(0, 100, label="NO2 (µg/m³)", value=20),
|
| 56 |
+
gr.Slider(0, 10, label="CO (mg/m³)", value=1),
|
| 57 |
+
gr.Slider(-10, 40, label="Temperature (°C)", value=20),
|
| 58 |
+
gr.Slider(0, 100, label="Humidity (%)", value=50)
|
| 59 |
+
],
|
| 60 |
+
outputs=[
|
| 61 |
+
gr.Textbox(label="Predictions"),
|
| 62 |
+
gr.Image(label="Model Comparison Plot")
|
| 63 |
+
],
|
| 64 |
+
title="Air Quality Prediction and Classification",
|
| 65 |
+
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."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
iface.launch()
|
linear_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:890576fa25a8490b5c0524045c0ee8cd1a8e63bbb73091ea4f143137932b2ec9
|
| 3 |
+
size 648
|
logistic_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d64fd0601891d0f3922b419c14405fa428a2208efe1ba1ea50f0750c776d44aa
|
| 3 |
+
size 911
|
poly_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:890576fa25a8490b5c0524045c0ee8cd1a8e63bbb73091ea4f143137932b2ec9
|
| 3 |
+
size 648
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
scikit-learn
|
| 3 |
+
pandas
|
| 4 |
+
numpy
|
| 5 |
+
matplotlib
|
| 6 |
+
seaborn
|
| 7 |
+
joblib
|
rf_classifier.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d64fd0601891d0f3922b419c14405fa428a2208efe1ba1ea50f0750c776d44aa
|
| 3 |
+
size 911
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9ac5b8a1b45308d11ae0dbef2e5d5cd209777f89f5de59c7ec4bd6635346c144
|
| 3 |
+
size 1127
|