Penguin_Island_Prediction / src /streamlit_app.py
Jotheeswaransakthivel's picture
Update src/streamlit_app.py
81c3dc5 verified
import streamlit as st
import joblib
import numpy as np
# Load model pipeline
pipeline = joblib.load("src/logistic_model_pipeline")
model = pipeline["model"]
num_scaler = pipeline["num_scaler"]
cat_encoder = pipeline["cat_encoder"]
y_encoder = pipeline["y_encoder"]
num_columns = pipeline["num_columns"]
cat_columns = pipeline["cat_columns"]
# Streamlit page setup
st.set_page_config(page_title="🐧 Penguin Classifier", layout="centered")
st.title("🐧 Penguin Species Prediction")
st.subheader("πŸ“₯ Enter the Penguin's Features")
# Numeric input using sliders with actual data ranges
st.markdown("### πŸ”’ Numeric Features")
num_inputs = []
slider_ranges = {
"bill_length_mm": (32.1, 59.6),
"bill_depth_mm": (13.1, 21.5),
"flipper_length_mm": (172.0, 231.0),
"body_mass_g": (2700.0, 6300.0)
}
for col in num_columns:
min_val, max_val = slider_ranges.get(col, (0.0, 100.0)) # fallback in case
val = st.slider(
f"{col.replace('_', ' ').title()}",
min_value=min_val,
max_value=max_val,
step=0.1
)
num_inputs.append(val)
# Categorical input
st.markdown("### 🧬 Categorical Features")
cat_inputs = []
for col in cat_columns:
if "island" in col.lower():
val = st.selectbox(f"{col.replace('_', ' ').title()}:", ["Dream", "Torgersen", "Biscoe"])
else:
val = st.text_input(f"{col.replace('_', ' ').title()}:")
cat_inputs.append(val)
# Prediction button
if st.button("πŸš€ Predict"):
try:
# Transform inputs
scaled_nums = num_scaler.transform([num_inputs])
encoded_cats = cat_encoder.transform([cat_inputs])
combined = np.hstack([scaled_nums, encoded_cats])
# Predict
result = model.predict(combined)
final_pred = y_encoder.inverse_transform(result)
st.success(f"βœ… The predicted species is: **{final_pred[0]}**")
st.balloons()
except Exception as e:
st.error(f"Oops! Something went wrong: {e}")