|
|
|
import gradio as gr |
|
import numpy as np |
|
import pandas as pd |
|
import pickle |
|
import xgboost as xgb |
|
from xgboost import XGBClassifier |
|
|
|
|
|
|
|
|
|
def load_ml_toolkit(file_path): |
|
with open(file_path, "rb") as file: |
|
loaded_toolkit = pickle.load(file) |
|
return loaded_toolkit |
|
|
|
|
|
|
|
loaded_toolkit = load_ml_toolkit(r"export/App_toolkit.pkl") |
|
|
|
encoder = loaded_toolkit["encoder"] |
|
scaler = loaded_toolkit["scaler"] |
|
|
|
|
|
model = XGBClassifier() |
|
model.load_model(r"xgb_model.json") |
|
|
|
|
|
|
|
input_cols = ["tenure", "montant", "frequence_rech", "arpu_segment", "frequence", "data_volume", "regularity", "freq_top_pack"] |
|
columns_to_scale = ["montant", "frequence_rech", "arpu_segment", "frequence", "data_volume", "regularity", "freq_top_pack"] |
|
categoricals = ["tenure"] |
|
|
|
|
|
|
|
|
|
def process_and_predict(*args, encoder=encoder, scaler=scaler, model=model): |
|
|
|
|
|
input_data = pd.DataFrame([args], columns=input_cols) |
|
|
|
|
|
input_data["tenure"] = encoder.transform(input_data["tenure"]) |
|
|
|
|
|
input_data[columns_to_scale] = scaler.transform(input_data[columns_to_scale]) |
|
|
|
|
|
model_output = model.predict(input_data) |
|
return {"Prediction: CHURN": float(model_output[0]), "Prediction: STAY": 1-float(model_output[0])} |
|
|
|
|
|
|
|
with gr.Blocks() as turn_on_the_gradio: |
|
gr.Markdown("# π EXPRESSO TELECOM CUSTOMER CHURN βοΈ") |
|
gr.Markdown(''' |
|
|
|
## WELCOME CHERISHED USERπ |
|
|
|
### PLEASE GO AHEAD AND MAKE A PREDICTION π''') |
|
|
|
|
|
|
|
gr.Markdown("**SECTION ONE**") |
|
gr.Markdown("**CUSTOMER NETWORK ACTTIVITIES**") |
|
with gr.Row(): |
|
montant = gr.Slider(label="Top-up amount", minimum=20, step=1, interactive=True, value=1, maximum= 500000) |
|
data_volume = gr.Slider(label="Number of connections", minimum=0, step=1, interactive=True, value=1, maximum= 2000000) |
|
|
|
|
|
with gr.Row(): |
|
frequence_rech = gr.Slider(label="Recharge Frequency", minimum=1, step=1, interactive=True, value=1, maximum=220) |
|
freq_top_pack = gr.Slider(label="Top Package Activation Frequency", minimum=1, step=1, interactive=True, value=1, maximum=1050) |
|
regularity = gr.Slider(label="Regularity (out of 90 days)", minimum=1, step=1, interactive=True, value=1, maximum=90) |
|
tenure = gr.Dropdown(label="Tenure (time on the network)", choices=["D 3-6 month", "E 6-9 month", "F 9-12 month", "G 12-15 month", "H 15-18 month", "I 18-21 month", "J 21-24 month", "K > 24 month"], value="K > 24 month") |
|
|
|
|
|
gr.Markdown("**SECTION 2**") |
|
gr.Markdown("**CUSTOMER INCOME DETAILS**") |
|
with gr.Row(): |
|
arpu_segment = gr.Slider(label="Income over the last 90 days", step=1, maximum=287000, interactive=True) |
|
frequence = gr.Slider(label="Number of times the customer has made an income", step=1, minimum=1, maximum=91, interactive=True) |
|
|
|
|
|
output = gr.Label("...") |
|
submit_button = gr.Button("Submit") |
|
|
|
|
|
|
|
submit_button.click(fn = process_and_predict, |
|
outputs = output, |
|
inputs=[tenure, montant, frequence_rech, arpu_segment, frequence, data_volume, regularity, freq_top_pack]) |
|
|
|
turn_on_the_gradio.launch(inbrowser= True) |