Skip to content

Commit 0b8d5f2

Browse files
authored
Merge pull request #131 from datakind/develop
model cards optimization
2 parents d31c826 + 915e433 commit 0b8d5f2

1 file changed

Lines changed: 17 additions & 29 deletions

File tree

src/webapp/routers/data.py

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import uuid
44
from datetime import datetime, date
55
from databricks.sdk import WorkspaceClient
6-
from typing import Annotated, Any, Dict, List
6+
from typing import Annotated, Any, Dict, List, cast, IO
77
from pydantic import BaseModel
88
from fastapi import APIRouter, Depends, HTTPException, status, Response
99
from fastapi.responses import FileResponse
@@ -1372,34 +1372,22 @@ def get_model_cards(
13721372
f"get_model_cards(): Workspace client initialization failed: {e}"
13731373
)
13741374

1375-
host = w.config.host # e.g. "https://12345.gcp.databricks.com"
1376-
1377-
# 2. Build the MLflow REST endpoint URL and params
1378-
download_endpoint = f"{host}/api/2.0/mlflow/artifacts/download"
1379-
artifact_path = f"model_card/model-card-{model_name}.pdf"
1380-
params = {"run_id": run_id, "path": artifact_path}
1381-
1382-
# 3. Let WorkspaceClient’s ApiClient perform the authenticated GET
13831375
try:
1384-
# perform_query will attach the same OAuth creds that WorkspaceClient uses
1385-
resp = w.api_client.perform_query( # type: ignore[attr-defined]
1386-
method="GET",
1387-
path=download_endpoint,
1388-
query_params=params,
1389-
) # type: ignore[attr-defined]
1390-
# resp here is the raw bytes of the PDF
1376+
volume_path = f"/Volumes/staging_sst_01/{query_result[0][0].name}_gold/gold_volume/model-card-{model_name}.pdf"
1377+
response = w.files.download(volume_path)
1378+
stream = cast(IO[bytes], response.contents)
1379+
pdf_bytes = stream.read()
1380+
13911381
except Exception as e:
1392-
raise HTTPException(
1393-
status_code=500,
1394-
detail=f"Could not download model card via MLflow REST API: {e}",
1395-
)
1382+
raise HTTPException(500, detail=f"Failed to fetch model card: {e}")
13961383

1397-
# 4. Write to a temp file and return it
1398-
with tempfile.TemporaryDirectory() as td:
1399-
out_path = pathlib.Path(td) / f"model-card-{model_name}.pdf"
1400-
out_path.write_bytes(resp)
1401-
return FileResponse(
1402-
path=str(out_path),
1403-
filename=out_path.name,
1404-
media_type="application/pdf",
1405-
)
1384+
# Stream back as FileResponse
1385+
tmp = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False)
1386+
tmp.write(pdf_bytes)
1387+
tmp.flush()
1388+
1389+
return FileResponse(
1390+
tmp.name,
1391+
filename=pathlib.Path(tmp.name).name,
1392+
media_type="application/pdf",
1393+
)

0 commit comments

Comments
 (0)