10
10
from typing import Dict , Optional
11
11
12
12
from ads .common import utils
13
+ from ads .common .object_storage_details import ObjectStorageDetails
13
14
from ads .model .common import utils as model_utils
14
15
from ads .model .service .oci_datascience_model import OCIDataScienceModel
15
16
@@ -29,7 +30,10 @@ def __init__(self, dsc_model: OCIDataScienceModel, artifact_path: str):
29
30
artifact_path: str
30
31
The model artifact location.
31
32
"""
32
- if not os .path .exists (artifact_path ):
33
+ if not (
34
+ ObjectStorageDetails .is_oci_path (artifact_path )
35
+ or os .path .exists (artifact_path )
36
+ ):
33
37
raise ValueError (f"The `{ artifact_path } ` does not exist" )
34
38
35
39
self .dsc_model = dsc_model
@@ -45,7 +49,7 @@ def upload(self):
45
49
) as progress :
46
50
self .progress = progress
47
51
self .progress .update ("Preparing model artifacts ZIP archive." )
48
- self ._prepare_artiact_tmp_zip ()
52
+ self ._prepare_artifact_tmp_zip ()
49
53
self .progress .update ("Uploading model artifacts." )
50
54
self ._upload ()
51
55
self .progress .update (
@@ -55,22 +59,19 @@ def upload(self):
55
59
except Exception :
56
60
raise
57
61
finally :
58
- self ._remove_artiact_tmp_zip ()
62
+ self ._remove_artifact_tmp_zip ()
59
63
60
- def _prepare_artiact_tmp_zip (self ) -> str :
64
+ def _prepare_artifact_tmp_zip (self ) -> str :
61
65
"""Prepares model artifacts ZIP archive.
62
66
63
- Parameters
64
- ----------
65
- progress: (TqdmProgressBar, optional). Defaults to `None`.
66
- The progress indicator.
67
-
68
67
Returns
69
68
-------
70
69
str
71
70
Path to the model artifact ZIP archive.
72
71
"""
73
- if os .path .isfile (self .artifact_path ) and self .artifact_path .lower ().endswith (
72
+ if ObjectStorageDetails .is_oci_path (self .artifact_path ):
73
+ self .artifact_zip_path = self .artifact_path
74
+ elif os .path .isfile (self .artifact_path ) and self .artifact_path .lower ().endswith (
74
75
".zip"
75
76
):
76
77
self .artifact_zip_path = self .artifact_path
@@ -80,7 +81,7 @@ def _prepare_artiact_tmp_zip(self) -> str:
80
81
)
81
82
return self .artifact_zip_path
82
83
83
- def _remove_artiact_tmp_zip (self ):
84
+ def _remove_artifact_tmp_zip (self ):
84
85
"""Removes temporary created artifact zip archive."""
85
86
if (
86
87
self .artifact_zip_path
@@ -112,7 +113,10 @@ class LargeArtifactUploader(ArtifactUploader):
112
113
Attributes
113
114
----------
114
115
artifact_path: str
115
- The model artifact location.
116
+ The model artifact location. Possible values are:
117
+ - object storage path to zip archive. Example: `oci://<bucket_name>@<namespace>/prefix/mymodel.zip`.
118
+ - local path to zip archive. Example: `./mymodel.zip`.
119
+ - local path to folder with artifacts. Example: `./mymodel`.
116
120
artifact_zip_path: str
117
121
The uri of the zip of model artifact.
118
122
auth: dict
@@ -124,6 +128,8 @@ class LargeArtifactUploader(ArtifactUploader):
124
128
The OCI Object Storage URI where model artifacts will be copied to.
125
129
The `bucket_uri` is only necessary for uploading large artifacts which
126
130
size is greater than 2GB. Example: `oci://<bucket_name>@<namespace>/prefix/`.
131
+ .. versionadded:: 2.8.10
132
+ If artifact_path is object storage path to a zip archive, bucket_uri will be ignored.
127
133
dsc_model: OCIDataScienceModel
128
134
The data scince model instance.
129
135
overwrite_existing_artifact: bool
@@ -145,7 +151,7 @@ def __init__(
145
151
self ,
146
152
dsc_model : OCIDataScienceModel ,
147
153
artifact_path : str ,
148
- bucket_uri : str ,
154
+ bucket_uri : str = None ,
149
155
auth : Optional [Dict ] = None ,
150
156
region : Optional [str ] = None ,
151
157
overwrite_existing_artifact : Optional [bool ] = True ,
@@ -159,11 +165,16 @@ def __init__(
159
165
dsc_model: OCIDataScienceModel
160
166
The data scince model instance.
161
167
artifact_path: str
162
- The model artifact location.
163
- bucket_uri: str
168
+ The model artifact location. Possible values are:
169
+ - object storage path to zip archive. Example: `oci://<bucket_name>@<namespace>/prefix/mymodel.zip`.
170
+ - local path to zip archive. Example: `./mymodel.zip`.
171
+ - local path to folder with artifacts. Example: `./mymodel`.
172
+ bucket_uri: (str, optional). Defaults to `None`.
164
173
The OCI Object Storage URI where model artifacts will be copied to.
165
- The `bucket_uri` is only necessary for uploading large artifacts which
174
+ The `bucket_uri` is only necessary for uploading large artifacts from local which
166
175
size is greater than 2GB. Example: `oci://<bucket_name>@<namespace>/prefix/`.
176
+ .. versionadded:: 2.8.10
177
+ If `artifact_path` is object storage path to a zip archive, `bucket_uri` will be ignored.
167
178
auth: (Dict, optional). Defaults to `None`.
168
179
The default authetication is set using `ads.set_auth` API.
169
180
If you need to override the default, use the `ads.common.auth.api_keys` or
@@ -179,11 +190,22 @@ def __init__(
179
190
parallel_process_count: (int, optional).
180
191
The number of worker processes to use in parallel for uploading individual parts of a multipart upload.
181
192
"""
193
+ self .auth = auth or dsc_model .auth
194
+ if ObjectStorageDetails .is_oci_path (artifact_path ):
195
+ if not artifact_path .endswith (".zip" ):
196
+ raise ValueError (
197
+ f"The `artifact_path={ artifact_path } ` is invalid."
198
+ "The remote path for model artifact should be a zip archive, "
199
+ "e.g. `oci://<bucket_name>@<namespace>/prefix/mymodel.zip`."
200
+ )
201
+ if not utils .is_path_exists (uri = artifact_path , auth = self .auth ):
202
+ raise ValueError (f"The `{ artifact_path } ` does not exist." )
203
+ bucket_uri = artifact_path
204
+
182
205
if not bucket_uri :
183
206
raise ValueError ("The `bucket_uri` must be provided." )
184
207
185
208
super ().__init__ (dsc_model = dsc_model , artifact_path = artifact_path )
186
- self .auth = auth or dsc_model .auth
187
209
self .region = region or utils .extract_region (self .auth )
188
210
self .bucket_uri = bucket_uri
189
211
self .overwrite_existing_artifact = overwrite_existing_artifact
@@ -192,38 +214,38 @@ def __init__(
192
214
193
215
def _upload (self ):
194
216
"""Uploads model artifacts to the model catalog."""
195
- self .progress .update ("Copying model artifact to the Object Storage bucket" )
196
-
197
217
bucket_uri = self .bucket_uri
198
- bucket_uri_file_name = os .path .basename (bucket_uri )
199
-
200
- if not bucket_uri_file_name :
201
- bucket_uri = os .path .join (bucket_uri , f"{ self .dsc_model .id } .zip" )
202
- elif not bucket_uri .lower ().endswith (".zip" ):
203
- bucket_uri = f"{ bucket_uri } .zip"
204
-
205
- if not self .overwrite_existing_artifact and utils .is_path_exists (
206
- uri = bucket_uri , auth = self .auth
207
- ):
208
- raise FileExistsError (
209
- f"The bucket_uri=`{ self .bucket_uri } ` exists. Please use a new file name or "
210
- "set `overwrite_existing_artifact` to `True` if you wish to overwrite."
211
- )
218
+ self .progress .update ("Copying model artifact to the Object Storage bucket" )
219
+ if not bucket_uri == self .artifact_zip_path :
220
+ bucket_uri_file_name = os .path .basename (bucket_uri )
221
+
222
+ if not bucket_uri_file_name :
223
+ bucket_uri = os .path .join (bucket_uri , f"{ self .dsc_model .id } .zip" )
224
+ elif not bucket_uri .lower ().endswith (".zip" ):
225
+ bucket_uri = f"{ bucket_uri } .zip"
226
+
227
+ if not self .overwrite_existing_artifact and utils .is_path_exists (
228
+ uri = bucket_uri , auth = self .auth
229
+ ):
230
+ raise FileExistsError (
231
+ f"The bucket_uri=`{ self .bucket_uri } ` exists. Please use a new file name or "
232
+ "set `overwrite_existing_artifact` to `True` if you wish to overwrite."
233
+ )
212
234
213
- try :
214
- utils .upload_to_os (
215
- src_uri = self .artifact_zip_path ,
216
- dst_uri = bucket_uri ,
217
- auth = self .auth ,
218
- parallel_process_count = self ._parallel_process_count ,
219
- force_overwrite = self .overwrite_existing_artifact ,
220
- progressbar_description = "Copying model artifact to the Object Storage bucket." ,
221
- )
222
- except Exception as ex :
223
- raise RuntimeError (
224
- f"Failed to upload model artifact to the given Object Storage path `{ self .bucket_uri } `."
225
- f"See Exception: { ex } "
226
- )
235
+ try :
236
+ utils .upload_to_os (
237
+ src_uri = self .artifact_zip_path ,
238
+ dst_uri = bucket_uri ,
239
+ auth = self .auth ,
240
+ parallel_process_count = self ._parallel_process_count ,
241
+ force_overwrite = self .overwrite_existing_artifact ,
242
+ progressbar_description = "Copying model artifact to the Object Storage bucket." ,
243
+ )
244
+ except Exception as ex :
245
+ raise RuntimeError (
246
+ f"Failed to upload model artifact to the given Object Storage path `{ self .bucket_uri } `."
247
+ f"See Exception: { ex } "
248
+ )
227
249
228
250
self .progress .update ("Exporting model artifact to the model catalog" )
229
251
self .dsc_model .export_model_artifact (bucket_uri = bucket_uri , region = self .region )
0 commit comments