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