Skip to content

Latest commit

Β 

History

19 Commits

Folders and files

Repository files navigation

πŸ—οΈ Loan Approval System β€” Architecture

High-Level Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                        LOAN APPROVAL SYSTEM                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Frontend      β”‚     Backend API       β”‚     ML Pipeline           β”‚
β”‚   (React/Vite)  β”‚     (FastAPI)         β”‚     (scikit-learn/XGB)    β”‚
β”‚                 β”‚                       β”‚                           β”‚
β”‚  loan-ui/       β”‚  api/                 β”‚  src/                     β”‚
β”‚  β”œβ”€ App.jsx     β”‚  β”œβ”€ main.py           β”‚  β”œβ”€ data/                 β”‚
β”‚  β”œβ”€ Predict.jsx β”‚  β”œβ”€ routes.py         β”‚  β”œβ”€ features/             β”‚
β”‚  └─ predict.js  β”‚  └─ schema.py         β”‚  β”œβ”€ models/               β”‚
β”‚                 β”‚                       β”‚  β”œβ”€ pipelines/            β”‚
β”‚  Port: 5173     β”‚  Port: 8000           β”‚  └─ utils/                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

System Architecture Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”       HTTP POST /predict       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              β”‚  ──────────────────────────►   β”‚                  β”‚
β”‚   React UI   β”‚       JSON Request             β”‚   FastAPI Server β”‚
β”‚  (Vite Dev)  β”‚                                β”‚   (Uvicorn)      β”‚
β”‚              β”‚  ◄──────────────────────────   β”‚                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜       JSON Response            β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                       {approval, probability,           β”‚
                        risk_level, explanations,        β”‚
                        insights}                        β”‚
                                                         β”‚ calls
                                                         β–Ό
                                                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                                β”‚  Prediction      β”‚
                                                β”‚  Engine          β”‚
                                                β”‚                  β”‚
                                                β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
                                                β”‚  β”‚ best_model β”‚  β”‚
                                                β”‚  β”‚   .pkl     β”‚  β”‚
                                                β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
                                                β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
                                                β”‚  β”‚   SHAP     β”‚  β”‚
                                                β”‚  β”‚ Explainer  β”‚  β”‚
                                                β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
                                                β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Component Breakdown

1. Frontend β€” loan-ui/

Item Detail
Framework React 19 + Vite 8
UI Library Material UI (MUI) v7
HTTP Client Axios
Theme Custom dark/gold "Premium Loan Analytics" theme
Dev Server http://localhost:5173

File Structure:

loan-ui/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ App.jsx              # Main app β€” form inputs, result display, MUI theme
β”‚   β”œβ”€β”€ components/
β”‚   β”‚   └── Predict.jsx      # Predict button with loading/error states
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   └── predict.js       # Axios POST to backend /predict endpoint
β”‚   β”œβ”€β”€ main.jsx             # React entry point
β”‚   β”œβ”€β”€ App.css
β”‚   └── index.css
β”œβ”€β”€ index.html
β”œβ”€β”€ vite.config.js
β”œβ”€β”€ package.json
└── tailwind.config.js

Data Flow:

  1. User fills in the loan application form (11 fields)
  2. Clicks "Generate Analysis"
  3. predict.js sends a POST request to http://127.0.0.1:8000/predict
  4. Response is rendered: approval status, confidence, SHAP-based feature impacts, and insights

2. Backend API β€” api/

Item Detail
Framework FastAPI
Server Uvicorn
Middleware CORS (allow all origins for dev)
Validation Pydantic BaseModel

File Structure:

api/
β”œβ”€β”€ main.py       # FastAPI app creation, CORS middleware, router include
β”œβ”€β”€ routes.py     # POST /predict endpoint β€” calls prediction engine
└── schema.py     # LoanRequest Pydantic model (11 input fields)

Endpoints:

Method Path Description
GET / Health check β€” returns {"message": "Loan Approval API running"}
POST /predict Accepts LoanRequest, returns prediction result

