Skip to content

Commit ba5fb25

Browse files
[DRCC] Create and Update Metadata artifact content changes (#1129)
2 parents be14d7e + cec856b commit ba5fb25

File tree

3 files changed

+132
-80
lines changed

3 files changed

+132
-80
lines changed

ads/common/utils.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22

3-
# Copyright (c) 2020, 2024 Oracle and/or its affiliates.
3+
# Copyright (c) 2020, 2025 Oracle and/or its affiliates.
44
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
55

66

@@ -152,22 +152,6 @@ def oci_key_location():
152152
)
153153

154154

155-
def text_sanitizer(content):
156-
if isinstance(content, str):
157-
return (
158-
content.replace("“", '"')
159-
.replace("”", '"')
160-
.replace("’", "'")
161-
.replace("‘", "'")
162-
.replace("—", "-")
163-
.encode("utf-8", "ignore")
164-
.decode("utf-8", "ignore")
165-
)
166-
if isinstance(content, dict):
167-
return json.dumps(content)
168-
return str(content)
169-
170-
171155
@deprecated(
172156
"2.5.10",
173157
details="Deprecated, use: from ads.common.auth import AuthState; AuthState().oci_config_path",

ads/model/datascience_model.py

Lines changed: 81 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ class InvalidArtifactType(Exception): # pragma: no cover
8989
pass
9090

9191

92+
class InvalidArtifactPathTypeOrContentError(Exception): # pragma: no cover
93+
def __init__(self, msg="Invalid type of Metdata artifact content"):
94+
super().__init__(msg)
95+
96+
9297
class CustomerNotificationType(ExtendedEnum):
9398
NONE = "NONE"
9499
ALL = "ALL"
@@ -2238,7 +2243,7 @@ def find_model_idx():
22382243
def create_custom_metadata_artifact(
22392244
self,
22402245
metadata_key_name: str,
2241-
artifact_path_or_content: str,
2246+
artifact_path_or_content: Union[str, bytes],
22422247
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
22432248
) -> ModelMetadataArtifactDetails:
22442249
"""Creates model custom metadata artifact for specified model.
@@ -2248,13 +2253,22 @@ def create_custom_metadata_artifact(
22482253
metadata_key_name: str
22492254
The name of the model custom metadata key
22502255
2251-
artifact_path_or_content: str
2252-
The model custom metadata artifact path to be upload. It can also be the actual content of the custom metadata
2256+
artifact_path_or_content: Union[str,bytes]
2257+
The model custom metadata artifact path to be uploaded. It can also be the actual content of the custom metadata artifact
2258+
The type is string when it represents local path or oss path.
2259+
The type is bytes when it represents content itself
22532260
22542261
path_type: MetadataArtifactPathType
22552262
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
22562263
Specifies what type of path is to be provided for metadata artifact.
2257-
Can be either local , oss or the actual content itself
2264+
2265+
Example:
2266+
>>> ds_model=DataScienceModel.from_id("ocid1.datasciencemodel.iad.xxyxz...")
2267+
>>> ds_model.create_custom_metadata_artifact(
2268+
... "README",
2269+
... artifact_path_or_content="/Users/<username>/Downloads/README.md",
2270+
... path_type=MetadataArtifactPathType.LOCAL
2271+
... )
22582272
22592273
Returns
22602274
-------
@@ -2273,16 +2287,23 @@ def create_custom_metadata_artifact(
22732287
}
22742288
22752289
"""
2290+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2291+
artifact_path_or_content, bytes
2292+
):
2293+
raise InvalidArtifactPathTypeOrContentError(
2294+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2295+
)
2296+
22762297
return self.dsc_model.create_custom_metadata_artifact(
22772298
metadata_key_name=metadata_key_name,
2278-
artifact_path=artifact_path_or_content,
2299+
artifact_path_or_content=artifact_path_or_content,
22792300
path_type=path_type,
22802301
)
22812302

22822303
def create_defined_metadata_artifact(
22832304
self,
22842305
metadata_key_name: str,
2285-
artifact_path_or_content: str,
2306+
artifact_path_or_content: Union[str, bytes],
22862307
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
22872308
) -> ModelMetadataArtifactDetails:
22882309
"""Creates model defined metadata artifact for specified model.
@@ -2292,14 +2313,24 @@ def create_defined_metadata_artifact(
22922313
metadata_key_name: str
22932314
The name of the model defined metadata key
22942315
2295-
artifact_path_or_content: str
2296-
The model defined metadata artifact path to be upload. It can also be the actual content of the defined metadata
2316+
artifact_path_or_content: Union[str,bytes]
2317+
The model defined metadata artifact path to be uploaded. It can also be the actual content of the defined metadata
2318+
The type is string when it represents local path or oss path.
2319+
The type is bytes when it represents content itself
22972320
22982321
path_type: MetadataArtifactPathType
22992322
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
23002323
Specifies what type of path is to be provided for metadata artifact.
23012324
Can be either local , oss or the actual content itself
23022325
2326+
Example:
2327+
>>> ds_model=DataScienceModel.from_id("ocid1.datasciencemodel.iad.xxyxz...")
2328+
>>> ds_model.create_defined_metadata_artifact(
2329+
... "README",
2330+
... artifact_path_or_content="oci://path/to/bucket/README.md",
2331+
... path_type=MetadataArtifactPathType.OSS
2332+
... )
2333+
23032334
Returns
23042335
-------
23052336
ModelMetadataArtifactDetails
@@ -2317,16 +2348,23 @@ def create_defined_metadata_artifact(
23172348
}
23182349
23192350
"""
2351+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2352+
artifact_path_or_content, bytes
2353+
):
2354+
raise InvalidArtifactPathTypeOrContentError(
2355+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2356+
)
2357+
23202358
return self.dsc_model.create_defined_metadata_artifact(
23212359
metadata_key_name=metadata_key_name,
2322-
artifact_path=artifact_path_or_content,
2360+
artifact_path_or_content=artifact_path_or_content,
23232361
path_type=path_type,
23242362
)
23252363

