ISSSTELEON WebApi
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
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
|
||||
from core.models import title
|
||||
from persistance import registry
|
||||
from webapi.runs import dagster
|
||||
|
||||
router = APIRouter(prefix="/models", tags=["models"])
|
||||
|
||||
|
||||
class Version(BaseModel):
|
||||
version: int
|
||||
created: int
|
||||
aliases: list[str]
|
||||
metrics: dict[str, float]
|
||||
params: dict[str, str]
|
||||
reports: list[str]
|
||||
|
||||
|
||||
class Model(BaseModel):
|
||||
name: str
|
||||
title: str
|
||||
|
||||
|
||||
@router.get("/{domain}")
|
||||
def models(domain: Domain) -> list[Model]:
|
||||
prefix = f"{domain.value}."
|
||||
return [Model(name=n.removeprefix(prefix), title=title(n)) for n in registry.names(prefix)]
|
||||
|
||||
|
||||
@router.get("/{domain}/{name}/versions")
|
||||
def versions(domain: Domain, name: str) -> list[Version]:
|
||||
if not (found := registry.versions(f"{domain.value}.{name}")):
|
||||
raise HTTPException(404, detail=f"no {domain.value} model {name}")
|
||||
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,
|
||||
)
|
||||
def read_report(domain: Domain, name: str, version: int, report: str) -> Response:
|
||||
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}"'}
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{domain}/{name}/versions/{version}/reports/clinical_validation")
|
||||
def add_clinical_validation(domain: Domain, name: str, version: int, file: UploadFile) -> dict:
|
||||
registry.add_report(
|
||||
f"{domain.value}.{name}",
|
||||
version,
|
||||
"clinical_validation",
|
||||
file.filename or "report",
|
||||
file.file.read(),
|
||||
)
|
||||
return {"reports": sorted(registry.reports(f"{domain.value}.{name}", version))}
|
||||
|
||||
|
||||
@router.post("/{domain}/{name}/versions/{version}/promote")
|
||||
def promote(domain: Domain, name: str, version: int) -> dict:
|
||||
try:
|
||||
registry.promote(f"{domain.value}.{name}", version)
|
||||
except registry.MissingReports as e:
|
||||
raise HTTPException(409, detail=str(e)) from e
|
||||
return {"champion": version}
|
||||
|
||||
|
||||
class Training(BaseModel):
|
||||
as_of: date | None = None
|
||||
models: list[str] = []
|
||||
|
||||
|
||||
def _submit(client: DagsterGraphQLClient, job: str, op: str, config: dict) -> dict[str, str]:
|
||||
try:
|
||||
run_id = client.submit_job_execution(job, run_config={"ops": {op: {"config": config}}})
|
||||
except DagsterGraphQLClientError as e:
|
||||
raise HTTPException(502, detail=str(e)) from e
|
||||
return {"run_id": run_id}
|
||||
|
||||
|
||||
@router.post("/{domain}/train")
|
||||
def train(
|
||||
domain: Domain, request: Training, client: DagsterGraphQLClient = Depends(dagster)
|
||||
) -> dict[str, str]:
|
||||
config: dict = {"domain": domain.value}
|
||||
if request.as_of:
|
||||
config["as_of"] = request.as_of.isoformat()
|
||||
if request.models:
|
||||
config["models"] = request.models
|
||||
return _submit(client, "train", "train_models", config)
|
||||
|
||||
|
||||
@router.post("/{domain}/analyze")
|
||||
def analyze(domain: Domain, client: DagsterGraphQLClient = Depends(dagster)) -> dict[str, str]:
|
||||
return _submit(client, "analyze", "analyze_studies", {"domain": domain.value})
|
||||
|
||||
|
||||
@router.post("/{domain}/score")
|
||||
def score(domain: Domain, client: DagsterGraphQLClient = Depends(dagster)) -> dict[str, str]:
|
||||
return _submit(client, "score", "score_models", {"domain": domain.value})
|
||||
Reference in New Issue
Block a user