import numpy as np import pandas as pd from sklearn.ensemble import RandomForestClassifier import gradio as gr df = pd.read_csv("credit_risk_dataset.csv") df = df.dropna() df.columns X =df.drop("loan_status", axis = 1) y = df['loan_status'] categorical_features = ["person_home_ownership", "loan_intent", "loan_grade", "cb_person_default_on_file"] X = pd.get_dummies(X, categorical_features) X.columns from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2) X_train.head() from sklearn.preprocessing import StandardScaler scaler_normal = StandardScaler() def scaler(data, runtime = False): normal_col = ['person_income','person_age','person_emp_length', 'loan_amnt','loan_int_rate','cb_person_cred_hist_length','loan_percent_income'] if(runtime == False): data.loc[:,normal_col] = scaler_normal.fit_transform(data.loc[:,normal_col]) else: data.loc[:,normal_col] = scaler_normal.transform(data.loc[:,normal_col]) return data X_train = scaler(X_train) X_test = scaler(X_test, True) rf_model = RandomForestClassifier(max_depth = 5) rf_model.fit(X_train, y_train) features = { "person_home_ownership": ['MORTGAGE', 'OTHER','OWN', 'RENT',], "loan_intent": ['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE'], "loan_grade": ['A','B', 'C', 'D', 'E','F', 'G'], "cb_person_default_on_file": ['N', 'Y'] } def preprocess(model_input): for feature in features: for option in features[feature]: selection = model_input[feature] if option is selection: model_input[f'{feature}_{option}'] = 1 else: model_input[f'{feature}_{option}'] = 0 model_input.drop([_ for _ in features], inplace = True, axis = 1) return model_input def credit_run(person_age, person_income, person_home_ownership, person_emp_length, loan_intent, loan_grade, loan_amnt, loan_int_rate, cb_person_default_on_file, cb_person_cred_hist_length): model_input = preprocess( pd.DataFrame( { 'person_age': person_age, 'person_income': person_income, 'person_home_ownership': person_home_ownership, 'person_emp_length': person_emp_length, 'loan_intent': loan_intent, 'loan_grade': loan_grade, 'loan_amnt': loan_amnt, 'loan_int_rate': loan_int_rate, 'loan_percent_income': loan_amnt / person_income, 'cb_person_default_on_file': cb_person_default_on_file, 'cb_person_cred_hist_length': cb_person_cred_hist_length }, index = [0] )) out = rf_model.predict(model_input) return "High risk of defaulting" if out[0] == 1 else "Low risk of defaulting" demo = gr.Interface( fn = credit_run, inputs = [ gr.Slider(label="Person Age(In Years)", minimum=18, maximum=90, step=1), gr.Number(label="Person Income(per month)"), gr.Radio(['MORTGAGE', 'OTHER','OWN', 'RENT'],label="Home Ownership Status"), gr.Slider(label="Pererson Emp Length(In Years)", minimum=0, maximum=60, step=1), gr.Radio(['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE'],label="Credit Intent"), gr.Radio(['A','B', 'C', 'D', 'E','F', 'G'],label="Type Of Credit"), gr.Number(label="Credit Amount"), gr.Number(label="Credit Interest Rate"), gr.Radio(['N', 'Y'],label="Person Defaulted in History"), gr.Number(label="Person's Credit History Length"), ], outputs = gr.Radio(['Low risk of defaulting', 'High risk of defaulting']), title = "Non Payment Credit Risk Predictor", theme=gr.themes.Soft(), examples = [[23,25000,'RENT',2,'EDUCATION','A',30000,8.9,'N',6], [34,50000,'OWN',1,'MEDICAL','B',62000,10.65,'N',3], [32,30000,'RENT',5,'VENTURE','D',100000,8.65,'Y',5], [42,30000,"MORTGAGE",12,'HOMEIMPROVEMENT','C',800000,7.9,'Y',8], [52,20000,"MORTGAGE",10,'PERSONAL','F',100000,15.25,'Y',5]] ) demo.launch(debug=True)