File size: 2,332 Bytes
b869d9d
3def15f
77428ca
3def15f
ff6e462
3def15f
 
 
 
 
 
 
789b328
3def15f
77428ca
3def15f
 
789b328
3def15f
 
 
 
77428ca
 
 
789b328
3def15f
77428ca
3def15f
789b328
3def15f
789b328
3def15f
789b328
3def15f
789b328
77428ca
 
789b328
77428ca
 
 
 
 
3def15f
 
789b328
 
 
 
 
 
 
 
 
3def15f
 
 
789b328
 
77428ca
 
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
import streamlit as st
import pandas as pd
import joblib

MODEL_PATH = 'src/water_quality.joblib'

FEATURES = [
    'ph', 'Hardness', 'Solids', 'Chloramines', 'Sulfate', 
    'Conductivity', 'Organic_carbon', 'Trihalomethanes', 'Turbidity'
]

@st.cache_resource
def load_pipeline():
    try:
        pipeline = joblib.load(MODEL_PATH)
        return pipeline
    except Exception as e:
        st.error(f"Error loading model. Ensure '{MODEL_PATH}' exists. Error: {e}")
        return None

def make_prediction(pipeline, input_data):
    df_input = pd.DataFrame([input_data])
    predictions = pipeline.predict(df_input)
    probability = pipeline.predict_proba(df_input)[:, 1][0]  # probability of potable
    label = int(predictions[0])
    return label, probability

# --- Streamlit Interface ---
st.set_page_config(page_title="Water Quality Predictor", layout="centered")
st.title("💧 Water Potability Prediction")

pipeline = load_pipeline()

if pipeline:
    st.sidebar.header("Water Quality Parameters")
    ph = st.sidebar.number_input("pH Level:", 0.0, 14.0, 7.0, 0.1)
    hardness = st.sidebar.number_input("Hardness:", 50.0, 400.0, 200.0)
    solids = st.sidebar.number_input("Solids (ppm):", 300.0, 60000.0, 20000.0)
    chloramines = st.sidebar.number_input("Chloramines (ppm):", 0.0, 15.0, 7.0, 0.1)
    sulfate = st.sidebar.number_input("Sulfate (ppm):", 100.0, 500.0, 300.0)
    conductivity = st.sidebar.number_input("Conductivity (μS/cm):", 200.0, 800.0, 400.0)
    organic_carbon = st.sidebar.number_input("Organic Carbon (ppm):", 5.0, 30.0, 15.0)
    trihalomethanes = st.sidebar.number_input("Trihalomethanes (ppm):", 10.0, 150.0, 70.0)
    turbidity = st.sidebar.number_input("Turbidity (NTU):", 1.0, 7.0, 4.0)

    input_data = {
        'ph': ph,
        'Hardness': hardness,
        'Solids': solids,
        'Chloramines': chloramines,
        'Sulfate': sulfate,
        'Conductivity': conductivity,
        'Organic_carbon': organic_carbon,
        'Trihalomethanes': trihalomethanes,
        'Turbidity': turbidity
    }

    if st.button("Predict Potability"):
        label, prob = make_prediction(pipeline, input_data)
        status = "POTABLE ✅" if label == 1 else "NOT POTABLE ❌"
        st.success(f"Prediction: {status}")
        st.info(f"Probability of being potable: {prob*100:.2f}%")