Elobike's picture
Update app.py
cadfc09 verified
import gradio as gr
import joblib
import numpy as np
# Load the trained loan model
model = joblib.load("loan_model.pkl")
def predict_loan_status(
married,
dependents,
education,
applicant_income,
coapplicant_income,
loan_amount,
loan_amount_term,
credit_history,
property_area
):
"""
This function:
- Receives user inputs
- Converts categorical inputs to numeric values
- Uses the trained model to predict loan status
"""
# Encoding categorical variables (must match training logic)
married = 1 if married == "Yes" else 0
education = 1 if education == "Graduate" else 0
property_area_map = {
"Urban": 2,
"Semiurban": 1,
"Rural": 0
}
property_area = property_area_map[property_area]
# Combine inputs into model-ready format
features = np.array([[
married,
dependents,
education,
applicant_income,
coapplicant_income,
loan_amount,
loan_amount_term,
credit_history,
property_area
]])
# Make prediction
prediction = model.predict(features)[0]
return "Loan Approved" if prediction == 1 else "Loan Rejected"
# Gradio Interface
interface = gr.Interface(
fn=predict_loan_status,
inputs=[
gr.Radio(["Yes", "No"], label="Married"),
gr.Number(label="Number of Dependents"),
gr.Radio(["Graduate", "Not Graduate"], label="Education"),
gr.Number(label="Applicant Income"),
gr.Number(label="Coapplicant Income"),
gr.Number(label="Loan Amount"),
gr.Number(label="Loan Amount Term"),
gr.Radio([1, 0], label="Credit History (1 = Good, 0 = Bad)"),
gr.Radio(["Urban", "Semiurban", "Rural"], label="Property Area"),
],
outputs="text",
title="Loan Status Prediction System",
description="Predict whether a loan application will be approved or rejected using a trained machine learning model."
)
if __name__ == "__main__":
interface.launch()