File tree Expand file tree Collapse file tree 11 files changed +109
-6
lines changed
{{cookiecutter.project_slug}} Expand file tree Collapse file tree 11 files changed +109
-6
lines changed Original file line number Diff line number Diff line change 1+ - Always run ` pytest {{cookiecutter.project_slug}}/tests ` and ensure all tests pass after any code changes.
2+ - Run tests even if not explicitly requested.
Original file line number Diff line number Diff line change @@ -3,3 +3,4 @@ DEBUG=True
33MODEL_PATH = {{cookiecutter.machine_learn_model_path}}
44MODEL_NAME = {{cookiecutter.machine_learn_model_name}}
55MEMOIZATION_FLAG = False
6+ DATABASE_URL = postgresql://postgres:postgres@db:5432/app
Original file line number Diff line number Diff line change 55from core .config import INPUT_EXAMPLE
66from fastapi import APIRouter , HTTPException
77from fastapi .concurrency import run_in_threadpool
8+ from loguru import logger
9+ from db import SessionLocal
10+ from models .log import RequestLog
811from models .prediction import (
912 HealthResponse ,
1013 MachineLearningDataInput ,
@@ -44,10 +47,24 @@ async def predict(data_input: MachineLearningDataInput):
4447 except Exception as err :
4548 raise HTTPException (status_code = 500 , detail = f"Exception: { err } " ) from err
4649
47- return MachineLearningResponse (
50+ response = MachineLearningResponse (
4851 prediction = prediction , prediction_label = prediction_label
4952 )
5053
54+ try :
55+ with SessionLocal () as db :
56+ db .add (
57+ RequestLog (
58+ request = json .dumps (data_input .model_dump ()),
59+ response = json .dumps (response .model_dump ()),
60+ )
61+ )
62+ db .commit ()
63+ except Exception :
64+ logger .exception ("failed to log request" )
65+
66+ return response
67+
5168
5269@router .get (
5370 "/health" ,
Original file line number Diff line number Diff line change 1515MIN_CONNECTIONS_COUNT : int = config ("MIN_CONNECTIONS_COUNT" , cast = int , default = 10 )
1616SECRET_KEY : Secret = config ("SECRET_KEY" , cast = Secret , default = "" )
1717MEMOIZATION_FLAG : bool = config ("MEMOIZATION_FLAG" , cast = bool , default = True )
18+ DATABASE_URL : str = config ("DATABASE_URL" , default = "sqlite:///./app.db" )
1819
1920PROJECT_NAME : str = config ("PROJECT_NAME" , default = "{{cookiecutter.project_name}}" )
2021
Original file line number Diff line number Diff line change 22
33import joblib
44from fastapi import FastAPI
5+ from core .config import MEMOIZATION_FLAG
6+ from db import Base , engine
57
68
79def preload_model ():
@@ -15,6 +17,8 @@ def preload_model():
1517
1618def create_start_app_handler (app : FastAPI ) -> Callable :
1719 def start_app () -> None :
18- preload_model ()
20+ if MEMOIZATION_FLAG :
21+ preload_model ()
22+ Base .metadata .create_all (bind = engine )
1923
2024 return start_app
Original file line number Diff line number Diff line change 1+ from sqlalchemy import create_engine
2+ from sqlalchemy .orm import sessionmaker , declarative_base
3+
4+ from core .config import DATABASE_URL
5+
6+ engine = create_engine (DATABASE_URL )
7+ SessionLocal = sessionmaker (autocommit = False , autoflush = False , bind = engine )
8+ Base = declarative_base ()
Original file line number Diff line number Diff line change 77def get_application () -> FastAPI :
88 application = FastAPI (title = PROJECT_NAME , debug = DEBUG , version = VERSION )
99 application .include_router (api_router , prefix = API_PREFIX )
10- if MEMOIZATION_FLAG :
11- application .add_event_handler ("startup" , create_start_app_handler (application ))
10+ application .add_event_handler ("startup" , create_start_app_handler (application ))
1211 return application
1312
1413
Original file line number Diff line number Diff line change 1+ from sqlalchemy import Column , Integer , Text
2+
3+ from db import Base
4+
5+
6+ class RequestLog (Base ):
7+ __tablename__ = "request_logs"
8+
9+ id = Column (Integer , primary_key = True , index = True )
10+ request = Column (Text , nullable = False )
11+ response = Column (Text , nullable = False )
Original file line number Diff line number Diff line change @@ -13,4 +13,19 @@ services:
1313 command : uvicorn main:app --reload --host 0.0.0.0 --port 8080
1414 volumes :
1515 - ./app:/app/
16- - ./ml/model/:/app/ml/model/
16+ - ./ml/model/:/app/ml/model/
17+ depends_on :
18+ - db
19+ db :
20+ image : postgres:16
21+ environment :
22+ POSTGRES_USER : postgres
23+ POSTGRES_PASSWORD : postgres
24+ POSTGRES_DB : app
25+ ports :
26+ - " 5432:5432"
27+ volumes :
28+ - postgres_data:/var/lib/postgresql/data
29+
30+ volumes :
31+ postgres_data :
Original file line number Diff line number Diff line change @@ -15,7 +15,9 @@ dependencies = [
1515 " joblib>=1.2.0" ,
1616 " scikit-learn>=1.1.3" ,
1717 " pandas>=2.2.3" ,
18- " httpx>=0.27.0"
18+ " httpx>=0.27.0" ,
19+ " sqlalchemy>=2.0.0" ,
20+ " psycopg2-binary>=2.9.0"
1921]
2022
2123[project .optional-dependencies ]
You can’t perform that action at this time.
0 commit comments