Akki2228 commited on
Commit
0b2eb15
Β·
verified Β·
1 Parent(s): b562b6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -28
app.py CHANGED
@@ -3,45 +3,64 @@ 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, "Annual": 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
@@ -62,10 +81,11 @@ interface = gr.Interface(
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()
 
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
+
62
+ except Exception as e:
63
+ return f"Error: {str(e)}", "", "", None
64
 
65
 
66
  # 🎨 Gradio UI
 
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()