| |
| from IPython.utils.py3compat import encode |
| import gradio as gr |
| import numpy as np |
| import pandas as pd |
| import pickle |
|
|
|
|
| |
| def load_saved_objets(filepath='ML_items'): |
| "Function to load saved objects" |
|
|
| with open(filepath, 'rb') as file: |
| loaded_object = pickle.load(file) |
| |
| return loaded_object |
|
|
| |
| loaded_object = load_saved_objets() |
| pipeline_of_my_app = loaded_object["pipeline"] |
| num_cols = loaded_object['numeric_columns'] |
| cat_cols = loaded_object['categorical_columns'] |
| encoder_categories = loaded_object["encoder_categories"] |
|
|
| |
| def predict_churn( |
| TotalCharges, |
| MonthlyCharges, |
| tenure, |
| StreamingTV, |
| PaperlessBilling, |
| DeviceProtection, |
| TechSupport, |
| InternetService, |
| OnlineSecurity, |
| StreamingMovies, |
| PaymentMethod, |
| Dependents, |
| Parter, |
| OnlineBackup, |
| gender, |
| SeniorCitizen, |
| MultipleLines, |
| Contract, |
| PhoneService, |
| ): |
| |
| df = pd.DataFrame( |
| [ |
| [ |
| TotalCharges, |
| MonthlyCharges, |
| tenure, |
| StreamingTV, |
| PaperlessBilling, |
| DeviceProtection, |
| TechSupport, |
| InternetService, |
| OnlineSecurity, |
| StreamingMovies, |
| PaymentMethod, |
| Dependents, |
| Parter, |
| OnlineBackup, |
| gender, |
| SeniorCitizen, |
| MultipleLines, |
| Contract, |
| PhoneService, |
| ] |
| ], |
| columns= num_cols + cat_cols, |
| ).replace("", np.nan) |
| |
| df[cat_cols] = df[cat_cols].astype("object") |
| |
| |
| output = pipeline_of_my_app.predict(df) |
| |
| |
| if output == 0: |
| model_output = "No" |
| else: |
| model_output = "Yes" |
|
|
| return model_output |
| |
|
|
| |
| inputs = [] |
|
|
| with gr.Blocks() as demo: |
| |
| |
| gr.Markdown("<h2 style='text-align: center;'> Customer Churn Prediction App </h2> ", unsafe_allow_html=True) |
| 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) |
| |
| with gr.Column(): |
| |
| with gr.Row(): |
|
|
| for i in num_cols: |
| inputs.append(gr.Number(label=f"Input {i} ")) |
| |
| with gr.Row(): |
|
|
| for (lab, choices) in zip(cat_cols, encoder_categories): |
| inputs.append(gr.inputs.Dropdown( |
| choices=choices.tolist(), |
| type="value", |
| label=f"Select {lab}", |
| default=choices.tolist()[0],)) |
| |
| with gr.Row(): |
| make_prediction = gr.Button("Predict") |
| |
| |
| with gr.Row(): |
| output_prediction = gr.Text(label="Will Customer Churn?") |
| make_prediction.click(predict_churn, inputs, output_prediction) |
|
|
| |
| demo.launch(debug=True,inline=False) |