Skip to content

Commit d515b75

Browse files
munkhuushmgldandhlee
authored andcommitted
Translate: migrate published v3 translate batch samples [(#2914)](#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
1 parent 52ce961 commit d515b75

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
<<<<<<< HEAD
12
google-cloud-translate==3.0.0
23
google-cloud-storage==1.30.0
34
google-cloud-automl==1.0.1
5+
=======
6+
google-cloud-translate==2.0.0
7+
google-cloud-storage==1.19.1
8+
google-cloud-automl==0.9.0
9+
>>>>>>> 65dd67b5 (Translate: migrate published v3 translate batch samples [(#2914)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2914))
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START translate_v3_batch_translate_text_with_model]
17+
from google.cloud import translate
18+
19+
20+
def batch_translate_text_with_model(
21+
input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt",
22+
output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/",
23+
project_id="YOUR_PROJECT_ID",
24+
model_id="YOUR_MODEL_ID",
25+
):
26+
"""Batch translate text using Translation model.
27+
Model can be AutoML or General[built-in] model. """
28+
29+
client = translate.TranslationServiceClient()
30+
31+
# Supported file types: https://cloud.google.com/translate/docs/supported-formats
32+
gcs_source = {"input_uri": input_uri}
33+
location = "us-central1"
34+
35+
input_configs_element = {
36+
"gcs_source": gcs_source,
37+
"mime_type": "text/plain" # Can be "text/plain" or "text/html".
38+
}
39+
gcs_destination = {"output_uri_prefix": output_uri}
40+
output_config = {"gcs_destination": gcs_destination}
41+
parent = client.location_path(project_id, location)
42+
43+
model_path = "projects/{}/locations/{}/models/{}".format(
44+
project_id, location, model_id # The location of AutoML model.
45+
)
46+
47+
# Supported language codes: https://cloud.google.com/translate/docs/languages
48+
models = {"ja": model_path} # takes a target lang as key.
49+
50+
operation = client.batch_translate_text(
51+
parent=parent,
52+
source_language_code="en",
53+
target_language_codes=["ja"], # Up to 10 language codes here.
54+
input_configs=[input_configs_element],
55+
output_config=output_config,
56+
models=models,
57+
)
58+
59+
print(u"Waiting for operation to complete...")
60+
response = operation.result()
61+
62+
# Display the translation for each input text provided.
63+
print(u"Total Characters: {}".format(response.total_characters))
64+
print(u"Translated Characters: {}".format(response.translated_characters))
65+
66+
67+
# [END translate_v3_batch_translate_text_with_model]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
import pytest
17+
import uuid
18+
import translate_v3_batch_translate_text_with_model
19+
from google.cloud import storage
20+
21+
PROJECT_ID = os.environ["GCLOUD_PROJECT"]
22+
MODEL_ID = "TRL3128559826197068699"
23+
24+
25+
@pytest.fixture(scope="function")
26+
def bucket():
27+
"""Create a temporary bucket to store annotation output."""
28+
bucket_name = str(uuid.uuid1())
29+
storage_client = storage.Client()
30+
bucket = storage_client.create_bucket(bucket_name)
31+
32+
yield bucket
33+
34+
bucket.delete(force=True)
35+
36+
37+
def test_batch_translate_text_with_model(capsys, bucket):
38+
translate_v3_batch_translate_text_with_model.batch_translate_text_with_model(
39+
"gs://cloud-samples-data/translation/custom_model_text.txt",
40+
"gs://{}/translation/BATCH_TRANSLATION_OUTPUT/".format(bucket.name),
41+
PROJECT_ID,
42+
MODEL_ID,
43+
)
44+
out, _ = capsys.readouterr()
45+
assert "Total Characters: 15" in out
46+
assert "Translated Characters: 15" in out

0 commit comments

Comments
 (0)