Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- app (1).py +160 -0
- label_encoder_target.pkl +3 -0
- label_encoders.pkl +3 -0
- logistic_regression_model.pkl +3 -0
- min_max_scaler.pkl +3 -0
- one_hot_encoder.pkl +3 -0
- requirements.txt.txt +5 -0
app (1).py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
label_encoder_target.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:64021f16ad0ddc7d9e4337b1cf092711d1e5d2c7a2184f1109c757b16797f490
|
| 3 |
+
size 537
|
label_encoders.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c7924da8ef9cff4754f9ecf4914bb393f76b129807e68eb8fb452e08dbf1090e
|
| 3 |
+
size 1704
|
logistic_regression_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:80bafde2eb586c26655b50edb2f2d134004785c293b6481e46c902a1bf7aaf18
|
| 3 |
+
size 1135
|
min_max_scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1b81e34b8f1602a0940e4f7db4bd99d20a8d4aa163a8270d2bc963df0756a32f
|
| 3 |
+
size 1151
|
one_hot_encoder.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3dd8a114e3d78c0dbdafee4322dc90430441eab7c9c189483128699f76bfcfd6
|
| 3 |
+
size 3901
|
requirements.txt.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy==1.21.5
|
| 3 |
+
scikit-learn==1.2.2
|
| 4 |
+
gradio
|
| 5 |
+
joblib
|