Skip to content

Commit c248ad5

Browse files
Merge branch 'fix/metadata-api-changes' into aqua/ADS_MS_changes
2 parents d49a88d + 0538144 commit c248ad5

File tree

3 files changed

+115
-62
lines changed

3 files changed

+115
-62
lines changed

ads/common/utils.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,6 @@ def oci_key_location():
153153
)
154154

155155

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

ads/model/datascience_model.py

Lines changed: 75 additions & 19 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"
@@ -2267,7 +2272,7 @@ def if_model_custom_metadata_artifact_exist(
22672272
def create_custom_metadata_artifact(
22682273
self,
22692274
metadata_key_name: str,
2270-
artifact_path_or_content: str,
2275+
artifact_path_or_content: Union[str, bytes],
22712276
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
22722277
) -> ModelMetadataArtifactDetails:
22732278
"""Creates model custom metadata artifact for specified model.
@@ -2277,13 +2282,22 @@ def create_custom_metadata_artifact(
22772282
metadata_key_name: str
22782283
The name of the model custom metadata key
22792284
2280-
artifact_path_or_content: str
2281-
The model custom metadata artifact path to be upload. It can also be the actual content of the custom metadata
2285+
artifact_path_or_content: Union[str,bytes]
2286+
The model custom metadata artifact path to be uploaded. It can also be the actual content of the custom metadata artifact
2287+
The type is string when it represents local path or oss path.
2288+
The type is bytes when it represents content itself
22822289
22832290
path_type: MetadataArtifactPathType
22842291
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
22852292
Specifies what type of path is to be provided for metadata artifact.
2286-
Can be either local , oss or the actual content itself
2293+
2294+
Example:
2295+
>>> ds_model=DataScienceModel.from_id("ocid1.datasciencemodel.iad.xxyxz...")
2296+
>>> ds_model.create_custom_metadata_artifact(
2297+
... "README",
2298+
... artifact_path_or_content="/Users/<username>/Downloads/README.md",
2299+
... path_type=MetadataArtifactPathType.LOCAL
2300+
... )
22872301
22882302
Returns
22892303
-------
@@ -2302,16 +2316,23 @@ def create_custom_metadata_artifact(
23022316
}
23032317
23042318
"""
2319+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2320+
artifact_path_or_content, bytes
2321+
):
2322+
raise InvalidArtifactPathTypeOrContentError(
2323+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2324+
)
2325+
23052326
return self.dsc_model.create_custom_metadata_artifact(
23062327
metadata_key_name=metadata_key_name,
2307-
artifact_path=artifact_path_or_content,
2328+
artifact_path_or_content=artifact_path_or_content,
23082329
path_type=path_type,
23092330
)
23102331

23112332
def create_defined_metadata_artifact(
23122333
self,
23132334
metadata_key_name: str,
2314-
artifact_path_or_content: str,
2335+
artifact_path_or_content: Union[str, bytes],
23152336
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
23162337
) -> ModelMetadataArtifactDetails:
23172338
"""Creates model defined metadata artifact for specified model.
@@ -2321,14 +2342,24 @@ def create_defined_metadata_artifact(
23212342
metadata_key_name: str
23222343
The name of the model defined metadata key
23232344
2324-
artifact_path_or_content: str
2325-
The model defined metadata artifact path to be upload. It can also be the actual content of the defined metadata
2345+
artifact_path_or_content: Union[str,bytes]
2346+
The model defined metadata artifact path to be uploaded. It can also be the actual content of the defined metadata
2347+
The type is string when it represents local path or oss path.
2348+
The type is bytes when it represents content itself
23262349
23272350
path_type: MetadataArtifactPathType
23282351
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
23292352
Specifies what type of path is to be provided for metadata artifact.
23302353
Can be either local , oss or the actual content itself
23312354
2355+
Example:
2356+
>>> ds_model=DataScienceModel.from_id("ocid1.datasciencemodel.iad.xxyxz...")
2357+
>>> ds_model.create_defined_metadata_artifact(
2358+
... "README",
2359+
... artifact_path_or_content="oci://path/to/bucket/README.md",
2360+
... path_type=MetadataArtifactPathType.OSS
2361+
... )
2362+
23322363
Returns
23332364
-------
23342365
ModelMetadataArtifactDetails
@@ -2346,16 +2377,23 @@ def create_defined_metadata_artifact(
23462377
}
23472378
23482379
"""
2380+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2381+
artifact_path_or_content, bytes
2382+
):
2383+
raise InvalidArtifactPathTypeOrContentError(
2384+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2385+
)
2386+
23492387
return self.dsc_model.create_defined_metadata_artifact(
23502388
metadata_key_name=metadata_key_name,
2351-
artifact_path=artifact_path_or_content,
2389+
artifact_path_or_content=artifact_path_or_content,
23522390
path_type=path_type,
23532391
)
23542392

