Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add predict insurance fee api #6

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions app/api/router/predict.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# -*- coding: utf-8 -*-
from fastapi import APIRouter
import os
import pickle
import numpy as np
from fastapi.param_functions import Depends
from app.api.schemas import RegModelPrediction
from fastapi import APIRouter, HTTPException
from sqlalchemy.orm import Session

from app import models
from app import crud, models
from app.database import engine
from app.database import SessionLocal

Expand All @@ -27,3 +33,47 @@ def get_db():
@router.get("/")
def hello_world():
return {"message": "Hello predict"}


@router.put("/insurance")
def predict_insurance(info: RegModelPrediction, db: Session = Depends(get_db)):
"""
Get information and predict insurance fee
param:
info:
# 임시로 int형태를 받도록 제작
# preprocess 단계를 거치도록 만들 예정
age: int
sex: int
bmi: float
children: int
smoker: int
region: int
return:
insurance_fee: float
"""
reg_model = crud.get_reg_model(db, model_name=info.model_name)

if reg_model:
loaded_model = pickle.load(open(
os.path.join(reg_model.path, f'{reg_model.model_name}.pkl'),
'rb')
)
test_set = np.array([
info.age,
info.sex,
info.bmi,
info.children,
info.smoker,
info.region
]).reshape(1, -1)

pred = loaded_model.predict(test_set)

return {"result": pred.tolist()[0]}
else:
raise HTTPException(
status_code=404,
detail="Model Not Found",
headers={"X-Error": "Model Not Found"},
)
18 changes: 18 additions & 0 deletions app/api/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ class ClfModelCreate(ClfModelBase):
class ClfModel(ClfModelBase):
class Config:
orm_mode = True


class RegModelBase(BaseModel):
model_name: str


class RegModelPrediction(RegModelBase):
age: int
sex: int
bmi: float
children: int
smoker: int
region: int


class RegModel(RegModelBase):
class Config:
orm_mode = True
6 changes: 6 additions & 0 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ def create_clf_model(db: Session, clf_model: schemas.ClfModelCreate):
db.commit()
db.refresh(db_cf_model)
return db_cf_model


def get_reg_model(db: Session, model_name: schemas.RegModelBase):
return db.query(models.RegModel).filter(
models.RegModel.model_name == model_name
).first()
2 changes: 1 addition & 1 deletion app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class RegModel(Base):
path = Column(String, nullable=False)

model_metadata = relationship(
"reg_model_metadata", backref="reg_model.model_name")
"RegModelMetadata", backref="reg_model.model_name")


class RegModelMetadata(Base):
Expand Down