Spaces:
Sleeping
Sleeping
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() |