churnpredict / app_.py
nurindahpratiwi
update file
8f104ab
import pandas as pd
from transformers import pipeline
import streamlit as st
import datetime
from huggingface_hub import hf_hub_download
import joblib
REPO_ID = "AlbieCofie/predict-customer-churn"
num_imputer = joblib.load(
hf_hub_download(repo_id=REPO_ID, filename="numerical_imputer.joblib")
)
cat_imputer = joblib.load(
hf_hub_download(repo_id=REPO_ID, filename="categorical_imputer.joblib")
)
encoder = joblib.load(
hf_hub_download(repo_id=REPO_ID, filename="encoder.joblib")
)
scaler = joblib.load(
hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib")
)
model = joblib.load(
hf_hub_download(repo_id=REPO_ID, filename="Final_model.joblib")
)
# Create a function that applies the ML pipeline and makes predictions
def predict(gender,SeniorCitizen,Partner,Dependents, tenure, PhoneService,MultipleLines,
InternetService,OnlineSecurity,OnlineBackup,DeviceProtection,TechSupport,StreamingTV,StreamingMovies,
Contract,PaperlessBilling,PaymentMethod,MonthlyCharges,TotalCharges):
# Create a dataframe with the input data
input_df = pd.DataFrame({
'gender': [gender],
'SeniorCitizen': [SeniorCitizen],
'Partner': [Partner],
'Dependents': [Dependents],
'tenure': [tenure],
'PhoneService': [PhoneService],
'MultipleLines': [MultipleLines],
'InternetService': [InternetService],
'OnlineSecurity': [OnlineSecurity],
'OnlineBackup': [OnlineBackup],
'DeviceProtection': [DeviceProtection],
'TechSupport': [TechSupport],
'StreamingTV': [StreamingTV],
'StreamingMovies': [StreamingMovies],
'Contract': [Contract],
'PaperlessBilling': [PaperlessBilling],
'PaymentMethod': [PaymentMethod],
'MonthlyCharges': [MonthlyCharges],
'TotalCharges': [TotalCharges]
})
# Selecting categorical and numerical columns separately
cat_columns = [col for col in input_df.columns if input_df[col].dtype == 'object']
num_columns = [col for col in input_df.columns if input_df[col].dtype != 'object']
# Apply the imputers on the input data
input_df_imputed_cat = cat_imputer.transform(input_df[cat_columns])
input_df_imputed_num = num_imputer.transform(input_df[num_columns])
# Encode the categorical columns
input_encoded_df = pd.DataFrame(encoder.transform(input_df_imputed_cat).toarray(),
columns=encoder.get_feature_names_out(cat_columns))
# Scale the numerical columns
input_df_scaled = scaler.transform(input_df_imputed_num)
input_scaled_df = pd.DataFrame(input_df_scaled , columns = num_columns)
#joining the cat encoded and num scaled
final_df = pd.concat([input_encoded_df, input_scaled_df], axis=1)
final_df = final_df.reindex(columns=['SeniorCitizen','tenure','MonthlyCharges','TotalCharges',
'gender_Female','gender_Male','Partner_No','Partner_Yes','Dependents_No','Dependents_Yes','PhoneService_No',
'PhoneService_Yes','MultipleLines_No','MultipleLines_Yes','InternetService_DSL','InternetService_Fiber optic',
'InternetService_No','OnlineSecurity_No','OnlineSecurity_Yes','OnlineBackup_No','OnlineBackup_Yes','DeviceProtection_No',
'DeviceProtection_Yes','TechSupport_No','TechSupport_Yes','StreamingTV_No','StreamingTV_Yes','StreamingMovies_No',
'StreamingMovies_Yes','Contract_Month-to-month','Contract_One year','Contract_Two year','PaperlessBilling_No',
'PaperlessBilling_Yes','PaymentMethod_Bank transfer (automatic)','PaymentMethod_Credit card (automatic)','PaymentMethod_Electronic check',
'PaymentMethod_Mailed check'])
# Make predictions using the model
predictions = model.predict(final_df)[0]
#prediction = model.predict(final_df)[0]
# Make predictions using the model
#predictions = model.predict(final_df)
# Convert the numpy array to an integer
#prediction_label = int(predictions.item())
prediction_label = "Beware!!! This customer is likely to Churn" if predictions.item() == "Yes" else "This customer is Not likely churn"
return prediction_label
#return predictions
if 'clicked' not in st.session_state:
st.session_state.clicked = False
def click_button():
st.session_state.clicked = True
st.title("CUSTOMER CHURN PREDICTION APP")
with st.form(key="customer-information"):
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")
gender = st.radio('Select your gender', ('male', 'female'))
SeniorCitizen = st.radio("Are you a Seniorcitizen; No=0 and Yes=1", ('0', '1'))
Partner = st.radio('Do you have Partner', ('Yes', 'No'))
Dependents = st.selectbox('Do you have any Dependents?', ('No', 'Yes'))
tenure = st.number_input('Lenght of tenure (no. of months with Telco)', min_value=0, max_value=90, value=1, step=1)
PhoneService = st.radio('Do you have PhoneService? ', ('No', 'Yes'))
MultipleLines = st.radio('Do you have MultipleLines', ('No', 'Yes'))
InternetService = st.radio('Do you have InternetService', ('DSL', 'Fiber optic', 'No'))
OnlineSecurity = st.radio('Do you have OnlineSecurity?', ('No', 'Yes'))
OnlineBackup = st.radio('Do you have OnlineBackup?', ('No', 'Yes'))
DeviceProtection = st.radio('Do you have DeviceProtection?', ('No', 'Yes'))
TechSupport = st.radio('Do you have TechSupport?', ('No', 'Yes'))
StreamingTV = st.radio('Do you have StreamingTV?', ('No', 'Yes'))
StreamingMovies = st.radio('Do you have StreamingMovies?', ('No', 'Yes'))
Contract = st.selectbox('which Contract do you use?', ('Month-to-month', 'One year', 'Two year'))
PaperlessBilling = st.radio('Do you prefer PaperlessBilling?', ('Yes', 'No'))
PaymentMethod = st.selectbox('Which PaymentMethod do you prefer?', ('Electronic check', 'Mailed check', 'Bank transfer (automatic)',
'Credit card (automatic)'))
MonthlyCharges = st.number_input("Enter monthly charges (the range should between 0-120)")
TotalCharges = st.number_input("Enter total charges (the range should between 0-10.000)")
st.form_submit_button('Predict', on_click=click_button)
if st.session_state.clicked:
# The message and nested widget will remain on the page
predict(gender,SeniorCitizen,Partner,Dependents, tenure, PhoneService,MultipleLines,
InternetService,OnlineSecurity,OnlineBackup,DeviceProtection,TechSupport,StreamingTV,StreamingMovies,
Contract,PaperlessBilling,PaymentMethod,MonthlyCharges,TotalCharges)