File size: 2,172 Bytes
79b0bef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
src/api/routes/model.py
────────────────────────────────────────────────────────────────
Endpoints for classifier training run metadata.

Routes
──────
  GET /model/metrics   β€” metrics for the currently deployed classifier
────────────────────────────────────────────────────────────────
"""

from __future__ import annotations

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from src.api.schemas import ModelMetricsResponse
from src.db.connection import get_db_session
from src.db.repository import ModelRunRepository
from src.utils.config import ClassifierConfig
from src.utils.logger import get_logger

logger = get_logger(__name__)
router = APIRouter(prefix="/model", tags=["Model"])


@router.get(
    "/metrics",
    response_model = ModelMetricsResponse,
    summary        = "Metrics for the currently deployed severity classifier",
)
def get_model_metrics(
    task: str       = ClassifierConfig.task,
    db:   Session    = Depends(get_db_session),
) -> ModelMetricsResponse:
    """Return training metrics for the currently deployed classifier run.

    Args:
        task: Classification task to look up (default: the globally
            configured task, currently "severity").

    Returns:
        :class:`ModelMetricsResponse` for the deployed run.

    Raises:
        HTTPException: 404 if no run has been recorded for this task
            yet (e.g. before the first `train_severity_classifier.py`
            run since this feature was added).
    """
    run = ModelRunRepository.get_deployed(db, task)
    if run is None:
        raise HTTPException(
            status_code = 404,
            detail = (
                f"No deployed model run recorded for task={task!r} yet. "
                "Run scripts/train_severity_classifier.py to populate this."
            ),
        )
    return ModelMetricsResponse.model_validate(run)