import gradio as gr import pickle # import time import pandas as pd import numpy as np from utils import create_new_columns, create_processed_dataframe pipeline_pkl = "full_pipeline.pkl" log_reg = "logistic_reg_class_model.pkl" # hist_df = "history.csv" # def check_csv(csv_file, data): # if os.path.isfile(csv_file): # data.to_csv(csv_file, mode='a', header=False, index=False, encoding='utf-8') # else: # history = data.copy() # history.to_csv(csv_file, index=False) def tenure_values(): cols = ['0-2', '3-5', '6-8', '9-11', '12-14', '15-17', '18-20', '21-23', '24-26', '27-29', '30-32', '33-35', '36-38', '39-41', '42-44', '45-47', '48-50', '51-53', '54-56', '57-59', '60-62', '63-65', '66-68', '69-71', '72-74'] return cols def predict_churn(gender, SeniorCitizen, Partner, Dependents, Tenure, PhoneService, MultipleLines, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,TechSupport,StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, TotalCharges): data = [gender, SeniorCitizen, Partner, Dependents, Tenure, PhoneService, MultipleLines, InternetService, OnlineSecurity, OnlineBackup, DeviceProtection,TechSupport,StreamingTV, StreamingMovies, Contract, PaperlessBilling, PaymentMethod, MonthlyCharges, TotalCharges] x = np.array([data]) dataframe = pd.DataFrame(x, columns=train_features) dataframe = dataframe.astype({'MonthlyCharges': 'float', 'TotalCharges': 'float', 'tenure': 'float'}) dataframe_ = create_new_columns(dataframe) try: processed_data = pipeline.transform(dataframe_) except Exception as e: raise gr.Error('Kindly make sure to check/select all') else: # check_csv(hist_df, dataframe) # history = pd.read_csv(hist_df) processed_dataframe = create_processed_dataframe(processed_data, dataframe) predictions = model.predict_proba(processed_dataframe) return round(predictions[0][0], 3), round(predictions[0][1], 3) theme = gr.themes.Default().set(body_background_fill="#0E1117", background_fill_secondary="#FFFFFF", background_fill_primary="#262730", body_text_color="#FF4B4B", checkbox_background_color='#FFFFFF', button_secondary_background_fill="#FF4B4B") def load_pickle(filename): with open(filename, 'rb') as file: data = pickle.load(file) return data pipeline = load_pickle(pipeline_pkl) model = load_pickle(log_reg) train_features = ['gender', 'SeniorCitizen', 'Partner', 'Dependents','tenure', 'PhoneService', 'MultipleLines', 'InternetService', 'OnlineSecurity', 'OnlineBackup', 'DeviceProtection','TechSupport','StreamingTV', 'StreamingMovies', 'Contract', 'PaperlessBilling', 'PaymentMethod', 'MonthlyCharges', 'TotalCharges'] # theme = gr.themes.Base() with gr.Blocks(theme=theme) as demo: gr.HTML("""