|
1 | | -import streamlit as st |
2 | | -import numpy as np |
3 | | -import tensorflow as tf |
4 | | -import pandas as pd |
| 1 | +import streamlit as st |
| 2 | +import numpy as np |
| 3 | +import tensorflow as tf |
| 4 | +import pandas as pd |
5 | 5 | import pickle |
6 | 6 | import os |
7 | 7 |
|
8 | | -# Define base paths |
| 8 | +# 🎨 Set Streamlit theme (for more color) |
| 9 | +st.set_page_config( |
| 10 | + page_title="Customer Churn Prediction", |
| 11 | + page_icon="🔮", |
| 12 | + layout="centered", |
| 13 | + initial_sidebar_state="auto" |
| 14 | +) |
| 15 | +# Background with gradient using markdown (limited colors in Streamlit natively) |
| 16 | +st.markdown( |
| 17 | + """ |
| 18 | + <style> |
| 19 | + .stApp { |
| 20 | + background: linear-gradient(135deg, #f8ffae 0%, #43cea2 100%); |
| 21 | + } |
| 22 | + .highlight { |
| 23 | + padding: 0.5em 1em; |
| 24 | + border-radius: 0.5em; |
| 25 | + margin: 1em 0; |
| 26 | + } |
| 27 | + .info-hl { background: #d0f9ff; } |
| 28 | + .warn-hl { background: #ffcccc; } |
| 29 | + .succ-hl { background: #dcffe4; } |
| 30 | + .predict-prob { |
| 31 | + font-size: 2em; |
| 32 | + font-weight: bold; |
| 33 | + color: #fa8231; |
| 34 | + } |
| 35 | + </style> |
| 36 | + """, |
| 37 | + unsafe_allow_html=True |
| 38 | +) |
| 39 | + |
9 | 40 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
10 | 41 | MODELS_DIR = os.path.join(BASE_DIR, 'models') |
11 | 42 |
|
|
21 | 52 | with open(os.path.join(MODELS_DIR, 'scaler.pkl'), 'rb') as file: |
22 | 53 | scaler = pickle.load(file) |
23 | 54 |
|
24 | | -st.title("Customer Churn Prediction") |
| 55 | +st.markdown( |
| 56 | + """ |
| 57 | + <div style='text-align:center;'> |
| 58 | + <h1 style='color:#3b6978; font-size:2.5em; margin-bottom:0.2em;'>🔮 Customer Churn Prediction 🔮</h1> |
| 59 | + <p style='color:#204051; font-size:1.2em;'> |
| 60 | + Empower your business with colorful, instant predictions! |
| 61 | + </p> |
| 62 | + </div> |
| 63 | + """, |
| 64 | + unsafe_allow_html=True, |
| 65 | +) |
| 66 | + |
| 67 | +# Add a colored horizontal rule for aesthetics |
| 68 | +st.markdown("<hr style='border-top: 2px dotted #00b894;'>", unsafe_allow_html=True) |
25 | 69 |
|
26 | | -# Input form |
27 | 70 | with st.form("churn_prediction_form"): |
28 | | - geography = st.selectbox('Geography', onehot_encoder_geo.categories_[0]) |
29 | | - gender = st.selectbox('Gender', label_encoder_gender.classes_) |
30 | | - age = st.slider('Age', 18, 92) |
31 | | - balance = st.number_input('Balance') |
32 | | - credit_score = st.number_input('Credit Score') |
33 | | - estimated_salary = st.number_input('Estimated Salary') |
34 | | - tenure = st.slider('Tenure', 0, 10) |
35 | | - num_of_products = st.slider('Number of Products', 1, 4) |
36 | | - has_cr_card = st.selectbox('Has Credit Card', [0, 1]) |
37 | | - is_active_member = st.selectbox('Is Active Member', [0, 1]) |
| 71 | + st.markdown("<div class='highlight info-hl'>Please fill out the customer details:</div>", unsafe_allow_html=True) |
| 72 | + c1, c2 = st.columns(2) |
| 73 | + with c1: |
| 74 | + geography = st.selectbox('🌎 Geography', onehot_encoder_geo.categories_[0]) |
| 75 | + gender = st.selectbox('🚻 Gender', label_encoder_gender.classes_) |
| 76 | + age = st.slider('🎂 Age', 18, 92, 30) |
| 77 | + credit_score = st.number_input('💳 Credit Score', min_value=0, max_value=1000, value=650) |
| 78 | + tenure = st.slider('⌛ Tenure (years)', 0, 10, 3) |
| 79 | + with c2: |
| 80 | + balance = st.number_input('🏦 Balance', min_value=0.0, value=10000.0) |
| 81 | + estimated_salary = st.number_input('💰 Estimated Salary', min_value=0.0, value=50000.0) |
| 82 | + num_of_products = st.slider('🛒 Number of Products', 1, 4, 1) |
| 83 | + has_cr_card = st.selectbox('💳 Has Credit Card', ['No', 'Yes']) |
| 84 | + is_active_member = st.selectbox('🟢 Is Active Member', ['No', 'Yes']) |
| 85 | + |
| 86 | + # Map Yes/No to 1/0 |
| 87 | + has_cr_card_val = 1 if has_cr_card == 'Yes' else 0 |
| 88 | + is_active_member_val = 1 if is_active_member == 'Yes' else 0 |
38 | 89 |
|
39 | | - # Submit button |
40 | | - submitted = st.form_submit_button("Predict Churn", use_container_width=True) |
| 90 | + # Rainbow button! |
| 91 | + submitted = st.form_submit_button( |
| 92 | + "🌈 Predict Churn 🌈", |
| 93 | + use_container_width=True |
| 94 | + ) |
41 | 95 |
|
42 | | -# Calculate prediction only when button is clicked |
43 | 96 | if submitted: |
44 | 97 | # Prepare input data |
45 | 98 | input_data = pd.DataFrame({ |
|
49 | 102 | 'Tenure': [tenure], |
50 | 103 | 'Balance': [balance], |
51 | 104 | 'NumOfProducts': [num_of_products], |
52 | | - 'HasCrCard': [has_cr_card], |
53 | | - 'IsActiveMember': [is_active_member], |
| 105 | + 'HasCrCard': [has_cr_card_val], |
| 106 | + 'IsActiveMember': [is_active_member_val], |
54 | 107 | 'EstimatedSalary': [estimated_salary] |
55 | 108 | }) |
56 | 109 |
|
|
65 | 118 | input_data_scaled = scaler.transform(input_data) |
66 | 119 |
|
67 | 120 | # Predict churn |
68 | | - with st.spinner('Calculating prediction...'): |
| 121 | + with st.spinner('✨ Calculating your colorful prediction...'): |
69 | 122 | prediction = model.predict(input_data_scaled, verbose=0) |
70 | 123 | prediction_proba = prediction[0][0] |
71 | 124 |
|
72 | | - # Display results |
73 | | - st.success("Prediction Complete!") |
74 | | - st.metric("Churn Probability", f"{prediction_proba:.2%}") |
75 | | - |
| 125 | + # Show result with custom coloring and emoji |
| 126 | + st.markdown( |
| 127 | + "<div class='highlight succ-hl'>🔥 <b>Prediction Complete!</b> 🔥</div>", |
| 128 | + unsafe_allow_html=True |
| 129 | + ) |
| 130 | + st.markdown( |
| 131 | + f"<div class='predict-prob'>Churn Probability: <span style='color:#0984e3'>{prediction_proba:.2%}</span></div>", |
| 132 | + unsafe_allow_html=True |
| 133 | + ) |
| 134 | + |
76 | 135 | if prediction_proba > 0.5: |
77 | | - st.warning('⚠️ The customer is likely to churn.') |
| 136 | + st.markdown( |
| 137 | + "<div class='highlight warn-hl'>" |
| 138 | + "⚠️ <span style='color:#d63031; font-weight:bold;'>The customer is <u>likely</u> to churn.</span> " |
| 139 | + "Take action now! 🚨" |
| 140 | + "</div>", |
| 141 | + unsafe_allow_html=True |
| 142 | + ) |
78 | 143 | else: |
79 | | - st.info('✅ The customer is not likely to churn.') |
| 144 | + st.markdown( |
| 145 | + "<div class='highlight succ-hl'>" |
| 146 | + "✅ <span style='color:#00b894; font-weight:bold;'>The customer is <u>not likely</u> to churn.</span> " |
| 147 | + "Keep engaging! 🎉" |
| 148 | + "</div>", |
| 149 | + unsafe_allow_html=True |
| 150 | + ) |
| 151 | +# End with a nice colorful footer |
| 152 | +st.markdown( |
| 153 | + """ |
| 154 | + <hr style='border-top:2px solid #fdcb6e;'> |
| 155 | + <div style="text-align:center;font-size:1em;color:#636e72;"> |
| 156 | + Made with <span style="color:#fd79a8;">❤</span> and <span style="color:#00b894;">Streamlit</span> |
| 157 | + </div> |
| 158 | + """, |
| 159 | + unsafe_allow_html=True |
| 160 | +) |
0 commit comments