-
Notifications
You must be signed in to change notification settings - Fork 350
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
227 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from google.cloud import aiplatform | ||
from typing import Dict | ||
|
||
# [START aiplatform_sdk_explain_tabular_sample] | ||
def explain_tabular_sample(project: str, location: str, endpoint_id: str, instance_dict: Dict): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
endpoint = aiplatform.Endpoint(endpoint_id) | ||
|
||
response = endpoint.explain(instances=[instance_dict], parameters={}) | ||
|
||
for prediction_ in response.predictions: | ||
print(prediction_) | ||
|
||
|
||
# [END aiplatform_sdk_explain_tabular_sample] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import explain_tabular_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_explain_tabular_sample(mock_sdk_init, mock_endpoint, mock_get_endpoint, mock_endpoint_explain): | ||
|
||
explain_tabular_sample.explain_tabular_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
endpoint_id=constants.ENDPOINT_NAME, | ||
instance_dict=constants.PREDICTION_TABULAR_INSTANCE, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
|
||
mock_get_endpoint.assert_called_once_with( | ||
constants.ENDPOINT_NAME, | ||
) | ||
|
||
mock_endpoint_explain.assert_called_once_with( | ||
instances=[constants.PREDICTION_TABULAR_INSTANCE], | ||
parameters={} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
samples/model-builder/upload_model_explain_tabular_managed_container_sample.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from google.cloud import aiplatform | ||
from typing import Optional, Sequence, Tuple, Dict | ||
from google.cloud.aiplatform import explain | ||
|
||
# [START aiplatform_sdk_upload_model_explain_tabular_managed_container_sample] | ||
def upload_model_explain_tabular_managed_container_sample( | ||
project, | ||
location, | ||
model_display_name: str, | ||
serving_container_image_uri: str, | ||
artifact_uri: Optional[str] = None, | ||
serving_container_predict_route: Optional[str] = None, | ||
serving_container_health_route: Optional[str] = None, | ||
description: Optional[str] = None, | ||
serving_container_command: Optional[Sequence[str]] = None, | ||
serving_container_args: Optional[Sequence[str]] = None, | ||
serving_container_environment_variables: Optional[Dict[str, str]] = None, | ||
serving_container_ports: Optional[Sequence[int]] = None, | ||
instance_schema_uri: Optional[str] = None, | ||
parameters_schema_uri: Optional[str] = None, | ||
prediction_schema_uri: Optional[str] = None, | ||
explanation_metadata: Optional[explain.ExplanationMetadata] = None, | ||
explanation_parameters: Optional[explain.ExplanationParameters] = None, | ||
sync: bool = True, | ||
): | ||
|
||
aiplatform.init(project=project, location=location) | ||
|
||
model = aiplatform.Model.upload( | ||
display_name=model_display_name, | ||
serving_container_image_uri=serving_container_image_uri, | ||
artifact_uri=artifact_uri, | ||
serving_container_predict_route=serving_container_predict_route, | ||
serving_container_health_route=serving_container_health_route, | ||
description=description, | ||
serving_container_command=serving_container_command, | ||
serving_container_args=serving_container_args, | ||
serving_container_environment_variables=serving_container_environment_variables, | ||
serving_container_ports=serving_container_ports, | ||
instance_schema_uri=instance_schema_uri, | ||
parameters_schema_uri=parameters_schema_uri, | ||
prediction_schema_uri=prediction_schema_uri, | ||
explanation_metadata=explanation_metadata, | ||
explanation_parameters=explanation_parameters, | ||
sync=sync, | ||
) | ||
|
||
model.wait() | ||
|
||
print(model.display_name) | ||
print(model.resource_name) | ||
return model | ||
|
||
|
||
# [END aiplatform_sdk_upload_model_explain_tabular_managed_container_sample] |
63 changes: 63 additions & 0 deletions
63
samples/model-builder/upload_model_explain_tabular_managed_container_sample_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
import upload_model_explain_tabular_managed_container_sample | ||
import test_constants as constants | ||
|
||
|
||
def test_upload_model_explain_tabular_managed_container_sample(mock_sdk_init, mock_model, mock_init_model, mock_upload_model): | ||
|
||
upload_model_explain_tabular_managed_container_sample.upload_model_explain_tabular_managed_container_sample( | ||
project=constants.PROJECT, | ||
location=constants.LOCATION, | ||
model_display_name=constants.MODEL_NAME, | ||
serving_container_image_uri=constants.SERVING_CONTAINER_IMAGE_URI, | ||
artifact_uri=constants.MODEL_ARTIFACT_URI, | ||
serving_container_predict_route=constants.SERVING_CONTAINER_PREDICT_ROUTE, | ||
serving_container_health_route=constants.SERVING_CONTAINER_HEALTH_ROUTE, | ||
description=constants.DESCRIPTION, | ||
serving_container_command=constants.SERVING_CONTAINER_COMMAND, | ||
serving_container_args=constants.SERVING_CONTAINER_ARGS, | ||
serving_container_environment_variables=constants.SERVING_CONTAINER_ENVIRONMENT_VARIABLES, | ||
serving_container_ports=constants.SERVING_CONTAINER_PORTS, | ||
instance_schema_uri=constants.INSTANCE_SCHEMA_URI, | ||
parameters_schema_uri=constants.PARAMETERS_SCHEMA_URI, | ||
prediction_schema_uri=constants.PREDICTION_SCHEMA_URI, | ||
explanation_metadata=constants.EXPLANATION_METADATA, | ||
explanation_parameters=constants.EXPLANATION_PARAMETERS, | ||
) | ||
|
||
mock_sdk_init.assert_called_once_with( | ||
project=constants.PROJECT, location=constants.LOCATION | ||
) | ||
|
||
mock_upload_model.assert_called_once_with( | ||
display_name=constants.MODEL_NAME, | ||
serving_container_image_uri=constants.SERVING_CONTAINER_IMAGE_URI, | ||
artifact_uri=constants.MODEL_ARTIFACT_URI, | ||
serving_container_predict_route=constants.SERVING_CONTAINER_PREDICT_ROUTE, | ||
serving_container_health_route=constants.SERVING_CONTAINER_HEALTH_ROUTE, | ||
description=constants.DESCRIPTION, | ||
serving_container_command=constants.SERVING_CONTAINER_COMMAND, | ||
serving_container_args=constants.SERVING_CONTAINER_ARGS, | ||
serving_container_environment_variables=constants.SERVING_CONTAINER_ENVIRONMENT_VARIABLES, | ||
serving_container_ports=constants.SERVING_CONTAINER_PORTS, | ||
instance_schema_uri=constants.INSTANCE_SCHEMA_URI, | ||
parameters_schema_uri=constants.PARAMETERS_SCHEMA_URI, | ||
prediction_schema_uri=constants.PREDICTION_SCHEMA_URI, | ||
explanation_metadata=constants.EXPLANATION_METADATA, | ||
explanation_parameters=constants.EXPLANATION_PARAMETERS, | ||
sync=True, | ||
) |