ESMATUGBA commited on
Commit
60ab12d
·
verified ·
1 Parent(s): dc4f851

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -120
app.py CHANGED
@@ -1,121 +1,121 @@
1
- import streamlit as st
2
- import numpy as np
3
- import joblib
4
-
5
- # MODEL YÜKLE / LOAD MODEL
6
- # Dosya adlarının klasöründekilerle aynı olduğundan emin ol!
7
- try:
8
- model = joblib.load("kmeans.pkl")
9
- scaler = joblib.load("scaler.pkl")
10
- except:
11
- st.error("Model dosyaları bulunamadı! / Model files not found!")
12
-
13
- st.set_page_config(page_title="Customer Segmentation", page_icon="💎")
14
- st.title("Customer Personality Segmentation / Müşteri Segmentasyonu")
15
- st.write("---")
16
-
17
- # 📊 SIDEBAR (Kriterler / Criteria)
18
- st.sidebar.header("📊 Segment Criteria / Segment Kriterleri")
19
-
20
- st.sidebar.subheader("💎 Premium")
21
- st.sidebar.write("Income / Gelir > 100,000")
22
- st.sidebar.write("Spending / Harcama > 2,000")
23
- st.sidebar.write("Children / Çocuk = 0")
24
-
25
- st.sidebar.write("---")
26
-
27
- st.sidebar.subheader("🛒 Budget / Bütçeli")
28
- st.sidebar.write("Income / Gelir > 40,000")
29
- st.sidebar.write("Spending / Harcama > 500")
30
-
31
- st.sidebar.write("---")
32
-
33
- st.sidebar.subheader("📉 Occasional / Seyrek")
34
- st.sidebar.write("Income / Gelir < 20,000")
35
- st.sidebar.write("Spending / Harcama < 100")
36
-
37
- st.sidebar.write("---")
38
-
39
- st.sidebar.subheader("⚠️ Alternative / Alternatif")
40
- st.sidebar.write("Others / Diğerleri")
41
-
42
- # 🎯 INPUTLAR / INPUTS
43
- col1, col2 = st.columns(2)
44
-
45
- with col1:
46
- # İstediğin 110.000 ve 3.000 değerlerini varsayılan (default) yaptım:
47
- income = st.number_input("Income / Gelir ($)", value=110000)
48
- spending = st.number_input("Spending / Harcama ($)", value=3000)
49
-
50
- with col2:
51
- kidhome = st.number_input("Kids / Çocuklar", value=0)
52
- teenhome = st.number_input("Teens / Gençler", value=0)
53
- recency = st.slider("Recency / Son Alışveriş (Gün)", 0, 100, 15)
54
-
55
- # FEATURE ENGINEERING
56
- children = kidhome + teenhome
57
-
58
- st.write("---")
59
-
60
- # 🔥 SEGMENT FONKSİYONU / SEGMENT FUNCTION
61
- def segment_belirle(income, spending, children):
62
- # Premium Kuralı
63
- if income > 100000 and spending > 2000 and children == 0:
64
- return "💎 Premium"
65
- # Budget Kuralı
66
- elif income > 40000 and spending > 500:
67
- return "🛒 Budget"
68
- # Occasional Kuralı
69
- elif income < 20000 and spending < 100:
70
- return "📉 Occasional"
71
- # Diğerleri
72
- else:
73
- return "⚠️ Alternative"
74
-
75
- # 🚀 BUTON / PREDICT
76
- if st.button("Predict Segment / Segmenti Tahmin Et", use_container_width=True):
77
-
78
- try:
79
- # MODEL GİRDİSİ (4 Özellik beklediğini varsayıyoruz)
80
- # Sıralama: [Income, Spending, Children, Recency]
81
- data = np.array([[income, spending, children, recency]])
82
- data_scaled = scaler.transform(data)
83
- cluster = model.predict(data_scaled)[0]
84
-
85
- # 🔥 ASIL SONUÇ (KURAL)
86
- segment = segment_belirle(income, spending, children)
87
-
88
- # Kullanıcıya bilgi ver / Info Box
89
- st.info(f"""
90
- 📌 **Entered Values / Girilen Değerler:**
91
- - Income / Gelir: **${income:,}**
92
- - Spending / Harcama: **${spending:,}**
93
- - Total Children / Toplam Çocuk: **{children}**
94
- - Recency / Güncellik: **{recency} days**
95
- """)
96
-
97
- st.write(f"🔍 **AI Cluster / Model Kümesi:** {cluster}")
98
-
99
- # 🎯 SONUÇ GÖSTERİMİ / RESULT DISPLAY
100
- if segment == "💎 Premium":
101
- st.balloons() # 🎈 Premium tebriği!
102
- st.success("### Result: 💎 Premium Customer / Değerli Müşteri")
103
- st.write("This customer is high-value and loyal. / Bu müşteri yüksek değerli ve sadıktır.")
104
-
105
- elif segment == "🛒 Budget":
106
- st.info("### Result: 🛒 Budget Customer / Bütçeli Müşteri")
107
- st.write("Standard customer with stable spending. / Dengeli harcaması olan standart müşteri.")
108
-
109
- elif segment == "📉 Occasional":
110
- st.warning("### Result: 📉 Occasional Customer / Seyrek Müşteri")
111
- st.write("Low frequency, potential for campaigns. / Düşük frekanslı, kampanya hedefi olabilir.")
112
-
113
- else:
114
- st.error("### Result: ⚠️ Alternative Customer / Alternatif Müşteri")
115
- st.write("Uncategorized or unique behavior. / Kategorize edilmemiş veya özel davranışlı.")
116
-
117
- except Exception as e:
118
- st.error(f"Error / Hata oluştu: {e}")
119
-
120
- st.write("---")
121
  st.caption("Marketing Campaign Analysis 2026 | Streamlit Multi-Language App")
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import joblib
4
+
5
+ # MODEL YÜKLE / LOAD MODEL
6
+ # Dosya adlarının klasöründekilerle aynı olduğundan emin ol!
7
+ try:
8
+ model = joblib.load("kmeans.pkl")
9
+ scaler = joblib.load("scaler.pkl")
10
+ except:
11
+ st.error("Model dosyaları bulunamadı! / Model files not found!")
12
+
13
+ st.set_page_config(page_title="Customer Segmentation", page_icon="💎")
14
+ st.title("Customer Personality Segmentation / Müşteri Segmentasyonu")
15
+ st.write("---")
16
+
17
+ # 📊 SIDEBAR (Kriterler / Criteria)
18
+ st.sidebar.header("📊 Segment Criteria / Segment Kriterleri")
19
+
20
+ st.sidebar.subheader("💎 Premium")
21
+ st.sidebar.write("Income / Gelir > 100,000")
22
+ st.sidebar.write("Spending / Harcama > 2,000")
23
+ st.sidebar.write("Children / Çocuk = 0")
24
+
25
+ st.sidebar.write("---")
26
+
27
+ st.sidebar.subheader("🛒 Budget / Bütçeli")
28
+ st.sidebar.write("Income / Gelir > 40,000")
29
+ st.sidebar.write("Spending / Harcama > 500")
30
+
31
+ st.sidebar.write("---")
32
+
33
+ st.sidebar.subheader("📉 Occasional / Seyrek")
34
+ st.sidebar.write("Income / Gelir < 20,000")
35
+ st.sidebar.write("Spending / Harcama < 100")
36
+
37
+ st.sidebar.write("---")
38
+
39
+ st.sidebar.subheader("⚠️ Alternative / Alternatif")
40
+ st.sidebar.write("Others / Diğerleri")
41
+
42
+ # 🎯 INPUTLAR / INPUTS
43
+ col1, col2 = st.columns(2)
44
+
45
+ with col1:
46
+ # İstediğin 110.000 ve 3.000 değerlerini varsayılan (default) yaptım:
47
+ income = st.number_input("Income / Gelir ($)", value=110000)
48
+ spending = st.number_input("Spending / Harcama ($)", value=3000)
49
+
50
+ with col2:
51
+ kidhome = st.number_input("Kids / Çocuklar", value=0)
52
+ teenhome = st.number_input("Teens / Gençler", value=0)
53
+ recency = st.slider("Recency / Son Alışveriş (Gün)", 0, 100, 15)
54
+
55
+ # FEATURE ENGINEERING
56
+ children = kidhome + teenhome
57
+
58
+ st.write("---")
59
+
60
+ # 🔥 SEGMENT FONKSİYONU / SEGMENT FUNCTION
61
+ def segment_belirle(income, spending, children):
62
+ # Premium Kuralı
63
+ if income > 100000 and spending > 2000 and children == 0:
64
+ return "💎 Premium"
65
+ # Budget Kuralı
66
+ elif income > 40000 and spending > 500:
67
+ return "🛒 Budget"
68
+ # Occasional Kuralı
69
+ elif income < 20000 and spending < 100:
70
+ return "📉 Occasional"
71
+ # Diğerleri
72
+ else:
73
+ return "⚠️ Alternative"
74
+
75
+ # 🚀 BUTON / PREDICT
76
+ if st.button("Predict Segment / Segmenti Tahmin Et", use_container_width=True):
77
+
78
+ try:
79
+ # MODEL GİRDİSİ (4 Özellik beklediğini varsayıyoruz)
80
+ # Sıralama: [Income, Spending, Children, Recency]
81
+ data = np.array([[income, spending, children, recency]])
82
+ data_scaled = scaler.transform(data)
83
+ cluster = model.predict(data_scaled)[0]
84
+
85
+ # 🔥 ASIL SONUÇ (KURAL)
86
+ segment = segment_belirle(income, spending, children)
87
+
88
+ # Kullanıcıya bilgi ver / Info Box
89
+ st.info(f"""
90
+ 📌 **Entered Values / Girilen Değerler:**
91
+ - Income / Gelir: **${income:,}**
92
+ - Spending / Harcama: **${spending:,}**
93
+ - Total Children / Toplam Çocuk: **{children}**
94
+ - Recency / Güncellik: **{recency} days**
95
+ """)
96
+
97
+ st.write(f"🔍 **AI Cluster / Model Kümesi:** {cluster}")
98
+
99
+ # 🎯 SONUÇ GÖSTERİMİ / RESULT DISPLAY
100
+ if segment == "💎 Premium":
101
+ st.balloons() # 🎈 Premium tebriği!
102
+ st.success("### Result: 💎 Premium Customer / Değerli Müşteri")
103
+ st.write("This customer is high-value and loyal. / Bu müşteri yüksek değerli ve sadıktır.")
104
+
105
+ elif segment == "🛒 Budget":
106
+ st.info("### Result: 🛒 Budget Customer / Bütçeli Müşteri")
107
+ st.write("Standard customer with stable spending. / Dengeli harcaması olan standart müşteri.")
108
+
109
+ elif segment == "📉 Occasional":
110
+ st.warning("### Result: 📉 Occasional Customer / Seyrek Müşteri")
111
+ st.write("Low frequency, potential for campaigns. / Düşük frekanslı, kampanya hedefi olabilir.")
112
+
113
+ else:
114
+ st.error("### Result: ⚠️ Alternative Customer / Alternatif Müşteri")
115
+ st.write("Uncategorized or unique behavior. / Kategorize edilmemiş veya özel davranışlı.")
116
+
117
+ except Exception as e:
118
+ st.error(f"Error / Hata oluştu: {e}")
119
+
120
+ st.write("---")
121
  st.caption("Marketing Campaign Analysis 2026 | Streamlit Multi-Language App")