import gradio as gr import random import numpy as np import pandas as pd import joblib # Define the function before loading the pipeline def replace_binary_values(df): df = df.copy() df['Gender'] = df['Gender'].replace({'F': 1, 'M': 0}) return df # Now load the pre-trained pipeline model_pipeline = joblib.load("full_model_pipeline.pkl") # Define ranges and categories for input fields numerical_ranges = { 'Customer_Age': (26, 73), 'Dependent_count': (0, 5), 'Months_on_book': (13, 56), 'Total_Relationship_Count': (1, 6), 'Months_Inactive_12_mon': (0, 6), 'Contacts_Count_12_mon': (0, 6), 'Credit_Limit': (1438.3, 34516.0), 'Total_Revolving_Bal': (0, 2517), 'Avg_Open_To_Buy': (3.0, 34516.0), 'Total_Amt_Chng_Q4_Q1': (0.0, 3.397), 'Total_Trans_Amt': (510, 18484), 'Total_Trans_Ct': (10, 139), 'Total_Ct_Chng_Q4_Q1': (0.0, 3.714), 'Avg_Utilization_Ratio': (0.0, 0.999) } categorical_options = { 'Gender': ['M', 'F'], 'Education_Level': ['High School', 'Graduate', 'Uneducated', 'Unknown', 'College', 'Post-Graduate', 'Doctorate'], 'Marital_Status': ['Married', 'Single', 'Unknown', 'Divorced'], 'Income_Category': ['$60K - $80K', 'Less than $40K', '$80K - $120K', '$40K - $60K', '$120K +', 'Unknown'], 'Card_Category': ['Blue', 'Gold', 'Silver', 'Platinum'] } # Function to auto-generate CLIENTNUM within its range def generate_clientnum(): return random.randint(708082083, 828343083) # Define the prediction function using the loaded model pipeline def predict_churn( Customer_Age, Gender, Dependent_count, Education_Level, Marital_Status, Income_Category, Card_Category, Months_on_book, Total_Relationship_Count, Months_Inactive_12_mon, Contacts_Count_12_mon, Credit_Limit, Total_Revolving_Bal, Avg_Open_To_Buy, Total_Amt_Chng_Q4_Q1, Total_Trans_Amt, Total_Trans_Ct, Total_Ct_Chng_Q4_Q1, Avg_Utilization_Ratio ): # Generate CLIENTNUM internally client_num = generate_clientnum() # Prepare input data for the model input_data = { "CLIENTNUM": client_num, # Include CLIENTNUM as required by the model "Customer_Age": Customer_Age, "Gender": Gender, "Dependent_count": Dependent_count, "Education_Level": Education_Level, "Marital_Status": Marital_Status, "Income_Category": Income_Category, "Card_Category": Card_Category, "Months_on_book": Months_on_book, "Total_Relationship_Count": Total_Relationship_Count, "Months_Inactive_12_mon": Months_Inactive_12_mon, "Contacts_Count_12_mon": Contacts_Count_12_mon, "Credit_Limit": Credit_Limit, "Total_Revolving_Bal": Total_Revolving_Bal, "Avg_Open_To_Buy": Avg_Open_To_Buy, "Total_Amt_Chng_Q4_Q1": Total_Amt_Chng_Q4_Q1, "Total_Trans_Amt": Total_Trans_Amt, "Total_Trans_Ct": Total_Trans_Ct, "Total_Ct_Chng_Q4_Q1": Total_Ct_Chng_Q4_Q1, "Avg_Utilization_Ratio": Avg_Utilization_Ratio } input_df = pd.DataFrame([input_data]) # Apply the binary transformation on Gender manually input_df = replace_binary_values(input_df) # Make prediction prediction = model_pipeline.predict(input_df)[0] # Interpret the prediction if prediction == 'Existing Customer': churn_status = "Tidak Churn" else: churn_status = "Churn" return churn_status # Create Gradio input components for the app inputs = [ # Removed CLIENTNUM input from user inputs gr.Slider(numerical_ranges['Customer_Age'][0], numerical_ranges['Customer_Age'][1], step=1, label="Umur Nasabah"), gr.Dropdown(categorical_options['Gender'], label="Gender Nasabah"), gr.Slider(numerical_ranges['Dependent_count'][0], numerical_ranges['Dependent_count'][1], step=1, label="Jumlah Tanggungan"), gr.Dropdown(categorical_options['Education_Level'], label="Tingkat Pendidikan"), gr.Dropdown(categorical_options['Marital_Status'], label="Status Pernikahan"), gr.Dropdown(categorical_options['Income_Category'], label="Kategori Pemasukan"), gr.Dropdown(categorical_options['Card_Category'], label="Kategori Kartu"), gr.Slider(numerical_ranges['Months_on_book'][0], numerical_ranges['Months_on_book'][1], step=1, label="Lama Menjadi Nasabah (bulan)"), gr.Slider(numerical_ranges['Total_Relationship_Count'][0], numerical_ranges['Total_Relationship_Count'][1], step=1, label="Jumlah Hubungan Finansial"), gr.Slider(numerical_ranges['Months_Inactive_12_mon'][0], numerical_ranges['Months_Inactive_12_mon'][1], step=1, label="Bulan Tidak Aktif (12 bulan terakhir)"), gr.Slider(numerical_ranges['Contacts_Count_12_mon'][0], numerical_ranges['Contacts_Count_12_mon'][1], step=1, label="Jumlah Kontak (12 bulan terakhir)"), gr.Slider(numerical_ranges['Credit_Limit'][0], numerical_ranges['Credit_Limit'][1], step=100, label="Limit Kredit"), gr.Slider(numerical_ranges['Total_Revolving_Bal'][0], numerical_ranges['Total_Revolving_Bal'][1], step=10, label="Saldo Revolving"), gr.Slider(numerical_ranges['Avg_Open_To_Buy'][0], numerical_ranges['Avg_Open_To_Buy'][1], step=100, label="Sisa Limit Kredit"), gr.Slider(numerical_ranges['Total_Amt_Chng_Q4_Q1'][0], numerical_ranges['Total_Amt_Chng_Q4_Q1'][1], step=0.1, label="Perubahan Jumlah Transaksi (Q4 ke Q1)"), gr.Slider(numerical_ranges['Total_Trans_Amt'][0], numerical_ranges['Total_Trans_Amt'][1], step=100, label="Total Nilai Transaksi"), gr.Slider(numerical_ranges['Total_Trans_Ct'][0], numerical_ranges['Total_Trans_Ct'][1], step=1, label="Jumlah Total Transaksi"), gr.Slider(numerical_ranges['Total_Ct_Chng_Q4_Q1'][0], numerical_ranges['Total_Ct_Chng_Q4_Q1'][1], step=0.1, label="Perubahan Jumlah Transaksi (Q4 ke Q1)"), gr.Slider(numerical_ranges['Avg_Utilization_Ratio'][0], numerical_ranges['Avg_Utilization_Ratio'][1], step=0.01, label="Rata-rata Penggunaan Kredit"), ] # Output component output = gr.Textbox(label="Churn Status") # Define Gradio app interface app = gr.Interface( fn=predict_churn, inputs=inputs, outputs=output, title="Prediksi Churn Nasabah", description="Prediksi apakah seorang nasabah cenderung churn berdasarkan data yang diberikan." ) # Launch the app app.launch(share=True)