Spaces:
Sleeping
Sleeping
Delete app (1).py
Browse files- app (1).py +0 -160
app (1).py
DELETED
|
@@ -1,160 +0,0 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
import joblib
|
| 4 |
-
import gradio as gr
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Load the preprocessing steps and the model
|
| 8 |
-
label_encoders = joblib.load("label_encoders.pkl")
|
| 9 |
-
one_hot_encoder = joblib.load("one_hot_encoder.pkl")
|
| 10 |
-
min_max_scaler = joblib.load("min_max_scaler.pkl")
|
| 11 |
-
model = joblib.load("logistic_regression_model.pkl")
|
| 12 |
-
le_target = joblib.load("label_encoder_target.pkl")
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def preprocess_data(data):
|
| 16 |
-
|
| 17 |
-
df = pd.DataFrame([data])
|
| 18 |
-
|
| 19 |
-
label_encode_cols = [
|
| 20 |
-
"Partner",
|
| 21 |
-
"Dependents",
|
| 22 |
-
"PhoneService",
|
| 23 |
-
"PaperlessBilling",
|
| 24 |
-
"gender",
|
| 25 |
-
]
|
| 26 |
-
one_hot_encode_cols = [
|
| 27 |
-
"MultipleLines",
|
| 28 |
-
"InternetService",
|
| 29 |
-
"OnlineSecurity",
|
| 30 |
-
"OnlineBackup",
|
| 31 |
-
"DeviceProtection",
|
| 32 |
-
"TechSupport",
|
| 33 |
-
"StreamingTV",
|
| 34 |
-
"StreamingMovies",
|
| 35 |
-
"Contract",
|
| 36 |
-
"PaymentMethod",
|
| 37 |
-
]
|
| 38 |
-
min_max_scale_cols = ["tenure", "MonthlyCharges", "TotalCharges"]
|
| 39 |
-
|
| 40 |
-
# Strip leading and trailing spaces from string inputs
|
| 41 |
-
for col in label_encode_cols + one_hot_encode_cols:
|
| 42 |
-
df[col] = df[col].str.strip()
|
| 43 |
-
|
| 44 |
-
# Convert non-numeric values to NaN and fill them with the mean of the column
|
| 45 |
-
df[min_max_scale_cols] = df[min_max_scale_cols].replace(" ", np.nan).astype(float)
|
| 46 |
-
|
| 47 |
-
df[min_max_scale_cols] = df[min_max_scale_cols].fillna(
|
| 48 |
-
df[min_max_scale_cols].mean()
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
# Label encode specified columns
|
| 52 |
-
for col in label_encode_cols:
|
| 53 |
-
le = label_encoders[col]
|
| 54 |
-
df[col] = le.transform(df[col])
|
| 55 |
-
|
| 56 |
-
# One-hot encode specified columns
|
| 57 |
-
one_hot_encoded = one_hot_encoder.transform(df[one_hot_encode_cols])
|
| 58 |
-
|
| 59 |
-
# Min-max scale specified columns
|
| 60 |
-
scaled_numerical = min_max_scaler.transform(df[min_max_scale_cols])
|
| 61 |
-
|
| 62 |
-
# Combine processed columns into one DataFrame
|
| 63 |
-
X_processed = np.hstack(
|
| 64 |
-
(df[label_encode_cols].values, scaled_numerical, one_hot_encoded)
|
| 65 |
-
)
|
| 66 |
-
|
| 67 |
-
return X_processed
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
def predict(
|
| 71 |
-
gender,
|
| 72 |
-
senior_citizen,
|
| 73 |
-
partner,
|
| 74 |
-
dependents,
|
| 75 |
-
tenure,
|
| 76 |
-
phone_service,
|
| 77 |
-
multiple_lines,
|
| 78 |
-
internet_service,
|
| 79 |
-
online_security,
|
| 80 |
-
online_backup,
|
| 81 |
-
device_protection,
|
| 82 |
-
tech_support,
|
| 83 |
-
streaming_tv,
|
| 84 |
-
streaming_movies,
|
| 85 |
-
contract,
|
| 86 |
-
paperless_billing,
|
| 87 |
-
payment_method,
|
| 88 |
-
monthly_charges,
|
| 89 |
-
total_charges,
|
| 90 |
-
):
|
| 91 |
-
|
| 92 |
-
data = {
|
| 93 |
-
"gender": gender,
|
| 94 |
-
"SeniorCitizen": senior_citizen,
|
| 95 |
-
"Partner": partner,
|
| 96 |
-
"Dependents": dependents,
|
| 97 |
-
"tenure": tenure,
|
| 98 |
-
"PhoneService": phone_service,
|
| 99 |
-
"MultipleLines": multiple_lines,
|
| 100 |
-
"InternetService": internet_service,
|
| 101 |
-
"OnlineSecurity": online_security,
|
| 102 |
-
"OnlineBackup": online_backup,
|
| 103 |
-
"DeviceProtection": device_protection,
|
| 104 |
-
"TechSupport": tech_support,
|
| 105 |
-
"StreamingTV": streaming_tv,
|
| 106 |
-
"StreamingMovies": streaming_movies,
|
| 107 |
-
"Contract": contract,
|
| 108 |
-
"PaperlessBilling": paperless_billing,
|
| 109 |
-
"PaymentMethod": payment_method,
|
| 110 |
-
"MonthlyCharges": monthly_charges,
|
| 111 |
-
"TotalCharges": total_charges,
|
| 112 |
-
}
|
| 113 |
-
|
| 114 |
-
try:
|
| 115 |
-
X_new = preprocess_data(data)
|
| 116 |
-
prediction = model.predict(X_new)
|
| 117 |
-
prediction = le_target.inverse_transform(prediction)
|
| 118 |
-
return "Churn" if prediction[0] == "Yes" else "No Churn"
|
| 119 |
-
except Exception as e:
|
| 120 |
-
print("Error during prediction:", e)
|
| 121 |
-
return str(e)
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
# Define the Gradio interface
|
| 125 |
-
inputs = [
|
| 126 |
-
gr.Radio(label="Gender", choices=["Female", "Male"]),
|
| 127 |
-
gr.Number(label="Senior Citizen (0 or 1)"),
|
| 128 |
-
gr.Radio(label="Partner", choices=["Yes", "No"]),
|
| 129 |
-
gr.Radio(label="Dependents", choices=["Yes", "No"]),
|
| 130 |
-
gr.Number(label="Tenure (integer)"),
|
| 131 |
-
gr.Radio(label="Phone Service", choices=["Yes", "No"]),
|
| 132 |
-
gr.Radio(label="Multiple Lines", choices=["Yes", "No", "No phone service"]),
|
| 133 |
-
gr.Radio(label="Internet Service", choices=["DSL", "Fiber optic", "No"]),
|
| 134 |
-
gr.Radio(label="Online Security", choices=["Yes", "No", "No internet service"]),
|
| 135 |
-
gr.Radio(label="Online Backup", choices=["Yes", "No", "No internet service"]),
|
| 136 |
-
gr.Radio(label="Device Protection", choices=["Yes", "No", "No internet service"]),
|
| 137 |
-
gr.Radio(label="Tech Support", choices=["Yes", "No", "No internet service"]),
|
| 138 |
-
gr.Radio(label="Streaming TV", choices=["Yes", "No", "No internet service"]),
|
| 139 |
-
gr.Radio(label="Streaming Movies", choices=["Yes", "No", "No internet service"]),
|
| 140 |
-
gr.Radio(label="Contract", choices=["Month-to-month", "One year", "Two year"]),
|
| 141 |
-
gr.Radio(label="Paperless Billing", choices=["Yes", "No"]),
|
| 142 |
-
gr.Radio(
|
| 143 |
-
label="Payment Method",
|
| 144 |
-
choices=[
|
| 145 |
-
"Electronic check",
|
| 146 |
-
"Mailed check",
|
| 147 |
-
"Bank transfer (automatic)",
|
| 148 |
-
"Credit card (automatic)",
|
| 149 |
-
],
|
| 150 |
-
),
|
| 151 |
-
gr.Number(label="Monthly Charges (float)"),
|
| 152 |
-
gr.Number(label="Total Charges (float)"),
|
| 153 |
-
]
|
| 154 |
-
|
| 155 |
-
outputs = gr.Textbox(label="Prediction")
|
| 156 |
-
|
| 157 |
-
# Create the Gradio interface
|
| 158 |
-
gr.Interface(
|
| 159 |
-
fn=predict, inputs=inputs, outputs=outputs, title="Churn Prediction Model"
|
| 160 |
-
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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