Spaces:
Build error
Build error
| from fastapi import FastAPI | |
| from fastapi.responses import JSONResponse | |
| from schema.user_input import UserInput | |
| from schema.prediction_response import PredictionResponse | |
| from model.predict import predict_output, model, MODEL_VERSION | |
| import uvicorn | |
| app = FastAPI() | |
| # human readable | |
| def home(): | |
| return {'message':'Insurance Premium Prediction API'} | |
| # machine readable | |
| def health_check(): | |
| return { | |
| 'status': 'OK', | |
| 'version': MODEL_VERSION, | |
| 'model_loaded': model is not None | |
| } | |
| def predict_premium(data: UserInput): | |
| user_input = { | |
| 'bmi': data.bmi, | |
| 'age_group': data.age_group, | |
| 'lifestyle_risk': data.lifestyle_risk, | |
| 'city_tier': data.city_tier, | |
| 'income_lpa': data.income_lpa, | |
| 'occupation': data.occupation | |
| } | |
| try: | |
| prediction = predict_output(user_input) | |
| return JSONResponse(status_code=200, content={'response': prediction}) | |
| except Exception as e: | |
| return JSONResponse(status_code=500, content=str(e)) | |
| if __name__ == "__main__": | |
| """ | |
| Run the FastAPI app using uvicorn. | |
| """ | |
| uvicorn.run(app, host="127.0.0.1", port=8000, reload=True) | |
| # To run the app, use the command: uvicorn app:app --reload |