darly9991 commited on
Commit
186d093
·
verified ·
1 Parent(s): 4e5ebe4

Update water_quality_index.py

Browse files
Files changed (1) hide show
  1. water_quality_index.py +86 -0
water_quality_index.py CHANGED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 models ===
11
+ svc_model = joblib.load("svc_pipeline.pkl")
12
+ xgb_model = joblib.load("xgb_pipeline.pkl")
13
+
14
+ # === App Config ===
15
+ st.set_page_config(page_title="Water Quality Classifier Dashboard", layout="wide")
16
+ st.title("💧 Water Quality Prediction and Model Dashboard")
17
+
18
+ # === Model Selector ===
19
+ model_choice = st.selectbox("Select Model", ["SVC + SMOTETomek", "XGBoost + SMOTETomek"])
20
+ model = svc_model if model_choice == "SVC + SMOTETomek" else xgb_model
21
+
22
+ # === Input Section ===
23
+ st.header("📥 Input Data")
24
+ data_option = st.radio("Input Method", ["Upload CSV", "Manual Entry"])
25
+ input_df = None
26
+
27
+ if data_option == "Upload CSV":
28
+ uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
29
+ if uploaded_file:
30
+ input_df = pd.read_csv(uploaded_file)
31
+ else:
32
+ with st.form("manual_form"):
33
+ ph = st.number_input("pH", min_value=1.0, max_value=14.0, value=7.0)
34
+ bod = st.number_input("BOD (mg/L)", min_value=0.0, max_value=100.0, value=2.0)
35
+ cod = st.number_input("COD (mg/L)", min_value=0.0, max_value=500.0, value=10.0)
36
+ tss = st.number_input("TSS (mg/L)", min_value=0.0, max_value=1000.0, value=20.0)
37
+ do = st.number_input("DO (mg/L)", min_value=0.0, max_value=20.0, value=5.0)
38
+ no3 = st.number_input("NO3N (mg/L)", min_value=0.0, max_value=10.0, value=1.0)
39
+ tp = st.number_input("Total Phosphat (mg/L)", min_value=0.0, max_value=10.0, value=0.1)
40
+ fecal = st.number_input("Fecal Coliform (MPN/100mL)", min_value=0.0, max_value=1000000.0, value=500.0)
41
+ submitted = st.form_submit_button("Predict")
42
+
43
+ if submitted:
44
+ input_df = pd.DataFrame([{
45
+ "pH (Potential Hydrogen)": ph,
46
+ "BOD (Biological Oxygen Demand) (mg/L)": bod,
47
+ "COD (Chemical Oxygen Demand) (mg/L)": cod,
48
+ "TSS (Total Suspended Solid) (mg/L)": tss,
49
+ "DO (Dissolved Oxygen) (mg/L)": do,
50
+ "NO3N (Nitrat) (mg/L)": no3,
51
+ "Total Phosphat (mg/L)": tp,
52
+ "Fecal Coliform (MPN/100 mL)": fecal
53
+ }])
54
+
55
+ # === Prediction Section ===
56
+ if input_df is not None:
57
+ st.header("🔍 Prediction Results")
58
+ y_proba = model.predict_proba(input_df)
59
+ y_pred = model.predict(input_df)
60
+
61
+ label_encoder = LabelEncoder()
62
+ label_encoder.classes_ = np.array(["Biological", "Chemical", "Eutrophication", "Safe"])
63
+ pred_class = label_encoder.inverse_transform(y_pred)[0]
64
+
65
+ st.markdown(f"### 🧪 Predicted Class: `{pred_class}`")
66
+
67
+ fig_pie = px.pie(
68
+ names=label_encoder.classes_,
69
+ values=y_proba[0],
70
+ title="Prediction Probability per Class",
71
+ color_discrete_sequence=px.colors.qualitative.Set3
72
+ )
73
+ st.plotly_chart(fig_pie, use_container_width=True)
74
+
75
+ # === Download CSV ===
76
+ st.subheader("📤 Download Prediction")
77
+ input_df["Predicted Class"] = pred_class
78
+ input_df[[f"Prob_{c}" for c in label_encoder.classes_]] = y_proba
79
+ csv = input_df.to_csv(index=False)
80
+ b64 = base64.b64encode(csv.encode()).decode()
81
+ href = f'<a href="data:file/csv;base64,{b64}" download="prediction_result.csv">Download CSV File</a>'
82
+ st.markdown(href, unsafe_allow_html=True)
83
+
84
+ # === Footer ===
85
+ st.markdown("---")
86
+ st.markdown("Developed with ❤️ for real-world decision support in water quality monitoring.")