-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloan_sahayak_api_py.py
executable file
·78 lines (66 loc) · 2.21 KB
/
loan_sahayak_api_py.py
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
73
74
75
76
77
78
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import pickle
import pandas as pd
import joblib
# Load the trained ML model and scaler
model = pickle.load(open('loan_status_predict.sav', 'rb'))
scaler = joblib.load('vector.pkl')
# Columns to be scaled
cols = ['ApplicantIncome', 'CoapplicantIncome', 'LoanAmount', 'Loan_Amount_Term']
# Initialize the FastAPI application
app = FastAPI()
# CORS settings
origins = [
"http://localhost:5173",
"https://loan-sahayak-z5n7.onrender.com",
]
# Add CORS middlewar
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allows all origins
allow_credentials=True,
allow_methods=["*"], # Allows all HTTP methods (GET, POST, OPTIONS, etc.)
allow_headers=["*"], # Allows all headers
)
# Define the request schema
class PredictionRequest(BaseModel):
Gender: int
Married: int
Dependents: int
Education: int
Self_Employed: int
ApplicantIncome: int
CoapplicantIncome: float
LoanAmount: float
Loan_Amount_Term: float
Credit_History: float
Property_Area: int
# Define the prediction endpoint
@app.post('/predict')
def predict(request: PredictionRequest):
input_df = pd.DataFrame([{
'Gender': request.Gender,
'Married': request.Married,
'Dependents': request.Dependents,
'Education': request.Education,
'Self_Employed': request.Self_Employed,
'ApplicantIncome': request.ApplicantIncome,
'CoapplicantIncome': request.CoapplicantIncome,
'LoanAmount': request.LoanAmount,
'Loan_Amount_Term': request.Loan_Amount_Term,
'Credit_History': request.Credit_History,
'Property_Area': request.Property_Area
}])
# Scale the required columns
input_df[cols] = scaler.transform(input_df[cols])
try:
prediction = model.predict(input_df)
return {'prediction': int(prediction[0])}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# Run the application
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='0.0.0.0', port=8031)