Spaces:
Sleeping
Sleeping
Siddhant Maji commited on
Commit ·
9afc8c7
1
Parent(s): 27362b1
added app.py and models
Browse files- app.py +96 -0
- data/feature_names.json +1 -0
- models/ffnn_model.keras +0 -0
- models/logistic_regression_model.pkl +3 -0
- models/standard_scaler.pkl +3 -0
- models/xgboost_model.pkl +3 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import joblib
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from tensorflow.keras.models import load_model
|
| 8 |
+
|
| 9 |
+
# Load models
|
| 10 |
+
log_reg = joblib.load("models/logistic_regression_model.pkl")
|
| 11 |
+
xgb = pickle.load(open("models/xgboost_model.pkl", "rb"))
|
| 12 |
+
ffnn = load_model("models/ffnn_model.keras")
|
| 13 |
+
scaler = joblib.load("models/standard_scaler.pkl")
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
|
| 17 |
+
with open("data/feature_names.json", "r") as f:
|
| 18 |
+
feature_names = json.load(f)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def predict_default(*inputs):
|
| 22 |
+
processed_inputs = []
|
| 23 |
+
for name, val in zip(feature_names, inputs):
|
| 24 |
+
if name in categorical_mappings:
|
| 25 |
+
val = categorical_mappings[name].index(val) # Convert string to int
|
| 26 |
+
processed_inputs.append(val)
|
| 27 |
+
|
| 28 |
+
input_df = pd.DataFrame([processed_inputs], columns=feature_names)
|
| 29 |
+
scaled = scaler.transform(input_df)
|
| 30 |
+
|
| 31 |
+
logit = log_reg.predict_proba(scaled)[0][1]
|
| 32 |
+
xgb_pred = xgb.predict_proba(input_df.values)[0][1]
|
| 33 |
+
ffnn_pred = ffnn.predict(scaled)[0][0]
|
| 34 |
+
|
| 35 |
+
return {
|
| 36 |
+
"Logistic Regression": float(logit),
|
| 37 |
+
"XGBoost": float(xgb_pred),
|
| 38 |
+
"FFNN": float(ffnn_pred),
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
default_values = [
|
| 43 |
+
56.0, # Age
|
| 44 |
+
85994.0, # Income
|
| 45 |
+
50587.0, # LoanAmount
|
| 46 |
+
520.0, # CreditScore
|
| 47 |
+
80.0, # MonthsEmployed
|
| 48 |
+
4.0, # NumCreditLines
|
| 49 |
+
15.23, # InterestRate
|
| 50 |
+
36.0, # LoanTerm
|
| 51 |
+
0.44, # DTIRatio
|
| 52 |
+
0.0, # Education
|
| 53 |
+
0.0, # EmploymentType
|
| 54 |
+
0.0, # MaritalStatus
|
| 55 |
+
1.0, # HasMortgage
|
| 56 |
+
1.0, # HasDependents
|
| 57 |
+
4.0, # LoanPurpose
|
| 58 |
+
1.0, # HasCoSigner
|
| 59 |
+
-0.895272, # AffRatio
|
| 60 |
+
0.431883, # TotalInterest
|
| 61 |
+
0.139637, # Debt
|
| 62 |
+
-1.28165, # AvgBorrowed
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
categorical_mappings = {
|
| 66 |
+
"Education": ["Bachelor's", "High School", "Master's", "PhD"],
|
| 67 |
+
"EmploymentType": ["Full-time", "Part-time", "Self-employed", "Unemployed"],
|
| 68 |
+
"MaritalStatus": ["Divorced", "Married", "Single"],
|
| 69 |
+
"HasMortgage": ["No", "Yes"],
|
| 70 |
+
"HasDependents": ["No", "Yes"],
|
| 71 |
+
"LoanPurpose": ["Auto", "Business", "Education", "Home", "Other"],
|
| 72 |
+
"HasCoSigner": ["No", "Yes"],
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
input_components = []
|
| 77 |
+
for name, val in zip(feature_names, default_values):
|
| 78 |
+
if name in categorical_mappings:
|
| 79 |
+
choices = categorical_mappings[name]
|
| 80 |
+
input_components.append(
|
| 81 |
+
gr.Dropdown(label=name, choices=choices, value=choices[int(val)])
|
| 82 |
+
)
|
| 83 |
+
else:
|
| 84 |
+
input_components.append(gr.Number(label=name, value=val))
|
| 85 |
+
output_components = gr.Label(num_top_classes=3)
|
| 86 |
+
|
| 87 |
+
demo = gr.Interface(
|
| 88 |
+
fn=predict_default,
|
| 89 |
+
inputs=input_components,
|
| 90 |
+
outputs=output_components,
|
| 91 |
+
title="Loan Default Risk Predictor",
|
| 92 |
+
description="Enter borrower info and see the default risk prediction from 3 models.",
|
| 93 |
+
flagging_mode="never",
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
demo.launch()
|
data/feature_names.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
["Age", "Income", "LoanAmount", "CreditScore", "MonthsEmployed", "NumCreditLines", "InterestRate", "LoanTerm", "DTIRatio", "Education", "EmploymentType", "MaritalStatus", "HasMortgage", "HasDependents", "LoanPurpose", "HasCoSigner", "AffRatio", "TotalInterest", "Debt", "AvgBorrowed"]
|
models/ffnn_model.keras
ADDED
|
Binary file (71.2 kB). View file
|
|
|
models/logistic_regression_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:95373bda426cb336224257d4a4fa794c73403e9f1faa9e8c731e642003f44c9e
|
| 3 |
+
size 1051
|
models/standard_scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f8845ddfdfee4999895437aa2d46f98ade95d5b11ba637a5c581940c6361a159
|
| 3 |
+
size 1607
|
models/xgboost_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d546ba56110501c951797c7758c516ab7f4523e2ee43778f1590eb081155de89
|
| 3 |
+
size 177107
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.41.0
|
| 2 |
+
joblib==1.5.1
|
| 3 |
+
numpy==2.3.2
|
| 4 |
+
pandas==2.3.1
|
| 5 |
+
tensorflow==2.20.0rc0
|