Spaces:
Sleeping
Sleeping
loan predictor update
Browse files- app.py +84 -0
- loan_model.pkl +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load the trained loan model
|
| 6 |
+
model = joblib.load("loan_model.pkl")
|
| 7 |
+
|
| 8 |
+
def predict_loan_status(
|
| 9 |
+
gender,
|
| 10 |
+
married,
|
| 11 |
+
dependents,
|
| 12 |
+
education,
|
| 13 |
+
self_employed,
|
| 14 |
+
applicant_income,
|
| 15 |
+
coapplicant_income,
|
| 16 |
+
loan_amount,
|
| 17 |
+
loan_amount_term,
|
| 18 |
+
credit_history,
|
| 19 |
+
property_area
|
| 20 |
+
):
|
| 21 |
+
"""
|
| 22 |
+
This function:
|
| 23 |
+
- Receives user inputs
|
| 24 |
+
- Converts categorical inputs to numeric values
|
| 25 |
+
- Uses the trained model to predict loan status
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
# Encoding categorical variables (must match training logic)
|
| 29 |
+
gender = 1 if gender == "Male" else 0
|
| 30 |
+
married = 1 if married == "Yes" else 0
|
| 31 |
+
education = 1 if education == "Graduate" else 0
|
| 32 |
+
self_employed = 1 if self_employed == "Yes" else 0
|
| 33 |
+
|
| 34 |
+
property_area_map = {
|
| 35 |
+
"Urban": 2,
|
| 36 |
+
"Semiurban": 1,
|
| 37 |
+
"Rural": 0
|
| 38 |
+
}
|
| 39 |
+
property_area = property_area_map[property_area]
|
| 40 |
+
|
| 41 |
+
# Combine inputs into model-ready format
|
| 42 |
+
features = np.array([[
|
| 43 |
+
gender,
|
| 44 |
+
married,
|
| 45 |
+
dependents,
|
| 46 |
+
education,
|
| 47 |
+
self_employed,
|
| 48 |
+
applicant_income,
|
| 49 |
+
coapplicant_income,
|
| 50 |
+
loan_amount,
|
| 51 |
+
loan_amount_term,
|
| 52 |
+
credit_history,
|
| 53 |
+
property_area
|
| 54 |
+
]])
|
| 55 |
+
|
| 56 |
+
# Make prediction
|
| 57 |
+
prediction = model.predict(features)[0]
|
| 58 |
+
|
| 59 |
+
return "Loan Approved" if prediction == 1 else "Loan Rejected"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Gradio Interface
|
| 63 |
+
interface = gr.Interface(
|
| 64 |
+
fn=predict_loan_status,
|
| 65 |
+
inputs=[
|
| 66 |
+
gr.Radio(["Male", "Female"], label="Gender"),
|
| 67 |
+
gr.Radio(["Yes", "No"], label="Married"),
|
| 68 |
+
gr.Number(label="Number of Dependents"),
|
| 69 |
+
gr.Radio(["Graduate", "Not Graduate"], label="Education"),
|
| 70 |
+
gr.Radio(["Yes", "No"], label="Self Employed"),
|
| 71 |
+
gr.Number(label="Applicant Income"),
|
| 72 |
+
gr.Number(label="Coapplicant Income"),
|
| 73 |
+
gr.Number(label="Loan Amount"),
|
| 74 |
+
gr.Number(label="Loan Amount Term"),
|
| 75 |
+
gr.Radio([1, 0], label="Credit History (1 = Good, 0 = Bad)"),
|
| 76 |
+
gr.Radio(["Urban", "Semiurban", "Rural"], label="Property Area"),
|
| 77 |
+
],
|
| 78 |
+
outputs="text",
|
| 79 |
+
title="Loan Status Prediction System",
|
| 80 |
+
description="Predict whether a loan application will be approved or rejected using a trained machine learning model."
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
+
interface.launch()
|
loan_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3c8389a3a39806c6b8a9aa3a9aff0ae73a84a348721743c9ba8e75e18ba1fc72
|
| 3 |
+
size 2414313
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
joblib
|
| 5 |
+
gradio
|