From a51bf7d711c44d1a4a32b24fd06a49128ebe516d Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 2 Jan 2020 14:11:39 -0700 Subject: [PATCH 1/2] automl: add translate ga samples --- automl/cloud-client/resources/input.txt | 1 + .../cloud-client/translate_create_dataset.py | 45 +++++++++++++++++++ .../translate_create_dataset_test.py | 40 +++++++++++++++++ automl/cloud-client/translate_create_model.py | 43 ++++++++++++++++++ .../translate_create_model_test.py | 35 +++++++++++++++ automl/cloud-client/translate_predict.py | 45 +++++++++++++++++++ automl/cloud-client/translate_predict_test.py | 42 +++++++++++++++++ 7 files changed, 251 insertions(+) create mode 100644 automl/cloud-client/resources/input.txt create mode 100644 automl/cloud-client/translate_create_dataset.py create mode 100644 automl/cloud-client/translate_create_dataset_test.py create mode 100644 automl/cloud-client/translate_create_model.py create mode 100644 automl/cloud-client/translate_create_model_test.py create mode 100644 automl/cloud-client/translate_predict.py create mode 100644 automl/cloud-client/translate_predict_test.py diff --git a/automl/cloud-client/resources/input.txt b/automl/cloud-client/resources/input.txt new file mode 100644 index 000000000000..5aecd6590fca --- /dev/null +++ b/automl/cloud-client/resources/input.txt @@ -0,0 +1 @@ +Tell me how this ends \ No newline at end of file diff --git a/automl/cloud-client/translate_create_dataset.py b/automl/cloud-client/translate_create_dataset.py new file mode 100644 index 000000000000..7afe64ac88a1 --- /dev/null +++ b/automl/cloud-client/translate_create_dataset.py @@ -0,0 +1,45 @@ +# Copyright 2020 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. + + +def create_dataset(project_id, display_name): + """Create a dataset.""" + # [START automl_translate_create_dataset] + from google.cloud import automl + + # TODO(developer): Uncomment and set the following variables + # project_id = "YOUR_PROJECT_ID" + # display_name = "YOUR_DATASET_NAME" + + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, "us-central1") + dataset_metadata = automl.types.TranslationDatasetMetadata( + source_language_code="en", target_language_code="ja" + ) + dataset = automl.types.Dataset( + display_name=display_name, + translation_dataset_metadata=dataset_metadata, + ) + + # Create a dataset with the dataset metadata in the region. + response = client.create_dataset(project_location, dataset) + + created_dataset = response.result() + + # Display the dataset information + print("Dataset name: {}".format(created_dataset.name)) + print("Dataset id: {}".format(created_dataset.name.split("/")[-1])) + # [END automl_translate_create_dataset] diff --git a/automl/cloud-client/translate_create_dataset_test.py b/automl/cloud-client/translate_create_dataset_test.py new file mode 100644 index 000000000000..1129babac7f3 --- /dev/null +++ b/automl/cloud-client/translate_create_dataset_test.py @@ -0,0 +1,40 @@ +# Copyright 2020 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 + +import translate_create_dataset + + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] + + +def test_translate_create_dataset(capsys): + # create dataset + dataset_name = "test_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + translate_create_dataset.create_dataset(PROJECT_ID, dataset_name) + out, _ = capsys.readouterr() + assert "Dataset id: " in out + + # Delete the created dataset + dataset_id = out.splitlines()[1].split()[2] + client = automl.AutoMlClient() + dataset_full_id = client.dataset_path( + PROJECT_ID, "us-central1", dataset_id + ) + response = client.delete_dataset(dataset_full_id) + response.result() diff --git a/automl/cloud-client/translate_create_model.py b/automl/cloud-client/translate_create_model.py new file mode 100644 index 000000000000..c83c1aa4ab49 --- /dev/null +++ b/automl/cloud-client/translate_create_model.py @@ -0,0 +1,43 @@ +# Copyright 2020 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. + + +def create_model(project_id, dataset_id, display_name): + """Create a model.""" + # [START automl_translate_create_model] + from google.cloud import automl + + # TODO(developer): Uncomment and set the following variables + # project_id = "YOUR_PROJECT_ID" + # dataset_id = "YOUR_DATASET_ID" + # display_name = "YOUR_MODEL_NAME" + + client = automl.AutoMlClient() + + # A resource that represents Google Cloud Platform location. + project_location = client.location_path(project_id, "us-central1") + # Leave model unset to use the default base model provided by Google + translation_model_metadata = automl.types.TranslationModelMetadata() + model = automl.types.Model( + display_name=display_name, + dataset_id=dataset_id, + translation_model_metadata=translation_model_metadata, + ) + + # Create a model with the model metadata in the region. + response = client.create_model(project_location, model) + + print("Training operation name: {}".format(response.operation.name)) + print("Training started...") + # [END automl_translate_create_model] diff --git a/automl/cloud-client/translate_create_model_test.py b/automl/cloud-client/translate_create_model_test.py new file mode 100644 index 000000000000..e055d66806c7 --- /dev/null +++ b/automl/cloud-client/translate_create_model_test.py @@ -0,0 +1,35 @@ +# Copyright 2020 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 + +from google.cloud import automl + +import translate_create_model + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +DATASET_ID = "TRL3876092572857648864" + + +def test_translate_create_model(capsys): + translate_create_model.create_model( + PROJECT_ID, DATASET_ID, "translate_test_create_model" + ) + out, _ = capsys.readouterr() + assert "Training started" in out + + # Cancel the operation + operation_id = out.split("Training operation name: ")[1].split("\n")[0] + client = automl.AutoMlClient() + client.transport._operations_client.cancel_operation(operation_id) diff --git a/automl/cloud-client/translate_predict.py b/automl/cloud-client/translate_predict.py new file mode 100644 index 000000000000..4ebe05e9da49 --- /dev/null +++ b/automl/cloud-client/translate_predict.py @@ -0,0 +1,45 @@ +# Copyright 2020 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. + + +def predict(project_id, model_id, file_path): + """Predict.""" + # [START automl_translate_predict] + from google.cloud import automl + + # TODO(developer): Uncomment and set the following variables + # project_id = "YOUR_PROJECT_ID" + # model_id = "YOUR_MODEL_ID" + # file_path = "path_to_local_file.txt" + + prediction_client = automl.PredictionServiceClient() + + # Get the full path of the model. + model_full_id = prediction_client.model_path( + project_id, "us-central1", model_id + ) + + # Read the file content for translation. + with open(file_path, "rb") as content_file: + content = content_file.read() + content.decode("utf-8") + + text_snippet = automl.types.TextSnippet(content=content) + payload = automl.types.ExamplePayload(text_snippet=text_snippet) + + response = prediction_client.predict(model_full_id, payload) + translated_content = response.payload[0].translation.translated_content + + print("Translated content: {}".format(translated_content.content)) + # [END automl_translate_predict] diff --git a/automl/cloud-client/translate_predict_test.py b/automl/cloud-client/translate_predict_test.py new file mode 100644 index 000000000000..32134ef1cfb5 --- /dev/null +++ b/automl/cloud-client/translate_predict_test.py @@ -0,0 +1,42 @@ +# Copyright 2020 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 + +from google.cloud import automl +import pytest + +import translate_predict + +PROJECT_ID = os.environ["GCLOUD_PROJECT"] +MODEL_ID = "TRL3128559826197068699" + + +@pytest.fixture(scope="function") +def verify_model_state(): + client = automl.AutoMlClient() + model_full_id = client.model_path(PROJECT_ID, "us-central1", MODEL_ID) + + model = client.get_model(model_full_id) + if model.deployment_state == automl.enums.Model.DeploymentState.UNDEPLOYED: + # Deploy model if it is not deployed + response = client.deploy_model(model_full_id) + response.result() + + +def test_predict(capsys, verify_model_state): + verify_model_state + translate_predict.predict(PROJECT_ID, MODEL_ID, "resources/input.txt") + out, _ = capsys.readouterr() + assert "Translated content: " in out From e1ee025314c05db92c41a264e613aaa5ab19e961 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Thu, 2 Jan 2020 14:21:35 -0700 Subject: [PATCH 2/2] While still testing python2 on kokoro, use unicode print for non-ascii strings --- automl/cloud-client/translate_predict.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/automl/cloud-client/translate_predict.py b/automl/cloud-client/translate_predict.py index 4ebe05e9da49..31c965e8643c 100644 --- a/automl/cloud-client/translate_predict.py +++ b/automl/cloud-client/translate_predict.py @@ -41,5 +41,5 @@ def predict(project_id, model_id, file_path): response = prediction_client.predict(model_full_id, payload) translated_content = response.payload[0].translation.translated_content - print("Translated content: {}".format(translated_content.content)) + print(u"Translated content: {}".format(translated_content.content)) # [END automl_translate_predict]