2323 get_session ,
2424 local_session ,
2525 InstTable ,
26+ ModelTable ,
27+ JobTable ,
2628)
2729
2830from ..databricks import DatabricksControl
@@ -424,11 +426,11 @@ def get_model_cards(
424426) -> FileResponse :
425427 has_access_to_inst_or_err (inst_id , current_user )
426428 local_session .set (sql_session )
427- query_result = (
428- local_session . get ()
429- . execute ( select (InstTable ).where (InstTable .id == str_to_uuid (inst_id ) ))
430- .all ()
431- )
429+ session = local_session . get ()
430+ query_result = session . execute (
431+ select (InstTable ).where (InstTable .id == str_to_uuid (inst_id ))
432+ ) .all ()
433+
432434 if not query_result or len (query_result ) == 0 :
433435 raise HTTPException (
434436 status_code = status .HTTP_404_NOT_FOUND ,
@@ -440,6 +442,22 @@ def get_model_cards(
440442 detail = "Institution duplicates found." ,
441443 )
442444
445+ job_result = session .scalars (
446+ select (JobTable )
447+ .join (ModelTable , JobTable .model_id == ModelTable .id )
448+ .where (
449+ ModelTable .name == model_name ,
450+ ModelTable .inst_id == str_to_uuid (inst_id ),
451+ )
452+ ).first ()
453+
454+ if job_result is None or not job_result .model_run_id :
455+ raise HTTPException (
456+ status_code = 404 , detail = "No model run found for this model."
457+ )
458+
459+ run_id = job_result .model_run_id
460+
443461 try :
444462 w = WorkspaceClient (
445463 host = databricks_vars ["DATABRICKS_HOST_URL" ],
@@ -466,7 +484,7 @@ def get_model_cards(
466484 )
467485 env_schema = SCHEMAS [env ]
468486
469- volume_path = f"/Volumes/{ env_schema } /{ databricksify_inst_name (query_result [0 ][0 ].name )} _gold/gold_volume/model_cards/model-card-{ model_name } .pdf"
487+ volume_path = f"/Volumes/{ env_schema } /{ databricksify_inst_name (query_result [0 ][0 ].name )} _gold/gold_volume/model_cards/{ run_id } / model-card-{ model_name } .pdf"
470488 LOGGER .info (f"Attempting to download from { volume_path } " )
471489 response = w .files .download (volume_path )
472490 stream = cast (IO [bytes ], response .contents )
0 commit comments