diff --git a/translation/samples/snippets/beta_snippets.py b/translation/samples/snippets/beta_snippets.py index 835e55b16ad6..6b770e6126d9 100644 --- a/translation/samples/snippets/beta_snippets.py +++ b/translation/samples/snippets/beta_snippets.py @@ -77,7 +77,7 @@ def batch_translate_text(project_id, input_uri, output_uri): } ) - result = operation.result(timeout=240) + result = operation.result(timeout=320) print("Total Characters: {}".format(result.total_characters)) print("Translated Characters: {}".format(result.translated_characters)) diff --git a/translation/samples/snippets/translate_v3_batch_translate_text_test.py b/translation/samples/snippets/translate_v3_batch_translate_text_test.py index 8629d47521b2..ad637cd8938e 100644 --- a/translation/samples/snippets/translate_v3_batch_translate_text_test.py +++ b/translation/samples/snippets/translate_v3_batch_translate_text_test.py @@ -42,7 +42,7 @@ def test_batch_translate_text(capsys, bucket): "gs://cloud-samples-data/translation/text.txt", "gs://{}/translation/BATCH_TRANSLATION_OUTPUT/".format(bucket.name), PROJECT_ID, - timeout=240, + timeout=320, ) out, _ = capsys.readouterr() assert "Total Characters" in out diff --git a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py index 6250034246b6..574b001c6651 100644 --- a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py +++ b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py @@ -22,7 +22,7 @@ def batch_translate_text_with_glossary( output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID", - timeout=180, + timeout=320, ): """Translates a batch of texts on GCS and stores the result in a GCS location. Glossary is applied for translation.""" diff --git a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py index e9a6905e8d46..3110dcee53cb 100644 --- a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py +++ b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py @@ -1,12 +1,10 @@ -# -*- 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 +# 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, @@ -14,29 +12,16 @@ # See the License for the specific language governing permissions and # limitations under the License. -# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "translate_v3_batch_translate_text_with_glossary_and_model") - -# To install the latest published package dependency, execute the following: -# pip install google-cloud-translate - -# sample-metadata -# title: Batch Translate with Glossary and Model -# description: Batch translate text with Glossary using AutoML Translation model -# usage: python3 translate_v3_batch_translate_text_with_glossary_and_model.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--target_language en] [--source_language de] [--model_id "{your-model-id}"] [--glossary_id "{your-glossary-id}"] - # [START translate_v3_batch_translate_text_with_glossary_and_model] from google.cloud import translate -def sample_batch_translate_text_with_glossary_and_model( - input_uri, - output_uri, - project_id, - location, - target_language, - source_language, - model_id, - glossary_id, +def batch_translate_text_with_glossary_and_model( + input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", + output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", + project_id="YOUR_PROJECT_ID", + model_id="YOUR_MODEL_ID", + glossary_id="YOUR_GLOSSARY_ID", ): """ Batch translate text with Glossary and Translation model @@ -44,16 +29,10 @@ def sample_batch_translate_text_with_glossary_and_model( client = translate.TranslationServiceClient() - # TODO(developer): Uncomment and set the following variables - # input_uri = 'gs://cloud-samples-data/text.txt' - # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' - # project = '[Google Cloud Project ID]' - # location = 'us-central1' - # target_language = 'en' - # source_language = 'de' - # model_id = '{your-model-id}' - # glossary_id = '[YOUR_GLOSSARY_ID]' - target_language_codes = [target_language] + # Supported language codes: https://cloud.google.com/translate/docs/languages + location = "us-central1" + + target_language_codes = ["ja"] gcs_source = {"input_uri": input_uri} # Optional. Can be "text/plain" or "text/html". @@ -66,7 +45,7 @@ def sample_batch_translate_text_with_glossary_and_model( model_path = "projects/{}/locations/{}/models/{}".format( project_id, "us-central1", model_id ) - models = {target_language: model_path} + models = {"ja": model_path} glossary_path = client.glossary_path( project_id, "us-central1", glossary_id # The location of the glossary @@ -96,39 +75,3 @@ def sample_batch_translate_text_with_glossary_and_model( # [END translate_v3_batch_translate_text_with_glossary_and_model] - - -def main(): - import argparse - - parser = argparse.ArgumentParser() - parser.add_argument( - "--input_uri", type=str, default="gs://cloud-samples-data/text.txt" - ) - parser.add_argument( - "--output_uri", type=str, default="gs://YOUR_BUCKET_ID/path_to_store_results/" - ) - parser.add_argument("--project_id", type=str, default="[Google Cloud Project ID]") - parser.add_argument("--location", type=str, default="us-central1") - parser.add_argument("--target_language", type=str, default="en") - parser.add_argument("--source_language", type=str, default="de") - parser.add_argument("--model_id", type=str, default="{your-model-id}") - parser.add_argument( - "--glossary_id", type=str, default="[YOUR_GLOSSARY_ID]", - ) - args = parser.parse_args() - - sample_batch_translate_text_with_glossary_and_model( - args.input_uri, - args.output_uri, - args.project_id, - args.location, - args.target_language, - args.source_language, - args.model_id, - args.glossary_id, - ) - - -if __name__ == "__main__": - main() diff --git a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py index 6ba15118fc59..6579831af275 100644 --- a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py +++ b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py @@ -56,13 +56,10 @@ def bucket(): def test_batch_translate_text_with_glossary_and_model(capsys, bucket, glossary): - translate_v3_batch_translate_text_with_glossary_and_model.sample_batch_translate_text_with_glossary_and_model( + translate_v3_batch_translate_text_with_glossary_and_model.batch_translate_text_with_glossary_and_model( "gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt", "gs://{}/translation/BATCH_TRANSLATION_OUTPUT/".format(bucket.name), PROJECT_ID, - "us-central1", - "ja", - "en", MODEL_ID, glossary, ) diff --git a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py index 726a8e0c066e..33a1f829b3a3 100644 --- a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py +++ b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py @@ -73,7 +73,7 @@ def test_batch_translate_text_with_glossary(capsys, bucket, glossary): "gs://{}/translation/BATCH_TRANSLATION_OUTPUT/".format(bucket.name), PROJECT_ID, glossary, - 240, + 320, ) out, _ = capsys.readouterr()