Skip to content

Commit 9b78da3

Browse files
committed
Feature: Update model card on model package request
1 parent c6981e2 commit 9b78da3

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/sagemaker/model.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
ModelCard,
4949
ModelPackageModelCard,
5050
)
51+
from sagemaker.model_card.helpers import _hash_content_str
5152
from sagemaker.model_card.schema_constraints import ModelApprovalStatusEnum
5253
from sagemaker.session import Session
5354
from sagemaker.model_metrics import ModelMetrics
@@ -2393,3 +2394,44 @@ def add_inference_specification(
23932394
)
23942395

23952396
sagemaker_session.sagemaker_client.update_model_package(**model_package_update_args)
2397+
2398+
def update_model_card(self, model_card: ModelCard | ModelPackageModelCard):
2399+
"""Updates Created model card content which created with model package
2400+
2401+
Args:
2402+
model_card (ModelCard | ModelPackageModelCard): Updated Model Card content
2403+
"""
2404+
2405+
sagemaker_session = self.sagemaker_session or sagemaker.Session()
2406+
desc_model_package = sagemaker_session.sagemaker_client.describe_model_package(
2407+
ModelPackageName=self.model_package_arn
2408+
)
2409+
update_model_card_req = model_card._create_request_args()
2410+
if update_model_card_req["ModelCardStatus"] is not None:
2411+
if (
2412+
desc_model_package["ModelCard"]["ModelCardStatus"]
2413+
== update_model_card_req["ModelCardStatus"]
2414+
):
2415+
del update_model_card_req["ModelCardStatus"]
2416+
2417+
if update_model_card_req.get("ModelCardName") is not None:
2418+
del update_model_card_req["ModelCardName"]
2419+
if update_model_card_req.get("Content") is not None:
2420+
previous_content_hash = _hash_content_str(
2421+
desc_model_package["ModelCard"]["ModelCardContent"]
2422+
)
2423+
current_content_hash = _hash_content_str(update_model_card_req["Content"])
2424+
if (
2425+
previous_content_hash == current_content_hash
2426+
or update_model_card_req.get("Content") == "{}"
2427+
or update_model_card_req.get("Content") == "null"
2428+
):
2429+
del update_model_card_req["Content"]
2430+
else:
2431+
update_model_card_req["ModelCardContent"] = update_model_card_req["Content"]
2432+
del update_model_card_req["Content"]
2433+
update_model_card_args = {
2434+
"ModelPackageArn": self.model_package_arn,
2435+
"ModelCard": update_model_card_req,
2436+
}
2437+
sagemaker_session.sagemaker_client.update_model_package(**update_model_card_args)

src/sagemaker/model_card/model_card.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,8 +1890,8 @@ class ModelPackageModelCard(object):
18901890

18911891
def __init__(
18921892
self,
1893-
model_card_content: Dict[str, Any],
1894-
model_card_status: str,
1893+
model_card_content: Optional[Dict[str, Any]] = None,
1894+
model_card_status: Optional[str] = None,
18951895
):
18961896

18971897
self.model_card_content = model_card_content

0 commit comments

Comments
 (0)