23552393
def update_custom_metadata_artifact(
23562394
self,
23572395
metadata_key_name: str,
2358-
artifact_path_or_content: str,
2396+
artifact_path_or_content: Union[str, bytes],
23592397
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
23602398
) -> ModelMetadataArtifactDetails:
23612399
"""Update model custom metadata artifact for specified model.
@@ -2365,8 +2403,10 @@ def update_custom_metadata_artifact(
23652403
metadata_key_name: str
23662404
The name of the model custom metadata key
23672405
2368-
artifact_path_or_content: str
2369-
The model custom metadata artifact path. It can also be the actual content of the custom metadata
2406+
artifact_path_or_content: Union[str,bytes]
2407+
The model custom metadata artifact path to be uploaded. It can also be the actual content of the custom metadata
2408+
The type is string when it represents local path or oss path.
2409+
The type is bytes when it represents content itself
23702410
23712411
path_type: MetadataArtifactPathType
23722412
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
@@ -2390,16 +2430,23 @@ def update_custom_metadata_artifact(
23902430
}
23912431
23922432
"""
2433+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2434+
artifact_path_or_content, bytes
2435+
):
2436+
raise InvalidArtifactPathTypeOrContentError(
2437+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2438+
)
2439+
23932440
return self.dsc_model.update_custom_metadata_artifact(
23942441
metadata_key_name=metadata_key_name,
2395-
artifact_path=artifact_path_or_content,
2442+
artifact_path_or_content=artifact_path_or_content,
23962443
path_type=path_type,
23972444
)
23982445

23992446
def update_defined_metadata_artifact(
24002447
self,
24012448
metadata_key_name: str,
2402-
artifact_path_or_content: str,
2449+
artifact_path_or_content: Union[str, bytes],
24032450
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
24042451
) -> ModelMetadataArtifactDetails:
24052452
"""Update model defined metadata artifact for specified model.
@@ -2409,8 +2456,10 @@ def update_defined_metadata_artifact(
24092456
metadata_key_name: str
24102457
The name of the model defined metadata key
24112458
2412-
artifact_path_or_content: str
2413-
The model defined metadata artifact path. It can also be the actual content of the defined metadata
2459+
artifact_path_or_content: Union[str,bytes]
2460+
The model defined metadata artifact path to be uploaded. It can also be the actual content of the defined metadata
2461+
The type is string when it represents local path or oss path.
2462+
The type is bytes when it represents content itself
24142463
24152464
path_type: MetadataArtifactPathType
24162465
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
@@ -2434,9 +2483,16 @@ def update_defined_metadata_artifact(
24342483
}
24352484
24362485
"""
2486+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2487+
artifact_path_or_content, bytes
2488+
):
2489+
raise InvalidArtifactPathTypeOrContentError(
2490+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2491+
)
2492+
24372493
return self.dsc_model.update_defined_metadata_artifact(
24382494
metadata_key_name=metadata_key_name,
2439-
artifact_path=artifact_path_or_content,
2495+
artifact_path_or_content=artifact_path_or_content,
24402496
path_type=path_type,
24412497
)
24422498

@@ -2471,7 +2527,7 @@ def get_custom_metadata_artifact(
24712527
)
24722528
artifact_file_path = os.path.join(target_dir, f"{metadata_key_name}")
24732529

2474-
if not override and os.path.exists(artifact_file_path):
2530+
if not override and is_path_exists(artifact_file_path):
24752531
raise FileExistsError(f"File already exists: {artifact_file_path}")
24762532

24772533
with open(artifact_file_path, "wb") as _file:
@@ -2510,7 +2566,7 @@ def get_defined_metadata_artifact(
25102566
)
25112567
artifact_file_path = os.path.join(target_dir, f"{metadata_key_name}")
25122568

2513-
if not override and os.path.exists(artifact_file_path):
2569+
if not override and is_path_exists(artifact_file_path):
25142570
raise FileExistsError(f"File already exists: {artifact_file_path}")
25152571

25162572
with open(artifact_file_path, "wb") as _file:

0 commit comments

Comments
 (0)