Spaces:
Runtime error
Runtime error
nurindahpratiwi
commited on
Commit
·
8e8fa12
1
Parent(s):
f8c1703
update
Browse files
app.py
CHANGED
@@ -6,12 +6,27 @@ from huggingface_hub import hf_hub_download
|
|
6 |
import joblib
|
7 |
import json
|
8 |
|
9 |
-
REPO_ID = "
|
10 |
-
FILENAME = "
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
model = joblib.load(
|
14 |
-
hf_hub_download(repo_id=REPO_ID, filename=
|
15 |
)
|
16 |
|
17 |
if 'clicked' not in st.session_state:
|
@@ -22,83 +37,57 @@ def click_button():
|
|
22 |
|
23 |
|
24 |
st.title("CUSTOMER CHURN PREDICTION APP")
|
|
|
25 |
|
26 |
with st.form(key="customer-information"):
|
27 |
st.markdown("This app predicts whether a customer will leave your company or not. Enter the details of the customer below to see the result")
|
28 |
-
gender = st.radio('Select your gender', ('male', 'female'))
|
29 |
-
SeniorCitizen = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
|
30 |
-
Partner = st.radio('Do you have Partner', ('Yes', 'No'))
|
31 |
-
Dependents = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
|
32 |
-
tenure = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
|
33 |
-
PhoneService = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
|
34 |
-
MultipleLines = st.radio('Do you have MultipleLines', ('No', 'Yes'))
|
35 |
-
InternetService = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
|
36 |
-
OnlineSecurity = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
|
37 |
-
OnlineBackup = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
|
38 |
-
DeviceProtection = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
|
39 |
-
TechSupport = st.radio('Do you have TechSupport?', ('No', 'Yes'))
|
40 |
-
StreamingTV = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
|
41 |
-
StreamingMovies = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
|
42 |
-
Contract = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
|
43 |
-
PaperlessBilling = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
|
44 |
-
PaymentMethod = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
|
45 |
'Credit card (automatic)'))
|
46 |
-
MonthlyCharges = st.number_input("Enter monthly charges (the range should between 0-120)")
|
47 |
-
TotalCharges = st.number_input("Enter total charges (the range should between 0-10.000)")
|
48 |
st.form_submit_button('Predict', on_click=click_button)
|
49 |
|
50 |
if st.session_state.clicked:
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
st.dataframe(
|
79 |
-
df,
|
80 |
-
column_config={
|
81 |
-
'gender': "gender",
|
82 |
-
'SeniorCitizen': "SeniorCitizen",
|
83 |
-
'Partner': "Partner",
|
84 |
-
'Dependents': "Dependents",
|
85 |
-
'tenure': "tenure",
|
86 |
-
'PhoneService': "PhoneService",
|
87 |
-
'MultipleLines': "MultipleLines",
|
88 |
-
'InternetService': "InternetService",
|
89 |
-
'OnlineSecurity': "OnlineSecurity",
|
90 |
-
'OnlineBackup': "OnlineBackup",
|
91 |
-
'DeviceProtection': "DeviceProtection",
|
92 |
-
'TechSupport': "TechSupport",
|
93 |
-
'StreamingTV': "StreamingTV",
|
94 |
-
'StreamingMovies': "StreamingMovies",
|
95 |
-
'Contract': "Contract",
|
96 |
-
'PaperlessBilling': "PaperlessBilling",
|
97 |
-
'PaymentMethod': "PaymentMethod",
|
98 |
-
'MonthlyCharges': "MonthlyCharges",
|
99 |
-
'TotalCharges': "TotalCharges"
|
100 |
-
},
|
101 |
-
hide_index=True,
|
102 |
-
)
|
103 |
-
|
104 |
-
model.predict(df(config["sklearn"][list_input]))
|
|
|
6 |
import joblib
|
7 |
import json
|
8 |
|
9 |
+
REPO_ID = "AlbieCofie/predict-customer-churn"
|
10 |
+
FILENAME = "sklearn_model.joblib"
|
11 |
+
|
12 |
+
num_imputer = joblib.load(
|
13 |
+
hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
|
14 |
+
)
|
15 |
+
|
16 |
+
cat_imputer = joblib.load(
|
17 |
+
hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib")
|
18 |
+
)
|
19 |
+
|
20 |
+
encoder = joblib.load(
|
21 |
+
hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib")
|
22 |
+
)
|
23 |
+
|
24 |
+
scaler = joblib.load(
|
25 |
+
hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
|
26 |
+
)
|
27 |
|
28 |
model = joblib.load(
|
29 |
+
hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
|
30 |
)
|
31 |
|
32 |
if 'clicked' not in st.session_state:
|
|
|
37 |
|
38 |
|
39 |
st.title("CUSTOMER CHURN PREDICTION APP")
|
40 |
+
input_data = {}
|
41 |
|
42 |
with st.form(key="customer-information"):
|
43 |
st.markdown("This app predicts whether a customer will leave your company or not. Enter the details of the customer below to see the result")
|
44 |
+
input_data["gender"] = st.radio('Select your gender', ('male', 'female'))
|
45 |
+
input_data["SeniorCitizen"] = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
|
46 |
+
input_data["Partner"] = st.radio('Do you have Partner', ('Yes', 'No'))
|
47 |
+
input_data["Dependents"] = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
|
48 |
+
input_data["tenure"] = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
|
49 |
+
input_data["PhoneService"] = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
|
50 |
+
input_data["MultipleLines"] = st.radio('Do you have MultipleLines', ('No', 'Yes'))
|
51 |
+
input_data["InternetService"] = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
|
52 |
+
input_data["OnlineSecurity"] = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
|
53 |
+
input_data["OnlineBackup"] = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
|
54 |
+
input_data["DeviceProtection"] = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
|
55 |
+
input_data["TechSupport"] = st.radio('Do you have TechSupport?', ('No', 'Yes'))
|
56 |
+
input_data["StreamingTV"] = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
|
57 |
+
input_data["StreamingMovies"] = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
|
58 |
+
input_data["Contract"] = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
|
59 |
+
input_data["PaperlessBilling"] = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
|
60 |
+
input_data["PaymentMethod"] = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
|
61 |
'Credit card (automatic)'))
|
62 |
+
input_data["MonthlyCharges"] = st.number_input("Enter monthly charges (the range should between 0-120)")
|
63 |
+
input_data["TotalCharges"] = st.number_input("Enter total charges (the range should between 0-10.000)")
|
64 |
st.form_submit_button('Predict', on_click=click_button)
|
65 |
|
66 |
if st.session_state.clicked:
|
67 |
+
input_df = pd.DataFrame([input_data])
|
68 |
+
|
69 |
+
# Selecting categorical and numerical columns separately
|
70 |
+
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
71 |
+
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
72 |
+
|
73 |
+
# Apply the imputers
|
74 |
+
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
75 |
+
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
76 |
+
|
77 |
+
# Encode the categorical columns
|
78 |
+
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
79 |
+
columns=encoder.get_feature_names(cat_columns))
|
80 |
+
|
81 |
+
# Scale the numerical columns
|
82 |
+
input_df_scaled = scaler.transform(input_df_imputed_num)
|
83 |
+
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
|
84 |
+
|
85 |
+
#joining the cat encoded and num scaled
|
86 |
+
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
87 |
+
|
88 |
+
# Make a prediction
|
89 |
+
prediction = model.predict(final_df)[0]
|
90 |
+
|
91 |
+
# Display the prediction
|
92 |
+
st.write(f"The predicted sales are: {prediction}.")
|
93 |
+
st.table(input_df)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|