ElifSB's picture
Upload 3 files
5dd8624 verified
Raw
History Blame Contribute Delete
3.51 kB
import streamlit as st
import pandas as pd
import numpy as np
import joblib
# 1. Page Configuration
st.set_page_config(page_title="ABC+ Product Segmentation", page_icon="📊", layout="wide")
# 2. Load Saved Models
@st.cache_resource
def load_assets():
model = joblib.load('ABC_Analysis_modeli.pkl')
scaler = joblib.load('scaler.pkl')
return model, scaler
try:
model, scaler = load_assets()
except Exception as e:
st.error(f"Model files could not be loaded: {e}")
# 3. Sidebar and Information
st.sidebar.header("About the Project")
st.sidebar.info(
"This application uses product data from the Wish platform to "
"segment products using the K-Means algorithm. "
"It determines the strategic value of products based on ABC Analysis logic."
)
st.sidebar.divider()
st.sidebar.warning(
"**Note on Currency:** All monetary values are processed in **Euro (€)** "
"to ensure analytical consistency across the dataset."
)
# 4. Main Header
st.title("📊 Smart Product Segmentation & ABC+ Analysis")
st.markdown("""
Enter the data of a new product to instantly learn which segment it belongs to and
its strategic importance for the business.
""")
# 5. User Input Fields
col1, col2 = st.columns(2)
with col1:
st.subheader("💰 Financial Data")
price = st.number_input("Selling Price (€)", min_value=0.0, value=10.0)
units_sold = st.number_input("Units Sold", min_value=0, value=1000)
# Revenue calculation
revenue = price * units_sold
st.write(f"**Calculated Total Revenue:** {revenue:,.2f} €")
with col2:
st.subheader("⭐ Performance Data")
rating = st.slider("Product Rating", 1.0, 5.0, 4.0)
discount_pct = st.slider("Discount Rate (%)", 0, 100, 20)
# 6. Prediction Mechanism
if st.button("Segment the Product"):
# Prepare data according to the column order seen in training
# Logarithmic transformation is applied before the model (np.log1p)
input_data = np.array([[
np.log1p(revenue),
np.log1p(units_sold),
rating,
price,
discount_pct
]])
# Scale the data with Scaler
scaled_data = scaler.transform(input_data)
# Make prediction (Returns cluster number)
cluster = model.predict(scaled_data)[0]
# Mapping (Dictionary structure from our analysis)
mapping = {
3: {'cat': 'A+ (Superstars)', 'color': 'gold', 'desc': 'The most valuable, high-revenue, and popular products of the store.'},
0: {'cat': 'A (High Quality)', 'color': 'green', 'desc': 'High customer satisfaction and products that generate regular income.'},
1: {'cat': 'B (Campaign Oriented)', 'color': 'orange', 'desc': 'Mid-segment products usually sold with high discounts.'},
2: {'cat': 'C (Low Performance)', 'color': 'red', 'desc': 'Products with low sales volume or weak ratings; risky products.'}
}
result = mapping.get(cluster, {'cat': 'Unknown', 'color': 'grey', 'desc': 'No specific description available.'})
# Result Screen
st.divider()
st.subheader(f"Predicted Segment: :{result['color']}[{result['cat']}]")
st.write(f"**Description:** {result['desc']}")
# Visual metric display
st.metric(label="Target Segment Alignment", value=result['cat'], delta="Analysis Completed")
# 7. Footer
st.divider()
st.caption("Developer: Elif Şensöz Beşiktepe | Data Science Project 2026")