Upload 6 files
Browse files- imputer.pkl +3 -0
- label_encoder.pkl +3 -0
- scaler.pkl +3 -0
- svc_model.pkl +3 -0
- water_quality_index.py +114 -0
- xgb_model.pkl +3 -0
imputer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ca769650d144cf1dc143fffc9c368ef47469de6671430be2d594823d424c294d
|
| 3 |
+
size 13713
|
label_encoder.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:32f95b42cdc2676ef36ff0ff99037431b1ba4d2601e88c04568d99b40e9dca48
|
| 3 |
+
size 582
|
scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:863c08f64c1de60c260b4c92fbccd6891f8987244f9b5c3dff420a93f1975427
|
| 3 |
+
size 807
|
svc_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f28410514081ad74524f60f8658661427acc3406c8da8c3f0096f14d03017348
|
| 3 |
+
size 146369
|
water_quality_index.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
import plotly.express as px
|
| 6 |
+
import base64
|
| 7 |
+
from sklearn.preprocessing import LabelEncoder
|
| 8 |
+
|
| 9 |
+
def run():
|
| 10 |
+
# === Load Trained Objects ===
|
| 11 |
+
svc_model = joblib.load("svc_model.pkl")
|
| 12 |
+
xgb_model = joblib.load("xgb_model.pkl")
|
| 13 |
+
imputer = joblib.load("imputer.pkl")
|
| 14 |
+
scaler = joblib.load("scaler.pkl")
|
| 15 |
+
label_encoder = joblib.load("label_encoder.pkl") # already fitted
|
| 16 |
+
|
| 17 |
+
# === Expected Columns ===
|
| 18 |
+
feature_cols = [
|
| 19 |
+
"pH (Potential Hydrogen)",
|
| 20 |
+
"BOD (Biological Oxygen Demand) (mg/L)",
|
| 21 |
+
"COD (Chemical Oxygen Demand) (mg/L)",
|
| 22 |
+
"TSS (Total Suspended Solid) (mg/L)",
|
| 23 |
+
"DO (Dissolved Oxygen) (mg/L)",
|
| 24 |
+
"NO3N (Nitrat) (mg/L)",
|
| 25 |
+
"Total Phosphat (mg/L)",
|
| 26 |
+
"Fecal Coliform (MPN/100 mL)"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# === Streamlit Setup ===
|
| 30 |
+
st.set_page_config(page_title="Water Quality Classifier Dashboard", layout="wide")
|
| 31 |
+
st.title("💧 Water Quality Prediction and Dashboard")
|
| 32 |
+
|
| 33 |
+
# === Model Selector ===
|
| 34 |
+
model_choice = st.selectbox("Select Model", ["SVC + SMOTETomek", "XGBoost + SMOTETomek"])
|
| 35 |
+
model = svc_model if model_choice == "SVC + SMOTETomek" else xgb_model
|
| 36 |
+
|
| 37 |
+
# === Input Section ===
|
| 38 |
+
st.header("📥 Input Data")
|
| 39 |
+
data_option = st.radio("Choose Input Method", ["Upload CSV", "Manual Entry"])
|
| 40 |
+
input_df = None
|
| 41 |
+
|
| 42 |
+
if data_option == "Upload CSV":
|
| 43 |
+
uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
|
| 44 |
+
if uploaded_file:
|
| 45 |
+
df = pd.read_csv(uploaded_file)
|
| 46 |
+
missing_cols = [col for col in feature_cols if col not in df.columns]
|
| 47 |
+
if missing_cols:
|
| 48 |
+
st.error(f"Missing required columns: {missing_cols}")
|
| 49 |
+
else:
|
| 50 |
+
input_df = df[feature_cols]
|
| 51 |
+
else:
|
| 52 |
+
with st.form("manual_form"):
|
| 53 |
+
ph = st.number_input("pH", min_value=0.0, max_value=14.0, value=7.0)
|
| 54 |
+
bod = st.number_input("BOD (mg/L)", min_value=0.0, max_value=100.0, value=2.0)
|
| 55 |
+
cod = st.number_input("COD (mg/L)", min_value=0.0, max_value=500.0, value=10.0)
|
| 56 |
+
tss = st.number_input("TSS (mg/L)", min_value=0.0, max_value=1000.0, value=20.0)
|
| 57 |
+
do = st.number_input("DO (mg/L)", min_value=0.0, max_value=20.0, value=5.0)
|
| 58 |
+
no3 = st.number_input("NO3N (mg/L)", min_value=0.0, max_value=10.0, value=1.0)
|
| 59 |
+
tp = st.number_input("Total Phosphat (mg/L)", min_value=0.0, max_value=10.0, value=0.1)
|
| 60 |
+
fecal = st.number_input("Fecal Coliform (MPN/100 mL)", min_value=0.0, max_value=1000000.0, value=500.0)
|
| 61 |
+
submitted = st.form_submit_button("Predict")
|
| 62 |
+
|
| 63 |
+
if submitted:
|
| 64 |
+
input_df = pd.DataFrame([{
|
| 65 |
+
"pH (Potential Hydrogen)": ph,
|
| 66 |
+
"BOD (Biological Oxygen Demand) (mg/L)": bod,
|
| 67 |
+
"COD (Chemical Oxygen Demand) (mg/L)": cod,
|
| 68 |
+
"TSS (Total Suspended Solid) (mg/L)": tss,
|
| 69 |
+
"DO (Dissolved Oxygen) (mg/L)": do,
|
| 70 |
+
"NO3N (Nitrat) (mg/L)": no3,
|
| 71 |
+
"Total Phosphat (mg/L)": tp,
|
| 72 |
+
"Fecal Coliform (MPN/100 mL)": fecal
|
| 73 |
+
}])
|
| 74 |
+
|
| 75 |
+
# === Prediction Section ===
|
| 76 |
+
if input_df is not None:
|
| 77 |
+
st.header("🔍 Prediction Results")
|
| 78 |
+
|
| 79 |
+
try:
|
| 80 |
+
# Preprocessing
|
| 81 |
+
X_imp = imputer.transform(input_df)
|
| 82 |
+
X_scaled = scaler.transform(X_imp)
|
| 83 |
+
|
| 84 |
+
# Prediction
|
| 85 |
+
y_proba = model.predict_proba(X_scaled)
|
| 86 |
+
y_pred = model.predict(X_scaled)
|
| 87 |
+
pred_class = label_encoder.inverse_transform(y_pred)[0]
|
| 88 |
+
|
| 89 |
+
st.markdown(f"### 🧪 Predicted Class: `{pred_class}`")
|
| 90 |
+
|
| 91 |
+
fig_pie = px.pie(
|
| 92 |
+
names=label_encoder.classes_,
|
| 93 |
+
values=y_proba[0],
|
| 94 |
+
title="Prediction Probability per Class",
|
| 95 |
+
color_discrete_sequence=px.colors.qualitative.Set3
|
| 96 |
+
)
|
| 97 |
+
st.plotly_chart(fig_pie, use_container_width=True)
|
| 98 |
+
|
| 99 |
+
# Export
|
| 100 |
+
st.subheader("📤 Download Prediction")
|
| 101 |
+
input_df["Predicted Class"] = pred_class
|
| 102 |
+
input_df[[f"Prob_{cls}" for cls in label_encoder.classes_]] = y_proba
|
| 103 |
+
csv = input_df.to_csv(index=False)
|
| 104 |
+
b64 = base64.b64encode(csv.encode()).decode()
|
| 105 |
+
href = f'<a href="data:file/csv;base64,{b64}" download="prediction_result.csv">Download CSV File</a>'
|
| 106 |
+
st.markdown(href, unsafe_allow_html=True)
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
st.error(f"Prediction failed: {e}")
|
| 110 |
+
|
| 111 |
+
# === Footer ===
|
| 112 |
+
st.markdown("---")
|
| 113 |
+
st.caption("Developed with ❤️ for real-time water quality analysis")
|
| 114 |
+
|
xgb_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dbb9a7b0ad941aad43124b0bc77282a36dad4e2b0baa2a75bebafed1deba3ad5
|
| 3 |
+
size 512625
|