Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Request, Form | |
| from fastapi.responses import HTMLResponse, JSONResponse | |
| import joblib | |
| import numpy as np | |
| from pydantic import BaseModel | |
| from fastapi.staticfiles import StaticFiles | |
| app = FastAPI() | |
| # Load trained model | |
| clf = joblib.load("loan_prediction_model.pkl") | |
| # Mount static files for Bootstrap and JavaScript | |
| # app.mount("/static", StaticFiles(directory="static"), name="static") | |
| # Define input schema | |
| class LoanApplication(BaseModel): | |
| income: float | |
| age: int | |
| experience: int | |
| marital_status: int | |
| house_ownership: int | |
| car_ownership: int | |
| profession: int | |
| city: int | |
| current_job_years: int | |
| current_house_years: int | |
| # Serve HTML UI at root ("/") | |
| async def serve_home(): | |
| with open("index.html", "r", encoding="utf-8") as file: | |
| return HTMLResponse(content=file.read()) | |
| # API endpoint to predict risk | |
| async def predict_risk(data: LoanApplication): | |
| input_data = np.array([[data.income, data.age, data.experience, data.marital_status, | |
| data.house_ownership, data.car_ownership, data.profession, | |
| data.city, data.current_job_years, data.current_house_years]]) | |
| prediction = clf.predict(input_data)[0] | |
| return JSONResponse(content={"prediction": int(prediction)}) | |