|
|
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(): |
|
|
|
|
|
svc_model = joblib.load("svc_pipeline.pkl") |
|
|
xgb_model = joblib.load("xgb_pipeline.pkl") |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Water Quality Classifier Dashboard", layout="wide") |
|
|
st.title("π§ Water Quality Prediction and Model Dashboard") |
|
|
|
|
|
|
|
|
model_choice = st.selectbox("Select Model", ["SVC + SMOTETomek", "XGBoost + SMOTETomek"]) |
|
|
model = svc_model if model_choice == "SVC + SMOTETomek" else xgb_model |
|
|
|
|
|
|
|
|
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 |
|
|
}]) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
st.markdown("---") |
|
|
st.markdown("Developed with β€οΈ for real-world decision support in water quality monitoring.") |
|
|
|