File size: 2,033 Bytes
1c095e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()