Spaces:
Runtime error
Runtime error
nurindahpratiwi
commited on
Commit
·
ce08d4c
1
Parent(s):
a885d38
initial commit
Browse files- app.py +147 -0
- packages.txt +2 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
from transformers import pipeline
|
3 |
+
import streamlit as st
|
4 |
+
import datetime
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
import joblib
|
7 |
+
|
8 |
+
REPO_ID = "AlbieCofie/predict-customer-churn"
|
9 |
+
FILENAME = "sklearn_model.joblib"
|
10 |
+
|
11 |
+
num_imputer = joblib.load(
|
12 |
+
hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
|
13 |
+
)
|
14 |
+
|
15 |
+
cat_imputer = joblib.load(
|
16 |
+
hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib")
|
17 |
+
)
|
18 |
+
|
19 |
+
encoder = joblib.load(
|
20 |
+
hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib")
|
21 |
+
)
|
22 |
+
|
23 |
+
scaler = joblib.load(
|
24 |
+
hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
|
25 |
+
)
|
26 |
+
|
27 |
+
model = joblib.load(
|
28 |
+
hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
|
29 |
+
)
|
30 |
+
|
31 |
+
# Create a function that applies the ML pipeline and makes predictions
|
32 |
+
def predict(gender,SeniorCitizen,Partner,Dependents, tenure, PhoneService,MultipleLines,
|
33 |
+
InternetService,OnlineSecurity,OnlineBackup,DeviceProtection,TechSupport,StreamingTV,StreamingMovies,
|
34 |
+
Contract,PaperlessBilling,PaymentMethod,MonthlyCharges,TotalCharges):
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
# Create a dataframe with the input data
|
39 |
+
input_df = pd.DataFrame({
|
40 |
+
'gender': [gender],
|
41 |
+
'SeniorCitizen': [SeniorCitizen],
|
42 |
+
'Partner': [Partner],
|
43 |
+
'Dependents': [Dependents],
|
44 |
+
'tenure': [tenure],
|
45 |
+
'PhoneService': [PhoneService],
|
46 |
+
'MultipleLines': [MultipleLines],
|
47 |
+
'InternetService': [InternetService],
|
48 |
+
'OnlineSecurity': [OnlineSecurity],
|
49 |
+
'OnlineBackup': [OnlineBackup],
|
50 |
+
'DeviceProtection': [DeviceProtection],
|
51 |
+
'TechSupport': [TechSupport],
|
52 |
+
'StreamingTV': [StreamingTV],
|
53 |
+
'StreamingMovies': [StreamingMovies],
|
54 |
+
'Contract': [Contract],
|
55 |
+
'PaperlessBilling': [PaperlessBilling],
|
56 |
+
'PaymentMethod': [PaymentMethod],
|
57 |
+
'MonthlyCharges': [MonthlyCharges],
|
58 |
+
'TotalCharges': [TotalCharges]
|
59 |
+
})
|
60 |
+
|
61 |
+
# Selecting categorical and numerical columns separately
|
62 |
+
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
|
63 |
+
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
|
64 |
+
|
65 |
+
# Apply the imputers on the input data
|
66 |
+
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
|
67 |
+
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
|
68 |
+
|
69 |
+
# Encode the categorical columns
|
70 |
+
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
|
71 |
+
columns=encoder.get_feature_names_out(cat_columns))
|
72 |
+
|
73 |
+
# Scale the numerical columns
|
74 |
+
input_df_scaled = scaler.transform(input_df_imputed_num)
|
75 |
+
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
|
76 |
+
|
77 |
+
|
78 |
+
#joining the cat encoded and num scaled
|
79 |
+
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
|
80 |
+
|
81 |
+
final_df = final_df.reindex(columns=['SeniorCitizen','tenure','MonthlyCharges','TotalCharges',
|
82 |
+
'gender_Female','gender_Male','Partner_No','Partner_Yes','Dependents_No','Dependents_Yes','PhoneService_No',
|
83 |
+
'PhoneService_Yes','MultipleLines_No','MultipleLines_Yes','InternetService_DSL','InternetService_Fiber optic',
|
84 |
+
'InternetService_No','OnlineSecurity_No','OnlineSecurity_Yes','OnlineBackup_No','OnlineBackup_Yes','DeviceProtection_No',
|
85 |
+
'DeviceProtection_Yes','TechSupport_No','TechSupport_Yes','StreamingTV_No','StreamingTV_Yes','StreamingMovies_No',
|
86 |
+
'StreamingMovies_Yes','Contract_Month-to-month','Contract_One year','Contract_Two year','PaperlessBilling_No',
|
87 |
+
'PaperlessBilling_Yes','PaymentMethod_Bank transfer (automatic)','PaymentMethod_Credit card (automatic)','PaymentMethod_Electronic check',
|
88 |
+
'PaymentMethod_Mailed check'])
|
89 |
+
|
90 |
+
# Make predictions using the model
|
91 |
+
predictions = model.predict(final_df)
|
92 |
+
|
93 |
+
# Make predictions using the model
|
94 |
+
#predictions = model.predict(final_df)
|
95 |
+
|
96 |
+
# Convert the numpy array to an integer
|
97 |
+
#prediction_label = int(predictions.item())
|
98 |
+
|
99 |
+
prediction_label = "Beware!!! This customer is likely to Churn" if predictions.item() == "Yes" else "This customer is Not likely churn"
|
100 |
+
|
101 |
+
|
102 |
+
return prediction_label
|
103 |
+
|
104 |
+
#return predictions
|
105 |
+
|
106 |
+
|
107 |
+
if 'clicked' not in st.session_state:
|
108 |
+
st.session_state.clicked = False
|
109 |
+
|
110 |
+
def click_button():
|
111 |
+
st.session_state.clicked = True
|
112 |
+
|
113 |
+
|
114 |
+
st.title("CUSTOMER CHURN PREDICTION APP")
|
115 |
+
|
116 |
+
with st.form(key="customer-information"):
|
117 |
+
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")
|
118 |
+
gender = st.radio('Select your gender', ('male', 'female'))
|
119 |
+
SeniorCitizen = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
|
120 |
+
Partner = st.radio('Do you have Partner', ('Yes', 'No'))
|
121 |
+
Dependents = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
|
122 |
+
tenure = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
|
123 |
+
PhoneService = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
|
124 |
+
MultipleLines = st.radio('Do you have MultipleLines', ('No', 'Yes'))
|
125 |
+
InternetService = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
|
126 |
+
OnlineSecurity = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
|
127 |
+
OnlineBackup = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
|
128 |
+
DeviceProtection = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
|
129 |
+
TechSupport = st.radio('Do you have TechSupport?', ('No', 'Yes'))
|
130 |
+
StreamingTV = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
|
131 |
+
StreamingMovies = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
|
132 |
+
Contract = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
|
133 |
+
PaperlessBilling = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
|
134 |
+
PaymentMethod = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
|
135 |
+
'Credit card (automatic)'))
|
136 |
+
MonthlyCharges = st.number_input("Enter monthly charges (the range should between 0-120)")
|
137 |
+
TotalCharges = st.number_input("Enter total charges (the range should between 0-10.000)")
|
138 |
+
st.form_submit_button('Predict', on_click=click_button)
|
139 |
+
|
140 |
+
if st.session_state.clicked:
|
141 |
+
# The message and nested widget will remain on the page
|
142 |
+
st.write(gender,SeniorCitizen,Partner,Dependents, tenure, PhoneService,MultipleLines,
|
143 |
+
InternetService,OnlineSecurity,OnlineBackup,DeviceProtection,TechSupport,StreamingTV,StreamingMovies,
|
144 |
+
Contract,PaperlessBilling,PaymentMethod,MonthlyCharges,TotalCharges)
|
145 |
+
#predict(gender,SeniorCitizen,Partner,Dependents, tenure, PhoneService,MultipleLines,
|
146 |
+
#InternetService,OnlineSecurity,OnlineBackup,DeviceProtection,TechSupport,StreamingTV,StreamingMovies,
|
147 |
+
#Contract,PaperlessBilling,PaymentMethod,MonthlyCharges,TotalCharges)
|
packages.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
freeglut3-dev
|
2 |
+
libgtk2.0-dev
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
streamlit
|
3 |
+
huggingface_hub
|
4 |
+
scikit-learn==1.2.2
|
5 |
+
joblib
|
6 |
+
torch
|
7 |
+
pandas
|