23262364
def update_custom_metadata_artifact(
23272365
self,
23282366
metadata_key_name: str,
2329-
artifact_path_or_content: str,
2367+
artifact_path_or_content: Union[str, bytes],
23302368
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
23312369
) -> ModelMetadataArtifactDetails:
23322370
"""Update model custom metadata artifact for specified model.
@@ -2336,8 +2374,10 @@ def update_custom_metadata_artifact(
23362374
metadata_key_name: str
23372375
The name of the model custom metadata key
23382376
2339-
artifact_path_or_content: str
2340-
The model custom metadata artifact path. It can also be the actual content of the custom metadata
2377+
artifact_path_or_content: Union[str,bytes]
2378+
The model custom metadata artifact path to be uploaded. It can also be the actual content of the custom metadata
2379+
The type is string when it represents local path or oss path.
2380+
The type is bytes when it represents content itself
23412381
23422382
path_type: MetadataArtifactPathType
23432383
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
@@ -2361,16 +2401,23 @@ def update_custom_metadata_artifact(
23612401
}
23622402
23632403
"""
2404+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2405+
artifact_path_or_content, bytes
2406+
):
2407+
raise InvalidArtifactPathTypeOrContentError(
2408+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2409+
)
2410+
23642411
return self.dsc_model.update_custom_metadata_artifact(
23652412
metadata_key_name=metadata_key_name,
2366-
artifact_path=artifact_path_or_content,
2413+
artifact_path_or_content=artifact_path_or_content,
23672414
path_type=path_type,
23682415
)
23692416

23702417
def update_defined_metadata_artifact(
23712418
self,
23722419
metadata_key_name: str,
2373-
artifact_path_or_content: str,
2420+
artifact_path_or_content: Union[str, bytes],
23742421
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
23752422
) -> ModelMetadataArtifactDetails:
23762423
"""Update model defined metadata artifact for specified model.
@@ -2380,8 +2427,10 @@ def update_defined_metadata_artifact(
23802427
metadata_key_name: str
23812428
The name of the model defined metadata key
23822429
2383-
artifact_path_or_content: str
2384-
The model defined metadata artifact path. It can also be the actual content of the defined metadata
2430+
artifact_path_or_content: Union[str,bytes]
2431+
The model defined metadata artifact path to be uploaded. It can also be the actual content of the defined metadata
2432+
The type is string when it represents local path or oss path.
2433+
The type is bytes when it represents content itself
23852434
23862435
path_type: MetadataArtifactPathType
23872436
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
@@ -2405,9 +2454,16 @@ def update_defined_metadata_artifact(
24052454
}
24062455
24072456
"""
2457+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2458+
artifact_path_or_content, bytes
2459+
):
2460+
raise InvalidArtifactPathTypeOrContentError(
2461+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2462+
)
2463+
24082464
return self.dsc_model.update_defined_metadata_artifact(
24092465
metadata_key_name=metadata_key_name,
2410-
artifact_path=artifact_path_or_content,
2466+
artifact_path_or_content=artifact_path_or_content,
24112467
path_type=path_type,
24122468
)
24132469

@@ -2442,8 +2498,10 @@ def get_custom_metadata_artifact(
24422498
)
24432499
artifact_file_path = os.path.join(target_dir, f"{metadata_key_name}")
24442500

2445-
if not override and os.path.exists(artifact_file_path):
2446-
raise FileExistsError(f"File already exists: {artifact_file_path}")
2501+
if not override and is_path_exists(artifact_file_path):
2502+
raise FileExistsError(
2503+
f"File already exists: {artifact_file_path}. Please use boolean override parameter to override the file content."
2504+
)
24472505

24482506
with open(artifact_file_path, "wb") as _file:
24492507
_file.write(file_content)
@@ -2481,8 +2539,10 @@ def get_defined_metadata_artifact(
24812539
)
24822540
artifact_file_path = os.path.join(target_dir, f"{metadata_key_name}")
24832541

2484-
if not override and os.path.exists(artifact_file_path):
2485-
raise FileExistsError(f"File already exists: {artifact_file_path}")
2542+
if not override and is_path_exists(artifact_file_path):
2543+
raise FileExistsError(
2544+
f"File already exists: {artifact_file_path}. Please use boolean override parameter to override the file content."
2545+
)
24862546

24872547
with open(artifact_file_path, "wb") as _file:
24882548
_file.write(file_content)

0 commit comments

Comments
 (0)