Request Schema (LoanRequest):

{
    "no_of_dependents": int,
    "education": str,            # "Graduate" | "Not Graduate"
    "self_employed": str,        # "Yes" | "No"
    "income_annum": float,
    "loan_amount": float,
    "loan_term": float,
    "cibil_score": float,
    "residential_assets_value": float,
    "commercial_assets_value": float,
    "luxury_assets_value": float,
    "bank_asset_value": float
}

Response Schema:

{
    "status": "success",
    "prediction": {
        "approval": "Approved | Rejected",
        "probability": 0.0 - 1.0,
        "risk_level": "Low Risk | Medium Risk | High Risk",
        "explanations": { "feature_name": shap_value, ... },
        "insights": ["feature X increased chances of approval", ...]
    }
}

3. ML Pipeline β€” src/

src/
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ load_data.py            # Loads CSV data via pandas
β”‚   └── preprocess.py           # Cleans columns, maps target, fixes negatives
β”œβ”€β”€ features/
β”‚   └── feature_engineering.py  # (placeholder)
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ train.py                # Entry point β€” runs training pipeline
β”‚   β”œβ”€β”€ predict.py              # Loads model, runs prediction + SHAP explanation
β”‚   └── evaluate.py             # (placeholder)
β”œβ”€β”€ pipelines/
β”‚   └── training_pipeline.py    # Full training pipeline: load β†’ preprocess β†’ train β†’ save
└── utils/
    └── helpers.py              # (placeholder)

Training Pipeline (training_pipeline.py)

Raw CSV Data
     β”‚
     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
│  Load & Clean   │  Read CSV, strip columns, map target (Approved→1, Rejected→0)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Feature Split  β”‚  X = all features (drop loan_id, loan_status)
β”‚                 β”‚  y = loan_status
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Preprocessor   β”‚  ColumnTransformer:
β”‚                 β”‚    β€’ Numerical β†’ StandardScaler
β”‚                 β”‚    β€’ Categorical (education, self_employed) β†’ OneHotEncoder
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Train/Test     β”‚  80/20 split, stratified by target
β”‚  Split          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Model Training β”‚  4 models trained in sklearn Pipeline:
β”‚                 β”‚    β€’ Logistic Regression
β”‚                 β”‚    β€’ Decision Tree (max_depth=5)
β”‚                 β”‚    β€’ Random Forest (150 trees, max_depth=6)
β”‚                 β”‚    β€’ XGBoost (300 trees, lr=0.05, regularized)
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Evaluation     β”‚  F1 Score comparison β†’ best model selected
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Save Model     β”‚  joblib.dump β†’ models/best_model.pkl
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Prediction Engine (predict.py)

Input Dict (11 features)
     β”‚
     β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Load Model      β”‚  joblib.load(best_model.pkl)
β”‚  + SHAP Explainerβ”‚  shap.TreeExplainer on model step
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Predict         β”‚  model.predict() β†’ Approved/Rejected
β”‚                  β”‚  model.predict_proba() β†’ confidence score
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  SHAP Explain    β”‚  Compute SHAP values on preprocessed input
β”‚                  β”‚  Normalize β†’ top 8 features by impact
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Risk Assessment β”‚  probability > 0.7 β†’ Low Risk
β”‚  + Insights      β”‚  probability > 0.4 β†’ Medium Risk
β”‚                  β”‚  else             β†’ High Risk
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

4. Data β€” data/

data/
β”œβ”€β”€ raw/
β”‚   └── loan_data.csv         # Original dataset
└── processed/
    └── clean_data.csv        # Cleaned dataset (post-preprocessing)

loan_approval_dataset.csv     # Root-level copy of dataset

Features (11 input columns):

Feature Type Description
no_of_dependents int Number of dependents
education categorical Graduate / Not Graduate
self_employed categorical Yes / No
income_annum float Annual income
loan_amount float Requested loan amount
loan_term float Loan term in months
cibil_score float Credit score
residential_assets_value float Residential asset value
commercial_assets_value float Commercial asset value
luxury_assets_value float Luxury asset value
bank_asset_value float Bank/liquid asset value

