diff --git a/samples/model-builder/conftest.py b/samples/model-builder/conftest.py index 01e944b19c..3f60573613 100644 --- a/samples/model-builder/conftest.py +++ b/samples/model-builder/conftest.py @@ -138,17 +138,58 @@ def mock_import_text_dataset(mock_text_dataset): @pytest.fixture -def mock_init_automl_image_training_job(): - with patch.object( - aiplatform.training_jobs.AutoMLImageTrainingJob, "__init__" - ) as mock: - mock.return_value = None +def mock_custom_training_job(): + mock = MagicMock(aiplatform.training_jobs.CustomTrainingJob) + yield mock + + +@pytest.fixture +def mock_image_training_job(): + mock = MagicMock(aiplatform.training_jobs.AutoMLImageTrainingJob) + yield mock + + +@pytest.fixture +def mock_tabular_training_job(): + mock = MagicMock(aiplatform.training_jobs.AutoMLTabularTrainingJob) + yield mock + + +@pytest.fixture +def mock_text_training_job(): + mock = MagicMock(aiplatform.training_jobs.AutoMLTextTrainingJob) + yield mock + + +@pytest.fixture +def mock_video_training_job(): + mock = MagicMock(aiplatform.training_jobs.AutoMLVideoTrainingJob) + yield mock + + +@pytest.fixture +def mock_get_automl_tabular_training_job(mock_tabular_training_job): + with patch.object(aiplatform, "AutoMLTabularTrainingJob") as mock: + mock.return_value = mock_tabular_training_job + yield mock + + +@pytest.fixture +def mock_run_automl_tabular_training_job(mock_tabular_training_job): + with patch.object(mock_tabular_training_job, "run") as mock: + yield mock + + +@pytest.fixture +def mock_get_automl_image_training_job(mock_image_training_job): + with patch.object(aiplatform, "AutoMLImageTrainingJob") as mock: + mock.return_value = mock_image_training_job yield mock @pytest.fixture -def mock_run_automl_image_training_job(): - with patch.object(aiplatform.training_jobs.AutoMLImageTrainingJob, "run") as mock: +def mock_run_automl_image_training_job(mock_image_training_job): + with patch.object(mock_image_training_job, "run") as mock: yield mock @@ -173,15 +214,21 @@ def mock_run_custom_training_job(): @pytest.fixture -def mock_init_model(): - with patch.object(aiplatform.models.Model, "__init__") as mock: - mock.return_value = None +def mock_model(): + mock = MagicMock(aiplatform.models.Model) + yield mock + + +@pytest.fixture +def mock_get_model(mock_model): + with patch.object(aiplatform, "Model") as mock: + mock.return_value = mock_model yield mock @pytest.fixture -def mock_batch_predict_model(): - with patch.object(aiplatform.models.Model, "batch_predict") as mock: +def mock_batch_predict_model(mock_model): + with patch.object(mock_model, "batch_predict") as mock: yield mock @@ -211,6 +258,12 @@ def mock_endpoint(): yield mock +@pytest.fixture +def mock_create_endpoint(): + with patch.object(aiplatform.Endpoint, "create") as mock: + yield mock + + @pytest.fixture def mock_get_endpoint(mock_endpoint): with patch.object(aiplatform, "Endpoint") as mock_get_endpoint: diff --git a/samples/model-builder/create_and_import_dataset_tabular_bigquery_sample.py b/samples/model-builder/create_and_import_dataset_tabular_bigquery_sample.py new file mode 100644 index 0000000000..b7f4ea8013 --- /dev/null +++ b/samples/model-builder/create_and_import_dataset_tabular_bigquery_sample.py @@ -0,0 +1,36 @@ +# 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 + + +# [START aiplatform_sdk_create_and_import_dataset_tabular_bigquery_sample] +def create_and_import_dataset_tabular_bigquery_sample( + display_name: str, project: str, location: str, bigquery_source: str, +): + + aiplatform.init(project=project, location=location) + + dataset = aiplatform.TabularDataset.create( + display_name=display_name, bigquery_source=bigquery_source, + ) + + dataset.wait() + + print(f'\tDataset: "{dataset.display_name}"') + print(f'\tname: "{dataset.resource_name}"') + + +# [END aiplatform_sdk_create_and_import_dataset_tabular_bigquery_sample] diff --git a/samples/model-builder/create_and_import_dataset_tabular_bigquery_sample_test.py b/samples/model-builder/create_and_import_dataset_tabular_bigquery_sample_test.py new file mode 100644 index 0000000000..6eefcf7702 --- /dev/null +++ b/samples/model-builder/create_and_import_dataset_tabular_bigquery_sample_test.py @@ -0,0 +1,36 @@ +# 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 create_and_import_dataset_tabular_bigquery_sample +import test_constants as constants + + +def test_create_and_import_dataset_tabular_bigquery_sample( + mock_sdk_init, mock_create_tabular_dataset +): + + create_and_import_dataset_tabular_bigquery_sample.create_and_import_dataset_tabular_bigquery_sample( + project=constants.PROJECT, + location=constants.LOCATION, + bigquery_source=constants.BIGQUERY_SOURCE, + display_name=constants.DISPLAY_NAME, + ) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + mock_create_tabular_dataset.assert_called_once_with( + display_name=constants.DISPLAY_NAME, bigquery_source=constants.BIGQUERY_SOURCE, + ) diff --git a/samples/model-builder/create_and_import_dataset_tabular_gcs_sample.py b/samples/model-builder/create_and_import_dataset_tabular_gcs_sample.py new file mode 100644 index 0000000000..cac7a64d89 --- /dev/null +++ b/samples/model-builder/create_and_import_dataset_tabular_gcs_sample.py @@ -0,0 +1,37 @@ +# 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 typing import List, Union + +from google.cloud import aiplatform + + +# [START aiplatform_sdk_create_and_import_dataset_tabular_gcs_sample] +def create_and_import_dataset_tabular_gcs_sample( + display_name: str, project: str, location: str, gcs_source: Union[str, List[str]], +): + + aiplatform.init(project=project, location=location) + + dataset = aiplatform.TabularDataset.create( + display_name=display_name, gcs_source=gcs_source, + ) + + dataset.wait() + + print(f'\tDataset: "{dataset.display_name}"') + print(f'\tname: "{dataset.resource_name}"') + + +# [END aiplatform_sdk_create_and_import_dataset_tabular_gcs_sample] diff --git a/samples/model-builder/create_and_import_dataset_tabular_gcs_sample_test.py b/samples/model-builder/create_and_import_dataset_tabular_gcs_sample_test.py new file mode 100644 index 0000000000..ca8679be01 --- /dev/null +++ b/samples/model-builder/create_and_import_dataset_tabular_gcs_sample_test.py @@ -0,0 +1,36 @@ +# 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 create_and_import_dataset_tabular_gcs_sample +import test_constants as constants + + +def test_create_and_import_dataset_tabular_gcs_sample( + mock_sdk_init, mock_create_tabular_dataset +): + + create_and_import_dataset_tabular_gcs_sample.create_and_import_dataset_tabular_gcs_sample( + project=constants.PROJECT, + location=constants.LOCATION, + gcs_source=constants.GCS_SOURCES, + display_name=constants.DISPLAY_NAME, + ) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + mock_create_tabular_dataset.assert_called_once_with( + display_name=constants.DISPLAY_NAME, gcs_source=constants.GCS_SOURCES, + ) diff --git a/samples/model-builder/create_batch_prediction_job_sample_test.py b/samples/model-builder/create_batch_prediction_job_sample_test.py index f39c1020b5..6d3c68e77e 100644 --- a/samples/model-builder/create_batch_prediction_job_sample_test.py +++ b/samples/model-builder/create_batch_prediction_job_sample_test.py @@ -18,7 +18,7 @@ def test_create_batch_prediction_job_sample( - mock_sdk_init, mock_init_model, mock_batch_predict_model + mock_sdk_init, mock_get_model, mock_batch_predict_model ): create_batch_prediction_job_sample.create_batch_prediction_job_sample( @@ -33,7 +33,7 @@ def test_create_batch_prediction_job_sample( mock_sdk_init.assert_called_once_with( project=constants.PROJECT, location=constants.LOCATION ) - mock_init_model.assert_called_once_with(constants.MODEL_NAME) + mock_get_model.assert_called_once_with(constants.MODEL_NAME) mock_batch_predict_model.assert_called_once_with( job_display_name=constants.DISPLAY_NAME, gcs_source=constants.GCS_SOURCES, diff --git a/samples/model-builder/create_endpoint_sample.py b/samples/model-builder/create_endpoint_sample.py new file mode 100644 index 0000000000..fa3762bd57 --- /dev/null +++ b/samples/model-builder/create_endpoint_sample.py @@ -0,0 +1,34 @@ +# 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 + + +# [START aiplatform_sdk_create_endpoint_sample] +def create_endpoint_sample( + project: str, display_name: str, location: str, sync: bool = True, +): + aiplatform.init(project=project, location=location) + + endpoint = aiplatform.Endpoint.create( + display_name=display_name, project=project, location=location, + ) + + print(endpoint.display_name) + print(endpoint.resource_name) + print(endpoint.uri) + return endpoint + + +# [END aiplatform_sdk_create_endpoint_sample] diff --git a/samples/model-builder/create_endpoint_sample_test.py b/samples/model-builder/create_endpoint_sample_test.py new file mode 100644 index 0000000000..af3631d93a --- /dev/null +++ b/samples/model-builder/create_endpoint_sample_test.py @@ -0,0 +1,36 @@ +# 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 create_endpoint_sample +import test_constants as constants + + +def test_create_endpoint_sample(mock_sdk_init, mock_create_endpoint): + + create_endpoint_sample.create_endpoint_sample( + project=constants.PROJECT, + display_name=constants.DISPLAY_NAME, + location=constants.LOCATION, + ) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + + mock_create_endpoint.assert_called_once_with( + display_name=constants.DISPLAY_NAME, + project=constants.PROJECT, + location=constants.LOCATION, + ) diff --git a/samples/model-builder/create_training_pipeline_image_classification_sample_test.py b/samples/model-builder/create_training_pipeline_image_classification_sample_test.py index cb91898938..1c7080e7a1 100644 --- a/samples/model-builder/create_training_pipeline_image_classification_sample_test.py +++ b/samples/model-builder/create_training_pipeline_image_classification_sample_test.py @@ -20,7 +20,7 @@ def test_create_training_pipeline_image_classification_sample( mock_sdk_init, mock_image_dataset, - mock_init_automl_image_training_job, + mock_get_automl_image_training_job, mock_run_automl_image_training_job, mock_get_image_dataset, ): @@ -43,7 +43,7 @@ def test_create_training_pipeline_image_classification_sample( mock_sdk_init.assert_called_once_with( project=constants.PROJECT, location=constants.LOCATION ) - mock_init_automl_image_training_job.assert_called_once_with( + mock_get_automl_image_training_job.assert_called_once_with( display_name=constants.DISPLAY_NAME ) mock_run_automl_image_training_job.assert_called_once_with( diff --git a/samples/model-builder/create_training_pipeline_tabular_classification_sample.py b/samples/model-builder/create_training_pipeline_tabular_classification_sample.py new file mode 100644 index 0000000000..6bd9405383 --- /dev/null +++ b/samples/model-builder/create_training_pipeline_tabular_classification_sample.py @@ -0,0 +1,59 @@ +# 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 + + +# [START aiplatform_sdk_create_training_pipeline_tabular_classification_sample] +def create_training_pipeline_tabular_classification_sample( + project: str, + display_name: str, + dataset_id: int, + location: str = "us-central1", + model_display_name: str = None, + training_fraction_split: float = 0.8, + validation_fraction_split: float = 0.1, + test_fraction_split: float = 0.1, + budget_milli_node_hours: int = 8000, + disable_early_stopping: bool = False, + sync: bool = True, +): + aiplatform.init(project=project, location=location) + + tabular_classification_job = aiplatform.AutoMLTabularTrainingJob( + display_name=display_name, + ) + + my_tabular_dataset = aiplatform.TabularDataset(dataset_id) + + model = tabular_classification_job.run( + dataset=my_tabular_dataset, + training_fraction_split=training_fraction_split, + validation_fraction_split=validation_fraction_split, + test_fraction_split=test_fraction_split, + budget_milli_node_hours=budget_milli_node_hours, + model_display_name=model_display_name, + disable_early_stopping=disable_early_stopping, + sync=sync, + ) + + model.wait() + + print(model.display_name) + print(model.resource_name) + print(model.uri) + return model + + +# [END aiplatform_sdk_create_training_pipeline_tabular_classification_sample] diff --git a/samples/model-builder/create_training_pipeline_tabular_classification_sample_test.py b/samples/model-builder/create_training_pipeline_tabular_classification_sample_test.py new file mode 100644 index 0000000000..c015e99785 --- /dev/null +++ b/samples/model-builder/create_training_pipeline_tabular_classification_sample_test.py @@ -0,0 +1,57 @@ +# 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 create_training_pipeline_tabular_classification_sample +import test_constants as constants + + +def test_create_training_pipeline_tabular_classification_sample( + mock_sdk_init, + mock_tabular_dataset, + mock_get_automl_tabular_training_job, + mock_run_automl_tabular_training_job, + mock_get_tabular_dataset, +): + + create_training_pipeline_tabular_classification_sample.create_training_pipeline_tabular_classification_sample( + project=constants.PROJECT, + display_name=constants.DISPLAY_NAME, + dataset_id=constants.RESOURCE_ID, + model_display_name=constants.DISPLAY_NAME_2, + training_fraction_split=constants.TRAINING_FRACTION_SPLIT, + validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT, + test_fraction_split=constants.TEST_FRACTION_SPLIT, + budget_milli_node_hours=constants.BUDGET_MILLI_NODE_HOURS_8000, + disable_early_stopping=False, + ) + + mock_get_tabular_dataset.assert_called_once_with(constants.RESOURCE_ID) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + mock_get_automl_tabular_training_job.assert_called_once_with( + display_name=constants.DISPLAY_NAME, + ) + mock_run_automl_tabular_training_job.assert_called_once_with( + dataset=mock_tabular_dataset, + model_display_name=constants.DISPLAY_NAME_2, + training_fraction_split=constants.TRAINING_FRACTION_SPLIT, + validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT, + test_fraction_split=constants.TEST_FRACTION_SPLIT, + budget_milli_node_hours=constants.BUDGET_MILLI_NODE_HOURS_8000, + disable_early_stopping=False, + sync=True, + ) diff --git a/samples/model-builder/create_training_pipeline_tabular_regression_sample.py b/samples/model-builder/create_training_pipeline_tabular_regression_sample.py new file mode 100644 index 0000000000..2404bb37e2 --- /dev/null +++ b/samples/model-builder/create_training_pipeline_tabular_regression_sample.py @@ -0,0 +1,59 @@ +# 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 + + +# [START aiplatform_sdk_create_training_pipeline_tabular_regression_sample] +def create_training_pipeline_tabular_regression_sample( + project: str, + display_name: str, + dataset_id: int, + location: str = "us-central1", + model_display_name: str = None, + training_fraction_split: float = 0.8, + validation_fraction_split: float = 0.1, + test_fraction_split: float = 0.1, + budget_milli_node_hours: int = 8000, + disable_early_stopping: bool = False, + sync: bool = True, +): + aiplatform.init(project=project, location=location) + + tabular_regression_job = aiplatform.AutoMLTabularTrainingJob( + display_name=display_name, + ) + + my_tabular_dataset = aiplatform.TabularDataset(dataset_id) + + model = tabular_regression_job.run( + dataset=my_tabular_dataset, + training_fraction_split=training_fraction_split, + validation_fraction_split=validation_fraction_split, + test_fraction_split=test_fraction_split, + budget_milli_node_hours=budget_milli_node_hours, + model_display_name=model_display_name, + disable_early_stopping=disable_early_stopping, + sync=sync, + ) + + model.wait() + + print(model.display_name) + print(model.resource_name) + print(model.uri) + return model + + +# [END aiplatform_sdk_create_training_pipeline_tabular_regression_sample] diff --git a/samples/model-builder/create_training_pipeline_tabular_regression_sample_test.py b/samples/model-builder/create_training_pipeline_tabular_regression_sample_test.py new file mode 100644 index 0000000000..1e897b5851 --- /dev/null +++ b/samples/model-builder/create_training_pipeline_tabular_regression_sample_test.py @@ -0,0 +1,57 @@ +# 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 create_training_pipeline_tabular_regression_sample +import test_constants as constants + + +def test_create_training_pipeline_tabular_regression_sample( + mock_sdk_init, + mock_tabular_dataset, + mock_get_automl_tabular_training_job, + mock_run_automl_tabular_training_job, + mock_get_tabular_dataset, +): + + create_training_pipeline_tabular_regression_sample.create_training_pipeline_tabular_regression_sample( + project=constants.PROJECT, + display_name=constants.DISPLAY_NAME, + dataset_id=constants.RESOURCE_ID, + model_display_name=constants.DISPLAY_NAME_2, + training_fraction_split=constants.TRAINING_FRACTION_SPLIT, + validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT, + test_fraction_split=constants.TEST_FRACTION_SPLIT, + budget_milli_node_hours=constants.BUDGET_MILLI_NODE_HOURS_8000, + disable_early_stopping=False, + ) + + mock_get_tabular_dataset.assert_called_once_with(constants.RESOURCE_ID) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + mock_get_automl_tabular_training_job.assert_called_once_with( + display_name=constants.DISPLAY_NAME, + ) + mock_run_automl_tabular_training_job.assert_called_once_with( + dataset=mock_tabular_dataset, + model_display_name=constants.DISPLAY_NAME_2, + training_fraction_split=constants.TRAINING_FRACTION_SPLIT, + validation_fraction_split=constants.VALIDATION_FRACTION_SPLIT, + test_fraction_split=constants.TEST_FRACTION_SPLIT, + budget_milli_node_hours=constants.BUDGET_MILLI_NODE_HOURS_8000, + disable_early_stopping=False, + sync=True, + ) diff --git a/samples/model-builder/import_data_text_entity_extraction_sample_test.py b/samples/model-builder/import_data_text_entity_extraction_sample_test.py index a3b93e9200..44ce9cc328 100644 --- a/samples/model-builder/import_data_text_entity_extraction_sample_test.py +++ b/samples/model-builder/import_data_text_entity_extraction_sample_test.py @@ -34,9 +34,7 @@ def test_import_data_text_entity_extraction_sample( project=constants.PROJECT, location=constants.LOCATION ) - mock_get_text_dataset.assert_called_once_with( - constants.DATASET_NAME, - ) + mock_get_text_dataset.assert_called_once_with(constants.DATASET_NAME,) mock_import_text_dataset.assert_called_once_with( gcs_source=constants.GCS_SOURCES, diff --git a/samples/model-builder/import_data_text_sentiment_analysis_sample_test.py b/samples/model-builder/import_data_text_sentiment_analysis_sample_test.py index 2134d66b35..8bfd6ac0c3 100644 --- a/samples/model-builder/import_data_text_sentiment_analysis_sample_test.py +++ b/samples/model-builder/import_data_text_sentiment_analysis_sample_test.py @@ -34,9 +34,7 @@ def test_import_data_text_sentiment_analysis_sample( project=constants.PROJECT, location=constants.LOCATION ) - mock_get_text_dataset.assert_called_once_with( - constants.DATASET_NAME, - ) + mock_get_text_dataset.assert_called_once_with(constants.DATASET_NAME,) mock_import_text_dataset.assert_called_once_with( gcs_source=constants.GCS_SOURCES, diff --git a/samples/model-builder/predict_tabular_classification_sample.py b/samples/model-builder/predict_tabular_classification_sample.py new file mode 100644 index 0000000000..e5b1a0283d --- /dev/null +++ b/samples/model-builder/predict_tabular_classification_sample.py @@ -0,0 +1,35 @@ +# 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 typing import Dict, List + +from google.cloud import aiplatform + + +# [START aiplatform_sdk_predict_tabular_classification_sample] +def predict_tabular_classification_sample( + project: str, location: str, endpoint: str, instances: List[Dict], +): + aiplatform.init(project=project, location=location) + + endpoint = aiplatform.Endpoint(endpoint) + + response = endpoint.predict(instances=instances) + + for prediction_ in response.predictions: + print(prediction_) + + +# [END aiplatform_sdk_predict_tabular_classification_sample] diff --git a/samples/model-builder/predict_tabular_classification_sample_test.py b/samples/model-builder/predict_tabular_classification_sample_test.py new file mode 100644 index 0000000000..49a701115b --- /dev/null +++ b/samples/model-builder/predict_tabular_classification_sample_test.py @@ -0,0 +1,33 @@ +# 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 predict_tabular_classification_sample +import test_constants as constants + + +def test_predict_tabular_classification_sample(mock_sdk_init, mock_get_endpoint): + + predict_tabular_classification_sample.predict_tabular_classification_sample( + project=constants.PROJECT, + location=constants.LOCATION, + endpoint=constants.ENDPOINT_NAME, + instances=constants.PREDICTION_TABULAR_CLASSIFICATION_INSTANCE, + ) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + + mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME,) diff --git a/samples/model-builder/predict_tabular_regression_sample.py b/samples/model-builder/predict_tabular_regression_sample.py new file mode 100644 index 0000000000..fee4d34e38 --- /dev/null +++ b/samples/model-builder/predict_tabular_regression_sample.py @@ -0,0 +1,34 @@ +# 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 typing import Dict, List + +from google.cloud import aiplatform + + +# [START aiplatform_sdk_predict_tabular_regression_sample] +def predict_tabular_regression_sample( + project: str, location: str, endpoint: str, instances: List[Dict], +): + aiplatform.init(project=project, location=location) + + endpoint = aiplatform.Endpoint(endpoint) + + response = endpoint.predict(instances=instances) + + for prediction_ in response.predictions: + print(prediction_) + + +# [END aiplatform_sdk_predict_tabular_regression_sample] diff --git a/samples/model-builder/predict_tabular_regression_sample_test.py b/samples/model-builder/predict_tabular_regression_sample_test.py new file mode 100644 index 0000000000..7491d7c1d5 --- /dev/null +++ b/samples/model-builder/predict_tabular_regression_sample_test.py @@ -0,0 +1,33 @@ +# 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 predict_tabular_regression_sample +import test_constants as constants + + +def test_predict_tabular_regression_sample(mock_sdk_init, mock_get_endpoint): + + predict_tabular_regression_sample.predict_tabular_regression_sample( + project=constants.PROJECT, + location=constants.LOCATION, + endpoint=constants.ENDPOINT_NAME, + instances=constants.PREDICTION_TABULAR_REGRESSOIN_INSTANCE, + ) + + mock_sdk_init.assert_called_once_with( + project=constants.PROJECT, location=constants.LOCATION + ) + + mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME,) diff --git a/samples/model-builder/predict_text_classification_single_label_sample_test.py b/samples/model-builder/predict_text_classification_single_label_sample_test.py index c446235a79..789f2962c3 100644 --- a/samples/model-builder/predict_text_classification_single_label_sample_test.py +++ b/samples/model-builder/predict_text_classification_single_label_sample_test.py @@ -32,6 +32,4 @@ def test_predict_text_classification_single_label_sample( project=constants.PROJECT, location=constants.LOCATION ) - mock_get_endpoint.assert_called_once_with( - constants.ENDPOINT_NAME, - ) + mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME,) diff --git a/samples/model-builder/predict_text_entity_extraction_sample_test.py b/samples/model-builder/predict_text_entity_extraction_sample_test.py index 3ca2b49b43..3b123ff148 100644 --- a/samples/model-builder/predict_text_entity_extraction_sample_test.py +++ b/samples/model-builder/predict_text_entity_extraction_sample_test.py @@ -30,6 +30,4 @@ def test_predict_text_entity_extraction_sample(mock_sdk_init, mock_get_endpoint) project=constants.PROJECT, location=constants.LOCATION ) - mock_get_endpoint.assert_called_once_with( - constants.ENDPOINT_NAME, - ) + mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME,) diff --git a/samples/model-builder/predict_text_sentiment_analysis_sample_test.py b/samples/model-builder/predict_text_sentiment_analysis_sample_test.py index c2ed180c9f..e3a3fad58c 100644 --- a/samples/model-builder/predict_text_sentiment_analysis_sample_test.py +++ b/samples/model-builder/predict_text_sentiment_analysis_sample_test.py @@ -30,6 +30,4 @@ def test_predict_text_sentiment_analysis_sample(mock_sdk_init, mock_get_endpoint project=constants.PROJECT, location=constants.LOCATION ) - mock_get_endpoint.assert_called_once_with( - constants.ENDPOINT_NAME, - ) + mock_get_endpoint.assert_called_once_with(constants.ENDPOINT_NAME,) diff --git a/samples/model-builder/test_constants.py b/samples/model-builder/test_constants.py index 1a27b2c530..aabd4f9637 100644 --- a/samples/model-builder/test_constants.py +++ b/samples/model-builder/test_constants.py @@ -39,6 +39,8 @@ MODEL_NAME = f"{PARENT}/models/{RESOURCE_ID}" TRAINING_JOB_NAME = f"{PARENT}/trainingJobs/{RESOURCE_ID}" +BIGQUERY_SOURCE = f"bq://{PROJECT}.{DATASET_NAME}.table1" + GCS_SOURCES = ["gs://bucket1/source1.jsonl", "gs://bucket7/source4.jsonl"] BIGQUERY_SOURCE = "bq://bigquery-public-data.ml_datasets.iris" GCS_DESTINATION = "gs://bucket3/output-dir/" @@ -53,6 +55,41 @@ PREDICTION_TEXT_INSTANCE = "This is some text for testing NLP prediction output" +PREDICTION_TABULAR_CLASSIFICATION_INSTANCE = [ + { + "petal_length": "1.4", + "petal_width": "1.3", + "sepal_length": "5.1", + "sepal_width": "2.8", + } +] +PREDICTION_TABULAR_REGRESSOIN_INSTANCE = [ + { + "BOOLEAN_2unique_NULLABLE": False, + "DATETIME_1unique_NULLABLE": "2019-01-01 00:00:00", + "DATE_1unique_NULLABLE": "2019-01-01", + "FLOAT_5000unique_NULLABLE": 1611, + "FLOAT_5000unique_REPEATED": [2320, 1192], + "INTEGER_5000unique_NULLABLE": "8", + "NUMERIC_5000unique_NULLABLE": 16, + "STRING_5000unique_NULLABLE": "str-2", + "STRUCT_NULLABLE": { + "BOOLEAN_2unique_NULLABLE": False, + "DATE_1unique_NULLABLE": "2019-01-01", + "DATETIME_1unique_NULLABLE": "2019-01-01 00:00:00", + "FLOAT_5000unique_NULLABLE": 1308, + "FLOAT_5000unique_REPEATED": [2323, 1178], + "FLOAT_5000unique_REQUIRED": 3089, + "INTEGER_5000unique_NULLABLE": "1777", + "NUMERIC_5000unique_NULLABLE": 3323, + "TIME_1unique_NULLABLE": "23:59:59.999999", + "STRING_5000unique_NULLABLE": "str-49", + "TIMESTAMP_1unique_NULLABLE": "1546387199999999", + }, + "TIMESTAMP_1unique_NULLABLE": "1546387199999999", + "TIME_1unique_NULLABLE": "23:59:59.999999", + } +] SCRIPT_PATH = "task.py" CONTAINER_URI = "gcr.io/my_project/my_image:latest" ARGS = ["--tfds", "tf_flowers:3.*.*"]