Sujith2121 commited on
Commit
0560438
·
verified ·
1 Parent(s): 849bf60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -47
app.py CHANGED
@@ -1,47 +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)}
 
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/base_random_forest_model.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)}