1
1
#!/usr/bin/env python
2
- # -*- coding: utf-8; -*-
3
2
4
3
# Copyright (c) 2022, 2024 Oracle and/or its affiliates.
5
4
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/
11
10
import shutil
12
11
import tempfile
13
12
from copy import deepcopy
14
- from typing import Dict , List , Optional , Union , Tuple
13
+ from typing import Dict , List , Optional , Tuple , Union
15
14
16
15
import pandas
17
16
import yaml
18
17
from jsonschema import ValidationError , validate
19
18
19
+ from ads .common import oci_client as oc
20
20
from ads .common import utils
21
21
from ads .common .extended_enum import ExtendedEnumMeta
22
22
from ads .common .object_storage_details import ObjectStorageDetails
23
+ from ads .config import (
24
+ AQUA_SERVICE_MODELS_BUCKET as SERVICE_MODELS_BUCKET ,
25
+ )
23
26
from ads .config import (
24
27
COMPARTMENT_OCID ,
25
28
PROJECT_OCID ,
26
- AQUA_SERVICE_MODELS_BUCKET as SERVICE_MODELS_BUCKET ,
27
29
)
28
30
from ads .feature_engineering .schema import Schema
29
31
from ads .jobs .builders .base import Builder
37
39
ModelCustomMetadata ,
38
40
ModelCustomMetadataItem ,
39
41
ModelProvenanceMetadata ,
40
- ModelTaxonomyMetadata , )
42
+ ModelTaxonomyMetadata ,
43
+ )
41
44
from ads .model .service .oci_datascience_model import (
42
45
ModelProvenanceNotFoundError ,
43
46
OCIDataScienceModel ,
44
47
)
45
- from ads .common import oci_client as oc
46
48
47
49
logger = logging .getLogger (__name__ )
48
50
@@ -84,14 +86,6 @@ class CustomerNotificationType(str, metaclass=ExtendedEnumMeta):
84
86
ON_FAILURE = "ON_FAILURE"
85
87
ON_SUCCESS = "ON_SUCCESS"
86
88
87
- @classmethod
88
- def is_valid (cls , value ):
89
- return value in (cls .NONE , cls .ALL , cls .ON_FAILURE , cls .ON_SUCCESS )
90
-
91
- @property
92
- def value (self ):
93
- return str (self )
94
-
95
89
96
90
class SettingStatus (str , metaclass = ExtendedEnumMeta ):
97
91
"""Enum to represent the status of retention settings."""
@@ -100,11 +94,6 @@ class SettingStatus(str, metaclass=ExtendedEnumMeta):
100
94
SUCCEEDED = "SUCCEEDED"
101
95
FAILED = "FAILED"
102
96
103
- @classmethod
104
- def is_valid (cls , state : str ) -> bool :
105
- """Validates the given state against allowed SettingStatus values."""
106
- return state in (cls .PENDING , cls .SUCCEEDED , cls .FAILED )
107
-
108
97
109
98
class ModelBackupSetting :
110
99
"""
@@ -167,10 +156,7 @@ def to_json(self) -> str:
167
156
@classmethod
168
157
def from_json (cls , json_str ) -> "ModelBackupSetting" :
169
158
"""Constructs backup settings from a JSON string or dictionary."""
170
- if isinstance (json_str , str ):
171
- data = json .loads (json_str )
172
- else :
173
- data = json_str
159
+ data = json .loads (json_str ) if isinstance (json_str , str ) else json_str
174
160
175
161
return cls .from_dict (data )
176
162
@@ -180,14 +166,12 @@ def to_yaml(self) -> str:
180
166
181
167
def validate (self ) -> bool :
182
168
"""Validates the backup settings details. Returns True if valid, False otherwise."""
183
- if not isinstance (self .is_backup_enabled , bool ):
184
- return False
185
- if self .backup_region and not isinstance (self .backup_region , str ):
186
- return False
187
- if not isinstance (self .customer_notification_type , str ) \
188
- or not CustomerNotificationType .is_valid (self .customer_notification_type ):
189
- return False
190
- return True
169
+ return all ([
170
+ isinstance (self .is_backup_enabled , bool ),
171
+ not self .backup_region or isinstance (self .backup_region , str ),
172
+ isinstance (self .customer_notification_type , str ) and self .customer_notification_type in
173
+ CustomerNotificationType .values ()
174
+ ])
191
175
192
176
def __repr__ (self ):
193
177
return self .to_yaml ()
@@ -252,10 +236,7 @@ def to_json(self) -> str:
252
236
@classmethod
253
237
def from_json (cls , json_str ) -> "ModelRetentionSetting" :
254
238
"""Constructs retention settings from a JSON string."""
255
- if isinstance (json_str , str ):
256
- data = json .loads (json_str )
257
- else :
258
- data = json_str
239
+ data = json .loads (json_str ) if isinstance (json_str , str ) else json_str
259
240
return cls .from_dict (data )
260
241
261
242
def to_yaml (self ) -> str :
@@ -264,18 +245,13 @@ def to_yaml(self) -> str:
264
245
265
246
def validate (self ) -> bool :
266
247
"""Validates the retention settings details. Returns True if valid, False otherwise."""
267
- if self .archive_after_days is not None and (
268
- not isinstance (self .archive_after_days , int ) or self .archive_after_days < 0
269
- ):
270
- return False
271
- if self .delete_after_days is not None and (
272
- not isinstance (self .delete_after_days , int ) or self .delete_after_days < 0
273
- ):
274
- return False
275
- if not isinstance (self .customer_notification_type , str ) or not \
276
- CustomerNotificationType .is_valid (self .customer_notification_type ):
277
- return False
278
- return True
248
+ return all ([
249
+ self .archive_after_days is None or (
250
+ isinstance (self .archive_after_days , int ) and self .archive_after_days >= 0 ),
251
+ self .delete_after_days is None or (isinstance (self .delete_after_days , int ) and self .delete_after_days >= 0 ),
252
+ isinstance (self .customer_notification_type , str ) and self .customer_notification_type in
253
+ CustomerNotificationType .values ()
254
+ ])
279
255
280
256
def __repr__ (self ):
281
257
return self .to_yaml ()
@@ -358,8 +334,8 @@ def validate(self) -> bool:
358
334
"""Validates the retention operation details."""
359
335
return all (
360
336
[
361
- self .archive_state is None or SettingStatus . is_valid ( self .archive_state ),
362
- self .delete_state is None or SettingStatus . is_valid ( self .delete_state ),
337
+ self .archive_state is None or self .archive_state in SettingStatus . values ( ),
338
+ self .delete_state is None or self .delete_state in SettingStatus . values ( ),
363
339
self .time_archival_scheduled is None
364
340
or isinstance (self .time_archival_scheduled , int ),
365
341
self .time_deletion_scheduled is None
@@ -395,18 +371,18 @@ def __init__(
395
371
self ,
396
372
backup_state : Optional [SettingStatus ] = None ,
397
373
backup_state_details : Optional [str ] = None ,
398
- time_last_backed_up : Optional [int ] = None ,
374
+ time_last_backup : Optional [int ] = None ,
399
375
):
400
376
self .backup_state = backup_state
401
377
self .backup_state_details = backup_state_details
402
- self .time_last_backed_up = time_last_backed_up
378
+ self .time_last_backup = time_last_backup
403
379
404
380
def to_dict (self ) -> Dict :
405
381
"""Serializes the backup operation details into a dictionary."""
406
382
return {
407
383
"backup_state" : self .backup_state or None ,
408
384
"backup_state_details" : self .backup_state_details ,
409
- "time_last_backed_up " : self .time_last_backed_up ,
385
+ "time_last_backup " : self .time_last_backup ,
410
386
}
411
387
412
388
@classmethod
@@ -415,7 +391,7 @@ def from_dict(cls, data: Dict) -> "ModelBackupOperationDetails":
415
391
return cls (
416
392
backup_state = SettingStatus (data .get ("backup_state" )) or None ,
417
393
backup_state_details = data .get ("backup_state_details" ),
418
- time_last_backed_up = data .get ("time_last_backed_up " ),
394
+ time_last_backup = data .get ("time_last_backup " ),
419
395
)
420
396
421
397
def to_json (self ) -> str :
@@ -434,13 +410,10 @@ def to_yaml(self) -> str:
434
410
435
411
def validate (self ) -> bool :
436
412
"""Validates the backup operation details."""
437
- if self .backup_state is not None and not SettingStatus .is_valid (self .backup_state ):
438
- return False
439
- if self .time_last_backed_up is not None and not isinstance (
440
- self .time_last_backed_up , int
441
- ):
442
- return False
443
- return True
413
+ return not (
414
+ (self .backup_state is not None and not self .backup_state in SettingStatus .values ()) or
415
+ (self .time_last_backup is not None and not isinstance (self .time_last_backup , int ))
416
+ )
444
417
445
418
def __repr__ (self ):
446
419
return self .to_yaml ()
@@ -1068,7 +1041,7 @@ def with_model_file_description(
1068
1041
elif json_string :
1069
1042
json_data = json .loads (json_string )
1070
1043
elif json_uri :
1071
- with open (json_uri , "r" ) as json_file :
1044
+ with open (json_uri ) as json_file :
1072
1045
json_data = json .load (json_file )
1073
1046
else :
1074
1047
raise ValueError ("Must provide either a valid json string or URI location." )
@@ -1423,17 +1396,11 @@ def restore_model(
1423
1396
return
1424
1397
1425
1398
# Optional: Validate restore_model_for_hours_specified
1426
- if restore_model_for_hours_specified is not None :
1427
- if (
1428
- not isinstance (restore_model_for_hours_specified , int )
1429
- or restore_model_for_hours_specified <= 0
1430
- ):
1431
- raise ValueError (
1432
- "restore_model_for_hours_specified must be a positive integer."
1433
- )
1399
+ if restore_model_for_hours_specified is not None and (
1400
+ not isinstance (restore_model_for_hours_specified , int ) or restore_model_for_hours_specified <= 0 ):
1401
+ raise ValueError ("restore_model_for_hours_specified must be a positive integer." )
1434
1402
1435
1403
self .dsc_model .restore_archived_model_artifact (
1436
- model_id = self .id ,
1437
1404
restore_model_for_hours_specified = restore_model_for_hours_specified ,
1438
1405
)
1439
1406
@@ -1692,8 +1659,8 @@ def _init_complex_attributes(self):
1692
1659
self .with_provenance_metadata (self .provenance_metadata )
1693
1660
self .with_input_schema (self .input_schema )
1694
1661
self .with_output_schema (self .output_schema )
1695
- self .with_backup_setting (self .backup_setting )
1696
- self .with_retention_setting (self .retention_setting )
1662
+ # self.with_backup_setting(self.backup_setting)
1663
+ # self.with_retention_setting(self.retention_setting)
1697
1664
1698
1665
def _to_oci_dsc_model (self , ** kwargs ):
1699
1666
"""Creates an `OCIDataScienceModel` instance from the `DataScienceModel`.
@@ -1753,6 +1720,8 @@ def _update_from_oci_dsc_model(
1753
1720
self .CONST_DEFINED_METADATA : ModelTaxonomyMetadata ._from_oci_metadata ,
1754
1721
self .CONST_BACKUP_SETTING : ModelBackupSetting .to_dict ,
1755
1722
self .CONST_RETENTION_SETTING : ModelRetentionSetting .to_dict ,
1723
+ self .CONST_BACKUP_OPERATION_DETAILS : ModelBackupOperationDetails .to_dict ,
1724
+ self .CONST_RETENTION_OPERATION_DETAILS : ModelRetentionOperationDetails .to_dict
1756
1725
}
1757
1726
1758
1727
# Update the main properties
0 commit comments