Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,64 +4,64 @@ import pandas as pd
|
|
| 4 |
import joblib
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
# Load saved
|
| 8 |
feature_columns = joblib.load('feature_columns.pkl')
|
| 9 |
num_cols = joblib.load('num_cols.pkl')
|
| 10 |
scaler = joblib.load('scaler.pkl')
|
| 11 |
best_model = joblib.load('best_model.pkl')
|
| 12 |
|
| 13 |
-
# Define Gradio prediction function
|
| 14 |
def predict_churn(SeniorCitizen, tenure, MonthlyCharges, TotalCharges,
|
| 15 |
gender, Partner, Dependents, PhoneService, MultipleLines,
|
| 16 |
InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,
|
| 17 |
TechSupport, StreamingTV, StreamingMovies, Contract,
|
| 18 |
PaperlessBilling, PaymentMethod):
|
| 19 |
try:
|
| 20 |
-
#
|
| 21 |
input_data = {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
|
| 43 |
# Convert to DataFrame
|
| 44 |
df = pd.DataFrame([input_data])
|
| 45 |
|
| 46 |
-
# One-hot encode categorical
|
| 47 |
-
df_encoded = pd.get_dummies(df)
|
| 48 |
|
| 49 |
-
# Align
|
| 50 |
df_encoded = df_encoded.reindex(columns=feature_columns, fill_value=0)
|
| 51 |
|
| 52 |
-
# Scale numerical
|
| 53 |
df_encoded[num_cols] = scaler.transform(df_encoded[num_cols])
|
| 54 |
|
| 55 |
-
#
|
| 56 |
pred = best_model.predict(df_encoded)[0]
|
|
|
|
| 57 |
return "✅ Churn: Yes" if pred == 1 else "❎ Churn: No"
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
return f"❌ Error occurred: {str(e)}"
|
| 61 |
|
| 62 |
-
# Define Gradio
|
| 63 |
inputs = [
|
| 64 |
-
gr.Radio([0, 1], label="SeniorCitizen"),
|
| 65 |
gr.Textbox(label="tenure"),
|
| 66 |
gr.Textbox(label="MonthlyCharges"),
|
| 67 |
gr.Textbox(label="TotalCharges"),
|
|
@@ -82,7 +82,7 @@ inputs = [
|
|
| 82 |
gr.Dropdown(["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"], label="PaymentMethod")
|
| 83 |
]
|
| 84 |
|
| 85 |
-
#
|
| 86 |
interface = gr.Interface(
|
| 87 |
fn=predict_churn,
|
| 88 |
inputs=inputs,
|
|
|
|
| 4 |
import joblib
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
+
# Load saved objects (make sure these files are in the same directory as app.py)
|
| 8 |
feature_columns = joblib.load('feature_columns.pkl')
|
| 9 |
num_cols = joblib.load('num_cols.pkl')
|
| 10 |
scaler = joblib.load('scaler.pkl')
|
| 11 |
best_model = joblib.load('best_model.pkl')
|
| 12 |
|
|
|
|
| 13 |
def predict_churn(SeniorCitizen, tenure, MonthlyCharges, TotalCharges,
|
| 14 |
gender, Partner, Dependents, PhoneService, MultipleLines,
|
| 15 |
InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,
|
| 16 |
TechSupport, StreamingTV, StreamingMovies, Contract,
|
| 17 |
PaperlessBilling, PaymentMethod):
|
| 18 |
try:
|
| 19 |
+
# Prepare input data as a dictionary
|
| 20 |
input_data = {
|
| 21 |
+
"SeniorCitizen": SeniorCitizen,
|
| 22 |
+
"tenure": float(tenure),
|
| 23 |
+
"MonthlyCharges": float(MonthlyCharges),
|
| 24 |
+
"TotalCharges": float(TotalCharges),
|
| 25 |
+
"gender": gender,
|
| 26 |
+
"Partner": Partner,
|
| 27 |
+
"Dependents": Dependents,
|
| 28 |
+
"PhoneService": PhoneService,
|
| 29 |
+
"MultipleLines": MultipleLines,
|
| 30 |
+
"InternetService": InternetService,
|
| 31 |
+
"OnlineSecurity": OnlineSecurity,
|
| 32 |
+
"OnlineBackup": OnlineBackup,
|
| 33 |
+
"DeviceProtection": DeviceProtection,
|
| 34 |
+
"TechSupport": TechSupport,
|
| 35 |
+
"StreamingTV": StreamingTV,
|
| 36 |
+
"StreamingMovies": StreamingMovies,
|
| 37 |
+
"Contract": Contract,
|
| 38 |
+
"PaperlessBilling": PaperlessBilling,
|
| 39 |
+
"PaymentMethod": PaymentMethod
|
| 40 |
+
}
|
| 41 |
|
| 42 |
# Convert to DataFrame
|
| 43 |
df = pd.DataFrame([input_data])
|
| 44 |
|
| 45 |
+
# One-hot encode categorical variables
|
| 46 |
+
df_encoded = pd.get_dummies(df)
|
| 47 |
|
| 48 |
+
# Align with training features - fill missing columns with 0
|
| 49 |
df_encoded = df_encoded.reindex(columns=feature_columns, fill_value=0)
|
| 50 |
|
| 51 |
+
# Scale numerical columns
|
| 52 |
df_encoded[num_cols] = scaler.transform(df_encoded[num_cols])
|
| 53 |
|
| 54 |
+
# Make prediction
|
| 55 |
pred = best_model.predict(df_encoded)[0]
|
| 56 |
+
|
| 57 |
return "✅ Churn: Yes" if pred == 1 else "❎ Churn: No"
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
return f"❌ Error occurred: {str(e)}"
|
| 61 |
|
| 62 |
+
# Define Gradio inputs
|
| 63 |
inputs = [
|
| 64 |
+
gr.Radio([0, 1], label="SeniorCitizen"),
|
| 65 |
gr.Textbox(label="tenure"),
|
| 66 |
gr.Textbox(label="MonthlyCharges"),
|
| 67 |
gr.Textbox(label="TotalCharges"),
|
|
|
|
| 82 |
gr.Dropdown(["Electronic check", "Mailed check", "Bank transfer (automatic)", "Credit card (automatic)"], label="PaymentMethod")
|
| 83 |
]
|
| 84 |
|
| 85 |
+
# Create the Gradio interface
|
| 86 |
interface = gr.Interface(
|
| 87 |
fn=predict_churn,
|
| 88 |
inputs=inputs,
|