Add governance and TEA preview REST endpoints
Use the models router for curves and training inputs, the EEG router for summary, and a separate preview router. Keep the preview result typed with the existing persistence model and declare multipart binary schemas for generated Blob parameters.
This commit is contained in:
+94
-6
@@ -4,16 +4,17 @@ scoring runs (Dagster). The API starts runs and reads the registry; it never tra
|
||||
import mimetypes
|
||||
import zipfile
|
||||
from datetime import date
|
||||
from typing import Annotated, Any
|
||||
from pathlib import Path
|
||||
from typing import Annotated, Any, Literal
|
||||
|
||||
from dagster_graphql import DagsterGraphQLClient
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile
|
||||
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
||||
from fastapi.responses import Response
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, Field, WithJsonSchema
|
||||
|
||||
from core import domains
|
||||
from core.domains import Domain
|
||||
from persistance import bundle, cluster, registry
|
||||
from core.domains import Domain, InputSpec
|
||||
from persistance import bundle, cluster, inputs, registry
|
||||
from persistance.cluster import Execution, Reference
|
||||
from webapi.runs import dagster, execution_config, submit
|
||||
|
||||
@@ -27,6 +28,7 @@ class Version(BaseModel):
|
||||
metrics: dict[str, float]
|
||||
params: dict[str, str]
|
||||
reports: list[str]
|
||||
status: Literal["champion", "retired", "candidate"]
|
||||
|
||||
|
||||
@router.get("/{domain}")
|
||||
@@ -131,11 +133,97 @@ def promote(domain: Domain, name: str, version: int) -> dict:
|
||||
reports: technical validation, interpretability and clinical validation."""
|
||||
try:
|
||||
registry.promote(f"{domain.value}.{name}", version)
|
||||
except registry.MissingReports as e:
|
||||
except registry.UnknownVersion as e:
|
||||
raise HTTPException(404, detail=str(e)) from e
|
||||
except ValueError as e:
|
||||
raise HTTPException(409, detail=str(e)) from e
|
||||
return {"champion": version}
|
||||
|
||||
|
||||
@router.get("/{domain}/{name}/versions/{version}/checks")
|
||||
def version_checks(domain: Domain, name: str, version: int) -> dict:
|
||||
registered = f"{domain.value}.{name}"
|
||||
try:
|
||||
status = registry.status(registered, version)
|
||||
except registry.UnknownVersion as e:
|
||||
raise HTTPException(404, detail=str(e)) from e
|
||||
checks = registry.checks(registered, version)
|
||||
return {
|
||||
"version": version,
|
||||
"status": status,
|
||||
"checks": checks,
|
||||
"promotable": status != "retired" and all(check["present"] for check in checks),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/{domain}/{name}/versions/{version}/retire")
|
||||
def retire(domain: Domain, name: str, version: int) -> dict[str, str]:
|
||||
try:
|
||||
registry.retire(f"{domain.value}.{name}", version)
|
||||
except registry.UnknownVersion as e:
|
||||
raise HTTPException(404, detail=str(e)) from e
|
||||
except ValueError as e:
|
||||
raise HTTPException(409, detail=str(e)) from e
|
||||
return {"status": "retired"}
|
||||
|
||||
|
||||
@router.get("/{domain}/{name}/versions/{version}/lineage")
|
||||
def lineage(domain: Domain, name: str, version: int) -> dict:
|
||||
try:
|
||||
return registry.lineage(f"{domain.value}.{name}", version)
|
||||
except registry.UnknownVersion as e:
|
||||
raise HTTPException(404, detail=str(e)) from e
|
||||
|
||||
|
||||
def _training_inputs(domain: Domain, name: str) -> tuple[InputSpec, Path]:
|
||||
declared = getattr(domains.part(domain), "TRAINING_INPUTS", {})
|
||||
spec = declared.get(name, declared.get("*"))
|
||||
if spec is None:
|
||||
raise HTTPException(404, detail=f"no training inputs for {domain.value}.{name}")
|
||||
try:
|
||||
return spec, inputs.root(spec)
|
||||
except LookupError as e:
|
||||
raise HTTPException(503, detail=str(e)) from e
|
||||
|
||||
|
||||
@router.get("/{domain}/{name}/inputs")
|
||||
def training_inputs(domain: Domain, name: str) -> dict:
|
||||
spec, root = _training_inputs(domain, name)
|
||||
return {"description": spec.description, "allowed": spec.allowed, "sets": inputs.sets(root)}
|
||||
|
||||
|
||||
@router.post("/{domain}/{name}/inputs/{set}/files")
|
||||
def upload_training_inputs(
|
||||
domain: Domain,
|
||||
name: str,
|
||||
set: str,
|
||||
files: Annotated[
|
||||
list[Annotated[UploadFile, WithJsonSchema({"type": "string", "format": "binary"})]],
|
||||
File(min_length=1),
|
||||
],
|
||||
) -> dict:
|
||||
spec, root = _training_inputs(domain, name)
|
||||
try:
|
||||
return inputs.upload(root, set, spec.allowed, [(f.filename or "", f.file) for f in files])
|
||||
except ValueError as e:
|
||||
raise HTTPException(422, detail=str(e)) from e
|
||||
|
||||
|
||||
@router.get("/{domain}/{name}/versions/{version}/curve")
|
||||
def curve(domain: Domain, name: str, version: int, feature: str = "value") -> list[dict]:
|
||||
if domain != Domain.TEA:
|
||||
raise HTTPException(404, detail="no age curve for this model")
|
||||
try:
|
||||
model = registry.load(f"{domain.value}.{name}", version).unwrap_python_model()
|
||||
if not hasattr(model, "curve"):
|
||||
raise HTTPException(404, detail="no age curve for this model")
|
||||
return model.curve(feature, range(16, 37))
|
||||
except KeyError as e:
|
||||
raise HTTPException(404, detail=f"no curve feature {feature!r}") from e
|
||||
except LookupError as e:
|
||||
raise HTTPException(404, detail=str(e)) from e
|
||||
|
||||
|
||||
class TrainingForm(BaseModel):
|
||||
as_of: date | None = Field(
|
||||
None, title="Datos hasta", description="Fecha de corte de los datos (por omisión, hoy)"
|
||||
|
||||
Reference in New Issue
Block a user