| | import gradio as gr |
| | import pandas as pd |
| | import pickle |
| |
|
| |
|
| | |
| | model = pickle.load(open("model.pkl", "rb")) |
| | encoder = pickle.load(open("encoder.pkl", "rb")) |
| | scaler = pickle.load(open("scaler.pkl", "rb")) |
| |
|
| | |
| | data = pd.read_csv('data.csv') |
| |
|
| | |
| | def create_gradio_inputs(data): |
| | input_components = [] |
| | for column in data.columns: |
| | if data[column].dtype == 'object' and len(data[column].unique()) > 3: |
| | input_components.append(gr.Dropdown(choices=list(data[column].unique()), label=column)) |
| | elif data[column].dtype == 'object' and len(data[column].unique()) <= 3: |
| | input_components.append(gr.Radio(choices=list(data[column].unique()), label=column)) |
| | elif data[column].dtype in ['int64', 'float64']: |
| | if data[column].min() == 1: |
| | input_components.append(gr.Slider(minimum=1, maximum=data[column].max(), step=1, label=column)) |
| | else: |
| | input_components.append(gr.Slider(maximum=data[column].max(), step=0.5, label=column)) |
| | return input_components |
| |
|
| | input_components = create_gradio_inputs(data) |
| |
|
| | output_components = [ |
| | gr.Label(label="Churn Prediction"), |
| | ] |
| |
|
| | |
| | def input_df_creator(gender, SeniorCitizen, Partner, Dependents, tenure, |
| | PhoneService, InternetService, OnlineBackup, TechSupport, |
| | Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, |
| | TotalCharges, StreamingService, SecurityService): |
| | input_data = pd.DataFrame({ |
| | "gender": [gender], |
| | "SeniorCitizen": [SeniorCitizen], |
| | "Partner": [Partner], |
| | "Dependents": [Dependents], |
| | "tenure": [int(tenure)], |
| | "PhoneService": [PhoneService], |
| | "InternetService": [InternetService], |
| | "OnlineBackup": [OnlineBackup], |
| | "TechSupport": [TechSupport], |
| | "Contract": [Contract], |
| | "PaperlessBilling": [PaperlessBilling], |
| | "PaymentMethod": [PaymentMethod], |
| | "StreamingService": [StreamingService], |
| | "SecurityService": [SecurityService], |
| | "MonthlyCharges": [float(MonthlyCharges)], |
| | "TotalCharges": [float(TotalCharges)], |
| | }) |
| | return input_data |
| |
|
| | |
| | def predict_churn(gender, SeniorCitizen, Partner, Dependents, tenure, |
| | PhoneService, InternetService, OnlineBackup, TechSupport, |
| | Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, |
| | TotalCharges, StreamingService, SecurityService): |
| | input_df = input_df_creator(gender, SeniorCitizen, Partner, Dependents, tenure, |
| | PhoneService, InternetService, OnlineBackup, TechSupport, |
| | Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, |
| | TotalCharges, StreamingService, SecurityService) |
| | |
| | |
| | cat_cols = data.select_dtypes(include=['object']).columns |
| | cat_encoded = encoder.transform(input_df[cat_cols]) |
| |
|
| | |
| | num_cols = data.select_dtypes(include=['int64', 'float64']).columns |
| | num_scaled = scaler.transform(input_df[num_cols]) |
| |
|
| | |
| | processed_df = pd.concat([num_scaled, cat_encoded], axis=1) |
| |
|
| | |
| | prediction = model.predict(processed_df) |
| | return "Churn" if prediction[0] == 1 else "No Churn" |
| | |
| | |
| | iface = gr.Interface(predict_churn, inputs=input_components, outputs=output_components) |
| | iface.launch(inbrowser= True, show_error= True) |
| |
|