File size: 750 Bytes
c9f187d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""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()