in src/sagemaker/model_card/model_card.py [0:0]
def from_model_name(cls, model_name: str, sagemaker_session: Session = None, **kwargs):
"""Initialize a model overview object from auto-discovered data.
Args:
model_name (str): The unique name of the model.
sagemaker_session (Session, optional): A SageMaker Session
object, used for SageMaker interactions (default: None). If not
specified, a SageMaker Session is created using the default AWS configuration
chain.
**kwargs: Other arguments in ModelOverview, i.e. model_description,
problem_type, algorithm_type, model_creator, model_owner, model_version
Raises:
ValueError: A model with this name does not exist.
ValueError: A model card already exists for this model.
"""
def call_describe_model():
"""Load existing model."""
try:
model_response = sagemaker_session.sagemaker_client.describe_model(
ModelName=model_name
)
except ClientError as e:
if e.response["Error"]["Message"].startswith( # pylint: disable=r1720
"Could not find model"
):
raise ValueError(
(
f"Model details for model {model_name} could not be found. "
"Make sure the model name is valid."
)
)
else:
raise
return model_response
def search_model_associated_model_cards(model_id: str):
"""Check if a model card already exists for this model.
Args:
model_id (str): A SageMaker model ID.
"""
response = sagemaker_session.sagemaker_client.search(
Resource="ModelCard",
SearchExpression={
"Filters": [
{
"Name": "ModelId",
"Operator": "Equals",
"Value": model_id,
}
]
},
)
return [c["ModelCard"]["ModelCardName"] for c in response["Results"]]
if not sagemaker_session:
sagemaker_session = Session() # pylint: disable=W0106
model_response = call_describe_model()
associated_model_cards = search_model_associated_model_cards(model_response["ModelArn"])
if associated_model_cards:
raise ValueError(
f"The model has been associated with {associated_model_cards} model cards."
)
if "Containers" in model_response: # inference pipeline model
artifacts = [c["ModelDataUrl"] for c in model_response["Containers"]]
elif (
"PrimaryContainer" in model_response
and "ModelDataUrl" in model_response["PrimaryContainer"]
):
artifacts = [model_response["PrimaryContainer"]["ModelDataUrl"]]
else:
artifacts = []
kwargs.update(
{
"model_name": model_name,
"model_id": model_response["ModelArn"],
"inference_environment": Environment(
container_image=[model_response["PrimaryContainer"]["Image"]]
),
"model_artifact": artifacts,
}
)
return cls(**kwargs)