Amit-kr26's picture
HF Spaces deployment
c9f187d
Raw
History Blame Contribute Delete
750 Bytes
"""FastAPI dependency injection: DB sessions and model loading."""
from functools import lru_cache
from typing import Generator
import joblib
from sqlalchemy.orm import Session
from customer_intelligence.config import settings
from customer_intelligence.db import SessionLocal
def get_db() -> Generator[Session, None, None]:
db = SessionLocal()
try:
yield db
finally:
db.close()
@lru_cache(maxsize=1)
def _load_churn_model():
return joblib.load(settings.churn_model_path)
@lru_cache(maxsize=1)
def _load_segmentation_model():
return joblib.load(settings.segmentation_model_path)
def get_churn_model():
return _load_churn_model()
def get_segmentation_model():
return _load_segmentation_model()