Sujith2121 commited on
Commit
3dbcdc6
·
verified ·
1 Parent(s): 52f21af

Upload 5 files

Browse files
Files changed (5) hide show
  1. .gitignore +63 -0
  2. Dockerfile +16 -0
  3. README.md +9 -12
  4. app.py +47 -0
  5. requirements.txt +8 -0
.gitignore ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -------------------------
2
+ # Python
3
+ # -------------------------
4
+ __pycache__/
5
+ *.py[cod]
6
+ *.pyo
7
+ *.pyd
8
+ *.so
9
+ *.egg
10
+ *.egg-info/
11
+ dist/
12
+ build/
13
+ .eggs/
14
+ *.log
15
+
16
+ # -------------------------
17
+ # Virtual environments
18
+ # -------------------------
19
+ .env
20
+ .venv
21
+ env/
22
+ venv/
23
+ ENV/
24
+ env.bak/
25
+ venv.bak/
26
+
27
+ # -------------------------
28
+ # Jupyter notebooks
29
+ # -------------------------
30
+ .ipynb_checkpoints/
31
+ .notebooks_cache/
32
+ *.nbconvert.ipynb
33
+
34
+ # -------------------------
35
+ # Data files
36
+ # -------------------------
37
+ data/raw/*
38
+ !data/raw/.gitkeep
39
+ data/processed/*
40
+ !data/processed/.gitkeep
41
+
42
+ # -------------------------
43
+ # Models
44
+ # -------------------------
45
+ models/*.pkl
46
+ models/*.joblib
47
+ !models/.gitkeep
48
+
49
+ # -------------------------
50
+ # Streamlit / FastAPI
51
+ # -------------------------
52
+ .streamlit/
53
+ *.db
54
+ *.sqlite3
55
+
56
+ # -------------------------
57
+ # OS / Editor files
58
+ # -------------------------
59
+ .DS_Store
60
+ Thumbs.db
61
+ desktop.ini
62
+ .vscode/
63
+ .idea/
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use official Python image
2
+ FROM python:3.10-slim
3
+
4
+ WORKDIR /app
5
+
6
+ # Install dependencies
7
+ COPY requirements.txt .
8
+ RUN pip install --no-cache-dir -r requirements.txt
9
+
10
+ # Copy app and models
11
+ COPY app.py .
12
+ COPY models/ ./models/
13
+
14
+ EXPOSE 8000
15
+
16
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
README.md CHANGED
@@ -1,12 +1,9 @@
1
- ---
2
- title: CREDIT RISK ASSESSMENT FASTAPI
3
- emoji: 🌖
4
- colorFrom: green
5
- colorTo: pink
6
- sdk: docker
7
- pinned: false
8
- license: other
9
- short_description: This is just a credit risk assesser using fastapi backend.
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # 💳 Credit Risk Prediction API
2
+
3
+ This is a FastAPI app that serves a trained Random Forest model for credit risk prediction.
4
+
5
+ ## 🚀 Run Locally
6
+
7
+ ### 1. Build Docker Image
8
+ ```bash
9
+ docker build -t credit-risk-api .
 
 
 
app.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import joblib
4
+ import pandas as pd
5
+
6
+ app = FastAPI()
7
+
8
+ # Load artifacts
9
+ model = joblib.load("models/balanced_model_random_forest.pkl")
10
+ scaler = joblib.load("models/scaler.pkl")
11
+ encoders = joblib.load("models/encoders_dict.pkl")
12
+
13
+ class LoanApplication(BaseModel):
14
+ person_age: float
15
+ person_gender: str
16
+ person_education: str
17
+ person_income: float
18
+ person_emp_exp: int
19
+ person_home_ownership: str
20
+ loan_amnt: float
21
+ loan_intent: str
22
+ loan_int_rate: float
23
+ loan_percent_income: float
24
+ cb_person_cred_hist_length: float
25
+ credit_score: int
26
+ previous_loan_defaults_on_file: str
27
+ debt_to_income: float
28
+ age_group: str
29
+
30
+ @app.get("/")
31
+ def root():
32
+ return {"message": "Credit Risk API is running inside Docker!"}
33
+
34
+ @app.post("/predict")
35
+ def predict(application: LoanApplication):
36
+ df = pd.DataFrame([application.dict()])
37
+
38
+ # Apply encoders
39
+ for col, encoder in encoders.items():
40
+ df[col] = encoder.transform(df[col].astype(str))
41
+
42
+ # Apply scaler
43
+ scaling_cols = ["person_age","person_income","loan_amnt","credit_score","loan_int_rate"]
44
+ df[scaling_cols] = scaler.transform(df[scaling_cols])
45
+
46
+ prediction = model.predict(df)[0]
47
+ return {"loan_status": int(prediction)}
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pandas
4
+ scikit-learn
5
+ joblib
6
+ xgboost
7
+ imbalanced-learn
8
+ pydantic