4
4
5
5
# pylint: disable=protected-access
6
6
7
+ from contextlib import contextmanager
7
8
from os import PathLike , path
8
9
from typing import Dict , Iterable , Optional , Union
9
- from contextlib import contextmanager
10
10
11
11
from marshmallow .exceptions import ValidationError as SchemaValidationError
12
12
13
- from azure .ai .ml ._utils ._experimental import experimental
14
13
from azure .ai .ml ._artifacts ._artifact_utilities import (
15
14
_check_and_upload_path ,
16
15
_get_default_datastore_info ,
21
20
CHANGED_ASSET_PATH_MSG ,
22
21
CHANGED_ASSET_PATH_MSG_NO_PERSONAL_DATA ,
23
22
)
24
- from azure .ai .ml ._utils ._arm_id_utils import is_ARM_id_for_resource
25
- from azure .ai .ml ._utils ._registry_utils import get_registry_client
26
23
from azure .ai .ml ._exception_helper import log_and_raise_error
27
24
from azure .ai .ml ._restclient .v2021_10_01_dataplanepreview import (
28
25
AzureMachineLearningWorkspaces as ServiceClient102021Dataplane ,
29
26
)
30
- from azure .ai .ml ._restclient .v2023_04_01_preview .models import ListViewType , ModelVersion
31
27
from azure .ai .ml ._restclient .v2023_04_01_preview import AzureMachineLearningWorkspaces as ServiceClient042023Preview
28
+ from azure .ai .ml ._restclient .v2023_04_01_preview .models import ListViewType , ModelVersion
32
29
from azure .ai .ml ._scope_dependent_operations import (
33
30
OperationConfig ,
31
+ OperationsContainer ,
34
32
OperationScope ,
35
33
_ScopeDependentOperations ,
36
- OperationsContainer ,
37
34
)
38
- from azure .ai .ml .entities ._assets ._artifacts .code import Code
39
-
40
- from azure .ai .ml .constants ._common import ARM_ID_PREFIX
41
35
from azure .ai .ml ._telemetry import ActivityType , monitor_with_activity
36
+ from azure .ai .ml ._utils ._arm_id_utils import is_ARM_id_for_resource
42
37
from azure .ai .ml ._utils ._asset_utils import (
43
38
_archive_or_restore ,
44
39
_get_latest ,
45
- _resolve_label_to_asset ,
46
40
_get_next_version_from_container ,
41
+ _resolve_label_to_asset ,
47
42
)
43
+ from azure .ai .ml ._utils ._experimental import experimental
48
44
from azure .ai .ml ._utils ._logger_utils import OpsLogger
49
45
from azure .ai .ml ._utils ._registry_utils import (
50
46
get_asset_body_for_registry_storage ,
47
+ get_registry_client ,
51
48
get_sas_uri_for_registry_asset ,
52
49
get_storage_details_for_registry_assets ,
53
50
)
54
51
from azure .ai .ml ._utils ._storage_utils import get_ds_name_and_path_prefix , get_storage_client
55
52
from azure .ai .ml ._utils .utils import resolve_short_datastore_url , validate_ml_flow_folder
56
- from azure .ai .ml .constants ._common import ASSET_ID_FORMAT , AzureMLResourceType
57
- from azure .ai .ml .entities ._assets import Model , ModelPackage , Environment
53
+ from azure .ai .ml .constants ._common import ARM_ID_PREFIX , ASSET_ID_FORMAT , AzureMLResourceType
54
+ from azure .ai .ml .entities ._assets import Environment , Model , ModelPackage
55
+ from azure .ai .ml .entities ._assets ._artifacts .code import Code
58
56
from azure .ai .ml .entities ._assets .workspace_asset_reference import WorkspaceAssetReference
59
57
from azure .ai .ml .entities ._credentials import AccountKeyConfiguration
60
58
from azure .ai .ml .exceptions import (
64
62
ValidationErrorType ,
65
63
ValidationException ,
66
64
)
67
- from azure .core .exceptions import ResourceNotFoundError
68
65
from azure .ai .ml .operations ._datastore_operations import DatastoreOperations
66
+ from azure .core .exceptions import ResourceNotFoundError
67
+
69
68
from ._operation_orchestrator import OperationOrchestrator
70
69
71
70
ops_logger = OpsLogger (__name__ )
@@ -77,6 +76,21 @@ class ModelOperations(_ScopeDependentOperations):
77
76
78
77
You should not instantiate this class directly. Instead, you should create an MLClient instance that instantiates it
79
78
for you and attaches it as an attribute.
79
+
80
+ :param operation_scope: Scope variables for the operations classes of an MLClient object.
81
+ :type operation_scope: ~azure.ai.ml._scope_dependent_operations.OperationScope
82
+ :param operation_config: Common configuration for operations classes of an MLClient object.
83
+ :type operation_config: ~azure.ai.ml._scope_dependent_operations.OperationConfig
84
+ :param service_client: Service client to allow end users to operate on Azure Machine Learning Workspace
85
+ resources (ServiceClient042023Preview or ServiceClient102021Dataplane).
86
+ :type service_client: typing.Union[
87
+ ~azure.ai.ml._restclient.v2023_04_01_preview._azure_machine_learning_workspaces.AzureMachineLearningWorkspaces,
88
+ ~azure.ai.ml._restclient.v2021_10_01_dataplanepreview._azure_machine_learning_workspaces.
89
+ AzureMachineLearningWorkspaces]
90
+ :param datastore_operations: Represents a client for performing operations on Datastores.
91
+ :type datastore_operations: ~azure.ai.ml.operations._datastore_operations.DatastoreOperations
92
+ :param all_operations: All operations classes of an MLClient object.
93
+ :type all_operations: ~azure.ai.ml._scope_dependent_operations.OperationsContainer
80
94
"""
81
95
82
96
# pylint: disable=unused-argument
@@ -116,6 +130,15 @@ def create_or_update(
116
130
:raises ~azure.ai.ml.exceptions.EmptyDirectoryError: Raised if local path provided points to an empty directory.
117
131
:return: Model asset object.
118
132
:rtype: ~azure.ai.ml.entities.Model
133
+
134
+ .. admonition:: Example:
135
+
136
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
137
+ :start-after: [START model_operations_create_or_update]
138
+ :end-before: [END model_operations_create_or_update]
139
+ :language: python
140
+ :dedent: 8
141
+ :caption: Create model example.
119
142
"""
120
143
try :
121
144
name = model .name
@@ -274,6 +297,15 @@ def get(self, name: str, version: Optional[str] = None, label: Optional[str] = N
274
297
Details will be provided in the error message.
275
298
:return: Model asset object.
276
299
:rtype: ~azure.ai.ml.entities.Model
300
+
301
+ .. admonition:: Example:
302
+
303
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
304
+ :start-after: [START model_operations_get]
305
+ :end-before: [END model_operations_get]
306
+ :language: python
307
+ :dedent: 8
308
+ :caption: Get model example.
277
309
"""
278
310
if version and label :
279
311
msg = "Cannot specify both version and label."
@@ -306,11 +338,23 @@ def get(self, name: str, version: Optional[str] = None, label: Optional[str] = N
306
338
def download (self , name : str , version : str , download_path : Union [PathLike , str ] = "." ) -> None :
307
339
"""Download files related to a model.
308
340
309
- :param str name: Name of the model.
310
- :param str version: Version of the model.
311
- :param Union[PathLike, str] download_path: Local path as download destination,
312
- defaults to current working directory of the current user. Contents will be overwritten.
313
- :raise: ResourceNotFoundError if can't find a model matching provided name.
341
+ :param name: Name of the model.
342
+ :type name: str
343
+ :param version: Version of the model.
344
+ :type version: str
345
+ :param download_path: Local path as download destination, defaults to current working directory of the current
346
+ user. Contents will be overwritten.
347
+ :type download_path: Union[PathLike, str]
348
+ :raises ResourceNotFoundError: if can't find a model matching provided name.
349
+
350
+ .. admonition:: Example:
351
+
352
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
353
+ :start-after: [START model_operations_download]
354
+ :end-before: [END model_operations_download]
355
+ :language: python
356
+ :dedent: 8
357
+ :caption: Download files to model example.
314
358
"""
315
359
316
360
model_uri = self .get (name = name , version = version ).path
@@ -379,6 +423,15 @@ def archive(
379
423
:type version: str
380
424
:param label: Label of the model asset. (mutually exclusive with version)
381
425
:type label: str
426
+
427
+ .. admonition:: Example:
428
+
429
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
430
+ :start-after: [START model_operations_archive]
431
+ :end-before: [END model_operations_archive]
432
+ :language: python
433
+ :dedent: 8
434
+ :caption: Archive a model example.
382
435
"""
383
436
_archive_or_restore (
384
437
asset_operations = self ,
@@ -402,6 +455,15 @@ def restore(
402
455
:type version: str
403
456
:param label: Label of the model asset. (mutually exclusive with version)
404
457
:type label: str
458
+
459
+ .. admonition:: Example:
460
+
461
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
462
+ :start-after: [START model_operations_restore]
463
+ :end-before: [END model_operations_restore]
464
+ :language: python
465
+ :dedent: 8
466
+ :caption: Restore a model example.
405
467
"""
406
468
_archive_or_restore (
407
469
asset_operations = self ,
@@ -431,7 +493,16 @@ def list(
431
493
:attr:`ListViewType.ACTIVE_ONLY`.
432
494
:type list_view_type: ListViewType
433
495
:return: An iterator like instance of Model objects
434
- :rtype: ~azure.core.paging.ItemPaged[Model]
496
+ :rtype: ~azure.core.paging.ItemPaged[~azure.ai.ml.entities.Model]
497
+
498
+ .. admonition:: Example:
499
+
500
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
501
+ :start-after: [START model_operations_list]
502
+ :end-before: [END model_operations_list]
503
+ :language: python
504
+ :dedent: 8
505
+ :caption: List all models example.
435
506
"""
436
507
if name :
437
508
return (
@@ -485,6 +556,15 @@ def share(self, name, version, *, share_with_name, share_with_version, registry_
485
556
:paramtype registry_name: str
486
557
:return: Model asset object.
487
558
:rtype: ~azure.ai.ml.entities.Model
559
+
560
+ .. admonition:: Example:
561
+
562
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
563
+ :start-after: [START model_operations_share]
564
+ :end-before: [END model_operations_share]
565
+ :language: python
566
+ :dedent: 8
567
+ :caption: Share a model example.
488
568
"""
489
569
490
570
# Get workspace info to get workspace GUID
@@ -574,6 +654,14 @@ def package(self, name: str, version: str, package_request: ModelPackage, **kwar
574
654
:return: Environment object
575
655
:rtype: ~azure.ai.ml.entities.Environment
576
656
657
+ .. admonition:: Example:
658
+
659
+ .. literalinclude:: ../../../../samples/ml_samples_misc.py
660
+ :start-after: [START model_operations_package]
661
+ :end-before: [END model_operations_package]
662
+ :language: python
663
+ :dedent: 8
664
+ :caption: Package a model example.
577
665
"""
578
666
579
667
if not kwargs .get ("skip_to_rest" , False ):
0 commit comments