File size: 3,739 Bytes
186d093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
87
import streamlit as st
import pandas as pd
import numpy as np
import joblib
import plotly.express as px
import base64
from sklearn.preprocessing import LabelEncoder

def run():
    # === Load models ===
    svc_model = joblib.load("svc_pipeline.pkl")
    xgb_model = joblib.load("xgb_pipeline.pkl")

    # === App Config ===
    st.set_page_config(page_title="Water Quality Classifier Dashboard", layout="wide")
    st.title("πŸ’§ Water Quality Prediction and Model Dashboard")

    # === Model Selector ===
    model_choice = st.selectbox("Select Model", ["SVC + SMOTETomek", "XGBoost + SMOTETomek"])
    model = svc_model if model_choice == "SVC + SMOTETomek" else xgb_model

    # === Input Section ===
    st.header("πŸ“₯ Input Data")
    data_option = st.radio("Input Method", ["Upload CSV", "Manual Entry"])
    input_df = None

    if data_option == "Upload CSV":
        uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
        if uploaded_file:
            input_df = pd.read_csv(uploaded_file)
    else:
        with st.form("manual_form"):
            ph = st.number_input("pH", min_value=1.0, max_value=14.0, value=7.0)
            bod = st.number_input("BOD (mg/L)", min_value=0.0, max_value=100.0, value=2.0)
            cod = st.number_input("COD (mg/L)", min_value=0.0, max_value=500.0, value=10.0)
            tss = st.number_input("TSS (mg/L)", min_value=0.0, max_value=1000.0, value=20.0)
            do = st.number_input("DO (mg/L)", min_value=0.0, max_value=20.0, value=5.0)
            no3 = st.number_input("NO3N (mg/L)", min_value=0.0, max_value=10.0, value=1.0)
            tp = st.number_input("Total Phosphat (mg/L)", min_value=0.0, max_value=10.0, value=0.1)
            fecal = st.number_input("Fecal Coliform (MPN/100mL)", min_value=0.0, max_value=1000000.0, value=500.0)
            submitted = st.form_submit_button("Predict")

        if submitted:
            input_df = pd.DataFrame([{
                "pH (Potential Hydrogen)": ph,
                "BOD (Biological Oxygen Demand) (mg/L)": bod,
                "COD (Chemical Oxygen Demand) (mg/L)": cod,
                "TSS (Total Suspended Solid) (mg/L)": tss,
                "DO (Dissolved Oxygen) (mg/L)": do,
                "NO3N (Nitrat) (mg/L)": no3,
                "Total Phosphat (mg/L)": tp,
                "Fecal Coliform (MPN/100 mL)": fecal
            }])

    # === Prediction Section ===
    if input_df is not None:
        st.header("πŸ” Prediction Results")
        y_proba = model.predict_proba(input_df)
        y_pred = model.predict(input_df)

        label_encoder = LabelEncoder()
        label_encoder.classes_ = np.array(["Biological", "Chemical", "Eutrophication", "Safe"])
        pred_class = label_encoder.inverse_transform(y_pred)[0]

        st.markdown(f"### πŸ§ͺ Predicted Class: `{pred_class}`")

        fig_pie = px.pie(
            names=label_encoder.classes_,
            values=y_proba[0],
            title="Prediction Probability per Class",
            color_discrete_sequence=px.colors.qualitative.Set3
        )
        st.plotly_chart(fig_pie, use_container_width=True)

        # === Download CSV ===
        st.subheader("πŸ“€ Download Prediction")
        input_df["Predicted Class"] = pred_class
        input_df[[f"Prob_{c}" for c in label_encoder.classes_]] = y_proba
        csv = input_df.to_csv(index=False)
        b64 = base64.b64encode(csv.encode()).decode()
        href = f'<a href="data:file/csv;base64,{b64}" download="prediction_result.csv">Download CSV File</a>'
        st.markdown(href, unsafe_allow_html=True)

    # === Footer ===
    st.markdown("---")
    st.markdown("Developed with ❀️ for real-world decision support in water quality monitoring.")