Skip to content

Commit cf22cb2

Browse files
Fixing bug related to upload metadata artifact changes
1 parent 27edc84 commit cf22cb2

File tree

3 files changed

+100
-62
lines changed

3 files changed

+100
-62
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: 58 additions & 17 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,8 +2253,10 @@ 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 upload. It can also be the actual content of the defined metadata
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
@@ -2273,16 +2280,23 @@ def create_custom_metadata_artifact(
22732280
}
22742281
22752282
"""
2283+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2284+
artifact_path_or_content, bytes
2285+
):
2286+
raise InvalidArtifactPathTypeOrContentError(
2287+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2288+
)
2289+
22762290
return self.dsc_model.create_custom_metadata_artifact(
22772291
metadata_key_name=metadata_key_name,
2278-
artifact_path=artifact_path_or_content,
2292+
artifact_path_or_content=artifact_path_or_content,
22792293
path_type=path_type,
22802294
)
22812295

22822296
def create_defined_metadata_artifact(
22832297
self,
22842298
metadata_key_name: str,
2285-
artifact_path_or_content: str,
2299+
artifact_path_or_content: Union[str, bytes],
22862300
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
22872301
) -> ModelMetadataArtifactDetails:
22882302
"""Creates model defined metadata artifact for specified model.
@@ -2292,8 +2306,10 @@ def create_defined_metadata_artifact(
22922306
metadata_key_name: str
22932307
The name of the model defined metadata key
22942308
2295-
artifact_path_or_content: str
2309+
artifact_path_or_content: Union[str,bytes]
22962310
The model defined metadata artifact path to be upload. It can also be the actual content of the defined metadata
2311+
The type is string when it represents local path or oss path.
2312+
The type is bytes when it represents content itself
22972313
22982314
path_type: MetadataArtifactPathType
22992315
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
@@ -2317,16 +2333,23 @@ def create_defined_metadata_artifact(
23172333
}
23182334
23192335
"""
2336+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2337+
artifact_path_or_content, bytes
2338+
):
2339+
raise InvalidArtifactPathTypeOrContentError(
2340+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2341+
)
2342+
23202343
return self.dsc_model.create_defined_metadata_artifact(
23212344
metadata_key_name=metadata_key_name,
2322-
artifact_path=artifact_path_or_content,
2345+
artifact_path_or_content=artifact_path_or_content,
23232346
path_type=path_type,
23242347
)
23252348

23262349
def update_custom_metadata_artifact(
23272350
self,
23282351
metadata_key_name: str,
2329-
artifact_path_or_content: str,
2352+
artifact_path_or_content: Union[str, bytes],
23302353
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
23312354
) -> ModelMetadataArtifactDetails:
23322355
"""Update model custom metadata artifact for specified model.
@@ -2336,8 +2359,10 @@ def update_custom_metadata_artifact(
23362359
metadata_key_name: str
23372360
The name of the model custom metadata key
23382361
2339-
artifact_path_or_content: str
2340-
The model custom metadata artifact path. It can also be the actual content of the custom metadata
2362+
artifact_path_or_content: Union[str,bytes]
2363+
The model custom metadata artifact path to be upload. It can also be the actual content of the defined metadata
2364+
The type is string when it represents local path or oss path.
2365+
The type is bytes when it represents content itself
23412366
23422367
path_type: MetadataArtifactPathType
23432368
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
@@ -2361,16 +2386,23 @@ def update_custom_metadata_artifact(
23612386
}
23622387
23632388
"""
2389+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2390+
artifact_path_or_content, bytes
2391+
):
2392+
raise InvalidArtifactPathTypeOrContentError(
2393+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2394+
)
2395+
23642396
return self.dsc_model.update_custom_metadata_artifact(
23652397
metadata_key_name=metadata_key_name,
2366-
artifact_path=artifact_path_or_content,
2398+
artifact_path_or_content=artifact_path_or_content,
23672399
path_type=path_type,
23682400
)
23692401

23702402
def update_defined_metadata_artifact(
23712403
self,
23722404
metadata_key_name: str,
2373-
artifact_path_or_content: str,
2405+
artifact_path_or_content: Union[str, bytes],
23742406
path_type: MetadataArtifactPathType = MetadataArtifactPathType.LOCAL,
23752407
) -> ModelMetadataArtifactDetails:
23762408
"""Update model defined metadata artifact for specified model.
@@ -2380,8 +2412,10 @@ def update_defined_metadata_artifact(
23802412
metadata_key_name: str
23812413
The name of the model defined metadata key
23822414
2383-
artifact_path_or_content: str
2384-
The model defined metadata artifact path. It can also be the actual content of the defined metadata
2415+
artifact_path_or_content: Union[str,bytes]
2416+
The model defined metadata artifact path to be upload. It can also be the actual content of the defined metadata
2417+
The type is string when it represents local path or oss path.
2418+
The type is bytes when it represents content itself
23852419
23862420
path_type: MetadataArtifactPathType
23872421
Can be either of MetadataArtifactPathType.LOCAL , MetadataArtifactPathType.OSS , MetadataArtifactPathType.CONTENT
@@ -2405,9 +2439,16 @@ def update_defined_metadata_artifact(
24052439
}
24062440
24072441
"""
2442+
if path_type == MetadataArtifactPathType.CONTENT and not isinstance(
2443+
artifact_path_or_content, bytes
2444+
):
2445+
raise InvalidArtifactPathTypeOrContentError(
2446+
f"Invalid type of artifact content: {type(artifact_path_or_content)}. It should be bytes."
2447+
)
2448+
24082449
return self.dsc_model.update_defined_metadata_artifact(
24092450
metadata_key_name=metadata_key_name,
2410-
artifact_path=artifact_path_or_content,
2451+
artifact_path_or_content=artifact_path_or_content,
24112452
path_type=path_type,
24122453
)
24132454

@@ -2442,7 +2483,7 @@ def get_custom_metadata_artifact(
24422483
)
24432484
artifact_file_path = os.path.join(target_dir, f"{metadata_key_name}")
24442485

2445-
if not override and os.path.exists(artifact_file_path):
2486+
if not override and is_path_exists(artifact_file_path):
24462487
raise FileExistsError(f"File already exists: {artifact_file_path}")
24472488

24482489
with open(artifact_file_path, "wb") as _file:
@@ -2481,7 +2522,7 @@ def get_defined_metadata_artifact(
24812522
)
24822523
artifact_file_path = os.path.join(target_dir, f"{metadata_key_name}")
24832524

2484-
if not override and os.path.exists(artifact_file_path):
2525+
if not override and is_path_exists(artifact_file_path):
24852526
raise FileExistsError(f"File already exists: {artifact_file_path}")
24862527

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

0 commit comments

Comments
 (0)