ElifSB's picture
Upload 3 files
76d272a verified
Raw
History Blame Contribute Delete
2.96 kB
import streamlit as st
import pandas as pd
import joblib
import numpy as np
# 1. Page Configuration
st.set_page_config(page_title="Water Quality AI Predictor", page_icon="💧", layout="centered")
# 2. Load Model and Scaler
@st.cache_resource
def load_assets():
# Ensure these files are uploaded to your Hugging Face Space
model = joblib.load('su_kalite_modeli.pkl')
scaler = joblib.load('su_scaler.pkl')
return model, scaler
try:
model, scaler = load_assets()
except Exception as e:
st.error("Error: Model or Scaler files not found. Please upload .pkl files to the repository.")
# 3. Header & Introduction
st.title("🌊 Water Quality AI Classification")
st.markdown("""
This AI-powered tool classifies water samples as **Healthy** or **Risky** using real-time sensor data.
Adjust the parameters on the left to see the prediction.
""")
# 4. Sidebar for User Input
st.sidebar.header("Manual Input Parameters")
def user_input_features():
salinity = st.sidebar.slider("Salinity (ppt)", 0.0, 40.0, 30.0)
oxygen = st.sidebar.slider("Dissolved Oxygen (mg/L)", 0.0, 15.0, 7.0)
ph = st.sidebar.slider("pH Level", 0.0, 14.0, 7.5)
secchi = st.sidebar.slider("Secchi Depth (m)", 0.0, 5.0, 1.0)
depth = st.sidebar.slider("Water Depth (m)", 0.0, 20.0, 5.0)
temp = st.sidebar.slider("Water Temp (°C)", 0.0, 40.0, 22.0)
air_temp = st.sidebar.slider("Air Temp (°C)", -10.0, 50.0, 25.0)
# Constant features based on your model's 15-feature requirement
year = 2024
site_b, site_bay, site_c, site_d, site_small_d = 0, 1, 0, 0, 0
year_feat = 2024
month_feat = 6
data = [[salinity, oxygen, ph, secchi, depth, temp, air_temp, year,
site_b, site_bay, site_c, site_d, site_small_d, year_feat, month_feat]]
return data
input_data = user_input_features()
# 5. Prediction Logic
if st.button("Run AI Analysis"):
# Scaling
input_scaled = scaler.transform(input_data)
# Prediction
prediction = model.predict(input_scaled)
prediction_proba = model.predict_proba(input_scaled)
confidence = np.max(prediction_proba) * 100
st.divider()
st.subheader("Analysis Result")
if prediction[0] == 1:
st.success(f"✅ **STATUS: HEALTHY**")
st.metric(label="Confidence Level", value=f"{confidence:.2f}%")
st.write("The water parameters are within the safe range for aquatic life.")
st.snow() # Water-drop-like effect instead of balloons
else:
st.error(f"🚨 **STATUS: RISKY**")
st.metric(label="Confidence Level", value=f"{confidence:.2f}%")
st.write("Warning: Oxygen or pH levels indicate a potential risk to the ecosystem.")
# 6. Model Info Footer
st.divider()
st.info(f"""
**Technical Specs:**
- **Model:** Gradient Boosting Classifier
- **Accuracy:** 84%
- **Top Feature:** Dissolved Oxygen
""")