Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,46 +3,59 @@ import pickle
|
|
| 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,
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
]])
|
| 27 |
|
|
|
|
| 28 |
pred = model.predict(input_data)[0]
|
| 29 |
prob = model.predict_proba(input_data)[0][1]
|
| 30 |
|
| 31 |
-
result = "β οΈ
|
| 32 |
|
| 33 |
-
# Risk
|
| 34 |
if prob > 0.7:
|
| 35 |
-
risk = "
|
| 36 |
elif prob > 0.4:
|
| 37 |
-
risk = "
|
| 38 |
else:
|
| 39 |
-
risk = "
|
| 40 |
|
| 41 |
-
#
|
| 42 |
fig, ax = plt.subplots()
|
| 43 |
ax.bar(["No Churn", "Churn"], [1 - prob, prob])
|
| 44 |
-
ax.set_title("Churn Probability
|
| 45 |
-
ax.
|
| 46 |
|
| 47 |
return result, f"{prob*100:.2f}%", risk, fig
|
| 48 |
|
|
@@ -50,49 +63,29 @@ def predict_churn(age, gender, tenure, usage, support, delay,
|
|
| 50 |
return f"Error: {str(e)}", "", "", None
|
| 51 |
|
| 52 |
|
| 53 |
-
# π¨
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"""
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 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()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
+
# Load trained model
|
| 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 |
+
gender_val,
|
| 28 |
+
tenure,
|
| 29 |
+
usage,
|
| 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 = "Churn β οΈ" if pred == 1 else "No Churn π"
|
| 45 |
|
| 46 |
+
# πΉ Risk Level
|
| 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 |
+
# πΉ Graph
|
| 55 |
fig, ax = plt.subplots()
|
| 56 |
ax.bar(["No Churn", "Churn"], [1 - prob, prob])
|
| 57 |
+
ax.set_title("Churn Probability")
|
| 58 |
+
ax.set_ylabel("Probability")
|
| 59 |
|
| 60 |
return result, f"{prob*100:.2f}%", risk, fig
|
| 61 |
|
|
|
|
| 63 |
return f"Error: {str(e)}", "", "", None
|
| 64 |
|
| 65 |
|
| 66 |
+
# π¨ Gradio UI
|
| 67 |
+
interface = gr.Interface(
|
| 68 |
+
fn=predict_churn,
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Number(label="Age"),
|
| 71 |
+
gr.Dropdown(["Male", "Female"], label="Gender"),
|
| 72 |
+
gr.Number(label="Tenure"),
|
| 73 |
+
gr.Number(label="Usage Frequency"),
|
| 74 |
+
gr.Number(label="Support Calls"),
|
| 75 |
+
gr.Number(label="Payment Delay"),
|
| 76 |
+
gr.Dropdown(["Basic", "Standard", "Premium"], label="Subscription Type"),
|
| 77 |
+
gr.Dropdown(["Monthly", "Quarterly", "Yearly"], label="Contract Length"),
|
| 78 |
+
gr.Number(label="Total Spend"),
|
| 79 |
+
gr.Number(label="Last Interaction")
|
| 80 |
+
],
|
| 81 |
+
outputs=[
|
| 82 |
+
gr.Text(label="Prediction"),
|
| 83 |
+
gr.Text(label="Churn Probability"),
|
| 84 |
+
gr.Text(label="Risk Level"),
|
| 85 |
+
gr.Plot(label="Graph")
|
| 86 |
+
],
|
| 87 |
+
title="π Customer Churn Prediction System",
|
| 88 |
+
description="Enter customer details to predict churn probability and risk level"
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|