File size: 2,234 Bytes
0f34980
88d9669
 
0f34980
2a4affb
88d9669
0f34980
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88d9669
0f34980
 
 
88d9669
0f34980
 
 
 
 
 
 
 
 
 
 
 
88d9669
0f34980
88d9669
0f34980
 
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
import gradio as gr
import pickle

# Load the CatBoost model
model = pickle.load(open("best_model_catboost.pkl", "rb"))

def predict_loan_default(
    person_age, person_income, person_emp_length, loan_amnt, loan_percent_income,
    cb_person_default_on_file, person_home_ownership, loan_intent, loan_grade
):
    # Map categorical inputs to one-hot encoded features
    home_ownership_map = {
        "OTHER": [1, 0, 0],
        "OWN": [0, 1, 0],
        "RENT": [0, 0, 1]
    }
    intent_map = {
        "DEBTCONSOLIDATION": [1, 0, 0, 0, 0, 0],
        "EDUCATION": [0, 1, 0, 0, 0, 0],
        "HOMEIMPROVEMENT": [0, 0, 1, 0, 0, 0],
        "MEDICAL": [0, 0, 0, 1, 0, 0],
        "PERSONAL": [0, 0, 0, 0, 1, 0],
        "VENTURE": [0, 0, 0, 0, 0, 1]
    }
    grade_map = {
        "A": [1, 0, 0, 0, 0, 0, 0],
        "B": [0, 1, 0, 0, 0, 0, 0],
        "C": [0, 0, 1, 0, 0, 0, 0],
        "D": [0, 0, 0, 1, 0, 0, 0],
        "E": [0, 0, 0, 0, 1, 0, 0],
        "F": [0, 0, 0, 0, 0, 1, 0],
        "G": [0, 0, 0, 0, 0, 0, 1]
    }

    # Prepare features for prediction
    features = [
        person_age,
        person_income,
        person_emp_length,
        loan_amnt,
        loan_percent_income,
        int(cb_person_default_on_file == "Yes"),
        *home_ownership_map[person_home_ownership],
        *intent_map[loan_intent],
        *grade_map[loan_grade]
    ]

    # Make prediction
    prediction = model.predict([features])
    return "Default" if prediction[0] == 1 else "No Default"

# Define Gradio interface
inputs = [
    gr.Number(label="Person Age"),
    gr.Number(label="Person Income"),
    gr.Number(label="Person Employment Length"),
    gr.Number(label="Loan Amount"),
    gr.Number(label="Loan Percent Income"),
    gr.Radio(["Yes", "No"], label="Default on File"),
    gr.Radio(["OTHER", "OWN", "RENT"], label="Home Ownership"),
    gr.Radio(["DEBTCONSOLIDATION", "EDUCATION", "HOMEIMPROVEMENT", "MEDICAL", "PERSONAL", "VENTURE"], label="Loan Intent"),
    gr.Radio(["A", "B", "C", "D", "E", "F", "G"], label="Loan Grade")
]

outputs = gr.Textbox(label="Prediction")

# Launch Gradio app
gr.Interface(fn=predict_loan_default, inputs=inputs, outputs=outputs, title="Credit Risk Prediction").launch()