Spaces:
Sleeping
Sleeping
File size: 2,434 Bytes
114c2d3 3bad285 2311676 | 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 63 64 65 66 67 68 69 70 | 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}") |