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