import streamlit as st import pandas as pd import joblib # --- Configuration --- MODEL_PATH = 'src/customer_model.joblib' SCALER_PATH = 'src/scaler.joblib' FEATURES = ['Income', 'Seniority', 'Spending'] @st.cache_resource def load_assets(): try: model = joblib.load(MODEL_PATH) scaler = joblib.load(SCALER_PATH) return model, scaler except FileNotFoundError: st.error(f"Error: Model or Scaler file not found. Ensure both '{MODEL_PATH}' and '{SCALER_PATH}' are uploaded to the Space.") return None, None except Exception as e: st.error(f"Error loading assets: {e}") return None, None def predict_cluster(model, scaler, input_data): input_df = pd.DataFrame([input_data]) scaled_data = scaler.transform(input_df[FEATURES]) prediction = model.predict(scaled_data) return prediction[0] # --- Streamlit Interface --- st.set_page_config(page_title="Customer Clustering App", layout="wide") st.title("🥇 Customer Personality Cluster Prediction") st.markdown("Use the sidebar to input customer features and predict their cluster.") model, scaler = load_assets() if model is not None and scaler is not None: st.sidebar.header("Input Customer Features") income = st.sidebar.slider("Income ($):", min_value=1000, max_value=200000, value=50000) seniority = st.sidebar.slider("Seniority (Years as customer):", min_value=0, max_value=50, value=10) spending = st.sidebar.slider("Total Spending ($):", min_value=0, max_value=3000, value=500) input_data = { 'Income': income, 'Seniority': seniority, 'Spending': spending } st.subheader("Current Input Data:") st.write(pd.DataFrame([input_data])) if st.button("Predict Cluster"): with st.spinner('Predicting...'): cluster_id = predict_cluster(model, scaler, input_data) cluster_descriptions = { 0: "👑 **Cluster 0: High Potential**", 1: "🚨 **Cluster 1: Need Attention**", 2: "⏳ **Cluster 2: Leaky Bucket**", 3: "⭐ **Cluster 3: Stars**", } description = cluster_descriptions.get(cluster_id, f"🔍 Cluster ID **{cluster_id}** (Undefined)") st.success(f"Prediction Complete! The customer belongs to:") st.markdown(f"## {description}")