|
3 | 3 | import uuid |
4 | 4 | from datetime import datetime, date |
5 | 5 | from databricks.sdk import WorkspaceClient |
6 | | -from typing import Annotated, Any, Dict, List |
| 6 | +from typing import Annotated, Any, Dict, List, cast, IO |
7 | 7 | from pydantic import BaseModel |
8 | 8 | from fastapi import APIRouter, Depends, HTTPException, status, Response |
9 | 9 | from fastapi.responses import FileResponse |
@@ -1372,34 +1372,22 @@ def get_model_cards( |
1372 | 1372 | f"get_model_cards(): Workspace client initialization failed: {e}" |
1373 | 1373 | ) |
1374 | 1374 |
|
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 |
1383 | 1375 | 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 | + |
1391 | 1381 | 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}") |
1396 | 1383 |
|
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