FHIR, follow-up lists and model jobs by domain; CaMa gate; report download for governance

This commit is contained in:
Roberto Treviño
2026-11-25 23:20:52 -06:00
parent 806302ee5d
commit 26cf994d1a
5 changed files with 311 additions and 74 deletions
+23 -8
View File
@@ -1,10 +1,12 @@
"""Model governance: versions and their reports (MLflow), promotion, and the training and
scoring runs (Dagster). The API starts runs and reads the registry; it never trains."""
import mimetypes
from datetime import date
from dagster_graphql import DagsterGraphQLClient, DagsterGraphQLClientError
from fastapi import APIRouter, Depends, HTTPException, UploadFile
from fastapi.responses import Response
from pydantic import BaseModel
from core.domains import Domain
@@ -12,7 +14,6 @@ from persistance import registry
from webapi.runs import dagster
router = APIRouter(prefix="/models", tags=["models"])
JOBS = {Domain.CACU: ("train_cacu", "score_cacu")}
class Version(BaseModel):
@@ -37,6 +38,22 @@ def versions(domain: Domain, name: str) -> list[Version]:
return [Version(**v) for v in found]
@router.get("/{domain}/{name}/versions/{version}/reports/{report}",
responses={200: {"content": {"application/json": {}, "application/octet-stream": {}},
"description": "the report as stored"}},
response_class=Response) # fmt: skip
def read_report(domain: Domain, name: str, version: int, report: str) -> Response:
"""A version's report: the generated ones as JSON (what the governance page draws), the
clinical validation as the file the clinical team uploaded."""
found = registry.report(f"{domain.value}.{name}", version, report)
if found is None:
raise HTTPException(404, detail=f"{domain.value}.{name} v{version} has no {report}")
filename, content = found
kind = mimetypes.guess_type(filename)[0] or "application/octet-stream"
return Response(content, media_type=kind,
headers={"Content-Disposition": f'inline; filename="{filename}"'}) # fmt: skip
@router.post("/{domain}/{name}/versions/{version}/reports/clinical_validation")
def add_clinical_validation(domain: Domain, name: str, version: int, file: UploadFile) -> dict:
"""The clinical team's validation of a version: the third report promotion needs."""
@@ -72,15 +89,13 @@ def _submit(client: DagsterGraphQLClient, job: str, op: str, config: dict) -> di
def train(domain: Domain, request: Training,
client: DagsterGraphQLClient = Depends(dagster)) -> dict[str, str]: # fmt: skip
"""Starts training a new version of every model of the domain; follow it at /runs."""
if domain not in JOBS:
raise HTTPException(404, detail=f"{domain.value} has no models yet")
config = {"as_of": request.as_of.isoformat()} if request.as_of else {}
return _submit(client, JOBS[domain][0], "train_models", config)
config = {"domain": domain.value}
if request.as_of:
config["as_of"] = request.as_of.isoformat()
return _submit(client, "train", "train_models", config)
@router.post("/{domain}/score")
def score(domain: Domain, client: DagsterGraphQLClient = Depends(dagster)) -> dict[str, str]:
"""Starts a risk batch from each model's champion; follow it at /runs."""
if domain not in JOBS:
raise HTTPException(404, detail=f"{domain.value} has no models yet")
return _submit(client, JOBS[domain][1], "score_models", {})
return _submit(client, "score", "score_models", {"domain": domain.value})