Target: loan_status β†’ Approved (1) / Rejected (0)


5. Configuration β€” config/

# config/config.yaml
model:
  test_size: 0.2
  random_state: 42

training:
  models:
    - logistic_regression
    - decision_tree
    - random_forest
    - xgboost

6. Saved Artifacts β€” models/

models/
β”œβ”€β”€ best_model.pkl     # Serialized sklearn Pipeline (preprocessor + best model)
└── metrics.json       # (placeholder for evaluation metrics)

Tech Stack Summary

Layer Technology
Frontend React 19, Vite 8, MUI 7, Axios
Backend FastAPI, Uvicorn, Pydantic
ML Models scikit-learn, XGBoost
Explainability SHAP (TreeExplainer)
Data Processing pandas, NumPy
Serialization joblib
Config YAML
Notebooks Jupyter (EDA)

How to Run

# 1. Install Python dependencies
pip install -r requirements.txt

# 2. Train the model (if needed)
python -m src.models.train

# 3. Start the API server
uvicorn api.main:app --host 0.0.0.0 --port 8000

# 4. Start the frontend (in a separate terminal)
cd loan-ui
npm install
npm run dev

Or use the startup script for the API:

PORT=8000 bash start.sh

Request/Response Flow

User fills form in React UI
        β”‚
        β–Ό
POST http://127.0.0.1:8000/predict
  Body: { no_of_dependents, education, ... }
        β”‚
        β–Ό
FastAPI validates request via Pydantic (LoanRequest)
        β”‚
        β–Ό
routes.py β†’ calls predict(request.dict())
        β”‚
        β–Ό
predict.py:
  1. Converts dict β†’ DataFrame
  2. model.predict() β†’ approval decision
  3. model.predict_proba() β†’ confidence
  4. Preprocessor transforms input
  5. SHAP TreeExplainer β†’ feature importance
  6. Normalizes & ranks top 8 features
  7. Generates human-readable insights
  8. Computes risk level
        β”‚
        β–Ό
JSON response returned to React UI
        β”‚
        β–Ό
UI renders: Verdict, Confidence %, Feature Impact Bars, Strategic Insights

Directory Tree

loan-approval-system/
β”œβ”€β”€ api/                          # FastAPI backend
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ routes.py
β”‚   └── schema.py
β”œβ”€β”€ config/
β”‚   └── config.yaml               # Model/training config
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ raw/
β”‚   β”‚   └── loan_data.csv
β”‚   └── processed/
β”‚       └── clean_data.csv
β”œβ”€β”€ loan-ui/                       # React frontend
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ api/predict.js
β”‚   β”‚   β”œβ”€β”€ components/Predict.jsx
β”‚   β”‚   β”œβ”€β”€ App.jsx
β”‚   β”‚   └── main.jsx
β”‚   β”œβ”€β”€ package.json
β”‚   └── vite.config.js
β”œβ”€β”€ models/
β”‚   β”œβ”€β”€ best_model.pkl             # Trained model artifact
β”‚   └── metrics.json
β”œβ”€β”€ notebooks/
β”‚   └── eda.ipynb                  # Exploratory Data Analysis
β”œβ”€β”€ src/                           # ML source code
β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”œβ”€β”€ load_data.py
β”‚   β”‚   └── preprocess.py
β”‚   β”œβ”€β”€ features/
β”‚   β”‚   └── feature_engineering.py
β”‚   β”œβ”€β”€ models/
β”‚   β”‚   β”œβ”€β”€ train.py
β”‚   β”‚   β”œβ”€β”€ predict.py
β”‚   β”‚   └── evaluate.py
β”‚   β”œβ”€β”€ pipelines/
β”‚   β”‚   └── training_pipeline.py
β”‚   └── utils/
β”‚       └── helpers.py
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ start.sh
└── README.md

Releases

Packages

Contributors

Languages