Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
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 |
+
# Prediction function
|
| 11 |
+
def predict_churn(age, gender, tenure, usage, support, delay,
|
| 12 |
+
subscription, contract, spend, interaction):
|
| 13 |
+
|
| 14 |
+
# Convert categorical to numeric (must match training!)
|
| 15 |
+
gender_map = {"Male": 0, "Female": 1}
|
| 16 |
+
sub_map = {"Basic": 0, "Standard": 1, "Premium": 2}
|
| 17 |
+
contract_map = {"Monthly": 0, "Quarterly": 1, "Yearly": 2}
|
| 18 |
+
|
| 19 |
+
input_data = np.array([[
|
| 20 |
+
age,
|
| 21 |
+
gender_map[gender],
|
| 22 |
+
tenure,
|
| 23 |
+
usage,
|
| 24 |
+
support,
|
| 25 |
+
delay,
|
| 26 |
+
sub_map[subscription],
|
| 27 |
+
contract_map[contract],
|
| 28 |
+
spend,
|
| 29 |
+
interaction
|
| 30 |
+
]])
|
| 31 |
+
|
| 32 |
+
# Prediction
|
| 33 |
+
pred = model.predict(input_data)[0]
|
| 34 |
+
prob = model.predict_proba(input_data)[0][1]
|
| 35 |
+
|
| 36 |
+
result = "Churn โ ๏ธ" if pred == 1 else "No Churn ๐"
|
| 37 |
+
|
| 38 |
+
# ๐ Graph: Probability Bar Chart
|
| 39 |
+
fig, ax = plt.subplots()
|
| 40 |
+
ax.bar(["No Churn", "Churn"], [1-prob, prob])
|
| 41 |
+
ax.set_title("Churn Probability")
|
| 42 |
+
ax.set_ylabel("Probability")
|
| 43 |
+
|
| 44 |
+
return result, f"{prob*100:.2f}%", fig
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
# ๐จ Gradio UI
|
| 48 |
+
interface = gr.Interface(
|
| 49 |
+
fn=predict_churn,
|
| 50 |
+
inputs=[
|
| 51 |
+
gr.Number(label="Age"),
|
| 52 |
+
gr.Dropdown(["Male", "Female"], label="Gender"),
|
| 53 |
+
gr.Number(label="Tenure"),
|
| 54 |
+
gr.Number(label="Usage Frequency"),
|
| 55 |
+
gr.Number(label="Support Calls"),
|
| 56 |
+
gr.Number(label="Payment Delay"),
|
| 57 |
+
gr.Dropdown(["Basic", "Standard", "Premium"], label="Subscription Type"),
|
| 58 |
+
gr.Dropdown(["Monthly", "Quarterly", "Yearly"], label="Contract Length"),
|
| 59 |
+
gr.Number(label="Total Spend"),
|
| 60 |
+
gr.Number(label="Last Interaction")
|
| 61 |
+
],
|
| 62 |
+
outputs=[
|
| 63 |
+
gr.Text(label="Prediction"),
|
| 64 |
+
gr.Text(label="Churn Probability"),
|
| 65 |
+
gr.Plot(label="Graph")
|
| 66 |
+
],
|
| 67 |
+
title="๐ Customer Churn Prediction System",
|
| 68 |
+
description="Enter customer details to predict churn probability"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
interface.launch()
|