Skip to content

Commit

Permalink
automl: add translate ga samples [(#2679)](GoogleCloudPlatform/python…
Browse files Browse the repository at this point in the history
…-docs-samples#2679)

* automl: add translate ga samples

* While still testing python2 on kokoro, use unicode print for non-ascii strings

Co-authored-by: Leah E. Cole <[email protected]>
  • Loading branch information
2 people authored and busunkim96 committed Aug 11, 2020
1 parent bbf17e4 commit 9f7408e
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Tell me how this ends
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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()
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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)
45 changes: 45 additions & 0 deletions packages/google-cloud-automl/samples/snippets/translate_predict.py
Original file line number Diff line number Diff line change
@@ -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(u"Translated content: {}".format(translated_content.content))
# [END automl_translate_predict]
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9f7408e

Please sign in to comment.