Download app.py from ESMATUGBA/customer-segmentation-app: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/spaces/ESMATUGBA/customer-segmentation-app/resolve/main/app.py
- Command line
-
hf download hf://spaces/ESMATUGBA/customer-segmentation-app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ESMATUGBA/customer-segmentation-app/resolve/main/app.py
4.3 kB
| import streamlit as st | |
| import numpy as np | |
| import joblib | |
| # MODEL YÜKLE / LOAD MODEL | |
| # Dosya adlarının klasöründekilerle aynı olduğundan emin ol! | |
| try: | |
| model = joblib.load("kmeans.pkl") | |
| scaler = joblib.load("scaler.pkl") | |
| except: | |
| st.error("Model dosyaları bulunamadı! / Model files not found!") | |
| st.set_page_config(page_title="Customer Segmentation", page_icon="💎") | |
| st.title("Customer Personality Segmentation / Müşteri Segmentasyonu") | |
| st.write("---") | |
| # 📊 SIDEBAR (Kriterler / Criteria) | |
| st.sidebar.header("📊 Segment Criteria / Segment Kriterleri") | |
| st.sidebar.subheader("💎 Premium") | |
| st.sidebar.write("Income / Gelir > 100,000") | |
| st.sidebar.write("Spending / Harcama > 2,000") | |
| st.sidebar.write("Children / Çocuk = 0") | |
| st.sidebar.write("---") | |
| st.sidebar.subheader("🛒 Budget / Bütçeli") | |
| st.sidebar.write("Income / Gelir > 40,000") | |
| st.sidebar.write("Spending / Harcama > 500") | |
| st.sidebar.write("---") | |
| st.sidebar.subheader("📉 Occasional / Seyrek") | |
| st.sidebar.write("Income / Gelir < 20,000") | |
| st.sidebar.write("Spending / Harcama < 100") | |
| st.sidebar.write("---") | |
| st.sidebar.subheader("⚠️ Alternative / Alternatif") | |
| st.sidebar.write("Others / Diğerleri") | |
| # 🎯 INPUTLAR / INPUTS | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| # İstediğin 110.000 ve 3.000 değerlerini varsayılan (default) yaptım: | |
| income = st.number_input("Income / Gelir ($)", value=110000) | |
| spending = st.number_input("Spending / Harcama ($)", value=3000) | |
| with col2: | |
| kidhome = st.number_input("Kids / Çocuklar", value=0) | |
| teenhome = st.number_input("Teens / Gençler", value=0) | |
| recency = st.slider("Recency / Son Alışveriş (Gün)", 0, 100, 15) | |
| # FEATURE ENGINEERING | |
| children = kidhome + teenhome | |
| st.write("---") | |
| # 🔥 SEGMENT FONKSİYONU / SEGMENT FUNCTION | |
| def segment_belirle(income, spending, children): | |
| # Premium Kuralı | |
| if income > 100000 and spending > 2000 and children == 0: | |
| return "💎 Premium" | |
| # Budget Kuralı | |
| elif income > 40000 and spending > 500: | |
| return "🛒 Budget" | |
| # Occasional Kuralı | |
| elif income < 20000 and spending < 100: | |
| return "📉 Occasional" | |
| # Diğerleri | |
| else: | |
| return "⚠️ Alternative" | |
| # 🚀 BUTON / PREDICT | |
| if st.button("Predict Segment / Segmenti Tahmin Et", use_container_width=True): | |
| try: | |
| # MODEL GİRDİSİ (4 Özellik beklediğini varsayıyoruz) | |
| # Sıralama: [Income, Spending, Children, Recency] | |
| data = np.array([[income, spending, children, recency]]) | |
| data_scaled = scaler.transform(data) | |
| cluster = model.predict(data_scaled)[0] | |
| # 🔥 ASIL SONUÇ (KURAL) | |
| segment = segment_belirle(income, spending, children) | |
| # Kullanıcıya bilgi ver / Info Box | |
| st.info(f""" | |
| 📌 **Entered Values / Girilen Değerler:** | |
| - Income / Gelir: **${income:,}** | |
| - Spending / Harcama: **${spending:,}** | |
| - Total Children / Toplam Çocuk: **{children}** | |
| - Recency / Güncellik: **{recency} days** | |
| """) | |
| st.write(f"🔍 **AI Cluster / Model Kümesi:** {cluster}") | |
| # 🎯 SONUÇ GÖSTERİMİ / RESULT DISPLAY | |
| if segment == "💎 Premium": | |
| st.balloons() # 🎈 Premium tebriği! | |
| st.success("### Result: 💎 Premium Customer / Değerli Müşteri") | |
| st.write("This customer is high-value and loyal. / Bu müşteri yüksek değerli ve sadıktır.") | |
| elif segment == "🛒 Budget": | |
| st.info("### Result: 🛒 Budget Customer / Bütçeli Müşteri") | |
| st.write("Standard customer with stable spending. / Dengeli harcaması olan standart müşteri.") | |
| elif segment == "📉 Occasional": | |
| st.warning("### Result: 📉 Occasional Customer / Seyrek Müşteri") | |
| st.write("Low frequency, potential for campaigns. / Düşük frekanslı, kampanya hedefi olabilir.") | |
| else: | |
| st.error("### Result: ⚠️ Alternative Customer / Alternatif Müşteri") | |
| st.write("Uncategorized or unique behavior. / Kategorize edilmemiş veya özel davranışlı.") | |
| except Exception as e: | |
| st.error(f"Error / Hata oluştu: {e}") | |
| st.write("---") | |
| st.caption("Marketing Campaign Analysis 2026 | Streamlit Multi-Language App") |