Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,59 +3,46 @@ import pickle
|
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
-
# Load
|
| 7 |
with open("DecisionTreeClassifier.pkl", "rb") as file:
|
| 8 |
model = pickle.load(file)
|
| 9 |
|
| 10 |
-
# Prediction function
|
| 11 |
def predict_churn(age, gender, tenure, usage, support, delay,
|
| 12 |
subscription, contract, spend, interaction):
|
| 13 |
try:
|
| 14 |
-
# πΉ Gender encoding
|
| 15 |
gender_val = 1 if gender == "Female" else 0
|
| 16 |
|
| 17 |
-
# πΉ One-hot encoding (MUST match training columns)
|
| 18 |
sub_premium = 1 if subscription == "Premium" else 0
|
| 19 |
sub_standard = 1 if subscription == "Standard" else 0
|
| 20 |
|
| 21 |
contract_monthly = 1 if contract == "Monthly" else 0
|
| 22 |
contract_quarterly = 1 if contract == "Quarterly" else 0
|
| 23 |
|
| 24 |
-
# πΉ Input array (order matters!)
|
| 25 |
input_data = np.array([[
|
| 26 |
-
age,
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
support,
|
| 31 |
-
delay,
|
| 32 |
-
spend,
|
| 33 |
-
interaction,
|
| 34 |
-
sub_premium,
|
| 35 |
-
sub_standard,
|
| 36 |
-
contract_monthly,
|
| 37 |
-
contract_quarterly
|
| 38 |
]])
|
| 39 |
|
| 40 |
-
# πΉ Prediction
|
| 41 |
pred = model.predict(input_data)[0]
|
| 42 |
prob = model.predict_proba(input_data)[0][1]
|
| 43 |
|
| 44 |
-
result = "
|
| 45 |
|
| 46 |
-
#
|
| 47 |
if prob > 0.7:
|
| 48 |
-
risk = "High Risk
|
| 49 |
elif prob > 0.4:
|
| 50 |
-
risk = "Medium Risk
|
| 51 |
else:
|
| 52 |
-
risk = "Low Risk
|
| 53 |
|
| 54 |
-
#
|
| 55 |
fig, ax = plt.subplots()
|
| 56 |
ax.bar(["No Churn", "Churn"], [1 - prob, prob])
|
| 57 |
-
ax.set_title("Churn Probability")
|
| 58 |
-
ax.
|
| 59 |
|
| 60 |
return result, f"{prob*100:.2f}%", risk, fig
|
| 61 |
|
|
@@ -63,29 +50,49 @@ def predict_churn(age, gender, tenure, usage, support, delay,
|
|
| 63 |
return f"Error: {str(e)}", "", "", None
|
| 64 |
|
| 65 |
|
| 66 |
-
# π¨
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
with open("DecisionTreeClassifier.pkl", "rb") as file:
|
| 8 |
model = pickle.load(file)
|
| 9 |
|
|
|
|
| 10 |
def predict_churn(age, gender, tenure, usage, support, delay,
|
| 11 |
subscription, contract, spend, interaction):
|
| 12 |
try:
|
|
|
|
| 13 |
gender_val = 1 if gender == "Female" else 0
|
| 14 |
|
|
|
|
| 15 |
sub_premium = 1 if subscription == "Premium" else 0
|
| 16 |
sub_standard = 1 if subscription == "Standard" else 0
|
| 17 |
|
| 18 |
contract_monthly = 1 if contract == "Monthly" else 0
|
| 19 |
contract_quarterly = 1 if contract == "Quarterly" else 0
|
| 20 |
|
|
|
|
| 21 |
input_data = np.array([[
|
| 22 |
+
age, gender_val, tenure, usage, support, delay,
|
| 23 |
+
spend, interaction,
|
| 24 |
+
sub_premium, sub_standard,
|
| 25 |
+
contract_monthly, contract_quarterly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
]])
|
| 27 |
|
|
|
|
| 28 |
pred = model.predict(input_data)[0]
|
| 29 |
prob = model.predict_proba(input_data)[0][1]
|
| 30 |
|
| 31 |
+
result = "β οΈ Likely to Churn" if pred == 1 else "β
Stable Customer"
|
| 32 |
|
| 33 |
+
# Risk label
|
| 34 |
if prob > 0.7:
|
| 35 |
+
risk = "π΄ High Risk"
|
| 36 |
elif prob > 0.4:
|
| 37 |
+
risk = "π Medium Risk"
|
| 38 |
else:
|
| 39 |
+
risk = "π’ Low Risk"
|
| 40 |
|
| 41 |
+
# Styled graph
|
| 42 |
fig, ax = plt.subplots()
|
| 43 |
ax.bar(["No Churn", "Churn"], [1 - prob, prob])
|
| 44 |
+
ax.set_title("Churn Probability Analysis")
|
| 45 |
+
ax.set_ylim(0, 1)
|
| 46 |
|
| 47 |
return result, f"{prob*100:.2f}%", risk, fig
|
| 48 |
|
|
|
|
| 50 |
return f"Error: {str(e)}", "", "", None
|
| 51 |
|
| 52 |
|
| 53 |
+
# π¨ Custom CSS
|
| 54 |
+
custom_css = """
|
| 55 |
+
body {background-color: #0f172a; color: white;}
|
| 56 |
+
.gradio-container {max-width: 900px; margin: auto;}
|
| 57 |
+
h1 {text-align: center; color: #38bdf8;}
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
# π UI with Blocks
|
| 61 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 62 |
+
|
| 63 |
+
gr.Markdown("# π Customer Churn Prediction Dashboard")
|
| 64 |
+
gr.Markdown("### Analyze customer behavior and predict churn risk")
|
| 65 |
+
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column():
|
| 68 |
+
age = gr.Slider(18, 80, label="Age")
|
| 69 |
+
gender = gr.Radio(["Male", "Female"], label="Gender")
|
| 70 |
+
tenure = gr.Slider(0, 60, label="Tenure (Months)")
|
| 71 |
+
usage = gr.Slider(0, 50, label="Usage Frequency")
|
| 72 |
+
|
| 73 |
+
with gr.Column():
|
| 74 |
+
support = gr.Slider(0, 20, label="Support Calls")
|
| 75 |
+
delay = gr.Slider(0, 30, label="Payment Delay")
|
| 76 |
+
subscription = gr.Radio(["Basic", "Standard", "Premium"], label="Subscription Type")
|
| 77 |
+
contract = gr.Radio(["Monthly", "Quarterly", "Yearly"], label="Contract Length")
|
| 78 |
+
|
| 79 |
+
spend = gr.Slider(0, 10000, label="Total Spend")
|
| 80 |
+
interaction = gr.Slider(0, 100, label="Last Interaction")
|
| 81 |
+
|
| 82 |
+
predict_btn = gr.Button("π Predict Churn", variant="primary")
|
| 83 |
+
|
| 84 |
+
gr.Markdown("## π Results")
|
| 85 |
+
|
| 86 |
+
result = gr.Textbox(label="Prediction")
|
| 87 |
+
prob = gr.Textbox(label="Churn Probability")
|
| 88 |
+
risk = gr.Textbox(label="Risk Level")
|
| 89 |
+
graph = gr.Plot()
|
| 90 |
+
|
| 91 |
+
predict_btn.click(
|
| 92 |
+
fn=predict_churn,
|
| 93 |
+
inputs=[age, gender, tenure, usage, support, delay,
|
| 94 |
+
subscription, contract, spend, interaction],
|
| 95 |
+
outputs=[result, prob, risk, graph]
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
demo.launch()
|