Skip to content

Commit

Permalink
Merge pull request #17 from State-of-The-MLOps/feature/logging
Browse files Browse the repository at this point in the history
Feature/logging
  • Loading branch information
ehddnr301 authored Sep 13, 2021
2 parents f9cc6c5 + 3241725 commit 439715d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.env
*.pkl
__pycache__
tf_model/**/*
tf_model/**/*
log.txt
22 changes: 15 additions & 7 deletions app/api/router/predict.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# -*- coding: utf-8 -*-
import numpy as np
from typing import List

import numpy as np
from fastapi import APIRouter
from starlette.concurrency import run_in_threadpool

from app import models
from app.api.schemas import ModelCorePrediction
from app.database import engine
from app.utils import ScikitLearnModel, my_model
from logger import L


models.Base.metadata.create_all(bind=engine)
Expand Down Expand Up @@ -38,7 +39,6 @@ async def predict_insurance(info: ModelCorePrediction, model_name: str):
return:
insurance_fee: float
"""

def sync_call(info, model_name):
model = ScikitLearnModel(model_name)
model.load_model()
Expand All @@ -47,25 +47,33 @@ def sync_call(info, model_name):
test_set = np.array([*info.values()]).reshape(1, -1)

pred = model.predict_target(test_set)

return {"result": pred.tolist()[0]}
try:
result = await run_in_threadpool(sync_call, info, model_name)
L.info(
f"Predict Args info: {info}\n\tmodel_name: {model_name}\n\tPrediction Result: {result}")
return result

result = await run_in_threadpool(sync_call, info, model_name)

return result
except Exception as e:
L.error(e)
return {'error': str(e)}


@router.put("/atmos")
async def predict_temperature(time_series: List[float]):
if len(time_series) != 72:
L.error(f'input time_series: {time_series} is not valid')
return "time series must have 72 values"

try:
tf_model = my_model.model
time_series = np.array(time_series).reshape(1, -1, 1)
result = tf_model.predict(time_series)

L.info(
f"Predict Args info: {time_series.flatten().tolist()}\n\tmodel_name: {tf_model}\n\tPrediction Result: {result.tolist()[0]}")
return result.tolist()

except Exception as e:
print(e)
L.error(e)
return {"error": str(e)}
8 changes: 6 additions & 2 deletions app/api/router/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi import APIRouter

from app.utils import write_yml
from logger import L


router = APIRouter(
Expand Down Expand Up @@ -33,8 +34,11 @@ def train_insurance(
Returns:
msg: Regardless of success or not, return address values including PORT.
"""
L.info(
f"Train Args info\n\texperiment_sec: {experiment_sec}\n\texperiment_name: {experiment_name}\n\texperimenter: {experimenter}\n\tmodel_name: {model_name}\n\tversion: {version}")
path = 'experiments/insurance/'
try:
L.info("Start NNi")
write_yml(
path,
experiment_name,
Expand All @@ -49,7 +53,7 @@ def train_insurance(
)

except Exception as e:
print('error')
print(e)
L.error(e)
return {'error': str(e)}

return {"msg": f'Check out http://127.0.0.1:{PORT}'}
19 changes: 19 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging
import logging.handlers

L = logging.getLogger('snowdeer_log')
L.setLevel(logging.DEBUG)

formatter = logging.Formatter(
fmt="%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)d - %(funcName)20s()]\n\t%(message)s",
datefmt='%y-%m-%d %H:%M:%S'
)

fileHandler = logging.FileHandler('./log.txt')
streamHandler = logging.StreamHandler()

fileHandler.setFormatter(formatter)
streamHandler.setFormatter(formatter)

L.addHandler(fileHandler)
L.addHandler(streamHandler)

0 comments on commit 439715d

Please sign in to comment.