Generic model bundles: export and import: export and import endpoints
This commit is contained in:
+63
-2
@@ -2,15 +2,17 @@
|
||||
scoring runs (Dagster). The API starts runs and reads the registry; it never trains."""
|
||||
|
||||
import mimetypes
|
||||
import zipfile
|
||||
from datetime import date
|
||||
from typing import Annotated
|
||||
|
||||
from dagster_graphql import DagsterGraphQLClient
|
||||
from fastapi import APIRouter, Depends, HTTPException, UploadFile
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query, UploadFile
|
||||
from fastapi.responses import Response
|
||||
from pydantic import BaseModel
|
||||
|
||||
from core.domains import Domain
|
||||
from persistance import registry
|
||||
from persistance import bundle, registry
|
||||
from persistance.cluster import Execution
|
||||
from webapi.runs import dagster, execution_config, submit
|
||||
|
||||
@@ -32,6 +34,65 @@ def models(domain: Domain) -> list[str]:
|
||||
return [n.removeprefix(prefix) for n in registry.names(prefix)]
|
||||
|
||||
|
||||
@router.get(
|
||||
"/{domain}/export",
|
||||
responses={200: {"content": {"application/zip": {}}}},
|
||||
response_class=Response,
|
||||
)
|
||||
def export_models(
|
||||
domain: Domain,
|
||||
names: Annotated[list[str] | None, Query()] = None,
|
||||
versions: bundle.Selection = "champion",
|
||||
) -> Response:
|
||||
try:
|
||||
exported = bundle.export(domain, names, versions)
|
||||
except bundle.Empty as e:
|
||||
raise HTTPException(409, detail=str(e)) from e
|
||||
except bundle.BundleError as e:
|
||||
raise HTTPException(422, detail=str(e)) from e
|
||||
return Response(
|
||||
exported.data,
|
||||
media_type="application/zip",
|
||||
headers={
|
||||
"X-Bundle-Sha256": exported.sha256,
|
||||
"X-Bundle-Versions": ",".join(f"{name}/{v}" for name, v in exported.versions),
|
||||
"Content-Disposition": (
|
||||
f'attachment; filename="{domain}-models-{exported.sha256[:12]}.zip"'
|
||||
),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{domain}/import")
|
||||
def import_models(
|
||||
domain: Domain,
|
||||
sha256: Annotated[str, Query(pattern=r"^[0-9a-fA-F]{64}$")],
|
||||
file: UploadFile,
|
||||
client: DagsterGraphQLClient = Depends(dagster),
|
||||
) -> dict[str, str]:
|
||||
computed, path = bundle.stage(file.file)
|
||||
if computed != sha256.lower():
|
||||
path.unlink(missing_ok=True)
|
||||
raise HTTPException(422, detail=f"the file's SHA-256 is {computed}, not the one given")
|
||||
if not zipfile.is_zipfile(path):
|
||||
path.unlink(missing_ok=True)
|
||||
raise HTTPException(422, detail="not a bundle")
|
||||
if not bundle.trusted(computed):
|
||||
path.unlink(missing_ok=True)
|
||||
raise HTTPException(
|
||||
403,
|
||||
detail=f"bundle {computed} is not trusted on this server: "
|
||||
f"the operator adds its SHA-256 to {bundle.trusted_file()}",
|
||||
)
|
||||
return submit(
|
||||
client,
|
||||
"import_bundle",
|
||||
"import_bundle_files",
|
||||
{"domain": domain.value, "sha256": computed},
|
||||
domain.value,
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{domain}/{name}/versions")
|
||||
def versions(domain: Domain, name: str) -> list[Version]:
|
||||
if not (found := registry.versions(f"{domain.value}.{name}")):
|
||||
|
||||
Reference in New Issue
Block a user