harish199 commited on
Commit
2106d25
1 Parent(s): 6e6988e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pandas as pd
3
+ from sklearn.ensemble import RandomForestClassifier
4
+ import gradio as gr
5
+
6
+ df = pd.read_csv("credit_risk_dataset.csv")
7
+
8
+ df = df.dropna()
9
+
10
+ df.columns
11
+
12
+ X =df.drop(["loan_status", "loan_percent_income"], axis = 1)
13
+ y = df['loan_status']
14
+
15
+ categorical_features = ["person_home_ownership", "loan_intent", "loan_grade", "cb_person_default_on_file"]
16
+ X = pd.get_dummies(X, categorical_features)
17
+ X.columns
18
+
19
+
20
+
21
+ from sklearn.model_selection import train_test_split
22
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
23
+
24
+
25
+ from sklearn.preprocessing import StandardScaler
26
+ scaler_normal = StandardScaler()
27
+ def scaler(data, runtime = False):
28
+ normal_col = ['person_income','person_age','person_emp_length', 'loan_amnt','loan_int_rate','cb_person_cred_hist_length']
29
+ if(runtime == False):
30
+ data.loc[:,normal_col] = scaler_normal.fit_transform(data.loc[:,normal_col])
31
+ else:
32
+ data.loc[:,normal_col] = scaler_normal.transform(data.loc[:,normal_col])
33
+ return data
34
+
35
+ X_train = scaler(X_train)
36
+ X_test = scaler(X_test, True)
37
+
38
+ rf_model = RandomForestClassifier(max_depth = 5)
39
+ rf_model.fit(X_train, y_train)
40
+
41
+ y_predict = rf_model.predict(X_test)
42
+ y_predict
43
+
44
+
45
+ features = {
46
+ "person_home_ownership": ['MORTGAGE', 'OTHER','OWN', 'RENT',],
47
+ "loan_intent": ['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE'],
48
+ "loan_grade": ['A','B', 'C', 'D', 'E','F', 'G'],
49
+ "cb_person_default_on_file": ['N', 'Y']
50
+ }
51
+ def preprocess(model_input):
52
+ for feature in features:
53
+ for option in features[feature]:
54
+ selection = model_input[feature]
55
+ if option is selection:
56
+ model_input[f'{feature}_{option}'] = 1
57
+ else:
58
+ model_input[f'{feature}_{option}'] = 0
59
+
60
+ model_input.drop([_ for _ in features], inplace = True, axis = 1)
61
+ return model_input
62
+
63
+ def credit_run(person_age, person_emp_length,person_home_ownership,cb_person_default_on_file,loan_intent,loan_grade,person_income, loan_amnt,
64
+ loan_int_rate, cb_person_cred_hist_length):
65
+ model_input = preprocess(
66
+ pd.DataFrame( { 'person_age': person_age,
67
+ 'person_income': person_income,
68
+ 'person_home_ownership': person_home_ownership,
69
+ 'person_emp_length': person_emp_length,
70
+ 'loan_intent': loan_intent,
71
+ 'loan_grade': loan_grade,
72
+ 'loan_amnt': loan_amnt,
73
+ 'loan_int_rate': loan_int_rate,
74
+ 'cb_person_default_on_file': cb_person_default_on_file,
75
+ 'cb_person_cred_hist_length': cb_person_cred_hist_length
76
+ }, index = [0]
77
+ ))
78
+ out = rf_model.predict(model_input)
79
+ return "High risk of defaulting" if out[0] == 1 else "Low risk of defaulting"
80
+ import gradio as gr
81
+ with gr.Blocks() as demo:
82
+ with gr.Row():
83
+ with gr.Column(scale=1,min_width=600):
84
+ gr.Image("non_payment_logo.jpg").style(height='7')
85
+ person_age=gr.Slider(label="Person Age(In Years)", minimum=18, maximum=90, step=1)
86
+ Pererson_Emp_Length=gr.Slider(label="Pererson Emp Length(In Years)", minimum=0, maximum=60, step=1)
87
+
88
+ with gr.Column(scale=2,min_width=600):
89
+ with gr.Row():
90
+ with gr.Column(scale=1,min_width=400):
91
+ Home_Ownership_Status=gr.Radio(['MORTGAGE', 'OTHER','OWN', 'RENT'],label="Home Ownership Status")
92
+ with gr.Column(scale=2,min_width=100):
93
+ Person_Defaulted_in_History=gr.Radio(['0', '1'],label="Person Defaulted in History")
94
+
95
+ with gr.Row():
96
+ with gr.Column(scale=3,min_width=300):
97
+ Credit_Intent=gr.Dropdown(['DEBTCONSOLIDATION', 'EDUCATION', 'HOMEIMPROVEMENT', 'MEDICAL', 'PERSONAL', 'VENTURE'],label="Credit Intent")
98
+ with gr.Column(scale=4,min_width=300):
99
+ Type_Of_Credit=gr.Dropdown(['A','B', 'C', 'D', 'E','F', 'G'],label="Type Of Credit")
100
+ with gr.Row():
101
+ with gr.Column(scale=3,min_width=300):
102
+ Person_Income=gr.Number(label="Person Income(per month)")
103
+ with gr.Column(scale=4,min_width=300):
104
+ Loan_Amount=gr.Number(label="Loan Amount")
105
+ with gr.Row():
106
+ with gr.Column(scale=3,min_width=300):
107
+ Loan_Interest_Rate=gr.Number(label="Loan Interest Rate")
108
+ with gr.Column(scale=4,min_width=300):
109
+ Person_Credit_History_Length=gr.Number(label="Person's Credit History Length")
110
+ with gr.Row():
111
+ with gr.Column():
112
+ default= gr.Radio(['Low risk of defaulting', 'High risk of defaulting'])
113
+
114
+ btn = gr.Button("PREDICT")
115
+ btn.click(fn=credit_run, inputs=[person_age,Person_Income,Home_Ownership_Status,Pererson_Emp_Length,Credit_Intent,Type_Of_Credit,Loan_Amount,Loan_Interest_Rate,Person_Defaulted_in_History,Person_Credit_History_Length], outputs=[default])
116
+ #gr.Examples(inputs=[person_age,Pererson_Emp_Length,Home_Ownership_Status,Person_Defaulted_in_History,Credit_Intent,Type_Of_Credit,Person_Income,Loan_Amount,Loan_Interest_Rate,Person_Credit_History_Length])
117
+ demo.launch(debug=True)