Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -2,29 +2,39 @@ import pandas as pd
|
|
2 |
import numpy as np
|
3 |
import joblib
|
4 |
import gradio as gr
|
|
|
|
|
5 |
# Load the preprocessing steps and the model
|
6 |
-
label_encoders = joblib.load(
|
7 |
-
one_hot_encoder = joblib.load(
|
8 |
-
min_max_scaler = joblib.load(
|
9 |
-
model = joblib.load(
|
10 |
-
le_target = joblib.load(
|
11 |
|
12 |
-
def preprocess_data(data):
|
13 |
-
"""
|
14 |
-
Preprocess the input data for prediction.
|
15 |
|
16 |
-
|
17 |
-
data (dict): Dictionary containing input data.
|
18 |
|
19 |
-
Returns:
|
20 |
-
np.array: Processed data ready for prediction.
|
21 |
-
"""
|
22 |
df = pd.DataFrame([data])
|
23 |
|
24 |
-
label_encode_cols = [
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
min_max_scale_cols = ["tenure", "MonthlyCharges", "TotalCharges"]
|
29 |
|
30 |
# Strip leading and trailing spaces from string inputs
|
@@ -32,8 +42,11 @@ def preprocess_data(data):
|
|
32 |
df[col] = df[col].str.strip()
|
33 |
|
34 |
# Convert non-numeric values to NaN and fill them with the mean of the column
|
35 |
-
df[min_max_scale_cols] = df[min_max_scale_cols].replace(
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
# Label encode specified columns
|
39 |
for col in label_encode_cols:
|
@@ -47,23 +60,35 @@ def preprocess_data(data):
|
|
47 |
scaled_numerical = min_max_scaler.transform(df[min_max_scale_cols])
|
48 |
|
49 |
# Combine processed columns into one DataFrame
|
50 |
-
X_processed = np.hstack(
|
|
|
|
|
51 |
|
52 |
return X_processed
|
53 |
|
54 |
|
55 |
-
def predict(
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
-
Returns:
|
65 |
-
str: Prediction result ("Churn" or "No Churn").
|
66 |
-
"""
|
67 |
data = {
|
68 |
"gender": gender,
|
69 |
"SeniorCitizen": senior_citizen,
|
@@ -83,18 +108,19 @@ def predict(gender, senior_citizen, partner, dependents, tenure, phone_service,
|
|
83 |
"PaperlessBilling": paperless_billing,
|
84 |
"PaymentMethod": payment_method,
|
85 |
"MonthlyCharges": monthly_charges,
|
86 |
-
"TotalCharges": total_charges
|
87 |
}
|
88 |
|
89 |
try:
|
90 |
X_new = preprocess_data(data)
|
91 |
prediction = model.predict(X_new)
|
92 |
prediction = le_target.inverse_transform(prediction)
|
93 |
-
return "Churn" if prediction[0] ==
|
94 |
except Exception as e:
|
95 |
print("Error during prediction:", e)
|
96 |
return str(e)
|
97 |
|
|
|
98 |
# Define the Gradio interface
|
99 |
inputs = [
|
100 |
gr.Radio(label="Gender", choices=["Female", "Male"]),
|
@@ -113,12 +139,22 @@ inputs = [
|
|
113 |
gr.Radio(label="Streaming Movies", choices=["Yes", "No", "No internet service"]),
|
114 |
gr.Radio(label="Contract", choices=["Month-to-month", "One year", "Two year"]),
|
115 |
gr.Radio(label="Paperless Billing", choices=["Yes", "No"]),
|
116 |
-
gr.Radio(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
gr.Number(label="Monthly Charges (float)"),
|
118 |
-
gr.Number(label="Total Charges (float)")
|
119 |
]
|
120 |
|
121 |
outputs = gr.Textbox(label="Prediction")
|
122 |
|
123 |
# Create the Gradio interface
|
124 |
-
gr.Interface(
|
|
|
|
|
|
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
|
|
|
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:
|
|
|
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,
|
|
|
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"]),
|
|
|
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)
|