GeezerVison is showing github.com — feat: Added deploy_model_with_dedicated_resources_sample, deploy_mode… · googleapis/python-aiplatform@ef4f6f8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/conftest.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -220,7 +220,7 @@ def mock_model(): | 220 | 220 |
| 221 | 221 |
| 222 | 222 | @pytest.fixture | 223 | | -def mock_get_model(mock_model): | | 223 | +def mock_init_model(mock_model): | 224 | 224 | with patch.object(aiplatform, "Model") as mock: | 225 | 225 | mock.return_value = mock_model | 226 | 226 | yield mock | @@ -232,6 +232,19 @@ def mock_batch_predict_model(mock_model): | 232 | 232 | yield mock | 233 | 233 |
| 234 | 234 |
| | 235 | +@pytest.fixture | | 236 | +def mock_upload_model(): | | 237 | + with patch.object(aiplatform.models.Model, "upload") as mock: | | 238 | + yield mock | | 239 | + | | 240 | + | | 241 | +@pytest.fixture | | 242 | +def mock_deploy_model(mock_model, mock_endpoint): | | 243 | + with patch.object(mock_model, "deploy") as mock: | | 244 | + mock.return_value = mock_endpoint | | 245 | + yield mock | | 246 | + | | 247 | + | 235 | 248 | """ | 236 | 249 | ---------------------------------------------------------------------------- | 237 | 250 | Job Fixtures | |
Collapse file Copy file name to clipboardExpand all lines: samples/model-builder/create_batch_prediction_job_sample_test.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 |
| 19 | 19 |
| 20 | 20 | def test_create_batch_prediction_job_sample( | 21 | | - mock_sdk_init, mock_get_model, mock_batch_predict_model | | 21 | + mock_sdk_init, mock_model, mock_init_model, mock_batch_predict_model | 22 | 22 | ): | 23 | 23 |
| 24 | 24 | create_batch_prediction_job_sample.create_batch_prediction_job_sample( | @@ -33,7 +33,7 @@ def test_create_batch_prediction_job_sample( | 33 | 33 | mock_sdk_init.assert_called_once_with( | 34 | 34 | project=constants.PROJECT, location=constants.LOCATION | 35 | 35 | ) | 36 | | - mock_get_model.assert_called_once_with(constants.MODEL_NAME) | | 36 | + mock_init_model.assert_called_once_with(constants.MODEL_NAME) | 37 | 37 | mock_batch_predict_model.assert_called_once_with( | 38 | 38 | job_display_name=constants.DISPLAY_NAME, | 39 | 39 | gcs_source=constants.GCS_SOURCES, | |
Collapse file Copy file name to clipboard +57Lines changed: 57 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +from typing import Dict, Optional, Sequence, Tuple | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | + | | 19 | + | | 20 | +# [START aiplatform_sdk_deploy_model_with_automatic_resources_sample] | | 21 | +def deploy_model_with_automatic_resources_sample( | | 22 | + project, | | 23 | + location, | | 24 | + model_name: str, | | 25 | + endpoint: Optional[aiplatform.Endpoint] = None, | | 26 | + deployed_model_display_name: Optional[str] = None, | | 27 | + traffic_percentage: Optional[int] = 0, | | 28 | + traffic_split: Optional[Dict[str, int]] = None, | | 29 | + min_replica_count: int = 1, | | 30 | + max_replica_count: int = 1, | | 31 | + metadata: Optional[Sequence[Tuple[str, str]]] = (), | | 32 | + sync: bool = True, | | 33 | +): | | 34 | + | | 35 | + aiplatform.init(project=project, location=location) | | 36 | + | | 37 | + model = aiplatform.Model(model_name=model_name) | | 38 | + | | 39 | + model.deploy( | | 40 | + endpoint=endpoint, | | 41 | + deployed_model_display_name=deployed_model_display_name, | | 42 | + traffic_percentage=traffic_percentage, | | 43 | + traffic_split=traffic_split, | | 44 | + min_replica_count=min_replica_count, | | 45 | + max_replica_count=max_replica_count, | | 46 | + metadata=metadata, | | 47 | + sync=sync, | | 48 | + ) | | 49 | + | | 50 | + model.wait() | | 51 | + | | 52 | + print(model.display_name) | | 53 | + print(model.resource_name) | | 54 | + return model | | 55 | + | | 56 | + | | 57 | +# [END aiplatform_sdk_deploy_model_with_automatic_resources_sample] |
Collapse file Copy file name to clipboard +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | + | | 16 | +import deploy_model_with_automatic_resources_sample | | 17 | +import test_constants as constants | | 18 | + | | 19 | + | | 20 | +def test_deploy_model_with_automatic_resources_sample( | | 21 | + mock_sdk_init, mock_model, mock_init_model, mock_deploy_model, | | 22 | +): | | 23 | + | | 24 | + deploy_model_with_automatic_resources_sample.deploy_model_with_automatic_resources_sample( | | 25 | + project=constants.PROJECT, | | 26 | + location=constants.LOCATION, | | 27 | + model_name=constants.MODEL_NAME, | | 28 | + endpoint=constants.ENDPOINT_NAME, | | 29 | + deployed_model_display_name=constants.DEPLOYED_MODEL_DISPLAY_NAME, | | 30 | + traffic_percentage=constants.TRAFFIC_PERCENTAGE, | | 31 | + traffic_split=constants.TRAFFIC_SPLIT, | | 32 | + min_replica_count=constants.MIN_REPLICA_COUNT, | | 33 | + max_replica_count=constants.MAX_REPLICA_COUNT, | | 34 | + metadata=constants.ENDPOINT_DEPLOY_METADATA, | | 35 | + ) | | 36 | + | | 37 | + mock_sdk_init.assert_called_once_with( | | 38 | + project=constants.PROJECT, location=constants.LOCATION | | 39 | + ) | | 40 | + | | 41 | + mock_init_model.assert_called_once_with(model_name=constants.MODEL_NAME) | | 42 | + | | 43 | + mock_deploy_model.assert_called_once_with( | | 44 | + endpoint=constants.ENDPOINT_NAME, | | 45 | + deployed_model_display_name=constants.DEPLOYED_MODEL_DISPLAY_NAME, | | 46 | + traffic_percentage=constants.TRAFFIC_PERCENTAGE, | | 47 | + traffic_split=constants.TRAFFIC_SPLIT, | | 48 | + min_replica_count=constants.MIN_REPLICA_COUNT, | | 49 | + max_replica_count=constants.MAX_REPLICA_COUNT, | | 50 | + metadata=constants.ENDPOINT_DEPLOY_METADATA, | | 51 | + sync=True, | | 52 | + ) |
Collapse file Copy file name to clipboard +70Lines changed: 70 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | +from typing import Dict, Optional, Sequence, Tuple | | 16 | + | | 17 | +from google.cloud import aiplatform | | 18 | +from google.cloud.aiplatform import explain | | 19 | + | | 20 | + | | 21 | +# [START aiplatform_sdk_deploy_model_with_dedicated_resources_sample] | | 22 | +def deploy_model_with_dedicated_resources_sample( | | 23 | + project, | | 24 | + location, | | 25 | + model_name: str, | | 26 | + machine_type: str, | | 27 | + endpoint: Optional[aiplatform.Endpoint] = None, | | 28 | + deployed_model_display_name: Optional[str] = None, | | 29 | + traffic_percentage: Optional[int] = 0, | | 30 | + traffic_split: Optional[Dict[str, int]] = None, | | 31 | + min_replica_count: int = 1, | | 32 | + max_replica_count: int = 1, | | 33 | + accelerator_type: Optional[str] = None, | | 34 | + accelerator_count: Optional[int] = None, | | 35 | + explanation_metadata: Optional[explain.ExplanationMetadata] = None, | | 36 | + explanation_parameters: Optional[explain.ExplanationParameters] = None, | | 37 | + metadata: Optional[Sequence[Tuple[str, str]]] = (), | | 38 | + sync: bool = True, | | 39 | +): | | 40 | + | | 41 | + aiplatform.init(project=project, location=location) | | 42 | + | | 43 | + model = aiplatform.Model(model_name=model_name) | | 44 | + | | 45 | + # The explanation_metadata and explanation_parameters should only be | | 46 | + # provided for a custom trained model and not an AutoML model. | | 47 | + model.deploy( | | 48 | + endpoint=endpoint, | | 49 | + deployed_model_display_name=deployed_model_display_name, | | 50 | + traffic_percentage=traffic_percentage, | | 51 | + traffic_split=traffic_split, | | 52 | + machine_type=machine_type, | | 53 | + min_replica_count=min_replica_count, | | 54 | + max_replica_count=max_replica_count, | | 55 | + accelerator_type=accelerator_type, | | 56 | + accelerator_count=accelerator_count, | | 57 | + explanation_metadata=explanation_metadata, | | 58 | + explanation_parameters=explanation_parameters, | | 59 | + metadata=metadata, | | 60 | + sync=sync, | | 61 | + ) | | 62 | + | | 63 | + model.wait() | | 64 | + | | 65 | + print(model.display_name) | | 66 | + print(model.resource_name) | | 67 | + return model | | 68 | + | | 69 | + | | 70 | +# [END aiplatform_sdk_deploy_model_with_dedicated_resources_sample] |
Collapse file Copy file name to clipboard +62Lines changed: 62 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | + | | 16 | +import deploy_model_with_dedicated_resources_sample | | 17 | +import test_constants as constants | | 18 | + | | 19 | + | | 20 | +def test_deploy_model_with_dedicated_resources_sample( | | 21 | + mock_sdk_init, mock_model, mock_init_model, mock_deploy_model | | 22 | +): | | 23 | + | | 24 | + deploy_model_with_dedicated_resources_sample.deploy_model_with_dedicated_resources_sample( | | 25 | + project=constants.PROJECT, | | 26 | + location=constants.LOCATION, | | 27 | + machine_type=constants.MACHINE_TYPE, | | 28 | + model_name=constants.MODEL_NAME, | | 29 | + endpoint=constants.ENDPOINT_NAME, | | 30 | + deployed_model_display_name=constants.DEPLOYED_MODEL_DISPLAY_NAME, | | 31 | + traffic_percentage=constants.TRAFFIC_PERCENTAGE, | | 32 | + traffic_split=constants.TRAFFIC_SPLIT, | | 33 | + min_replica_count=constants.MIN_REPLICA_COUNT, | | 34 | + max_replica_count=constants.MAX_REPLICA_COUNT, | | 35 | + accelerator_type=constants.ACCELERATOR_TYPE, | | 36 | + accelerator_count=constants.ACCELERATOR_COUNT, | | 37 | + explanation_metadata=constants.EXPLANATION_METADATA, | | 38 | + explanation_parameters=constants.EXPLANATION_PARAMETERS, | | 39 | + metadata=constants.ENDPOINT_DEPLOY_METADATA, | | 40 | + ) | | 41 | + | | 42 | + mock_sdk_init.assert_called_once_with( | | 43 | + project=constants.PROJECT, location=constants.LOCATION | | 44 | + ) | | 45 | + | | 46 | + mock_init_model.assert_called_once_with(model_name=constants.MODEL_NAME) | | 47 | + | | 48 | + mock_deploy_model.assert_called_once_with( | | 49 | + endpoint=constants.ENDPOINT_NAME, | | 50 | + deployed_model_display_name=constants.DEPLOYED_MODEL_DISPLAY_NAME, | | 51 | + traffic_percentage=constants.TRAFFIC_PERCENTAGE, | | 52 | + traffic_split=constants.TRAFFIC_SPLIT, | | 53 | + machine_type=constants.MACHINE_TYPE, | | 54 | + min_replica_count=constants.MIN_REPLICA_COUNT, | | 55 | + max_replica_count=constants.MAX_REPLICA_COUNT, | | 56 | + accelerator_type=constants.ACCELERATOR_TYPE, | | 57 | + accelerator_count=constants.ACCELERATOR_COUNT, | | 58 | + explanation_metadata=constants.EXPLANATION_METADATA, | | 59 | + explanation_parameters=constants.EXPLANATION_PARAMETERS, | | 60 | + metadata=constants.ENDPOINT_DEPLOY_METADATA, | | 61 | + sync=True, | | 62 | + ) |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | + | | 16 | +from google.cloud import aiplatform | | 17 | + | | 18 | + | | 19 | +# [START aiplatform_sdk_get_model_sample] | | 20 | +def get_model_sample(project: str, location: str, model_name: str): | | 21 | + | | 22 | + aiplatform.init(project=project, location=location) | | 23 | + | | 24 | + model = aiplatform.Model(model_name=model_name) | | 25 | + | | 26 | + print(model.display_name) | | 27 | + print(model.resource_name) | | 28 | + return model | | 29 | + | | 30 | + | | 31 | +# [END aiplatform_sdk_get_model_sample] |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Copyright 2021 Google LLC | | 2 | +# | | 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); | | 4 | +# you may not use this file except in compliance with the License. | | 5 | +# You may obtain a copy of the License at | | 6 | +# | | 7 | +# https://www.apache.org/licenses/LICENSE-2.0 | | 8 | +# | | 9 | +# Unless required by applicable law or agreed to in writing, software | | 10 | +# distributed under the License is distributed on an "AS IS" BASIS, | | 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | | 12 | +# See the License for the specific language governing permissions and | | 13 | +# limitations under the License. | | 14 | + | | 15 | + | | 16 | +import get_model_sample | | 17 | +import test_constants as constants | | 18 | + | | 19 | + | | 20 | +def test_get_model_sample(mock_sdk_init, mock_init_model): | | 21 | + | | 22 | + get_model_sample.get_model_sample( | | 23 | + project=constants.PROJECT, | | 24 | + location=constants.LOCATION, | | 25 | + model_name=constants.MODEL_NAME, | | 26 | + ) | | 27 | + | | 28 | + mock_sdk_init.assert_called_once_with( | | 29 | + project=constants.PROJECT, location=constants.LOCATION | | 30 | + ) | | 31 | + | | 32 | + mock_init_model.assert_called_once_with(model_name=constants.MODEL_NAME) |
|