Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import numpy as np | |
| import joblib | |
| import os | |
| # Load model and scaler | |
| kmeans_model = joblib.load("Prediction_model_v1_0.joblib") | |
| scaler = joblib.load("scaler_model_v1_0.joblib") | |
| st.title("Customer Segmentation using K-Means") | |
| st.write("This app predicts which customer segment a user belongs to.") | |
| # Function to handle input (text overrides slider if provided) | |
| def mixed_input(label, slider_min, slider_max, slider_default): | |
| col1, col2 = st.columns([2, 1]) | |
| slider_val = col1.slider(label, slider_min, slider_max, slider_default) | |
| text_val = col2.text_input(f"Enter exact {label} (optional)", "") | |
| return float(text_val) if text_val.strip() != "" else slider_val | |
| # Inputs | |
| income = mixed_input("Income", 10000, 200000, 50000) | |
| age = mixed_input("Age", 18, 80, 35) | |
| recency = mixed_input("Recency (days since last purchase)", 0, 100, 10) | |
| wines = mixed_input("Amount Wines", 0, 200, 50) | |
| fruits = mixed_input("Amount Fruits", 0, 100, 10) | |
| sweet = mixed_input("Amount Sweet Products", 0, 100, 5) | |
| meat_fish = mixed_input("Amount Meat/Fish", 0, 200, 30) | |
| web = mixed_input("Web Purchases", 0, 20, 4) | |
| store = mixed_input("Store Purchases", 0, 20, 6) | |
| # Prepare data | |
| data = np.array([[income, age, recency, wines, fruits, sweet, meat_fish, web, store]]) | |
| data_scaled = scaler.transform(data) | |
| # Predict | |
| cluster = kmeans_model.predict(data_scaled)[0] | |
| segment_names = { | |
| 0: "High-Value Loyal Shoppers", | |
| 1: "Dormant Low-Engagement Customers", | |
| 2: "Budget Frequent Buyers", | |
| 3: "Premium Wine & Gourmet Enthusiasts" | |
| } | |
| segment_label = segment_names.get(cluster, "Unknown Segment") | |
| st.write("### Predicted Customer Segment:") | |
| st.subheader(f"🚀 Customer Segment: {segment_label}") | |