Skip to content

Commit

Permalink
Add predict insurance fee api
Browse files Browse the repository at this point in the history
parameter๋ฅผ ๋ฐ›์•„์„œ predict ํ•˜๋Š” api์ž…๋‹ˆ๋‹ค.
์ž„์‹œ๋กœ preprocess ๊ณผ์ •์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ณ  ์ง„ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด
ํ”„๋ก ํŠธ์—์„œ ์ „์ฒ˜๋ฆฌํ•˜์—ฌ ๊ฐ’์„ ๋„˜๊ธด๋‹ค๋Š” ๊ฐ€์ •์œผ๋กœ
๋ฏธ๋ฆฌ ์ „์ฒ˜๋ฆฌ๋œ ๊ฐ’์„ ๋„ฃ๋„๋ก ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์„ธํŒ…ํ–ˆ์Šต๋‹ˆ๋‹ค.
์ถ”ํ›„ ์ˆ˜์ •๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.
  • Loading branch information
ehddnr301 committed Sep 7, 2021
1 parent 4319cff commit f769af8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
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]}

This comment has been minimized.

Copy link
@Ilevk

Ilevk Sep 7, 2021

55๋ฒˆ์งธ ๋ผ์ธ๋ถ€ํ„ฐ ์—ฌ๊ธฐ๊นŒ์ง€ ํ•จ์ˆ˜๋กœ ๋”ฐ๋กœ ๋นผ์‹œ๊ณ , ๋น„๋™๊ธฐ ํ˜ธ์ถœ๋กœ ๋˜๋Œ๋ ค ๋ฐ›์œผ์„ธ์š”.
return {"result": await run_in_threadpool(predict_insurance, info, db) }

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

0 comments on commit f769af8

Please sign in to comment.