-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (59 loc) · 2.28 KB
/
Copy pathmain.py
File metadata and controls
72 lines (59 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import pickle
import pandas as pd
from fastapi import FastAPI
from pydantic import BaseModel, Field
from preprocessing_utils import encode_features, scale_features
app = FastAPI(
title="Credit Risk Classification and Deployment Engine",
description="A machine learning-powered REST API for classifying the credit risk of loan applicants.",
version="1.0.0",
)
# Load trained artifacts once at startup
with open("model.pkl", "rb") as f:
model = pickle.load(f)
with open("encoders.pkl", "rb") as f:
encoders = pickle.load(f)
with open("scaler.pkl", "rb") as f:
scaler = pickle.load(f)
with open("label_encoder_y.pkl", "rb") as f:
label_encoder_y = pickle.load(f)
class CreditRiskRequest(BaseModel):
gender: str = Field(..., examples=["Male"])
married: str = Field(..., examples=["Yes"])
dependents: str = Field(..., examples=["0"])
education: str = Field(..., examples=["Graduate"])
self_employed: str = Field(..., examples=["No"])
loan_amount: float = Field(..., examples=[150.0])
loan_amount_term: float = Field(..., examples=[360.0])
credit_history: float = Field(..., examples=[1.0])
@app.get("/")
def root():
return {
"status": "Credit Risk Classification and Deployment Engine is running"
}
@app.post("/predict-risk")
def predict(application: CreditRiskRequest):
# Build a single-row DataFrame matching the training feature columns/order
row = pd.DataFrame(
[
{
"Gender": application.gender,
"Married": application.married,
"Dependents": application.dependents,
"Education": application.education,
"Self_Employed": application.self_employed,
"LoanAmount": application.loan_amount,
"Loan_Amount_Term": application.loan_amount_term,
"Credit_History": application.credit_history,
}
]
)
X = encode_features(row, encoders, fit=False)
X = scale_features(X, scaler, fit=False)
prediction = model.predict(X)[0]
confidence = max(model.predict_proba(X)[0])
status = label_encoder_y.inverse_transform([prediction])[0]
return {
"credit_risk": "Low Risk" if status == "Y" else "High Risk",
"confidence": round(float(confidence), 3),
}