FKBaffour/Customer_Churn_Prediction_App
0
1# Importing required Libraries2from IPython.utils.py3compat import encode3import gradio as gr4import numpy as np5import pandas as pd6import pickle7 8 9# Loading Machine Learning Objects10def load_saved_objets(filepath='ML_items'):11 "Function to load saved objects"12 13 with open(filepath, 'rb') as file:14 loaded_object = pickle.load(file)15 16 return loaded_object17 18# Instantiating ML_items19loaded_object = load_saved_objets()20pipeline_of_my_app = loaded_object["pipeline"]21num_cols = loaded_object['numeric_columns']22cat_cols = loaded_object['categorical_columns']23encoder_categories = loaded_object["encoder_categories"]24 25# Main function to collect the inputs process them and outpuT the predicition26def predict_churn(27 TotalCharges,28 MonthlyCharges,29 tenure, 30 StreamingTV,31 PaperlessBilling,32 DeviceProtection,33 TechSupport,34 InternetService,35 OnlineSecurity,36 StreamingMovies,37 PaymentMethod,38 Dependents,39 Parter,40 tenure_group,41 OnlineBackup,42 gender,43 SeniorCitizen,44 MultipleLines,45 Contract,46 PhoneService,47):48 49 df = pd.DataFrame(50 [51 [52 TotalCharges,53 MonthlyCharges,54 tenure, 55 StreamingTV,56 PaperlessBilling,57 DeviceProtection,58 TechSupport,59 InternetService,60 OnlineSecurity,61 StreamingMovies,62 PaymentMethod,63 Dependents,64 Parter,65 tenure_group,66 OnlineBackup,67 gender,68 SeniorCitizen,69 MultipleLines,70 Contract,71 PhoneService,72 ]73 ], 74 columns= num_cols + cat_cols,75 ).replace("", np.nan)76 77 df[cat_cols] = df[cat_cols].astype("object")78 79 # Passing data to pipeline to make prediction80 output = pipeline_of_my_app.predict(df)81 82 # Labelling Model output83 if output == 0:84 model_output = "No"85 else:86 model_output = "Yes"87 88 return model_output89 90 91# Setting up app interface and data inputs92inputs = []93 94with gr.Blocks() as demo:95 96 # Setting Titles for App97 gr.Markdown("<h2 style='text-align: center;'> Customer Churn Prediction App </h2> ", unsafe_allow_html=True)98 gr.Markdown("<h6 style='text-align: center;'> (Fill in the details below and click on PREDICT button to make a prediction for Customer Churn) </h6> ", unsafe_allow_html=True) 99 100 with gr.Column(): #main frame 101 102 with gr.Row(): #col 1 : for num features103 104 for i in num_cols:105 inputs.append(gr.Number(label=f"Input {i} "))106 107 with gr.Row(): #col 2 : for cat features108 109 for (lab, choices) in zip(cat_cols, encoder_categories):110 inputs.append(gr.inputs.Dropdown(111 choices=choices.tolist(),112 type="value",113 label=f"Select {lab}",114 default=choices.tolist()[0],))115 # Setting up preediction Button116 with gr.Row():117 make_prediction = gr.Button("Predict")118 119 # Setting up prediction output Row120 with gr.Row():121 output_prediction = gr.Text(label="Will Customer Churn?")122 make_prediction.click(predict_churn, inputs, output_prediction)123 124# Launching app125demo.launch()