Upload 3 files
Browse files- app.py +92 -0
- churn_model.pkl +3 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import shap
|
| 5 |
+
import matplotlib
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
|
| 8 |
+
# Gunakan backend 'Agg' agar matplotlib aman dijalankan di server web
|
| 9 |
+
matplotlib.use('Agg')
|
| 10 |
+
|
| 11 |
+
# ==========================================
|
| 12 |
+
# 1. LOAD MODEL & EXPLAINER
|
| 13 |
+
# ==========================================
|
| 14 |
+
print("Memuat model ChurnGuard...")
|
| 15 |
+
model = joblib.load('churn_model.pkl')
|
| 16 |
+
explainer = shap.Explainer(model)
|
| 17 |
+
|
| 18 |
+
# ==========================================
|
| 19 |
+
# 2. FUNGSI PREDIKSI & SHAP EXPLANATION
|
| 20 |
+
# ==========================================
|
| 21 |
+
def predict_and_explain(credit_score, age, tenure, balance, num_products, has_crcard, is_active, salary):
|
| 22 |
+
# 1. Format input user ke dalam DataFrame (Nama kolom WAJIB sama dengan saat training)
|
| 23 |
+
input_data = pd.DataFrame([[
|
| 24 |
+
credit_score, age, tenure, balance, num_products, has_crcard, is_active, salary
|
| 25 |
+
]], columns=['CreditScore', 'Age', 'Tenure', 'Balance', 'NumOfProducts', 'HasCrCard', 'IsActiveMember', 'EstimatedSalary'])
|
| 26 |
+
|
| 27 |
+
# 2. Lakukan Prediksi
|
| 28 |
+
prob_churn = model.predict_proba(input_data)[0][1]
|
| 29 |
+
prob_retain = 1 - prob_churn
|
| 30 |
+
|
| 31 |
+
if prob_churn > 0.5:
|
| 32 |
+
keputusan = "### ⚠️ RISIKO TINGGI: Pelanggan diprediksi akan CHURN (Berhenti)."
|
| 33 |
+
else:
|
| 34 |
+
keputusan = "### ✅ AMAN: Pelanggan diprediksi RETAIN (Bertahan)."
|
| 35 |
+
|
| 36 |
+
# 3. Generate Grafik SHAP (Explainable AI)
|
| 37 |
+
shap_values = explainer(input_data)
|
| 38 |
+
|
| 39 |
+
plt.figure(figsize=(8, 4))
|
| 40 |
+
# Membuat waterfall plot untuk melihat fitur mana yang mendorong probabilitas churn
|
| 41 |
+
shap.plots.waterfall(shap_values[0], show=False)
|
| 42 |
+
fig = plt.gcf()
|
| 43 |
+
plt.tight_layout()
|
| 44 |
+
|
| 45 |
+
return keputusan, {"Churn": prob_churn, "Retain": prob_retain}, fig
|
| 46 |
+
|
| 47 |
+
# ==========================================
|
| 48 |
+
# 3. ANTARMUKA GRADIO
|
| 49 |
+
# ==========================================
|
| 50 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 51 |
+
gr.Markdown("""
|
| 52 |
+
<h1 style='text-align: center;'>🛡️ ChurnGuard: AI Customer Retention</h1>
|
| 53 |
+
<p style='text-align: center;'>Aplikasi Machine Learning (XGBoost) untuk memprediksi probabilitas pelanggan bank yang akan berhenti, lengkap dengan analisis <b>Explainable AI (SHAP)</b>.</p>
|
| 54 |
+
""")
|
| 55 |
+
|
| 56 |
+
with gr.Row():
|
| 57 |
+
# BAGIAN KIRI: Form Input
|
| 58 |
+
with gr.Column(scale=1):
|
| 59 |
+
gr.Markdown("**👤 Profil Pelanggan**")
|
| 60 |
+
age = gr.Slider(18, 100, value=35, step=1, label="Umur (Age)")
|
| 61 |
+
tenure = gr.Slider(0, 10, value=3, step=1, label="Lama Menjadi Nasabah (Tenure - Tahun)")
|
| 62 |
+
balance = gr.Number(value=50000, label="Saldo Tabungan (Balance - $)")
|
| 63 |
+
salary = gr.Number(value=60000, label="Estimasi Gaji Tahunan (EstimatedSalary - $)")
|
| 64 |
+
|
| 65 |
+
gr.Markdown("**📊 Status & Produk**")
|
| 66 |
+
credit_score = gr.Slider(300, 850, value=650, step=1, label="Skor Kredit (CreditScore)")
|
| 67 |
+
num_products = gr.Slider(1, 4, value=1, step=1, label="Jumlah Produk Bank yang Dipakai")
|
| 68 |
+
|
| 69 |
+
# Ubah input Yes/No menjadi 1/0 agar sesuai dengan model
|
| 70 |
+
has_crcard = gr.Radio(choices=[1, 0], value=1, label="Punya Kartu Kredit? (1=Ya, 0=Tidak)")
|
| 71 |
+
is_active = gr.Radio(choices=[1, 0], value=1, label="Member Aktif? (1=Ya, 0=Tidak)")
|
| 72 |
+
|
| 73 |
+
btn_predict = gr.Button("🔮 Prediksi & Analisis Risiko", variant="primary")
|
| 74 |
+
|
| 75 |
+
# BAGIAN KANAN: Output Hasil & Analisis
|
| 76 |
+
with gr.Column(scale=1):
|
| 77 |
+
gr.Markdown("**📈 Hasil Keputusan AI**")
|
| 78 |
+
out_keputusan = gr.Markdown()
|
| 79 |
+
out_prob = gr.Label(label="Probabilitas")
|
| 80 |
+
|
| 81 |
+
gr.Markdown("**🧠 Mengapa AI Memutuskan Demikian? (SHAP Analysis)**")
|
| 82 |
+
out_plot = gr.Plot(label="Analisis Fitur Pendukung Churn")
|
| 83 |
+
|
| 84 |
+
# Hubungkan tombol dengan fungsi utama
|
| 85 |
+
btn_predict.click(
|
| 86 |
+
fn=predict_and_explain,
|
| 87 |
+
inputs=[credit_score, age, tenure, balance, num_products, has_crcard, is_active, salary],
|
| 88 |
+
outputs=[out_keputusan, out_prob, out_plot]
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|
churn_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7402d6e3f7b6714753c08a30ae21a471633e8567fee913875cae3ec204a87f91
|
| 3 |
+
size 291876
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
scikit-learn
|
| 3 |
+
xgboost
|
| 4 |
+
joblib
|
| 5 |
+
shap
|
| 6 |
+
matplotlib
|