From c1ea05167a17fd9d69d8faae4116ed46fd6efa5e Mon Sep 17 00:00:00 2001 From: Torry Yang Date: Fri, 20 Jul 2018 16:24:34 -0700 Subject: [PATCH 01/17] automl beta [(#1575)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1575) * automl initial commit * lint * fix import groupings * add requirements.txt * address review comments --- .../snippets/automl_translation_dataset.py | 278 ++++++++++++++++ samples/snippets/automl_translation_model.py | 300 ++++++++++++++++++ .../snippets/automl_translation_predict.py | 109 +++++++ samples/snippets/dataset_test.py | 69 ++++ samples/snippets/model_test.py | 78 +++++ samples/snippets/predict_test.py | 31 ++ 6 files changed, 865 insertions(+) create mode 100755 samples/snippets/automl_translation_dataset.py create mode 100755 samples/snippets/automl_translation_model.py create mode 100644 samples/snippets/automl_translation_predict.py create mode 100644 samples/snippets/dataset_test.py create mode 100644 samples/snippets/model_test.py create mode 100644 samples/snippets/predict_test.py diff --git a/samples/snippets/automl_translation_dataset.py b/samples/snippets/automl_translation_dataset.py new file mode 100755 index 00000000..e579ac35 --- /dev/null +++ b/samples/snippets/automl_translation_dataset.py @@ -0,0 +1,278 @@ +#!/usr/bin/env python + +# Copyright 2018 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 +# +# http://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. + +"""This application demonstrates how to perform basic operations on dataset +with the Google AutoML Translation API. + +For more information, see the documentation at +https://cloud.google.com/translate/automl/docs +""" + +import argparse +import os + + +def create_dataset(project_id, compute_region, dataset_name, source, target): + """Create a dataset.""" + # [START automl_translation_create_dataset] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # dataset_name = 'DATASET_NAME_HERE' + # source = 'LANGUAGE_CODE_OF_SOURCE_LANGUAGE' + # target = 'LANGUAGE_CODE_OF_TARGET_LANGUAGE' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, compute_region) + + # Specify the source and target language. + dataset_metadata = { + "source_language_code": source, + "target_language_code": target, + } + # Set dataset name and dataset metadata + my_dataset = { + "display_name": dataset_name, + "translation_dataset_metadata": dataset_metadata, + } + + # Create a dataset with the dataset metadata in the region. + dataset = client.create_dataset(project_location, my_dataset) + + # Display the dataset information + print("Dataset name: {}".format(dataset.name)) + print("Dataset id: {}".format(dataset.name.split("/")[-1])) + print("Dataset display name: {}".format(dataset.display_name)) + print("Translation dataset Metadata:") + print( + "\tsource_language_code: {}".format( + dataset.translation_dataset_metadata.source_language_code + ) + ) + print( + "\ttarget_language_code: {}".format( + dataset.translation_dataset_metadata.target_language_code + ) + ) + print("Dataset create time:") + print("\tseconds: {}".format(dataset.create_time.seconds)) + print("\tnanos: {}".format(dataset.create_time.nanos)) + + # [END automl_translation_create_dataset] + + +def list_datasets(project_id, compute_region, filter_): + """List Datasets.""" + # [START automl_translation_list_datasets] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # filter_ = 'filter expression here' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, compute_region) + + # List all the datasets available in the region by applying filter. + response = client.list_datasets(project_location, filter_) + + print("List of datasets:") + for dataset in response: + # Display the dataset information + print("Dataset name: {}".format(dataset.name)) + print("Dataset id: {}".format(dataset.name.split("/")[-1])) + print("Dataset display name: {}".format(dataset.display_name)) + print("Translation dataset metadata:") + print( + "\tsource_language_code: {}".format( + dataset.translation_dataset_metadata.source_language_code + ) + ) + print( + "\ttarget_language_code: {}".format( + dataset.translation_dataset_metadata.target_language_code + ) + ) + print("Dataset create time:") + print("\tseconds: {}".format(dataset.create_time.seconds)) + print("\tnanos: {}".format(dataset.create_time.nanos)) + + # [END automl_translation_list_datasets] + + +def get_dataset(project_id, compute_region, dataset_id): + """Get the dataset.""" + # [START automl_translation_get_dataset] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # dataset_id = 'DATASET_ID_HERE' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # Get the full path of the dataset + dataset_full_id = client.dataset_path( + project_id, compute_region, dataset_id + ) + + # Get complete detail of the dataset. + dataset = client.get_dataset(dataset_full_id) + + # Display the dataset information + print("Dataset name: {}".format(dataset.name)) + print("Dataset id: {}".format(dataset.name.split("/")[-1])) + print("Dataset display name: {}".format(dataset.display_name)) + print("Translation dataset metadata:") + print( + "\tsource_language_code: {}".format( + dataset.translation_dataset_metadata.source_language_code + ) + ) + print( + "\ttarget_language_code: {}".format( + dataset.translation_dataset_metadata.target_language_code + ) + ) + print("Dataset create time:") + print("\tseconds: {}".format(dataset.create_time.seconds)) + print("\tnanos: {}".format(dataset.create_time.nanos)) + + # [END automl_translation_get_dataset] + + +def import_data(project_id, compute_region, dataset_id, path): + """Import sentence pairs to the dataset.""" + # [START automl_translation_import_data] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # dataset_id = 'DATASET_ID_HERE' + # path = 'gs://path/to/file.csv' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # Get the full path of the dataset. + dataset_full_id = client.dataset_path( + project_id, compute_region, dataset_id + ) + + # Get the multiple Google Cloud Storage URIs + input_uris = path.split(",") + input_config = {"gcs_source": {"input_uris": input_uris}} + + # Import data from the input URI + response = client.import_data(dataset_full_id, input_config) + + print("Processing import...") + # synchronous check of operation status + print("Data imported. {}".format(response.result())) + + # [END automl_translation_import_data] + + +def delete_dataset(project_id, compute_region, dataset_id): + """Delete a dataset.""" + # [START automl_translation_delete_dataset]] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # dataset_id = 'DATASET_ID_HERE' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # Get the full path of the dataset. + dataset_full_id = client.dataset_path( + project_id, compute_region, dataset_id + ) + + # Delete a dataset. + response = client.delete_dataset(dataset_full_id) + + # synchronous check of operation status + print("Dataset deleted. {}".format(response.result())) + + # [END automl_translation_delete_dataset] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter, + ) + subparsers = parser.add_subparsers(dest="command") + + create_dataset_parser = subparsers.add_parser( + "create_dataset", help=create_dataset.__doc__ + ) + create_dataset_parser.add_argument("dataset_name") + create_dataset_parser.add_argument("source") + create_dataset_parser.add_argument("target") + + list_datasets_parser = subparsers.add_parser( + "list_datasets", help=list_datasets.__doc__ + ) + list_datasets_parser.add_argument("filter", nargs="?", default="") + + import_data_parser = subparsers.add_parser( + "import_data", help=import_data.__doc__ + ) + import_data_parser.add_argument("dataset_id") + import_data_parser.add_argument("path") + + delete_dataset_parser = subparsers.add_parser( + "delete_dataset", help=delete_dataset.__doc__ + ) + delete_dataset_parser.add_argument("dataset_id") + + get_dataset_parser = subparsers.add_parser( + "get_dataset", help=get_dataset.__doc__ + ) + get_dataset_parser.add_argument("dataset_id") + + project_id = os.environ["PROJECT_ID"] + compute_region = os.environ["REGION_NAME"] + + args = parser.parse_args() + + if args.command == "create_dataset": + create_dataset( + project_id, + compute_region, + args.dataset_name, + args.source, + args.target, + ) + if args.command == "list_datasets": + list_datasets(project_id, compute_region, args.filter) + if args.command == "get_dataset": + get_dataset(project_id, compute_region, args.dataset_id) + if args.command == "import_data": + import_data(project_id, compute_region, args.dataset_id, args.path) + if args.command == "delete_dataset": + delete_dataset(project_id, compute_region, args.dataset_id) diff --git a/samples/snippets/automl_translation_model.py b/samples/snippets/automl_translation_model.py new file mode 100755 index 00000000..0b9b6f53 --- /dev/null +++ b/samples/snippets/automl_translation_model.py @@ -0,0 +1,300 @@ +#!/usr/bin/env python + +# Copyright 2018 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 +# +# http://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. + +"""This application demonstrates how to perform basic operations on model +with the Google AutoML Translation API. + +For more information, see the documentation at +https://cloud.google.com/translate/automl/docs +""" + +import argparse +import os + + +def create_model(project_id, compute_region, dataset_id, model_name): + """Create a model.""" + # [START automl_translation_create_model] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # dataset_id = 'DATASET_ID_HERE' + # model_name = 'MODEL_NAME_HERE' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, compute_region) + + # Set model name and dataset. + my_model = { + "display_name": model_name, + "dataset_id": dataset_id, + "translation_model_metadata": {"base_model": ""}, + } + + # Create a model with the model metadata in the region. + response = client.create_model(project_location, my_model) + + print("Training operation name: {}".format(response.operation.name)) + print("Training started...") + + # [END automl_translation_create_model] + + +def list_models(project_id, compute_region, filter_): + """List all models.""" + # [START automl_translation_list_models] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # filter_ = 'DATASET_ID_HERE' + + from google.cloud import automl_v1beta1 as automl + from google.cloud.automl_v1beta1 import enums + + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, compute_region) + + # List all the models available in the region by applying filter. + response = client.list_models(project_location, filter_) + + print("List of models:") + for model in response: + # Display the model information. + if model.deployment_state == enums.Model.DeploymentState.DEPLOYED: + deployment_state = "deployed" + else: + deployment_state = "undeployed" + + print("Model name: {}".format(model.name)) + print("Model id: {}".format(model.name.split("/")[-1])) + print("Model display name: {}".format(model.display_name)) + print("Model create time:") + print("\tseconds: {}".format(model.create_time.seconds)) + print("\tnanos: {}".format(model.create_time.nanos)) + print("Model deployment state: {}".format(deployment_state)) + + # [END automl_translation_list_models] + + +def get_model(project_id, compute_region, model_id): + """Get model details.""" + # [START automl_translation_get_model] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # model_id = 'MODEL_ID_HERE' + + from google.cloud import automl_v1beta1 as automl + from google.cloud.automl_v1beta1 import enums + + client = automl.AutoMlClient() + + # Get the full path of the model. + model_full_id = client.model_path(project_id, compute_region, model_id) + + # Get complete detail of the model. + model = client.get_model(model_full_id) + + # Retrieve deployment state. + if model.deployment_state == enums.Model.DeploymentState.DEPLOYED: + deployment_state = "deployed" + else: + deployment_state = "undeployed" + + # Display the model information. + print("Model name: {}".format(model.name)) + print("Model id: {}".format(model.name.split("/")[-1])) + print("Model display name: {}".format(model.display_name)) + print("Model create time:") + print("\tseconds: {}".format(model.create_time.seconds)) + print("\tnanos: {}".format(model.create_time.nanos)) + print("Model deployment state: {}".format(deployment_state)) + + # [END automl_translation_get_model] + + +def list_model_evaluations(project_id, compute_region, model_id, filter_): + """List model evaluations.""" + # [START automl_translation_list_model_evaluations] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # model_id = 'MODEL_ID_HERE' + # filter_ = 'filter expression here' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # Get the full path of the model. + model_full_id = client.model_path(project_id, compute_region, model_id) + + print("List of model evaluations:") + for element in client.list_model_evaluations(model_full_id, filter_): + print(element) + + # [END automl_translation_list_model_evaluations] + + +def get_model_evaluation( + project_id, compute_region, model_id, model_evaluation_id +): + """Get model evaluation.""" + # [START automl_translation_get_model_evaluation] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # model_id = 'MODEL_ID_HERE' + # model_evaluation_id = 'MODEL_EVALUATION_ID_HERE' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # Get the full path of the model evaluation. + model_evaluation_full_id = client.model_evaluation_path( + project_id, compute_region, model_id, model_evaluation_id + ) + + # Get complete detail of the model evaluation. + response = client.get_model_evaluation(model_evaluation_full_id) + + print(response) + + # [END automl_translation_get_model_evaluation] + + +def delete_model(project_id, compute_region, model_id): + """Delete a model.""" + # [START automl_translation_delete_model] + # TODO(developer): Uncomment and set the following variables + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # model_id = 'MODEL_ID_HERE' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # Get the full path of the model. + model_full_id = client.model_path(project_id, compute_region, model_id) + + # Delete a model. + response = client.delete_model(model_full_id) + + # synchronous check of operation status. + print("Model deleted. {}".format(response.result())) + + # [END automl_translation_delete_model] + + +def get_operation_status(operation_full_id): + """Get operation status.""" + # [START automl_translation_get_operation_status] + # TODO(developer): Uncomment and set the following variables + # operation_full_id = + # 'projects//locations//operations/' + + from google.cloud import automl_v1beta1 as automl + + client = automl.AutoMlClient() + + # Get the latest state of a long-running operation. + response = client.transport._operations_client.get_operation( + operation_full_id + ) + + print("Operation status: {}".format(response)) + + # [END automl_translation_get_operation_status] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter, + ) + subparsers = parser.add_subparsers(dest="command") + + create_model_parser = subparsers.add_parser( + "create_model", help=create_model.__doc__ + ) + create_model_parser.add_argument("dataset_id") + create_model_parser.add_argument("model_name") + + list_model_evaluations_parser = subparsers.add_parser( + "list_model_evaluations", help=list_model_evaluations.__doc__ + ) + list_model_evaluations_parser.add_argument("model_id") + list_model_evaluations_parser.add_argument("filter", nargs="?", default="") + + get_model_evaluation_parser = subparsers.add_parser( + "get_model_evaluation", help=get_model_evaluation.__doc__ + ) + get_model_evaluation_parser.add_argument("model_id") + get_model_evaluation_parser.add_argument("model_evaluation_id") + + get_model_parser = subparsers.add_parser( + "get_model", help=get_model.__doc__ + ) + get_model_parser.add_argument("model_id") + + get_operation_status_parser = subparsers.add_parser( + "get_operation_status", help=get_operation_status.__doc__ + ) + get_operation_status_parser.add_argument("operation_full_id") + + list_models_parser = subparsers.add_parser( + "list_models", help=list_models.__doc__ + ) + list_models_parser.add_argument("filter", nargs="?", default="") + + delete_model_parser = subparsers.add_parser( + "delete_model", help=delete_model.__doc__ + ) + delete_model_parser.add_argument("model_id") + + project_id = os.environ["PROJECT_ID"] + compute_region = os.environ["REGION_NAME"] + + args = parser.parse_args() + + if args.command == "create_model": + create_model( + project_id, compute_region, args.dataset_id, args.model_name + ) + if args.command == "list_models": + list_models(project_id, compute_region, args.filter) + if args.command == "get_model": + get_model(project_id, compute_region, args.model_id) + if args.command == "list_model_evaluations": + list_model_evaluations( + project_id, compute_region, args.model_id, args.filter + ) + if args.command == "get_model_evaluation": + get_model_evaluation( + project_id, compute_region, args.model_id, args.model_evaluation_id + ) + if args.command == "delete_model": + delete_model(project_id, compute_region, args.model_id) + if args.command == "get_operation_status": + get_operation_status(args.operation_full_id) diff --git a/samples/snippets/automl_translation_predict.py b/samples/snippets/automl_translation_predict.py new file mode 100644 index 00000000..1dac70b7 --- /dev/null +++ b/samples/snippets/automl_translation_predict.py @@ -0,0 +1,109 @@ +#!/usr/bin/env python + +# Copyright 2018 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 +# +# http://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. + +"""This application demonstrates how to perform basic operations on prediction +with the Google AutoML Translation API. + +For more information, see the documentation at +https://cloud.google.com/translate/automl/docs +""" + +import argparse +import os + + +def predict( + project_id, + compute_region, + model_id, + file_path, + translation_allow_fallback=False, +): + """Translate the content.""" + # [START automl_translation_predict] + # project_id = 'PROJECT_ID_HERE' + # compute_region = 'COMPUTE_REGION_HERE' + # model_id = 'MODEL_ID_HERE' + # file_path = '/local/path/to/file' + # translation_allow_fallback = True allows fallback to Google Translate + + from google.cloud import automl_v1beta1 as automl + + automl_client = automl.AutoMlClient() + + # Create client for prediction service. + prediction_client = automl.PredictionServiceClient() + + # Get the full path of the model. + model_full_id = automl_client.model_path( + project_id, compute_region, model_id + ) + + # Read the file content for translation. + with open(file_path, "rb") as content_file: + content = content_file.read() + content.decode("utf-8") + + # Set the payload by giving the content of the file. + payload = {"text_snippet": {"content": content}} + + # params is additional domain-specific parameters. + # translation_allow_fallback allows to use Google translation model. + params = {} + if translation_allow_fallback: + params = {"translation_allow_fallback": "True"} + + response = prediction_client.predict(model_full_id, payload, params) + translated_content = response.payload[0].translation.translated_content + + print(u"Translated content: {}".format(translated_content.content)) + + # [END automl_translation_predict] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter, + ) + subparsers = parser.add_subparsers(dest="command") + + predict_parser = subparsers.add_parser("predict", help=predict.__doc__) + predict_parser.add_argument("model_id") + predict_parser.add_argument("file_path") + predict_parser.add_argument( + "translation_allow_fallback", + nargs="?", + choices=["False", "True"], + default="False", + ) + + project_id = os.environ["PROJECT_ID"] + compute_region = os.environ["REGION_NAME"] + + args = parser.parse_args() + + if args.command == "predict": + translation_allow_fallback = ( + True if args.translation_allow_fallback == "True" else False + ) + predict( + project_id, + compute_region, + args.model_id, + args.file_path, + translation_allow_fallback, + ) diff --git a/samples/snippets/dataset_test.py b/samples/snippets/dataset_test.py new file mode 100644 index 00000000..29e3e5c9 --- /dev/null +++ b/samples/snippets/dataset_test.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python + +# Copyright 2018 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 +# +# http://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 datetime +import os + +import pytest + +import automl_translation_dataset + +project_id = os.environ["GCLOUD_PROJECT"] +compute_region = "us-central1" + + +@pytest.mark.slow +def test_dataset_create_import_delete(capsys): + # create dataset + dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + automl_translation_dataset.create_dataset( + project_id, compute_region, dataset_name, "en", "ja" + ) + out, _ = capsys.readouterr() + create_dataset_output = out.splitlines() + assert "Dataset id: " in create_dataset_output[1] + + # import data + dataset_id = create_dataset_output[1].split()[2] + data = "gs://{}-vcm/en-ja.csv".format(project_id) + automl_translation_dataset.import_data( + project_id, compute_region, dataset_id, data + ) + out, _ = capsys.readouterr() + assert "Data imported." in out + + # delete dataset + automl_translation_dataset.delete_dataset( + project_id, compute_region, dataset_id + ) + out, _ = capsys.readouterr() + assert "Dataset deleted." in out + + +def test_dataset_list_get(capsys): + # list datasets + automl_translation_dataset.list_datasets(project_id, compute_region, "") + out, _ = capsys.readouterr() + list_dataset_output = out.splitlines() + assert "Dataset id: " in list_dataset_output[2] + + # get dataset + dataset_id = list_dataset_output[2].split()[2] + automl_translation_dataset.get_dataset( + project_id, compute_region, dataset_id + ) + out, _ = capsys.readouterr() + assert "Dataset name: " in out diff --git a/samples/snippets/model_test.py b/samples/snippets/model_test.py new file mode 100644 index 00000000..7f915c5d --- /dev/null +++ b/samples/snippets/model_test.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python + +# Copyright 2018 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 +# +# http://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 datetime +import os + +from google.cloud import automl_v1beta1 as automl + +import automl_translation_model + +project_id = os.environ["GCLOUD_PROJECT"] +compute_region = "us-central1" + + +def test_model_create_status_delete(capsys): + # create model + client = automl.AutoMlClient() + model_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + project_location = client.location_path(project_id, compute_region) + my_model = { + "display_name": model_name, + "dataset_id": "3876092572857648864", + "translation_model_metadata": {"base_model": ""}, + } + response = client.create_model(project_location, my_model) + operation_name = response.operation.name + assert operation_name + + # get operation status + automl_translation_model.get_operation_status(operation_name) + out, _ = capsys.readouterr() + assert "Operation status: " in out + + # cancel operation + response.cancel() + + +def test_model_list_get_evaluate(capsys): + # list models + automl_translation_model.list_models(project_id, compute_region, "") + out, _ = capsys.readouterr() + list_models_output = out.splitlines() + assert "Model id: " in list_models_output[2] + + # get model + model_id = list_models_output[2].split()[2] + automl_translation_model.get_model(project_id, compute_region, model_id) + out, _ = capsys.readouterr() + assert "Model name: " in out + + # list model evaluations + automl_translation_model.list_model_evaluations( + project_id, compute_region, model_id, "" + ) + out, _ = capsys.readouterr() + list_evals_output = out.splitlines() + assert "name: " in list_evals_output[1] + + # get model evaluation + model_evaluation_id = list_evals_output[1].split("/")[-1][:-1] + automl_translation_model.get_model_evaluation( + project_id, compute_region, model_id, model_evaluation_id + ) + out, _ = capsys.readouterr() + assert "evaluation_metric" in out diff --git a/samples/snippets/predict_test.py b/samples/snippets/predict_test.py new file mode 100644 index 00000000..87aea8fa --- /dev/null +++ b/samples/snippets/predict_test.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python + +# Copyright 2018 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 +# +# http://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 os + +import automl_translation_predict + +project_id = os.environ["GCLOUD_PROJECT"] +compute_region = "us-central1" + + +def test_predict(capsys): + model_id = "3128559826197068699" + automl_translation_predict.predict( + project_id, compute_region, model_id, "resources/input.txt", False + ) + out, _ = capsys.readouterr() + assert "Translated content: " in out From 81c7fc39f694bee77f09b7c423e9732ea2d064fc Mon Sep 17 00:00:00 2001 From: Torry Yang Date: Tue, 24 Jul 2018 09:19:56 -0700 Subject: [PATCH 02/17] remove translate prediction fallback [(#1598)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1598) --- .../snippets/automl_translation_predict.py | 29 ++----------------- samples/snippets/predict_test.py | 2 +- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/samples/snippets/automl_translation_predict.py b/samples/snippets/automl_translation_predict.py index 1dac70b7..653cf388 100644 --- a/samples/snippets/automl_translation_predict.py +++ b/samples/snippets/automl_translation_predict.py @@ -25,20 +25,13 @@ import os -def predict( - project_id, - compute_region, - model_id, - file_path, - translation_allow_fallback=False, -): +def predict(project_id, compute_region, model_id, file_path): """Translate the content.""" # [START automl_translation_predict] # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' # model_id = 'MODEL_ID_HERE' # file_path = '/local/path/to/file' - # translation_allow_fallback = True allows fallback to Google Translate from google.cloud import automl_v1beta1 as automl @@ -61,10 +54,7 @@ def predict( payload = {"text_snippet": {"content": content}} # params is additional domain-specific parameters. - # translation_allow_fallback allows to use Google translation model. params = {} - if translation_allow_fallback: - params = {"translation_allow_fallback": "True"} response = prediction_client.predict(model_full_id, payload, params) translated_content = response.payload[0].translation.translated_content @@ -84,12 +74,6 @@ def predict( predict_parser = subparsers.add_parser("predict", help=predict.__doc__) predict_parser.add_argument("model_id") predict_parser.add_argument("file_path") - predict_parser.add_argument( - "translation_allow_fallback", - nargs="?", - choices=["False", "True"], - default="False", - ) project_id = os.environ["PROJECT_ID"] compute_region = os.environ["REGION_NAME"] @@ -97,13 +81,4 @@ def predict( args = parser.parse_args() if args.command == "predict": - translation_allow_fallback = ( - True if args.translation_allow_fallback == "True" else False - ) - predict( - project_id, - compute_region, - args.model_id, - args.file_path, - translation_allow_fallback, - ) + predict(project_id, compute_region, args.model_id, args.file_path) diff --git a/samples/snippets/predict_test.py b/samples/snippets/predict_test.py index 87aea8fa..c9fb7e04 100644 --- a/samples/snippets/predict_test.py +++ b/samples/snippets/predict_test.py @@ -25,7 +25,7 @@ def test_predict(capsys): model_id = "3128559826197068699" automl_translation_predict.predict( - project_id, compute_region, model_id, "resources/input.txt", False + project_id, compute_region, model_id, "resources/input.txt" ) out, _ = capsys.readouterr() assert "Translated content: " in out From 069f434d28adb35db8746464a2c14b544b515d21 Mon Sep 17 00:00:00 2001 From: Torry Yang Date: Thu, 2 Aug 2018 17:40:16 -0700 Subject: [PATCH 03/17] skip automl model create/delete test [(#1608)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1608) * skip model create/delete test * add skip reason --- samples/snippets/model_test.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/snippets/model_test.py b/samples/snippets/model_test.py index 7f915c5d..0d37a85c 100644 --- a/samples/snippets/model_test.py +++ b/samples/snippets/model_test.py @@ -18,6 +18,7 @@ import os from google.cloud import automl_v1beta1 as automl +import pytest import automl_translation_model @@ -25,6 +26,7 @@ compute_region = "us-central1" +@pytest.mark.skip(reason="creates too many models") def test_model_create_status_delete(capsys): # create model client = automl.AutoMlClient() From 5e2dd8e2a036eb4e6b045bd830e9abbd2718449c Mon Sep 17 00:00:00 2001 From: DPE bot Date: Tue, 28 Aug 2018 11:17:45 -0700 Subject: [PATCH 04/17] Auto-update dependencies. [(#1658)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1658) * Auto-update dependencies. * Rollback appengine/standard/bigquery/. * Rollback appengine/standard/iap/. * Rollback bigtable/metricscaler. * Rolledback appengine/flexible/datastore. * Rollback dataproc/ * Rollback jobs/api_client * Rollback vision/cloud-client. * Rollback functions/ocr/app. * Rollback iot/api-client/end_to_end_example. * Rollback storage/cloud-client. * Rollback kms/api-client. * Rollback dlp/ * Rollback bigquery/cloud-client. * Rollback iot/api-client/manager. * Rollback appengine/flexible/cloudsql_postgresql. --- samples/snippets/requirements.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index f6a42ea2..8d8575ea 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,2 +1,6 @@ +<<<<<<< HEAD google-cloud-automl==1.0.1 google-cloud-storage==1.29.0 +======= +google-cloud-automl==0.1.1 +>>>>>>> d89df5c (Auto-update dependencies. [(#1658)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1658)) From d91c4f107192916af58d8e039e1bcc0bb184e50b Mon Sep 17 00:00:00 2001 From: Alix Hamilton Date: Wed, 29 Aug 2018 12:37:06 -0700 Subject: [PATCH 05/17] Update AutoML region tags to use standard product prefixes [(#1669)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1669) --- .../snippets/automl_translation_dataset.py | 20 ++++++------- samples/snippets/automl_translation_model.py | 28 +++++++++---------- .../snippets/automl_translation_predict.py | 4 +-- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/samples/snippets/automl_translation_dataset.py b/samples/snippets/automl_translation_dataset.py index e579ac35..c60ef544 100755 --- a/samples/snippets/automl_translation_dataset.py +++ b/samples/snippets/automl_translation_dataset.py @@ -27,7 +27,7 @@ def create_dataset(project_id, compute_region, dataset_name, source, target): """Create a dataset.""" - # [START automl_translation_create_dataset] + # [START automl_translate_create_dataset] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -75,12 +75,12 @@ def create_dataset(project_id, compute_region, dataset_name, source, target): print("\tseconds: {}".format(dataset.create_time.seconds)) print("\tnanos: {}".format(dataset.create_time.nanos)) - # [END automl_translation_create_dataset] + # [END automl_translate_create_dataset] def list_datasets(project_id, compute_region, filter_): """List Datasets.""" - # [START automl_translation_list_datasets] + # [START automl_translate_list_datasets] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -117,12 +117,12 @@ def list_datasets(project_id, compute_region, filter_): print("\tseconds: {}".format(dataset.create_time.seconds)) print("\tnanos: {}".format(dataset.create_time.nanos)) - # [END automl_translation_list_datasets] + # [END automl_translate_list_datasets] def get_dataset(project_id, compute_region, dataset_id): """Get the dataset.""" - # [START automl_translation_get_dataset] + # [START automl_translate_get_dataset] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -159,12 +159,12 @@ def get_dataset(project_id, compute_region, dataset_id): print("\tseconds: {}".format(dataset.create_time.seconds)) print("\tnanos: {}".format(dataset.create_time.nanos)) - # [END automl_translation_get_dataset] + # [END automl_translate_get_dataset] def import_data(project_id, compute_region, dataset_id, path): """Import sentence pairs to the dataset.""" - # [START automl_translation_import_data] + # [START automl_translate_import_data] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -191,12 +191,12 @@ def import_data(project_id, compute_region, dataset_id, path): # synchronous check of operation status print("Data imported. {}".format(response.result())) - # [END automl_translation_import_data] + # [END automl_translate_import_data] def delete_dataset(project_id, compute_region, dataset_id): """Delete a dataset.""" - # [START automl_translation_delete_dataset]] + # [START automl_translate_delete_dataset]] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -217,7 +217,7 @@ def delete_dataset(project_id, compute_region, dataset_id): # synchronous check of operation status print("Dataset deleted. {}".format(response.result())) - # [END automl_translation_delete_dataset] + # [END automl_translate_delete_dataset] if __name__ == "__main__": diff --git a/samples/snippets/automl_translation_model.py b/samples/snippets/automl_translation_model.py index 0b9b6f53..77a4ed73 100755 --- a/samples/snippets/automl_translation_model.py +++ b/samples/snippets/automl_translation_model.py @@ -27,7 +27,7 @@ def create_model(project_id, compute_region, dataset_id, model_name): """Create a model.""" - # [START automl_translation_create_model] + # [START automl_translate_create_model] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -54,12 +54,12 @@ def create_model(project_id, compute_region, dataset_id, model_name): print("Training operation name: {}".format(response.operation.name)) print("Training started...") - # [END automl_translation_create_model] + # [END automl_translate_create_model] def list_models(project_id, compute_region, filter_): """List all models.""" - # [START automl_translation_list_models] + # [START automl_translate_list_models] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -92,12 +92,12 @@ def list_models(project_id, compute_region, filter_): print("\tnanos: {}".format(model.create_time.nanos)) print("Model deployment state: {}".format(deployment_state)) - # [END automl_translation_list_models] + # [END automl_translate_list_models] def get_model(project_id, compute_region, model_id): """Get model details.""" - # [START automl_translation_get_model] + # [START automl_translate_get_model] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -129,12 +129,12 @@ def get_model(project_id, compute_region, model_id): print("\tnanos: {}".format(model.create_time.nanos)) print("Model deployment state: {}".format(deployment_state)) - # [END automl_translation_get_model] + # [END automl_translate_get_model] def list_model_evaluations(project_id, compute_region, model_id, filter_): """List model evaluations.""" - # [START automl_translation_list_model_evaluations] + # [START automl_translate_list_model_evaluations] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -152,14 +152,14 @@ def list_model_evaluations(project_id, compute_region, model_id, filter_): for element in client.list_model_evaluations(model_full_id, filter_): print(element) - # [END automl_translation_list_model_evaluations] + # [END automl_translate_list_model_evaluations] def get_model_evaluation( project_id, compute_region, model_id, model_evaluation_id ): """Get model evaluation.""" - # [START automl_translation_get_model_evaluation] + # [START automl_translate_get_model_evaluation] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -180,12 +180,12 @@ def get_model_evaluation( print(response) - # [END automl_translation_get_model_evaluation] + # [END automl_translate_get_model_evaluation] def delete_model(project_id, compute_region, model_id): """Delete a model.""" - # [START automl_translation_delete_model] + # [START automl_translate_delete_model] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' @@ -204,12 +204,12 @@ def delete_model(project_id, compute_region, model_id): # synchronous check of operation status. print("Model deleted. {}".format(response.result())) - # [END automl_translation_delete_model] + # [END automl_translate_delete_model] def get_operation_status(operation_full_id): """Get operation status.""" - # [START automl_translation_get_operation_status] + # [START automl_translate_get_operation_status] # TODO(developer): Uncomment and set the following variables # operation_full_id = # 'projects//locations//operations/' @@ -225,7 +225,7 @@ def get_operation_status(operation_full_id): print("Operation status: {}".format(response)) - # [END automl_translation_get_operation_status] + # [END automl_translate_get_operation_status] if __name__ == "__main__": diff --git a/samples/snippets/automl_translation_predict.py b/samples/snippets/automl_translation_predict.py index 653cf388..b15e0e30 100644 --- a/samples/snippets/automl_translation_predict.py +++ b/samples/snippets/automl_translation_predict.py @@ -27,7 +27,7 @@ def predict(project_id, compute_region, model_id, file_path): """Translate the content.""" - # [START automl_translation_predict] + # [START automl_translate_predict] # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' # model_id = 'MODEL_ID_HERE' @@ -61,7 +61,7 @@ def predict(project_id, compute_region, model_id, file_path): print(u"Translated content: {}".format(translated_content.content)) - # [END automl_translation_predict] + # [END automl_translate_predict] if __name__ == "__main__": From 0650bcc9d5964ceb376de0f3e8caf68bc0f02ab1 Mon Sep 17 00:00:00 2001 From: Alix Hamilton Date: Thu, 6 Sep 2018 10:54:34 -0700 Subject: [PATCH 06/17] Fix AutoML region tag typos [(#1687)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1687) * fixes vision delete dataset region tag * removes extra bracket --- samples/snippets/automl_translation_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/automl_translation_dataset.py b/samples/snippets/automl_translation_dataset.py index c60ef544..cf3e50ae 100755 --- a/samples/snippets/automl_translation_dataset.py +++ b/samples/snippets/automl_translation_dataset.py @@ -196,7 +196,7 @@ def import_data(project_id, compute_region, dataset_id, path): def delete_dataset(project_id, compute_region, dataset_id): """Delete a dataset.""" - # [START automl_translate_delete_dataset]] + # [START automl_translate_delete_dataset] # TODO(developer): Uncomment and set the following variables # project_id = 'PROJECT_ID_HERE' # compute_region = 'COMPUTE_REGION_HERE' From 027a481e27fbb824e90b2bd0f4b1dd7876c1f1ce Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Fri, 19 Oct 2018 15:21:41 -0700 Subject: [PATCH 07/17] Fixed name of model [(#1779)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1779) * Fixed name of model * update model ids --- samples/snippets/predict_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/predict_test.py b/samples/snippets/predict_test.py index c9fb7e04..f9d98dfb 100644 --- a/samples/snippets/predict_test.py +++ b/samples/snippets/predict_test.py @@ -23,7 +23,7 @@ def test_predict(capsys): - model_id = "3128559826197068699" + model_id = "TRL3128559826197068699" automl_translation_predict.predict( project_id, compute_region, model_id, "resources/input.txt" ) From 83232725df055db51d0e2d6926033f91c41cd3c0 Mon Sep 17 00:00:00 2001 From: DPEBot Date: Wed, 6 Feb 2019 12:06:35 -0800 Subject: [PATCH 08/17] Auto-update dependencies. [(#1980)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1980) * Auto-update dependencies. * Update requirements.txt * Update requirements.txt --- samples/snippets/requirements.txt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index 8d8575ea..4b8bd4dd 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,6 +1,2 @@ -<<<<<<< HEAD google-cloud-automl==1.0.1 -google-cloud-storage==1.29.0 -======= -google-cloud-automl==0.1.1 ->>>>>>> d89df5c (Auto-update dependencies. [(#1658)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/1658)) +google-cloud-storage==1.29.0 \ No newline at end of file From cea0af755b248cee8c490a4c0e9100842b7e2732 Mon Sep 17 00:00:00 2001 From: Charles Engelke Date: Fri, 26 Apr 2019 14:44:38 -0700 Subject: [PATCH 09/17] Updated beta version of automl [(#2124)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2124) --- samples/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index 4b8bd4dd..f6a42ea2 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,2 +1,2 @@ google-cloud-automl==1.0.1 -google-cloud-storage==1.29.0 \ No newline at end of file +google-cloud-storage==1.29.0 From 22adca8c8ff3cc77479f1a34ee512a5e2580af11 Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Mon, 2 Mar 2020 15:29:23 -0800 Subject: [PATCH 10/17] Translate: migrate published v3 translate batch samples [(#2914)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2914) * Translate: migrate published b v3 tch samples * added missing requirements * extended wait time * inlined some vals and specified input and output * added link to supported file types & modified default values of input uri * fixed small nit --- samples/snippets/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index f6a42ea2..8225b703 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,2 +1,3 @@ +google-cloud-translate==2.0.0 google-cloud-automl==1.0.1 google-cloud-storage==1.29.0 From 92028e3fc5cb907a1cbe1a64657b4a0ed97e4686 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Tue, 19 May 2020 04:18:01 +0200 Subject: [PATCH 11/17] chore(deps): update dependency google-cloud-storage to v1.28.1 [(#3785)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/3785) * chore(deps): update dependency google-cloud-storage to v1.28.1 * [asset] testing: use uuid instead of time Co-authored-by: Takashi Matsuo --- samples/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index 8225b703..e20862e9 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,3 +1,3 @@ -google-cloud-translate==2.0.0 +google-cloud-translate==2.0.1 google-cloud-automl==1.0.1 google-cloud-storage==1.29.0 From 975e4f0791d37282091354ac26b716eba0a3d0a9 Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> Date: Tue, 9 Jun 2020 14:34:27 -0700 Subject: [PATCH 12/17] Replace GCLOUD_PROJECT with GOOGLE_CLOUD_PROJECT. [(#4022)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4022) --- samples/snippets/dataset_test.py | 2 +- samples/snippets/model_test.py | 2 +- samples/snippets/predict_test.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/snippets/dataset_test.py b/samples/snippets/dataset_test.py index 29e3e5c9..4430ec54 100644 --- a/samples/snippets/dataset_test.py +++ b/samples/snippets/dataset_test.py @@ -21,7 +21,7 @@ import automl_translation_dataset -project_id = os.environ["GCLOUD_PROJECT"] +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] compute_region = "us-central1" diff --git a/samples/snippets/model_test.py b/samples/snippets/model_test.py index 0d37a85c..e19a50ea 100644 --- a/samples/snippets/model_test.py +++ b/samples/snippets/model_test.py @@ -22,7 +22,7 @@ import automl_translation_model -project_id = os.environ["GCLOUD_PROJECT"] +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] compute_region = "us-central1" diff --git a/samples/snippets/predict_test.py b/samples/snippets/predict_test.py index f9d98dfb..d00a4658 100644 --- a/samples/snippets/predict_test.py +++ b/samples/snippets/predict_test.py @@ -18,7 +18,7 @@ import automl_translation_predict -project_id = os.environ["GCLOUD_PROJECT"] +project_id = os.environ["GOOGLE_CLOUD_PROJECT"] compute_region = "us-central1" From 5082deeb5a49b71e82aa6cf0906eda31de3cd082 Mon Sep 17 00:00:00 2001 From: Takashi Matsuo Date: Thu, 23 Jul 2020 11:23:31 -0700 Subject: [PATCH 13/17] fix(translate): fix a broken test [(#4360)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4360) * fix(translate): fix a broken test fixes #4353 * use uuid * fix builds --- samples/snippets/dataset_test.py | 4 ++-- samples/snippets/model_test.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/snippets/dataset_test.py b/samples/snippets/dataset_test.py index 4430ec54..eb5796d5 100644 --- a/samples/snippets/dataset_test.py +++ b/samples/snippets/dataset_test.py @@ -14,8 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import datetime import os +import uuid import pytest @@ -28,7 +28,7 @@ @pytest.mark.slow def test_dataset_create_import_delete(capsys): # create dataset - dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + dataset_name = f"test_{uuid.uuid4().hex[:27]}" automl_translation_dataset.create_dataset( project_id, compute_region, dataset_name, "en", "ja" ) diff --git a/samples/snippets/model_test.py b/samples/snippets/model_test.py index e19a50ea..fd2fabc3 100644 --- a/samples/snippets/model_test.py +++ b/samples/snippets/model_test.py @@ -77,4 +77,4 @@ def test_model_list_get_evaluate(capsys): project_id, compute_region, model_id, model_evaluation_id ) out, _ = capsys.readouterr() - assert "evaluation_metric" in out + assert model_evaluation_id in out From 5c10928945d4f557df5c0f93ea40b5ff64496d50 Mon Sep 17 00:00:00 2001 From: WhiteSource Renovate Date: Wed, 5 Aug 2020 01:36:03 +0200 Subject: [PATCH 14/17] chore(deps): update dependency google-cloud-translate to v2.0.2 [(#4426)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/4426) This PR contains the following updates: | Package | Update | Change | |---|---|---| | [google-cloud-translate](https://togithub.com/googleapis/python-translate) | patch | `==2.0.1` -> `==2.0.2` | ---
googleapis/python-translate [Compare Source](https://togithub.com/googleapis/python-translate/compare/v2.0.1...v2.0.2)
--- :date: **Schedule**: At any time (no schedule defined). :vertical_traffic_light: **Automerge**: Disabled by config. Please merge this manually once you are satisfied. :recycle: **Rebasing**: Never, or you tick the rebase/retry checkbox. :no_bell: **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] If you want to rebase/retry this PR, check this box --- This PR has been generated by [WhiteSource Renovate](https://renovate.whitesourcesoftware.com). View repository job log [here](https://app.renovatebot.com/dashboard#GoogleCloudPlatform/python-docs-samples). --- samples/snippets/requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index e20862e9..f60c9855 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,3 +1,3 @@ -google-cloud-translate==2.0.1 -google-cloud-automl==1.0.1 +google-cloud-translate==2.0.2 google-cloud-storage==1.29.0 +google-cloud-automl==1.0.1 From 0089f38bd412e8ef487910344af921b5b70998b4 Mon Sep 17 00:00:00 2001 From: Renovate Bot Date: Wed, 29 Jul 2020 18:11:30 +0000 Subject: [PATCH 15/17] Update dependency google-cloud-storage to v1.30.0 --- samples/snippets/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/snippets/requirements.txt b/samples/snippets/requirements.txt index f60c9855..185fa19f 100644 --- a/samples/snippets/requirements.txt +++ b/samples/snippets/requirements.txt @@ -1,3 +1,3 @@ google-cloud-translate==2.0.2 -google-cloud-storage==1.29.0 +google-cloud-storage==1.30.0 google-cloud-automl==1.0.1 From d5a944b74d17ec772d8ba4586e87cf071fd4edb6 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Tue, 18 Aug 2020 00:33:29 +0000 Subject: [PATCH 16/17] docs: add cancel operation sample --- samples/beta/cancel_operation.py | 63 ++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 samples/beta/cancel_operation.py diff --git a/samples/beta/cancel_operation.py b/samples/beta/cancel_operation.py new file mode 100644 index 00000000..9240a468 --- /dev/null +++ b/samples/beta/cancel_operation.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2019 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 sys + +# [START automl_cancel_operation] + +from google.cloud import automl_v1beta1 + + +def sample_cancel_operation(project, operation_id): + """ + Cancel Long-Running Operation + + Args: + project Required. Your Google Cloud Project ID. + operation_id Required. The ID of the Operation. + """ + + client = automl_v1beta1.AutoMlClient() + + operations_client = client.transport._operations_client + + # project = '[Google Cloud Project ID]' + # operation_id = '[Operation ID]' + name = "projects/{}/locations/us-central1/operations/{}".format( + project, operation_id + ) + + operations_client.cancel_operation(name) + + print(u"Cancelled operation: {}".format(name)) + + +# [END automl_cancel_operation] + + +def main(): + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument("--project", type=str, default="[Google Cloud Project ID]") + parser.add_argument("--operation_id", type=str, default="[Operation ID]") + args = parser.parse_args() + + sample_cancel_operation(args.project, args.operation_id) + + +if __name__ == "__main__": + main() \ No newline at end of file From b7b2677f38849eb51893c4d1cd2f48c26fb2ab72 Mon Sep 17 00:00:00 2001 From: Bu Sun Kim Date: Tue, 18 Aug 2020 01:22:32 +0000 Subject: [PATCH 17/17] chore: lint --- samples/beta/cancel_operation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/beta/cancel_operation.py b/samples/beta/cancel_operation.py index 9240a468..a30fe2a4 100644 --- a/samples/beta/cancel_operation.py +++ b/samples/beta/cancel_operation.py @@ -14,7 +14,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import sys # [START automl_cancel_operation] @@ -60,4 +59,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main()