Spaces:
Sleeping
Sleeping
File size: 3,773 Bytes
a1e0967 e189a50 76dbe9d e189a50 5ec67b2 e189a50 5ec67b2 e189a50 5ec67b2 e189a50 a1e0967 e189a50 5ec67b2 e189a50 5ec67b2 e189a50 5ec67b2 e189a50 5ec67b2 e189a50 5ec67b2 e189a50 5ec67b2 a1e0967 5ec67b2 a1e0967 76dbe9d a1e0967 5ec67b2 e189a50 a1e0967 e189a50 a1e0967 | 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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | # app.py
import pandas as pd
import joblib
import gradio as gr
# Load saved objects (make sure these files are in the same directory as app.py)
feature_columns = joblib.load('feature_columns.pkl')
num_cols = joblib.load('num_cols.pkl')
scaler = joblib.load('scaler.pkl')
best_model = joblib.load('best_model.pkl')
def predict_churn(SeniorCitizen, tenure, MonthlyCharges, TotalCharges,
gender, Partner, Dependents, PhoneService, MultipleLines,
InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,
TechSupport, StreamingTV, StreamingMovies, Contract,
PaperlessBilling, PaymentMethod):
try:
# Prepare input data as a dictionary
input_data = {
"SeniorCitizen": SeniorCitizen,
"tenure": float(tenure),
"MonthlyCharges": float(MonthlyCharges),
"TotalCharges": float(TotalCharges),
"gender": gender,
"Partner": Partner,
"Dependents": Dependents,
"PhoneService": PhoneService,
"MultipleLines": MultipleLines,
"InternetService": InternetService,
"OnlineSecurity": OnlineSecurity,
"OnlineBackup": OnlineBackup,
"DeviceProtection": DeviceProtection,
"TechSupport": TechSupport,
"StreamingTV": StreamingTV,
"StreamingMovies": StreamingMovies,
"Contract": Contract,
"PaperlessBilling": PaperlessBilling,
"PaymentMethod": PaymentMethod
}
# Convert to DataFrame
df = pd.DataFrame([input_data])
# One-hot encode categorical variables
df_encoded = pd.get_dummies(df)
# Align with training features - fill missing columns with 0
df_encoded = df_encoded.reindex(columns=feature_columns, fill_value=0)
# Scale numerical columns
df_encoded[num_cols] = scaler.transform(df_encoded[num_cols])
# Make prediction
pred = best_model.predict(df_encoded)[0]
return "✅ Churn: Yes" if pred == 1 else "❎ Churn: No"
except Exception as e:
return f"❌ Error occurred: {str(e)}"
# Define Gradio inputs
inputs = [
gr.Radio([0, 1], label="SeniorCitizen"),
gr.Textbox(label="tenure"),
gr.Textbox(label="MonthlyCharges"),
gr.Textbox(label="TotalCharges"),
gr.Dropdown(["Male", "Female"], label="gender"),
gr.Dropdown(["Yes", "No"], label="Partner"),
gr.Dropdown(["Yes", "No"], label="Dependents"),
gr.Dropdown(["Yes", "No"], label="PhoneService"),
gr.Dropdown(["Yes", "No", "No phone service"], label="MultipleLines"),
gr.Dropdown(["DSL", "Fiber optic", "No"], label="InternetService"),
gr.Dropdown(["Yes", "No", "No internet service"], label="OnlineSecurity"),
gr.Dropdown(["Yes", "No", "No internet service"], label="OnlineBackup"),
gr.Dropdown(["Yes", "No", "No internet service"], label="DeviceProtection"),
gr.Dropdown(["Yes", "No", "No internet service"], label="TechSupport"),
gr.Dropdown(["Yes", "No", "No internet service"], label="StreamingTV"),
gr.Dropdown(["Yes", "No", "No internet service"], label="StreamingMovies"),
gr.Dropdown(["Month-to-month", "One year", "Two year"], label="Contract"),
gr.Dropdown(["Yes", "No"], label="PaperlessBilling"),
gr.Dropdown(["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"], label="PaymentMethod")
]
# Create the Gradio interface
interface = gr.Interface(
fn=predict_churn,
inputs=inputs,
outputs="text",
title="Customer Churn Predictor",
description="Enter customer details to predict churn likelihood"
)
if __name__ == "__main__":
interface.launch(share=True)
|