6
6
import os
7
7
from oci .data_science .models import Metadata
8
8
import ads
9
+
9
10
# from ads.common import logger
10
11
import logging
11
12
14
15
15
16
16
17
class ModelDescription :
18
+ """
19
+ Represents a model description for multi model artifacts. Provides methods to ease the process of creating such models.
20
+
21
+ Methods:
22
+ - add(namespace, bucket, prefix=None, files=None): Adds information about objects to the model description JSON.
23
+ - remove(namespace, bucket, prefix=None): Removes information about objects from the model description JSON.
24
+ - show(): Displays the current model description JSON in a human-readable format.
25
+ - build(): Builds the model description JSON and writes it to a file.
26
+ - save(project_ocid, compartment_ocid, display_name=None): Saves the model to the Model Catalog of OCI Data Science service.
27
+ """
17
28
18
29
empty_json = {
19
30
"version" : "1.0" ,
@@ -22,6 +33,19 @@ class ModelDescription:
22
33
}
23
34
24
35
def auth (self ):
36
+ """
37
+ Internal method that authenticates the model description instance by initializing OCI clients.
38
+
39
+ Parameters:
40
+ - None
41
+
42
+ Returns:
43
+ - None
44
+
45
+ Note:
46
+ - This method retrieves authentication data using default signer from the `ads.common.auth` module.
47
+ - The region information is extracted from the authentication data.
48
+ """
25
49
authData = ads .common .auth .default_signer ()
26
50
signer = authData ["signer" ]
27
51
self .region = authData ["config" ]["region" ]
@@ -36,6 +60,29 @@ def auth(self):
36
60
)
37
61
38
62
def __init__ (self , model_ocid = None ):
63
+ """
64
+ Initializes a new instance of the ModelDescription class.
65
+
66
+ Parameters:
67
+ - model_ocid (str, optional): The OCID (Oracle Cloud Identifier) of the model.
68
+ If provided, retrieves the takes the model artifact content as starting point or else initializes from blank artifact.
69
+
70
+ Returns:
71
+ - None
72
+
73
+ Raises:
74
+ - json.JSONDecodeError: If there is an error decoding the JSON content retrieved
75
+ from the backend.
76
+ - Exception: If an unexpected error occurs while retrieving the model artifact content.
77
+
78
+ Note:
79
+ - If `model_ocid` is provided, this method attempts to retrieve the model artifact
80
+ content from the backend using the provided OCID. If successful, it initializes
81
+ `modelDescriptionJson` with the retrieved content. If unsuccessful, it logs the
82
+ error and raises an exception.
83
+ - If `model_ocid` is not provided, `modelDescriptionJson` is initialized with an
84
+ empty JSON structure.
85
+ """
39
86
40
87
self .region = ""
41
88
self .auth ()
@@ -62,6 +109,28 @@ def __init__(self, model_ocid=None):
62
109
raise e
63
110
64
111
def add (self , namespace , bucket , prefix = None , files = None ):
112
+ """
113
+ Adds information about objects in a specified bucket to the model description JSON.
114
+
115
+ Parameters:
116
+ - namespace (str): The namespace of the object storage.
117
+ - bucket (str): The name of the bucket containing the objects.
118
+ - prefix (str, optional): The prefix used to filter objects within the bucket. Defaults to None.
119
+ - files (list of str, optional): A list of file names to include in the model description.
120
+ If provided, only objects with matching file names will be included. Defaults to None.
121
+
122
+ Returns:
123
+ - None
124
+
125
+ Raises:
126
+ - ValueError: If no files are found to add to the model description.
127
+
128
+ Note:
129
+ - If `files` is not provided, it retrieves information about all objects in the bucket.
130
+ If `files` is provided, it only retrieves information about objects with matching file names.
131
+ - If no objects are found to add to the model description, a ValueError is raised.
132
+ """
133
+
65
134
# Remove if the model already exists
66
135
self .remove (namespace , bucket , prefix )
67
136
@@ -137,6 +206,25 @@ def listObjectVersionsUnpaginated():
137
206
)
138
207
139
208
def remove (self , namespace , bucket , prefix = None ):
209
+ """
210
+ Removes information about objects in a specified bucket from the model description JSON.
211
+
212
+ Parameters:
213
+ - namespace (str): The namespace of the object storage.
214
+ - bucket (str): The name of the bucket containing the objects.
215
+ - prefix (str, optional): The prefix used to filter objects within the bucket. Defaults to None.
216
+
217
+ Returns:
218
+ - None
219
+
220
+ Note:
221
+ - This method removes information about objects in the specified bucket from the
222
+ instance of the ModelDescription.
223
+ - If a matching model (with the specified namespace, bucket name, and prefix) is found
224
+ in the model description JSON, it is removed.
225
+ - If no matching model is found, the method returns without making any changes.
226
+ """
227
+
140
228
def findModelIdx ():
141
229
for idx , model in enumerate (self .modelDescriptionJson ["models" ]):
142
230
if (
@@ -155,9 +243,34 @@ def findModelIdx():
155
243
self .modelDescriptionJson ["models" ].pop (modelSearchIdx )
156
244
157
245
def show (self ):
246
+ """
247
+ Displays the current model description JSON in a human-readable format.
248
+
249
+ Parameters:
250
+ - None
251
+
252
+ Returns:
253
+ - None
254
+
255
+ Note:
256
+ - The JSON representation of the model description is formatted with an indentation
257
+ of 4 spaces.
258
+ """
158
259
logger .info (json .dumps (self .modelDescriptionJson , indent = 4 ))
159
260
160
261
def build (self ):
262
+ """
263
+ Builds the model description JSON and writes it to a file.
264
+
265
+ Parameters:
266
+ - None
267
+
268
+ Returns:
269
+ - str: The absolute file path where the model description JSON is stored.
270
+
271
+ Note:
272
+ - This method serializes the current model description attribute to a JSON file named 'resultModelDescription.json' with an indentation of 2 spaces.
273
+ """
161
274
logger .info ("Building..." )
162
275
file_path = "resultModelDescription.json"
163
276
try :
@@ -171,10 +284,25 @@ def build(self):
171
284
logger .error (
172
285
f"An unexpected error occurred: { e } "
173
286
) # Handle other unexpected exceptions
174
- logger .info ("Model Artifact stored at location: 'resultModelDescription.json' " )
287
+ logger .info ("Model Artifact stored successfully. " )
175
288
return os .path .abspath (file_path )
176
289
177
290
def save (self , project_ocid , compartment_ocid , display_name = None ):
291
+ """
292
+ Saves the model to the Model Catalog of Oracle Cloud Infrastructure (OCI) Data Science service.
293
+
294
+ Parameters:
295
+ - project_ocid (str): The OCID (Oracle Cloud Identifier) of the OCI Data Science project.
296
+ - compartment_ocid (str): The OCID of the compartment in which the model will be created.
297
+ - display_name (str, optional): The display name for the created model. If not provided,
298
+ a default display name indicating the creation timestamp is used. Defaults to None.
299
+
300
+ Returns:
301
+ - str: The OCID of the created model.
302
+
303
+ Note:
304
+ - The display name defaults to a string indicating the creation timestamp if not provided.
305
+ """
178
306
display_name = (
179
307
"Created by MMS SDK on "
180
308
+ datetime .datetime .now (pytz .utc ).strftime ("%Y-%m-%d %H:%M:%S %Z" )
@@ -196,5 +324,5 @@ def save(self, project_ocid, compartment_ocid, display_name=None):
196
324
json .dumps (self .modelDescriptionJson ),
197
325
content_disposition = 'attachment; filename="modelDescription.json"' ,
198
326
)
199
- logger .info ("Successfully created model with OCID: " , model .data .id )
327
+ logger .info (f "Successfully created model with OCID: { model .data .id } " )
200
328
return model .data .id
0 commit comments