File size: 5,980 Bytes
2106d25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c73aef0
2106d25
 
 
 
 
 
 
 
 
0a7ecd0
 
2106d25
2accf89
 
 
 
2106d25
 
 
 
 
 
 
 
 
 
 
 
 
 
2accf89
2106d25
 
 
6a7bd95
2accf89
 
2106d25
 
 
 
 
 
 
 
 
 
 
 
 
0a7ecd0
2accf89
6a7bd95
2106d25
6e92684
2106d25
be8d99c
2d7f38f
be8d99c
4def6fe
ea44b0c
2106d25
 
 
1680c14
2106d25
1680c14
12f3142
2106d25
 
 
ea44b0c
2106d25
 
 
 
4def6fe
2106d25
ea44b0c
2106d25
 
b40e98c
2106d25
4def6fe
2106d25
 
1b0a625
2106d25
 
50384a6
6e92684
 
 
 
160abf4
6e92684
160abf4
6e92684
50384a6
6e92684
b40e98c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
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", "loan_percent_income"], 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)


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']
    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)

y_predict = rf_model.predict(X_test)
y_predict


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_emp_length,person_home_ownership,cb_person_default_on_file,loan_intent,loan_grade,person_income, loan_amnt,
       loan_int_rate, 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,
                   '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"
           
import gradio as gr
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    with gr.Row():
        with gr.Column(scale=1,min_width=400):
            gr.Image("Non_Payment_Logo.png").style(height='5')
        with gr.Column(scale=1,min_width=600):
            person_age=gr.Slider(label="Customer Age(In Years)", minimum=18, maximum=90, step=1)
            Person_Emp_Length=gr.Slider(label="Customer Employement Length(In Years)", minimum=0, maximum=60, step=1)

        with gr.Column(scale=2,min_width=600):
            with gr.Row():
                with gr.Column(scale=1,min_width=500):
                    Home_Ownership_Status=gr.Radio(['MORTGAGE', 'OTHER','OWN', 'RENT'],label="Home Ownership Status")
                with gr.Column(scale=2,min_width=100):
                    Person_Defaulted_in_History=gr.Radio(['N', 'Y'],label="Missed Payment in History")

            with gr.Row():
                with gr.Column(scale=3,min_width=300):
                    Credit_Intent=gr.Dropdown(['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE'],label="Intent")
                with gr.Column(scale=4,min_width=300):
                    Type_Of_Credit=gr.Dropdown(['A','B', 'C', 'D', 'E','F', 'G'],label="Type Of Credit")
            with gr.Row():
                with gr.Column(scale=3,min_width=300):
                    Person_Income=gr.Number(label="Customer Income(per month)")
                with gr.Column(scale=4,min_width=300):
                    Credit_Amount=gr.Number(label="Premium Amount")
            with gr.Row():
                with gr.Column(scale=3,min_width=300):
                    Interest_Rate=gr.Number(label="Interest Rate")
                with gr.Column(scale=4,min_width=300):
                    Person_Credit_History_Length=gr.Number(label="Customer's Credit History Length")
            with gr.Row():
                with gr.Column():
                    default= gr.Radio(['Low risk of defaulting', 'High risk of defaulting'],label="Chances Of Defaulting")

            btn = gr.Button("PREDICT")
            btn.click(fn=credit_run, inputs=[person_age,Person_Emp_Length,Home_Ownership_Status,Person_Defaulted_in_History,Credit_Intent,Type_Of_Credit,Person_Income,Credit_Amount,Interest_Rate,Person_Credit_History_Length], outputs=[default])
            gr.Examples(
                [["23","2","RENT","N","EDUCATION","A","12000","30000","8.9","6"],
                 ["35","10","OWN","N","MEDICAL","C","20000","40000","8.3","8"],
                 ["28","6","RENT","N","VENTURE","B","32000","30000","8.2","6"],
                 ["32","10","MORTGAGE","Y","HOMEIMPROVEMENT","E","20000","600000","8.6","8"],
                 ["41","18","OWN","Y","PERSONAL","A","10000","300000","14.3","4"],
                 ["30","7","OTHER","Y","MEDICAL","C","13000","1000000","9.5","10"]],
                 
                inputs=[person_age,Person_Emp_Length,Home_Ownership_Status,Person_Defaulted_in_History,Credit_Intent,Type_Of_Credit,Person_Income,Credit_Amount,Interest_Rate,Person_Credit_History_Length]
                )
demo.launch(debug=True)