File size: 2,767 Bytes
3809050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80f6825
3125dec
 
 
 
3809050
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import gradio as gr
import joblib
import pandas as pd
from PIL import Image

best_model = joblib.load("best_model.pkl")
roc_img = Image.open("roc_curve_rf_tuned.png")

def churn_prediction(age, gender, tenure, usage_frequency, support_calls, payment_delay, last_interaction, total_spend, subscription_type, contract_length):
    try:
        input_data = {
            "Age": age,
            "Gender_Male": 1 if gender == "Male" else 0,
            "Gender_Female": 1 if gender == "Female" else 0,
            "Usage Frequency": usage_frequency,
            "Support Calls": support_calls,
            "Contract Length_Monthly": 1 if contract_length == "Monthly" else 0,
            "Contract Length_Quarterly": 1 if contract_length == "Quarterly" else 0,
            "Contract Length_Annual": 1 if contract_length == "Annual" else 0,
            "Payment Delay": payment_delay,
            "Last Interaction": last_interaction,
            "Total Spend": total_spend,
            "Tenure": tenure,
            "Subscription Type_Basic": 1 if subscription_type == "Basic" else 0,
            "Subscription Type_Premium": 1 if subscription_type == "Premium" else 0,
            "Subscription Type_Standard": 1 if subscription_type == "Standard" else 0,
        }
        
        input_df = pd.DataFrame([input_data])
        
        # Predict churn and probability
        prediction = best_model.predict(input_df)
        prediction_proba = best_model.predict_proba(input_df)[:, 1]
        
        churn_probability = f"{prediction_proba[0]:.2f}"
        if prediction_proba[0] < 0.8:
            churn_result = "No"
        else:
            churn_result = "Yes" if prediction[0] == 1 else "No"

        
        return churn_result, churn_probability, roc_img
    except Exception as e:
        return f"Error: {str(e)}", "N/A", None

inputs = [
    gr.Slider(18, 100, value=40, label="Age"),
    gr.Dropdown(["Female", "Male"], value="Male", label="Gender"),
    gr.Slider(1, 60, value=30, label="Tenure (months)"),
    gr.Slider(1, 30, value=15, label="Usage Frequency"),
    gr.Slider(0, 10, value=4, label="Support Calls"),
    gr.Slider(0, 30, value=15, label="Payment Delay"),
    gr.Slider(1, 30, value=15, label="Last Interaction (days ago)"),
    gr.Slider(100, 1000, value=620, label="Total Spend"),
    gr.Dropdown(["Premium", "Standard", "Basic"], value="Standard", label="Subscription Type"),
    gr.Dropdown(["Monthly", "Quarterly", "Annual"], value="Annual", label="Contract Length")
]

outputs = [
    gr.Textbox(label="Churn Prediction"),
    gr.Textbox(label="Churn Probability"),
    gr.Image(label="ROC Curve")
]

gr.Interface(
    fn=churn_prediction,
    inputs=inputs,
    outputs=outputs,
    title="Customer Churn Prediction"
).launch()