|
14 | 14 | import logging |
15 | 15 | from sqlalchemy.exc import IntegrityError |
16 | 16 | from ..config import databricks_vars, env_vars, gcs_vars |
| 17 | +from mlflow.exceptions import MlflowException |
17 | 18 | import tempfile |
18 | 19 | import pathlib |
19 | 20 |
|
@@ -1372,34 +1373,17 @@ def get_model_cards( |
1372 | 1373 | f"get_model_cards(): Workspace client initialization failed: {e}" |
1373 | 1374 | ) |
1374 | 1375 |
|
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 | 1376 | 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 |
| 1377 | + volume_path = f"/Volumes/staging_sst_01/{query_result[0][0].name}_gold/gold_volume/model-card-{model_name}.pdf" |
| 1378 | + with w.files.download(volume_path) as stream: |
| 1379 | + pdf_bytes = stream.read() |
1391 | 1380 | 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 | | - ) |
1396 | | - |
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 | | - ) |
| 1381 | + raise HTTPException(500, detail=f"Failed to fetch model card: {e}") |
| 1382 | + |
| 1383 | + # Stream back as FileResponse |
| 1384 | + tmp = tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) |
| 1385 | + tmp.write(pdf_bytes) |
| 1386 | + tmp.flush() |
| 1387 | + return FileResponse( |
| 1388 | + tmp.name, filename=tmp.name.split("/")[-1], media_type="application/pdf" |
| 1389 | + ) |
0 commit comments