diff --git a/packages/google-cloud-translate/samples/README.md b/packages/google-cloud-translate/samples/README.md new file mode 100644 index 000000000000..5ee776ecfe22 --- /dev/null +++ b/packages/google-cloud-translate/samples/README.md @@ -0,0 +1,5 @@ +# Samples for python-translate + +The hand-written code samples for the googleapis/python-translate repo have +been moved to +[GoogleCloudPlatform/python-docs-samples](https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/translate/samples). \ No newline at end of file diff --git a/packages/google-cloud-translate/samples/cloud-nebulous-serverless/README.md b/packages/google-cloud-translate/samples/cloud-nebulous-serverless/README.md deleted file mode 100644 index e2c82823a7d1..000000000000 --- a/packages/google-cloud-translate/samples/cloud-nebulous-serverless/README.md +++ /dev/null @@ -1 +0,0 @@ -This sample, demonstrating how to access the [Cloud Translation API](https://cloud.google.com/translate) from [Google Cloud serverless platforms](https://cloud.google.com/serverless) (App Engine, Cloud Functions, Cloud Run) can be found at . Versions in Python (2.7 and 3.7+) and Node.js (10+) are available along with hands-on tutorials. diff --git a/packages/google-cloud-translate/samples/snippets/README.rst b/packages/google-cloud-translate/samples/snippets/README.rst deleted file mode 100644 index 559b57e2a501..000000000000 --- a/packages/google-cloud-translate/samples/snippets/README.rst +++ /dev/null @@ -1,202 +0,0 @@ - -.. This file is automatically generated. Do not edit this file directly. - -Google Translation API Python Samples -=============================================================================== - -.. image:: https://gstatic.com/cloudssh/images/open-btn.png - :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/README.rst - - -This directory contains samples for Google Translation API. With `Google Translation API`, you can dynamically translate text between thousands of language pairs. - - - - -.. _Google Translation API: https://cloud.google.com/translate/docs - - -Setup -------------------------------------------------------------------------------- - - - -Authentication -++++++++++++++ - -This sample requires you to have authentication setup. Refer to the -`Authentication Getting Started Guide`_ for instructions on setting up -credentials for applications. - -.. _Authentication Getting Started Guide: - https://cloud.google.com/docs/authentication/getting-started - - - - -Install Dependencies -++++++++++++++++++++ - -#. Clone python-docs-samples and change directory to the sample directory you want to use. - - .. code-block:: bash - - $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git - -#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. - - .. _Python Development Environment Setup Guide: - https://cloud.google.com/python/setup - -#. Create a virtualenv. Samples are compatible with Python 3.6+. - - .. code-block:: bash - - $ virtualenv env - $ source env/bin/activate - -#. Install the dependencies needed to run the samples. - - .. code-block:: bash - - $ pip install -r requirements.txt - -.. _pip: https://pip.pypa.io/ -.. _virtualenv: https://virtualenv.pypa.io/ - - - - - - -Samples -------------------------------------------------------------------------------- - - -Quickstart -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -.. image:: https://gstatic.com/cloudssh/images/open-btn.png - :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/quickstart.py,translate/cloud-client/README.rst - - - - -To run this sample: - -.. code-block:: bash - - $ python quickstart.py - - - - -Snippets -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -.. image:: https://gstatic.com/cloudssh/images/open-btn.png - :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/snippets.py,translate/cloud-client/README.rst - - - - -To run this sample: - -.. code-block:: bash - - $ python snippets.py - - - usage: snippets.py [-h] - {detect-language,list-languages,list-languages-with-target,translate-text} - ... - - This application demonstrates how to perform basic operations with the - Google Cloud Translate API - - For more information, the documentation at - https://cloud.google.com/translate/docs. - - positional arguments: - {detect-language,list-languages,list-languages-with-target,translate-text} - detect-language Detects the text's language. - list-languages Lists all available languages. - list-languages-with-target - Lists all available languages and localizes them to - the target language. Target must be an ISO 639-1 - language code. See - https://g.co/cloud/translate/v2/translate- - reference#supported_languages - translate-text Translates text into the target language. Target must - be an ISO 639-1 language code. See - https://g.co/cloud/translate/v2/translate- - reference#supported_languages - - optional arguments: - -h, --help show this help message and exit - - - - - -Beta Snippets -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -.. image:: https://gstatic.com/cloudssh/images/open-btn.png - :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/beta_snippets.py,translate/cloud-client/README.rst - - - - -To run this sample: - -.. code-block:: bash - - $ python beta_snippets.py - - - usage: beta_snippets.py [-h] - {translate-text,batch-translate-text,detect-language,list-languages,list-languages-with-target,create-glossary,get-glossary,list-glossaries,delete-glossary,translate-with-glossary} - ... - - positional arguments: - {translate-text,batch-translate-text,detect-language,list-languages,list-languages-with-target,create-glossary,get-glossary,list-glossaries,delete-glossary,translate-with-glossary} - translate-text - batch-translate-text - detect-language - list-languages - list-languages-with-target - create-glossary - get-glossary - list-glossaries - delete-glossary - translate-with-glossary - - optional arguments: - -h, --help show this help message and exit - - - - - - - - - -The client library -------------------------------------------------------------------------------- - -This sample uses the `Google Cloud Client Library for Python`_. -You can read the documentation for more details on API usage and use GitHub -to `browse the source`_ and `report issues`_. - -.. _Google Cloud Client Library for Python: - https://googlecloudplatform.github.io/google-cloud-python/ -.. _browse the source: - https://github.com/GoogleCloudPlatform/google-cloud-python -.. _report issues: - https://github.com/GoogleCloudPlatform/google-cloud-python/issues - - - -.. _Google Cloud SDK: https://cloud.google.com/sdk/ diff --git a/packages/google-cloud-translate/samples/snippets/README.rst.in b/packages/google-cloud-translate/samples/snippets/README.rst.in deleted file mode 100644 index ba804e74de3d..000000000000 --- a/packages/google-cloud-translate/samples/snippets/README.rst.in +++ /dev/null @@ -1,27 +0,0 @@ -# This file is used to generate README.rst - -product: - name: Google Translation API - short_name: Translation API - url: https://cloud.google.com/translate/docs - description: > - With `Google Translation API`, you can dynamically translate text between - thousands of language pairs. - -setup: -- auth -- install_deps - -samples: -- name: Quickstart - file: quickstart.py -- name: Snippets - file: snippets.py - show_help: true -- name: Beta Snippets - file: beta_snippets.py - show_help: true - -cloud_client_library: true - -folder: translate/cloud-client \ No newline at end of file diff --git a/packages/google-cloud-translate/samples/snippets/beta_snippets.py b/packages/google-cloud-translate/samples/snippets/beta_snippets.py deleted file mode 100644 index 5ec1f2984134..000000000000 --- a/packages/google-cloud-translate/samples/snippets/beta_snippets.py +++ /dev/null @@ -1,382 +0,0 @@ -# -*- 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 -# -# 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 argparse - - -def translate_text(project_id, text): - # [START translate_translate_text_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = YOUR_PROJECT_ID - # text = 'Text you wish to translate' - location = "global" - - parent = f"projects/{project_id}/locations/{location}" - - response = client.translate_text( - request={ - "parent": parent, - "contents": [text], - "mime_type": "text/plain", # mime types: text/plain, text/html - "source_language_code": "en-US", - "target_language_code": "sr-Latn", - } - ) - - for translation in response.translations: - print("Translated Text: {}".format(translation)) - # [END translate_translate_text_beta] - - -def batch_translate_text(project_id, input_uri, output_uri): - # [START translate_batch_translate_text_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = YOUR_PROJECT_ID - # input_uri = 'gs://cloud-samples-data/translation/text.txt' - # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' - location = "us-central1" - - parent = f"projects/{project_id}/locations/{location}" - - gcs_source = translate.types.GcsSource(input_uri=input_uri) - - input_config = translate.types.InputConfig( - mime_type="text/plain", # mime types: text/plain, text/html - gcs_source=gcs_source, - ) - - gcs_destination = translate.types.GcsDestination(output_uri_prefix=output_uri) - - output_config = translate.types.OutputConfig(gcs_destination=gcs_destination) - - operation = client.batch_translate_text( - request={ - "parent": parent, - "source_language_code": "en-US", - "target_language_codes": ["sr-Latn"], - "input_configs": [input_config], - "output_config": output_config, - } - ) - - result = operation.result(timeout=400) - - print("Total Characters: {}".format(result.total_characters)) - print("Translated Characters: {}".format(result.translated_characters)) - # [END translate_batch_translate_text_beta] - - -def detect_language(project_id, text): - # [START translate_detect_language_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = YOUR_PROJECT_ID - # text = 'Text you wish to translate' - location = "global" - - parent = f"projects/{project_id}/locations/{location}" - - response = client.detect_language( - request={ - "parent": parent, - "content": text, - "mime_type": "text/plain", # mime types: text/plain, text/html - } - ) - - for language in response.languages: - print( - "Language Code: {} (Confidence: {})".format( - language.language_code, language.confidence - ) - ) - # [END translate_detect_language_beta] - - -def list_languages(project_id): - # [START translate_list_codes_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = YOUR_PROJECT_ID - location = "global" - - parent = f"projects/{project_id}/locations/{location}" - - response = client.get_supported_languages(parent=parent) - - print("Supported Languages:") - for language in response.languages: - print("Language Code: {}".format(language.language_code)) - # [END translate_list_codes_beta] - - -def list_languages_with_target(project_id, display_language_code): - # [START translate_list_language_names_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = YOUR_PROJECT_ID - # display_language_code = 'is' - location = "global" - - parent = f"projects/{project_id}/locations/{location}" - - response = client.get_supported_languages( - parent=parent, display_language_code=display_language_code - ) - - print("Supported Languages:") - for language in response.languages: - print("Language Code: {}".format(language.language_code)) - print("Display Name: {}\n".format(language.display_name)) - # [END translate_list_language_names_beta] - - -def create_glossary(project_id, glossary_id): - # [START translate_create_glossary_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = 'YOUR_PROJECT_ID' - # glossary_id = 'glossary-id' - location = "us-central1" # The location of the glossary - - name = client.glossary_path(project_id, location, glossary_id) - - language_codes_set = translate.types.Glossary.LanguageCodesSet( - language_codes=["en", "es"] - ) - - gcs_source = translate.types.GcsSource( - input_uri="gs://cloud-samples-data/translation/glossary.csv" - ) - - input_config = translate.types.GlossaryInputConfig(gcs_source=gcs_source) - - glossary = translate.types.Glossary( - name=name, language_codes_set=language_codes_set, input_config=input_config - ) - - parent = f"projects/{project_id}/locations/{location}" - - operation = client.create_glossary(parent=parent, glossary=glossary) - - result = operation.result(timeout=90) - print("Created: {}".format(result.name)) - print("Input Uri: {}".format(result.input_config.gcs_source.input_uri)) - # [END translate_create_glossary_beta] - - -def list_glossaries(project_id): - # [START translate_list_glossary_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = 'YOUR_PROJECT_ID' - location = "us-central1" # The location of the glossary - - parent = f"projects/{project_id}/locations/{location}" - - for glossary in client.list_glossaries(parent=parent): - print("Name: {}".format(glossary.name)) - print("Entry count: {}".format(glossary.entry_count)) - print("Input uri: {}".format(glossary.input_config.gcs_source.input_uri)) - for language_code in glossary.language_codes_set.language_codes: - print("Language code: {}".format(language_code)) - # [END translate_list_glossary_beta] - - -def get_glossary(project_id, glossary_id): - # [START translate_get_glossary_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = 'YOUR_PROJECT_ID' - # glossary_id = 'GLOSSARY_ID' - - name = client.glossary_path( - project_id, "us-central1", glossary_id # The location of the glossary - ) - - response = client.get_glossary(name=name) - print("Name: {}".format(response.name)) - print("Language Pair:") - print( - "\tSource Language Code: {}".format(response.language_pair.source_language_code) - ) - print( - "\tTarget Language Code: {}".format(response.language_pair.target_language_code) - ) - print("Input Uri: {}".format(response.input_config.gcs_source.input_uri)) - # [END translate_get_glossary_beta] - - -def delete_glossary(project_id, glossary_id): - # [START translate_delete_glossary_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = 'YOUR_PROJECT_ID' - # glossary_id = 'GLOSSARY_ID' - - name = client.glossary_path( - project_id, "us-central1", glossary_id # The location of the glossary - ) - - operation = client.delete_glossary(name=name) - result = operation.result(timeout=90) - print("Deleted: {}".format(result.name)) - # [END translate_delete_glossary_beta] - - -def translate_text_with_glossary(project_id, glossary_id, text): - # [START translate_translate_text_with_glossary_beta] - from google.cloud import translate_v3beta1 as translate - - client = translate.TranslationServiceClient() - - # project_id = 'YOUR_PROJECT_ID' - # glossary_id = 'GLOSSARY_ID' - # text = 'Text you wish to translate' - location = "us-central1" # The location of the glossary - - glossary = client.glossary_path( - project_id, "us-central1", glossary_id # The location of the glossary - ) - - glossary_config = translate.types.TranslateTextGlossaryConfig(glossary=glossary) - - parent = f"projects/{project_id}/locations/{location}" - - result = client.translate_text( - request={ - "parent": parent, - "contents": [text], - "mime_type": "text/plain", - "source_language_code": "en", - "target_language_code": "es", - "glossary_config": glossary_config, - } - ) - - for translation in result.glossary_translations: - print(translation) - # [END translate_translate_text_with_glossary_beta] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - - subparsers = parser.add_subparsers(dest="command") - - translate_text_parser = subparsers.add_parser( - "translate-text", help=translate_text.__doc__ - ) - translate_text_parser.add_argument("project_id") - translate_text_parser.add_argument("text") - - batch_translate_text_parser = subparsers.add_parser( - "batch-translate-text", help=translate_text.__doc__ - ) - batch_translate_text_parser.add_argument("project_id") - batch_translate_text_parser.add_argument("gcs_source") - batch_translate_text_parser.add_argument("gcs_destination") - - detect_langage_parser = subparsers.add_parser( - "detect-language", help=detect_language.__doc__ - ) - detect_langage_parser.add_argument("project_id") - detect_langage_parser.add_argument("text") - - list_languages_parser = subparsers.add_parser( - "list-languages", help=list_languages.__doc__ - ) - list_languages_parser.add_argument("project_id") - - list_languages_with_target_parser = subparsers.add_parser( - "list-languages-with-target", help=list_languages_with_target.__doc__ - ) - list_languages_with_target_parser.add_argument("project_id") - list_languages_with_target_parser.add_argument("display_language_code") - - create_glossary_parser = subparsers.add_parser( - "create-glossary", help=create_glossary.__doc__ - ) - create_glossary_parser.add_argument("project_id") - create_glossary_parser.add_argument("glossary_id") - - get_glossary_parser = subparsers.add_parser( - "get-glossary", help=get_glossary.__doc__ - ) - get_glossary_parser.add_argument("project_id") - get_glossary_parser.add_argument("glossary_id") - - list_glossary_parser = subparsers.add_parser( - "list-glossaries", help=list_glossaries.__doc__ - ) - list_glossary_parser.add_argument("project_id") - - delete_glossary_parser = subparsers.add_parser( - "delete-glossary", help=delete_glossary.__doc__ - ) - delete_glossary_parser.add_argument("project_id") - delete_glossary_parser.add_argument("glossary_id") - - translate_with_glossary_parser = subparsers.add_parser( - "translate-with-glossary", help=translate_text_with_glossary.__doc__ - ) - translate_with_glossary_parser.add_argument("project_id") - translate_with_glossary_parser.add_argument("glossary_id") - translate_with_glossary_parser.add_argument("text") - - args = parser.parse_args() - - if args.command == "translate-text": - translate_text(args.project_id, args.text) - elif args.command == "batch-translate-text": - batch_translate_text(args.project_id, args.gcs_source, args.gcs_destination) - elif args.command == "detect-language": - detect_language(args.project_id, args.text) - elif args.command == "list-languages": - list_languages(args.project_id) - elif args.command == "list-languages-with-target": - list_languages_with_target(args.project_id, args.display_language_code) - elif args.command == "create-glossary": - create_glossary(args.project_id, args.glossary_id) - elif args.command == "get-glossary": - get_glossary(args.project_id, args.glossary_id) - elif args.command == "list-glossaries": - list_glossaries(args.project_id) - elif args.command == "delete-glossary": - delete_glossary(args.project_id, args.glossary_id) - elif args.command == "translate-with-glossary": - translate_text_with_glossary(args.project_id, args.glossary_id, args.text) diff --git a/packages/google-cloud-translate/samples/snippets/beta_snippets_test.py b/packages/google-cloud-translate/samples/snippets/beta_snippets_test.py deleted file mode 100644 index f925e63f870b..000000000000 --- a/packages/google-cloud-translate/samples/snippets/beta_snippets_test.py +++ /dev/null @@ -1,139 +0,0 @@ -# -*- 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 -# -# 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 uuid - -from google.cloud import storage -import pytest - -import beta_snippets - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -@pytest.fixture(scope="function") -def bucket(): - """Create a temporary bucket to store annotation output.""" - bucket_name = f"tmp-{uuid.uuid4().hex}" - storage_client = storage.Client() - bucket = storage_client.create_bucket(bucket_name) - - yield bucket - - bucket.delete(force=True) - - -@pytest.fixture(scope="session") -def glossary(): - """Get the ID of a glossary available to session (do not mutate/delete).""" - glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) - beta_snippets.create_glossary(PROJECT_ID, glossary_id) - - yield glossary_id - - try: - beta_snippets.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass - - -@pytest.fixture(scope="function") -def unique_glossary_id(): - """Get a unique ID. Attempts to delete glossary with this ID after test.""" - glossary_id = "must-start-with-letters-" + str(uuid.uuid1()) - - yield glossary_id - - try: - beta_snippets.delete_glossary(PROJECT_ID, glossary_id) - except Exception: - pass - - -def test_translate_text(capsys): - beta_snippets.translate_text(PROJECT_ID, "Hello world") - out, _ = capsys.readouterr() - assert "Translated Text:" in out - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_batch_translate_text(capsys, bucket): - beta_snippets.batch_translate_text( - PROJECT_ID, - "gs://cloud-samples-data/translation/text.txt", - "gs://{}/translation/BATCH_TRANSLATION_BETA_OUTPUT/".format(bucket.name), - ) - out, _ = capsys.readouterr() - assert "Total Characters: 13" in out - assert "Translated Characters: 13" in out - - -def test_detect_language(capsys): - beta_snippets.detect_language(PROJECT_ID, "Hæ sæta") - out, _ = capsys.readouterr() - assert "is" in out - - -def test_list_languages(capsys): - beta_snippets.list_languages(PROJECT_ID) - out, _ = capsys.readouterr() - assert "zh" in out - - -def test_list_languages_with_target(capsys): - beta_snippets.list_languages_with_target(PROJECT_ID, "is") - out, _ = capsys.readouterr() - assert "Language Code: sq" in out - assert "Display Name: albanska" in out - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_create_glossary(capsys, unique_glossary_id): - beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) - out, _ = capsys.readouterr() - assert "Created" in out - assert unique_glossary_id in out - assert "gs://cloud-samples-data/translation/glossary.csv" in out - - -def test_get_glossary(capsys, glossary): - beta_snippets.get_glossary(PROJECT_ID, glossary) - out, _ = capsys.readouterr() - assert glossary in out - assert "gs://cloud-samples-data/translation/glossary.csv" in out - - -def test_list_glossary(capsys, glossary): - beta_snippets.list_glossaries(PROJECT_ID) - out, _ = capsys.readouterr() - assert glossary in out - assert "gs://cloud-samples-data/translation/glossary.csv" in out - - -def test_translate_text_with_glossary(capsys, glossary): - beta_snippets.translate_text_with_glossary(PROJECT_ID, glossary, "account") - out, _ = capsys.readouterr() - assert "cuenta" in out - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_delete_glossary(capsys, unique_glossary_id): - beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id) - beta_snippets.delete_glossary(PROJECT_ID, unique_glossary_id) - out, _ = capsys.readouterr() - assert "us-central1" in out - assert unique_glossary_id in out diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/README.rst b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/README.rst deleted file mode 100644 index e68b8114ce3c..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/README.rst +++ /dev/null @@ -1,114 +0,0 @@ - -.. This file is automatically generated. Do not edit this file directly. - -Google Translation API Python Samples -=============================================================================== - -.. image:: https://gstatic.com/cloudssh/images/open-btn.png - :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/README.rst - - -This directory contains samples for Google Translation API. With `Google Translation API`, you can dynamically translate text between thousands of language pairs. - - - - -.. _Google Translation API: https://cloud.google.com/translate/docs - - -Setup -------------------------------------------------------------------------------- - - - -Authentication -++++++++++++++ - -This sample requires you to have authentication setup. Refer to the -`Authentication Getting Started Guide`_ for instructions on setting up -credentials for applications. - -.. _Authentication Getting Started Guide: - https://cloud.google.com/docs/authentication/getting-started - - - - -Install Dependencies -++++++++++++++++++++ - -#. Clone python-docs-samples and change directory to the sample directory you want to use. - - .. code-block:: bash - - $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git - -#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. - - .. _Python Development Environment Setup Guide: - https://cloud.google.com/python/setup - -#. Create a virtualenv. Samples are compatible with Python 3.6+. - - .. code-block:: bash - - $ virtualenv env - $ source env/bin/activate - -#. Install the dependencies needed to run the samples. - - .. code-block:: bash - - $ pip install -r requirements.txt - -.. _pip: https://pip.pypa.io/ -.. _virtualenv: https://virtualenv.pypa.io/ - - - - - - -Samples -------------------------------------------------------------------------------- - - -Using glossaries with vision and text-to-speech -+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ - -.. image:: https://gstatic.com/cloudssh/images/open-btn.png - :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/hybrid_tutorial.py,/README.rst - - - - -To run this sample: - -.. code-block:: bash - - $ python hybrid_tutorial.py - - - - - - - - -The client library -------------------------------------------------------------------------------- - -This sample uses the `Google Cloud Client Library for Python`_. -You can read the documentation for more details on API usage and use GitHub -to `browse the source`_ and `report issues`_. - -.. _Google Cloud Client Library for Python: - https://googlecloudplatform.github.io/google-cloud-python/ -.. _browse the source: - https://github.com/GoogleCloudPlatform/google-cloud-python -.. _report issues: - https://github.com/GoogleCloudPlatform/google-cloud-python/issues - - - -.. _Google Cloud SDK: https://cloud.google.com/sdk/ diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/README.rst.in b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/README.rst.in deleted file mode 100644 index 882f3666fc9f..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/README.rst.in +++ /dev/null @@ -1,22 +0,0 @@ - - -# This file is used to generate README.rst - -product: - name: Google Translation API - short_name: Translation API - url: https://cloud.google.com/translate/docs - description: > - With `Google Translation API`, you can dynamically translate text between - thousands of language pairs. - -setup: -- auth -- install_deps - -samples: -- name: Using glossaries with vision and text-to-speech - file: hybrid_tutorial.py - -cloud_client_library: true - diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/hybrid_tutorial.py b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/hybrid_tutorial.py deleted file mode 100644 index 9eae168ada3e..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/hybrid_tutorial.py +++ /dev/null @@ -1,257 +0,0 @@ -# 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 -# -# 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. - - -# [START translate_hybrid_imports] -import html -import io -import os - -# Imports the Google Cloud client libraries -from google.api_core.exceptions import AlreadyExists -from google.cloud import texttospeech -from google.cloud import translate_v3beta1 as translate -from google.cloud import vision - -# [END translate_hybrid_imports] - - -# [START translate_hybrid_project_id] -# extract GCP project id -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -# [END translate_hybrid_project_id] - - -# [START translate_hybrid_vision] -def pic_to_text(infile): - """Detects text in an image file - - ARGS - infile: path to image file - - RETURNS - String of text detected in image - """ - - # Instantiates a client - client = vision.ImageAnnotatorClient() - - # Opens the input image file - with io.open(infile, "rb") as image_file: - content = image_file.read() - - image = vision.Image(content=content) - - # For dense text, use document_text_detection - # For less dense text, use text_detection - response = client.document_text_detection(image=image) - text = response.full_text_annotation.text - print("Detected text: {}".format(text)) - - return text - # [END translate_hybrid_vision] - - -# [START translate_hybrid_create_glossary] -def create_glossary(languages, project_id, glossary_name, glossary_uri): - """Creates a GCP glossary resource - Assumes you've already manually uploaded a glossary to Cloud Storage - - ARGS - languages: list of languages in the glossary - project_id: GCP project id - glossary_name: name you want to give this glossary resource - glossary_uri: the uri of the glossary you uploaded to Cloud Storage - - RETURNS - nothing - """ - - # Instantiates a client - client = translate.TranslationServiceClient() - - # Designates the data center location that you want to use - location = "us-central1" - - # Set glossary resource name - name = client.glossary_path(project_id, location, glossary_name) - - # Set language codes - language_codes_set = translate.Glossary.LanguageCodesSet( - language_codes=languages - ) - - gcs_source = translate.GcsSource(input_uri=glossary_uri) - - input_config = translate.GlossaryInputConfig(gcs_source=gcs_source) - - # Set glossary resource information - glossary = translate.Glossary( - name=name, language_codes_set=language_codes_set, input_config=input_config - ) - - parent = f"projects/{project_id}/locations/{location}" - - # Create glossary resource - # Handle exception for case in which a glossary - # with glossary_name already exists - try: - operation = client.create_glossary(parent=parent, glossary=glossary) - operation.result(timeout=90) - print("Created glossary " + glossary_name + ".") - except AlreadyExists: - print( - "The glossary " - + glossary_name - + " already exists. No new glossary was created." - ) - # [END translate_hybrid_create_glossary] - - -# [START translate_hybrid_translate] -def translate_text( - text, source_language_code, target_language_code, project_id, glossary_name -): - """Translates text to a given language using a glossary - - ARGS - text: String of text to translate - source_language_code: language of input text - target_language_code: language of output text - project_id: GCP project id - glossary_name: name you gave your project's glossary - resource when you created it - - RETURNS - String of translated text - """ - - # Instantiates a client - client = translate.TranslationServiceClient() - - # Designates the data center location that you want to use - location = "us-central1" - - glossary = client.glossary_path(project_id, location, glossary_name) - - glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary) - - parent = f"projects/{project_id}/locations/{location}" - - result = client.translate_text( - request={ - "parent": parent, - "contents": [text], - "mime_type": "text/plain", # mime types: text/plain, text/html - "source_language_code": source_language_code, - "target_language_code": target_language_code, - "glossary_config": glossary_config, - } - ) - - # Extract translated text from API response - return result.glossary_translations[0].translated_text - # [END translate_hybrid_translate] - - -# [START translate_hybrid_tts] -def text_to_speech(text, outfile): - """Converts plaintext to SSML and - generates synthetic audio from SSML - - ARGS - text: text to synthesize - outfile: filename to use to store synthetic audio - - RETURNS - nothing - """ - - # Replace special characters with HTML Ampersand Character Codes - # These Codes prevent the API from confusing text with - # SSML commands - # For example, '<' --> '<' and '&' --> '&' - escaped_lines = html.escape(text) - - # Convert plaintext to SSML in order to wait two seconds - # between each line in synthetic speech - ssml = "{}".format( - escaped_lines.replace("\n", '\n') - ) - - # Instantiates a client - client = texttospeech.TextToSpeechClient() - - # Sets the text input to be synthesized - synthesis_input = texttospeech.SynthesisInput(ssml=ssml) - - # Builds the voice request, selects the language code ("en-US") and - # the SSML voice gender ("MALE") - voice = texttospeech.VoiceSelectionParams( - language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.MALE - ) - - # Selects the type of audio file to return - audio_config = texttospeech.AudioConfig( - audio_encoding=texttospeech.AudioEncoding.MP3 - ) - - # Performs the text-to-speech request on the text input with the selected - # voice parameters and audio file type - - request = texttospeech.SynthesizeSpeechRequest( - input=synthesis_input, voice=voice, audio_config=audio_config - ) - - response = client.synthesize_speech(request=request) - - # Writes the synthetic audio to the output file. - with open(outfile, "wb") as out: - out.write(response.audio_content) - print("Audio content written to file " + outfile) - # [END translate_hybrid_tts] - - -# [START translate_hybrid_integration] -def main(): - - # Photo from which to extract text - infile = "resources/example.png" - # Name of file that will hold synthetic speech - outfile = "resources/example.mp3" - - # Defines the languages in the glossary - # This list must match the languages in the glossary - # Here, the glossary includes French and English - glossary_langs = ["fr", "en"] - # Name that will be assigned to your project's glossary resource - glossary_name = "bistro-glossary" - # uri of .csv file uploaded to Cloud Storage - glossary_uri = "gs://cloud-samples-data/translation/bistro_glossary.csv" - - create_glossary(glossary_langs, PROJECT_ID, glossary_name, glossary_uri) - - # photo -> detected text - text_to_translate = pic_to_text(infile) - # detected text -> translated text - text_to_speak = translate_text( - text_to_translate, "fr", "en", PROJECT_ID, glossary_name - ) - # translated text -> synthetic audio - text_to_speech(text_to_speak, outfile) - # [END translate_hybrid_integration] - - -if __name__ == "__main__": - main() diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py deleted file mode 100644 index 8f81f325bade..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py +++ /dev/null @@ -1,108 +0,0 @@ -# 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 -# -# 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 sys -import uuid - -from hybrid_tutorial import create_glossary -from hybrid_tutorial import pic_to_text -from hybrid_tutorial import text_to_speech -from hybrid_tutorial import translate_text - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -# VISION TESTS - - -def test_vision_standard_format(): - # Generate text using Vision API - text = pic_to_text('resources/standard_format.jpeg') - - assert len(text) > 0 - - -# TRANSLATE TESTS - - -def test_create_and_delete_glossary(): - sys.path.insert(1, "../") - from beta_snippets import delete_glossary - - languages = ["fr", "en"] - glossary_name = f"test-glossary-{uuid.uuid4()}" - glossary_uri = "gs://cloud-samples-data/translation/bistro_glossary.csv" - - # create_glossary will raise an exception if creation fails - create_glossary(languages, PROJECT_ID, glossary_name, glossary_uri) - - # Delete glossary so that future tests will pass - # delete_glossary will raise an exception if deletion fails - delete_glossary(PROJECT_ID, glossary_name) - - -def test_translate_standard(): - - expected_text = "Hello" - - # attempt to create glossary, fails if it already exists - languages = ["fr", "en"] - glossary_name = "bistro-glossary" - glossary_uri = f"gs://cloud-samples-data/translation/{glossary_name}.csv" - create_glossary(languages, PROJECT_ID, glossary_name, glossary_uri) - - text = translate_text("Bonjour", "fr", "en", PROJECT_ID, "bistro-glossary") - - assert text == expected_text - - -def test_translate_glossary(): - - expected_text = "I eat goat cheese" - input_text = "Je mange du chevre" - - # attempt to create glossary, fails if it already exists - languages = ["fr", "en"] - glossary_name = "bistro-glossary" - glossary_uri = f"gs://cloud-samples-data/translation/{glossary_name}.csv" - create_glossary(languages, PROJECT_ID, glossary_name, glossary_uri) - - text = translate_text(input_text, "fr", "en", PROJECT_ID, "bistro-glossary") - - assert text == expected_text - - -# TEXT-TO-SPEECH TESTS - - -def test_tts_standard(capsys): - outfile = "resources/test_standard_text.mp3" - textfile = "resources/standard_format.txt" - - with open(textfile, "r") as f: - text = f.read() - - text_to_speech(text, outfile) - - # Assert audio file generated - assert os.path.isfile(outfile) - out, err = capsys.readouterr() - - # Assert success message printed - assert "Audio content written to file " + outfile in out - - # Delete test file - os.remove(outfile) diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/noxfile.py b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/noxfile.py deleted file mode 100644 index f5c32b22789b..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/noxfile.py +++ /dev/null @@ -1,292 +0,0 @@ -# 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 -# -# 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. - -from __future__ import print_function - -import glob -import os -from pathlib import Path -import sys -from typing import Callable, Dict, Optional - -import nox - - -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING -# DO NOT EDIT THIS FILE EVER! -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING - -BLACK_VERSION = "black==22.3.0" -ISORT_VERSION = "isort==5.10.1" - -# Copy `noxfile_config.py` to your directory and modify it instead. - -# `TEST_CONFIG` dict is a configuration hook that allows users to -# modify the test configurations. The values here should be in sync -# with `noxfile_config.py`. Users will copy `noxfile_config.py` into -# their directory and modify it. - -TEST_CONFIG = { - # You can opt out from the test for specific Python versions. - "ignored_versions": [], - # Old samples are opted out of enforcing Python type hints - # All new samples should feature them - "enforce_type_hints": False, - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - # If you need to use a specific version of pip, - # change pip_version_override to the string representation - # of the version number, for example, "20.2.4" - "pip_version_override": None, - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - "envs": {}, -} - - -try: - # Ensure we can import noxfile_config in the project's directory. - sys.path.append(".") - from noxfile_config import TEST_CONFIG_OVERRIDE -except ImportError as e: - print("No user noxfile_config found: detail: {}".format(e)) - TEST_CONFIG_OVERRIDE = {} - -# Update the TEST_CONFIG with the user supplied values. -TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) - - -def get_pytest_env_vars() -> Dict[str, str]: - """Returns a dict for pytest invocation.""" - ret = {} - - # Override the GCLOUD_PROJECT and the alias. - env_key = TEST_CONFIG["gcloud_project_env"] - # This should error out if not set. - ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] - - # Apply user supplied envs. - ret.update(TEST_CONFIG["envs"]) - return ret - - -# DO NOT EDIT - automatically generated. -# All versions used to test samples. -ALL_VERSIONS = ["3.7", "3.8", "3.9", "3.10"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( - "True", - "true", -) - -# Error if a python version is missing -nox.options.error_on_missing_interpreters = True - -# -# Style Checks -# - - -# Linting with flake8. -# -# We ignore the following rules: -# E203: whitespace before ‘:’ -# E266: too many leading ‘#’ for block comment -# E501: line too long -# I202: Additional newline in a section of imports -# -# We also need to specify the rules which are ignored by default: -# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session: nox.sessions.Session) -> None: - if not TEST_CONFIG["enforce_type_hints"]: - session.install("flake8") - else: - session.install("flake8", "flake8-annotations") - - args = FLAKE8_COMMON_ARGS + [ - ".", - ] - session.run("flake8", *args) - - -# -# Black -# - - -@nox.session -def blacken(session: nox.sessions.Session) -> None: - """Run black. Format code to uniform standard.""" - session.install(BLACK_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - session.run("black", *python_files) - - -# -# format = isort + black -# - -@nox.session -def format(session: nox.sessions.Session) -> None: - """ - Run isort to sort imports. Then run black - to format code to uniform standard. - """ - session.install(BLACK_VERSION, ISORT_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - # Use the --fss option to sort imports using strict alphabetical order. - # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections - session.run("isort", "--fss", *python_files) - session.run("black", *python_files) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests( - session: nox.sessions.Session, post_install: Callable = None -) -> None: - # check for presence of tests - test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob("**/test_*.py", recursive=True) - test_list.extend(glob.glob("**/tests", recursive=True)) - - if len(test_list) == 0: - print("No tests found, skipping directory.") - return - - if TEST_CONFIG["pip_version_override"]: - pip_version = TEST_CONFIG["pip_version_override"] - session.install(f"pip=={pip_version}") - """Runs py.test for a particular project.""" - concurrent_args = [] - if os.path.exists("requirements.txt"): - if os.path.exists("constraints.txt"): - session.install("-r", "requirements.txt", "-c", "constraints.txt") - else: - session.install("-r", "requirements.txt") - with open("requirements.txt") as rfile: - packages = rfile.read() - - if os.path.exists("requirements-test.txt"): - if os.path.exists("constraints-test.txt"): - session.install( - "-r", "requirements-test.txt", "-c", "constraints-test.txt" - ) - else: - session.install("-r", "requirements-test.txt") - with open("requirements-test.txt") as rtfile: - packages += rtfile.read() - - if INSTALL_LIBRARY_FROM_SOURCE: - session.install("-e", _get_repo_root()) - - if post_install: - post_install(session) - - if "pytest-parallel" in packages: - concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto']) - elif "pytest-xdist" in packages: - concurrent_args.extend(['-n', 'auto']) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args), - # Pytest will return 5 when no tests are collected. This can happen - # on travis where slow and flaky tests are excluded. - # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html - success_codes=[0, 5], - env=get_pytest_env_vars(), - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session: nox.sessions.Session) -> None: - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - session.skip( - "SKIPPED: {} tests are disabled for this sample.".format(session.python) - ) - - -# -# Readmegen -# - - -def _get_repo_root() -> Optional[str]: - """ Returns the root folder of the project. """ - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - # .git is not available in repos cloned via Cloud Build - # setup.py is always in the library's root, so use that instead - # https://github.com/googleapis/synthtool/issues/792 - if Path(p / "setup.py").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session: nox.sessions.Session, path: str) -> None: - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - ) diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/requirements-test.txt b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/requirements-test.txt deleted file mode 100644 index 49780e035690..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/requirements-test.txt +++ /dev/null @@ -1 +0,0 @@ -pytest==7.2.0 diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/requirements.txt b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/requirements.txt deleted file mode 100644 index fee36e967edf..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -google-cloud-translate==3.8.4 -google-cloud-vision==3.1.4 -google-cloud-texttospeech==2.12.3 diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/bistro_glossary.csv b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/bistro_glossary.csv deleted file mode 100644 index 99b451445529..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/bistro_glossary.csv +++ /dev/null @@ -1,13 +0,0 @@ -fr,en, -chevre,goat cheese, -Chevre,Goat cheese, -chèvre,goat cheese, -Chèvre,Goat cheese, -crème brulée,crème brulée, -Crème brulée,Crème brulée, -Crème Brulée,Crème Brulée, -bouillabaisse,fish stew, -Bouillabaisse,Fish stew, -steak frites,steak with french fries, -Steak frites,Steak with french fries, -Steak Frites,Steak with French Fries, diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/example.mp3 b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/example.mp3 deleted file mode 100644 index 5b5f02512dcb..000000000000 Binary files a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/example.mp3 and /dev/null differ diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/example.png b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/example.png deleted file mode 100644 index a3ac25ab053b..000000000000 Binary files a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/example.png and /dev/null differ diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/expected_standard_text.mp3 b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/expected_standard_text.mp3 deleted file mode 100644 index c731fbd787d3..000000000000 Binary files a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/expected_standard_text.mp3 and /dev/null differ diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/standard_format.jpeg b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/standard_format.jpeg deleted file mode 100644 index f985a540dad9..000000000000 Binary files a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/standard_format.jpeg and /dev/null differ diff --git a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/standard_format.txt b/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/standard_format.txt deleted file mode 100644 index d91d49e8680b..000000000000 --- a/packages/google-cloud-translate/samples/snippets/hybrid_glossaries/resources/standard_format.txt +++ /dev/null @@ -1,2 +0,0 @@ -This is -a test! diff --git a/packages/google-cloud-translate/samples/snippets/noxfile.py b/packages/google-cloud-translate/samples/snippets/noxfile.py deleted file mode 100644 index f5c32b22789b..000000000000 --- a/packages/google-cloud-translate/samples/snippets/noxfile.py +++ /dev/null @@ -1,292 +0,0 @@ -# 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 -# -# 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. - -from __future__ import print_function - -import glob -import os -from pathlib import Path -import sys -from typing import Callable, Dict, Optional - -import nox - - -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING -# DO NOT EDIT THIS FILE EVER! -# WARNING - WARNING - WARNING - WARNING - WARNING -# WARNING - WARNING - WARNING - WARNING - WARNING - -BLACK_VERSION = "black==22.3.0" -ISORT_VERSION = "isort==5.10.1" - -# Copy `noxfile_config.py` to your directory and modify it instead. - -# `TEST_CONFIG` dict is a configuration hook that allows users to -# modify the test configurations. The values here should be in sync -# with `noxfile_config.py`. Users will copy `noxfile_config.py` into -# their directory and modify it. - -TEST_CONFIG = { - # You can opt out from the test for specific Python versions. - "ignored_versions": [], - # Old samples are opted out of enforcing Python type hints - # All new samples should feature them - "enforce_type_hints": False, - # An envvar key for determining the project id to use. Change it - # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a - # build specific Cloud project. You can also use your own string - # to use your own Cloud project. - "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", - # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', - # If you need to use a specific version of pip, - # change pip_version_override to the string representation - # of the version number, for example, "20.2.4" - "pip_version_override": None, - # A dictionary you want to inject into your test. Don't put any - # secrets here. These values will override predefined values. - "envs": {}, -} - - -try: - # Ensure we can import noxfile_config in the project's directory. - sys.path.append(".") - from noxfile_config import TEST_CONFIG_OVERRIDE -except ImportError as e: - print("No user noxfile_config found: detail: {}".format(e)) - TEST_CONFIG_OVERRIDE = {} - -# Update the TEST_CONFIG with the user supplied values. -TEST_CONFIG.update(TEST_CONFIG_OVERRIDE) - - -def get_pytest_env_vars() -> Dict[str, str]: - """Returns a dict for pytest invocation.""" - ret = {} - - # Override the GCLOUD_PROJECT and the alias. - env_key = TEST_CONFIG["gcloud_project_env"] - # This should error out if not set. - ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key] - - # Apply user supplied envs. - ret.update(TEST_CONFIG["envs"]) - return ret - - -# DO NOT EDIT - automatically generated. -# All versions used to test samples. -ALL_VERSIONS = ["3.7", "3.8", "3.9", "3.10"] - -# Any default versions that should be ignored. -IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"] - -TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS]) - -INSTALL_LIBRARY_FROM_SOURCE = os.environ.get("INSTALL_LIBRARY_FROM_SOURCE", False) in ( - "True", - "true", -) - -# Error if a python version is missing -nox.options.error_on_missing_interpreters = True - -# -# Style Checks -# - - -# Linting with flake8. -# -# We ignore the following rules: -# E203: whitespace before ‘:’ -# E266: too many leading ‘#’ for block comment -# E501: line too long -# I202: Additional newline in a section of imports -# -# We also need to specify the rules which are ignored by default: -# ['E226', 'W504', 'E126', 'E123', 'W503', 'E24', 'E704', 'E121'] -FLAKE8_COMMON_ARGS = [ - "--show-source", - "--builtin=gettext", - "--max-complexity=20", - "--exclude=.nox,.cache,env,lib,generated_pb2,*_pb2.py,*_pb2_grpc.py", - "--ignore=E121,E123,E126,E203,E226,E24,E266,E501,E704,W503,W504,I202", - "--max-line-length=88", -] - - -@nox.session -def lint(session: nox.sessions.Session) -> None: - if not TEST_CONFIG["enforce_type_hints"]: - session.install("flake8") - else: - session.install("flake8", "flake8-annotations") - - args = FLAKE8_COMMON_ARGS + [ - ".", - ] - session.run("flake8", *args) - - -# -# Black -# - - -@nox.session -def blacken(session: nox.sessions.Session) -> None: - """Run black. Format code to uniform standard.""" - session.install(BLACK_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - session.run("black", *python_files) - - -# -# format = isort + black -# - -@nox.session -def format(session: nox.sessions.Session) -> None: - """ - Run isort to sort imports. Then run black - to format code to uniform standard. - """ - session.install(BLACK_VERSION, ISORT_VERSION) - python_files = [path for path in os.listdir(".") if path.endswith(".py")] - - # Use the --fss option to sort imports using strict alphabetical order. - # See https://pycqa.github.io/isort/docs/configuration/options.html#force-sort-within-sections - session.run("isort", "--fss", *python_files) - session.run("black", *python_files) - - -# -# Sample Tests -# - - -PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"] - - -def _session_tests( - session: nox.sessions.Session, post_install: Callable = None -) -> None: - # check for presence of tests - test_list = glob.glob("**/*_test.py", recursive=True) + glob.glob("**/test_*.py", recursive=True) - test_list.extend(glob.glob("**/tests", recursive=True)) - - if len(test_list) == 0: - print("No tests found, skipping directory.") - return - - if TEST_CONFIG["pip_version_override"]: - pip_version = TEST_CONFIG["pip_version_override"] - session.install(f"pip=={pip_version}") - """Runs py.test for a particular project.""" - concurrent_args = [] - if os.path.exists("requirements.txt"): - if os.path.exists("constraints.txt"): - session.install("-r", "requirements.txt", "-c", "constraints.txt") - else: - session.install("-r", "requirements.txt") - with open("requirements.txt") as rfile: - packages = rfile.read() - - if os.path.exists("requirements-test.txt"): - if os.path.exists("constraints-test.txt"): - session.install( - "-r", "requirements-test.txt", "-c", "constraints-test.txt" - ) - else: - session.install("-r", "requirements-test.txt") - with open("requirements-test.txt") as rtfile: - packages += rtfile.read() - - if INSTALL_LIBRARY_FROM_SOURCE: - session.install("-e", _get_repo_root()) - - if post_install: - post_install(session) - - if "pytest-parallel" in packages: - concurrent_args.extend(['--workers', 'auto', '--tests-per-worker', 'auto']) - elif "pytest-xdist" in packages: - concurrent_args.extend(['-n', 'auto']) - - session.run( - "pytest", - *(PYTEST_COMMON_ARGS + session.posargs + concurrent_args), - # Pytest will return 5 when no tests are collected. This can happen - # on travis where slow and flaky tests are excluded. - # See http://doc.pytest.org/en/latest/_modules/_pytest/main.html - success_codes=[0, 5], - env=get_pytest_env_vars(), - ) - - -@nox.session(python=ALL_VERSIONS) -def py(session: nox.sessions.Session) -> None: - """Runs py.test for a sample using the specified version of Python.""" - if session.python in TESTED_VERSIONS: - _session_tests(session) - else: - session.skip( - "SKIPPED: {} tests are disabled for this sample.".format(session.python) - ) - - -# -# Readmegen -# - - -def _get_repo_root() -> Optional[str]: - """ Returns the root folder of the project. """ - # Get root of this repository. Assume we don't have directories nested deeper than 10 items. - p = Path(os.getcwd()) - for i in range(10): - if p is None: - break - if Path(p / ".git").exists(): - return str(p) - # .git is not available in repos cloned via Cloud Build - # setup.py is always in the library's root, so use that instead - # https://github.com/googleapis/synthtool/issues/792 - if Path(p / "setup.py").exists(): - return str(p) - p = p.parent - raise Exception("Unable to detect repository root.") - - -GENERATED_READMES = sorted([x for x in Path(".").rglob("*.rst.in")]) - - -@nox.session -@nox.parametrize("path", GENERATED_READMES) -def readmegen(session: nox.sessions.Session, path: str) -> None: - """(Re-)generates the readme for a sample.""" - session.install("jinja2", "pyyaml") - dir_ = os.path.dirname(path) - - if os.path.exists(os.path.join(dir_, "requirements.txt")): - session.install("-r", os.path.join(dir_, "requirements.txt")) - - in_file = os.path.join(dir_, "README.rst.in") - session.run( - "python", _get_repo_root() + "/scripts/readme-gen/readme_gen.py", in_file - ) diff --git a/packages/google-cloud-translate/samples/snippets/pytest.ini b/packages/google-cloud-translate/samples/snippets/pytest.ini deleted file mode 100644 index f139880676b3..000000000000 --- a/packages/google-cloud-translate/samples/snippets/pytest.ini +++ /dev/null @@ -1,3 +0,0 @@ -[pytest] -# don't recurse into hybrid_glossaries directory -norecursedirs = * \ No newline at end of file diff --git a/packages/google-cloud-translate/samples/snippets/quickstart.py b/packages/google-cloud-translate/samples/snippets/quickstart.py deleted file mode 100644 index 9fc4027a8b80..000000000000 --- a/packages/google-cloud-translate/samples/snippets/quickstart.py +++ /dev/null @@ -1,40 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2016 Google Inc. All Rights Reserved. -# -# 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 run_quickstart(): - # [START translate_quickstart] - # Imports the Google Cloud client library - from google.cloud import translate_v2 as translate - - # Instantiates a client - translate_client = translate.Client() - - # The text to translate - text = u"Hello, world!" - # The target language - target = "ru" - - # Translates some text into Russian - translation = translate_client.translate(text, target_language=target) - - print(u"Text: {}".format(text)) - print(u"Translation: {}".format(translation["translatedText"])) - # [END translate_quickstart] - - -if __name__ == "__main__": - run_quickstart() diff --git a/packages/google-cloud-translate/samples/snippets/quickstart_test.py b/packages/google-cloud-translate/samples/snippets/quickstart_test.py deleted file mode 100644 index e4dc4886bc33..000000000000 --- a/packages/google-cloud-translate/samples/snippets/quickstart_test.py +++ /dev/null @@ -1,23 +0,0 @@ -# -*- coding: utf-8 -*- -# Copyright 2016 Google Inc. All Rights Reserved. -# -# 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 quickstart - - -def test_quickstart(capsys): - quickstart.run_quickstart() - out, _ = capsys.readouterr() - assert u"Translation" in out diff --git a/packages/google-cloud-translate/samples/snippets/requirements-test.txt b/packages/google-cloud-translate/samples/snippets/requirements-test.txt deleted file mode 100644 index 6c0e08f306e8..000000000000 --- a/packages/google-cloud-translate/samples/snippets/requirements-test.txt +++ /dev/null @@ -1,3 +0,0 @@ -backoff==2.2.1 -flaky==3.7.0 -pytest==7.2.0 diff --git a/packages/google-cloud-translate/samples/snippets/requirements.txt b/packages/google-cloud-translate/samples/snippets/requirements.txt deleted file mode 100644 index 5d27a85755d0..000000000000 --- a/packages/google-cloud-translate/samples/snippets/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -google-cloud-translate==3.8.4 -google-cloud-storage==2.6.0 -google-cloud-automl==2.8.3 diff --git a/packages/google-cloud-translate/samples/snippets/resources/fake_invoice.pdf b/packages/google-cloud-translate/samples/snippets/resources/fake_invoice.pdf deleted file mode 100644 index b9107fba129d..000000000000 Binary files a/packages/google-cloud-translate/samples/snippets/resources/fake_invoice.pdf and /dev/null differ diff --git a/packages/google-cloud-translate/samples/snippets/snippets.py b/packages/google-cloud-translate/samples/snippets/snippets.py deleted file mode 100644 index e0be2e1b0659..000000000000 --- a/packages/google-cloud-translate/samples/snippets/snippets.py +++ /dev/null @@ -1,168 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2016 Google, Inc. -# -# 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 with the -Google Cloud Translate API - -For more information, the documentation at -https://cloud.google.com/translate/docs. -""" - -import argparse - - -# [START translate_detect_language] -def detect_language(text): - """Detects the text's language.""" - from google.cloud import translate_v2 as translate - - translate_client = translate.Client() - - # Text can also be a sequence of strings, in which case this method - # will return a sequence of results for each text. - result = translate_client.detect_language(text) - - print("Text: {}".format(text)) - print("Confidence: {}".format(result["confidence"])) - print("Language: {}".format(result["language"])) - - -# [END translate_detect_language] - -# [START translate_list_codes] -def list_languages(): - """Lists all available languages.""" - from google.cloud import translate_v2 as translate - - translate_client = translate.Client() - - results = translate_client.get_languages() - - for language in results: - print(u"{name} ({language})".format(**language)) - - -# [END translate_list_codes] - -# [START translate_list_language_names] -def list_languages_with_target(target): - """Lists all available languages and localizes them to the target language. - - Target must be an ISO 639-1 language code. - See https://g.co/cloud/translate/v2/translate-reference#supported_languages - """ - from google.cloud import translate_v2 as translate - - translate_client = translate.Client() - - results = translate_client.get_languages(target_language=target) - - for language in results: - print(u"{name} ({language})".format(**language)) - - -# [END translate_list_language_names] - -# [START translate_text_with_model] -def translate_text_with_model(target, text, model="nmt"): - """Translates text into the target language. - - Make sure your project is allowlisted. - - Target must be an ISO 639-1 language code. - See https://g.co/cloud/translate/v2/translate-reference#supported_languages - """ - from google.cloud import translate_v2 as translate - - translate_client = translate.Client() - - if isinstance(text, bytes): - text = text.decode("utf-8") - - # Text can also be a sequence of strings, in which case this method - # will return a sequence of results for each text. - result = translate_client.translate(text, target_language=target, model=model) - - print(u"Text: {}".format(result["input"])) - print(u"Translation: {}".format(result["translatedText"])) - print(u"Detected source language: {}".format(result["detectedSourceLanguage"])) - - -# [END translate_text_with_model] - -# [START translate_translate_text] -def translate_text(target, text): - """Translates text into the target language. - - Target must be an ISO 639-1 language code. - See https://g.co/cloud/translate/v2/translate-reference#supported_languages - """ - import six - from google.cloud import translate_v2 as translate - - translate_client = translate.Client() - - if isinstance(text, six.binary_type): - text = text.decode("utf-8") - - # Text can also be a sequence of strings, in which case this method - # will return a sequence of results for each text. - result = translate_client.translate(text, target_language=target) - - print(u"Text: {}".format(result["input"])) - print(u"Translation: {}".format(result["translatedText"])) - print(u"Detected source language: {}".format(result["detectedSourceLanguage"])) - - -# [END translate_translate_text] - - -if __name__ == "__main__": - parser = argparse.ArgumentParser( - description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter - ) - subparsers = parser.add_subparsers(dest="command") - - detect_langage_parser = subparsers.add_parser( - "detect-language", help=detect_language.__doc__ - ) - detect_langage_parser.add_argument("text") - - list_languages_parser = subparsers.add_parser( - "list-languages", help=list_languages.__doc__ - ) - - list_languages_with_target_parser = subparsers.add_parser( - "list-languages-with-target", help=list_languages_with_target.__doc__ - ) - list_languages_with_target_parser.add_argument("target") - - translate_text_parser = subparsers.add_parser( - "translate-text", help=translate_text.__doc__ - ) - translate_text_parser.add_argument("target") - translate_text_parser.add_argument("text") - - args = parser.parse_args() - - if args.command == "detect-language": - detect_language(args.text) - elif args.command == "list-languages": - list_languages() - elif args.command == "list-languages-with-target": - list_languages_with_target(args.target) - elif args.command == "translate-text": - translate_text(args.target, args.text) diff --git a/packages/google-cloud-translate/samples/snippets/snippets_test.py b/packages/google-cloud-translate/samples/snippets/snippets_test.py deleted file mode 100644 index b5fe362cafc8..000000000000 --- a/packages/google-cloud-translate/samples/snippets/snippets_test.py +++ /dev/null @@ -1,49 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright 2016 Google, Inc. -# -# 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 snippets - - -def test_detect_language(capsys): - snippets.detect_language("Hæ sæta") - out, _ = capsys.readouterr() - assert "is" in out - - -def test_list_languages(capsys): - snippets.list_languages() - out, _ = capsys.readouterr() - assert "Icelandic (is)" in out - - -def test_list_languages_with_target(capsys): - snippets.list_languages_with_target("is") - out, _ = capsys.readouterr() - assert u"íslenska (is)" in out - - -def test_translate_text(capsys): - snippets.translate_text("is", "Hello world") - out, _ = capsys.readouterr() - assert u"Halló heimur" in out - - -def test_translate_utf8(capsys): - text = u"파인애플 13개" - snippets.translate_text("en", text) - out, _ = capsys.readouterr() - assert u"13 pineapples" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text.py deleted file mode 100644 index 514eae2c59db..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text.py +++ /dev/null @@ -1,59 +0,0 @@ -# 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. - -# [START translate_v3_batch_translate_text] -from google.cloud import translate - - -def batch_translate_text( - 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", - timeout=180, -): - """Translates a batch of texts on GCS and stores the result in a GCS location.""" - - client = translate.TranslationServiceClient() - - location = "us-central1" - # Supported file types: https://cloud.google.com/translate/docs/supported-formats - gcs_source = {"input_uri": input_uri} - - input_configs_element = { - "gcs_source": gcs_source, - "mime_type": "text/plain", # Can be "text/plain" or "text/html". - } - gcs_destination = {"output_uri_prefix": output_uri} - output_config = {"gcs_destination": gcs_destination} - parent = f"projects/{project_id}/locations/{location}" - - # Supported language codes: https://cloud.google.com/translate/docs/languages - operation = client.batch_translate_text( - request={ - "parent": parent, - "source_language_code": "en", - "target_language_codes": ["ja"], # Up to 10 language codes here. - "input_configs": [input_configs_element], - "output_config": output_config, - } - ) - - print("Waiting for operation to complete...") - response = operation.result(timeout) - - print("Total Characters: {}".format(response.total_characters)) - print("Translated Characters: {}".format(response.translated_characters)) - - -# [END translate_v3_batch_translate_text] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_test.py deleted file mode 100644 index ad637cd8938e..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_test.py +++ /dev/null @@ -1,48 +0,0 @@ -# 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 -import uuid - -from google.cloud import storage -import pytest - -import translate_v3_batch_translate_text - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -@pytest.fixture(scope="function") -def bucket(): - """Create a temporary bucket to store annotation output.""" - bucket_name = "test-{}".format(uuid.uuid4()) - storage_client = storage.Client() - bucket = storage_client.create_bucket(bucket_name) - - yield bucket - - bucket.delete(force=True) - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_batch_translate_text(capsys, bucket): - translate_v3_batch_translate_text.batch_translate_text( - "gs://cloud-samples-data/translation/text.txt", - "gs://{}/translation/BATCH_TRANSLATION_OUTPUT/".format(bucket.name), - PROJECT_ID, - timeout=320, - ) - out, _ = capsys.readouterr() - assert "Total Characters" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary.py deleted file mode 100644 index 574b001c6651..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary.py +++ /dev/null @@ -1,75 +0,0 @@ -# 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. - - -# [START translate_v3_batch_translate_text_with_glossary] -from google.cloud import translate - - -def batch_translate_text_with_glossary( - 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", - glossary_id="YOUR_GLOSSARY_ID", - timeout=320, -): - """Translates a batch of texts on GCS and stores the result in a GCS location. - Glossary is applied for translation.""" - - client = translate.TranslationServiceClient() - - # Supported language codes: https://cloud.google.com/translate/docs/languages - location = "us-central1" - - # Supported file types: https://cloud.google.com/translate/docs/supported-formats - gcs_source = {"input_uri": input_uri} - - input_configs_element = { - "gcs_source": gcs_source, - "mime_type": "text/plain", # Can be "text/plain" or "text/html". - } - gcs_destination = {"output_uri_prefix": output_uri} - output_config = {"gcs_destination": gcs_destination} - - parent = f"projects/{project_id}/locations/{location}" - - # glossary is a custom dictionary Translation API uses - # to translate the domain-specific terminology. - glossary_path = client.glossary_path( - project_id, "us-central1", glossary_id # The location of the glossary - ) - - glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary_path) - - glossaries = {"ja": glossary_config} # target lang as key - - operation = client.batch_translate_text( - request={ - "parent": parent, - "source_language_code": "en", - "target_language_codes": ["ja"], # Up to 10 language codes here. - "input_configs": [input_configs_element], - "glossaries": glossaries, - "output_config": output_config, - } - ) - - print("Waiting for operation to complete...") - response = operation.result(timeout) - - print("Total Characters: {}".format(response.total_characters)) - print("Translated Characters: {}".format(response.translated_characters)) - - -# [END translate_v3_batch_translate_text_with_glossary] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py deleted file mode 100644 index 3110dcee53cb..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py +++ /dev/null @@ -1,77 +0,0 @@ -# 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 -# -# 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. - -# [START translate_v3_batch_translate_text_with_glossary_and_model] -from google.cloud import translate - - -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 - """ - - client = translate.TranslationServiceClient() - - # 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". - mime_type = "text/plain" - input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type} - input_configs = [input_configs_element] - gcs_destination = {"output_uri_prefix": output_uri} - output_config = {"gcs_destination": gcs_destination} - parent = f"projects/{project_id}/locations/{location}" - model_path = "projects/{}/locations/{}/models/{}".format( - project_id, "us-central1", model_id - ) - models = {"ja": model_path} - - glossary_path = client.glossary_path( - project_id, "us-central1", glossary_id # The location of the glossary - ) - - glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary_path) - glossaries = {"ja": glossary_config} # target lang as key - - operation = client.batch_translate_text( - request={ - "parent": parent, - "source_language_code": "en", - "target_language_codes": target_language_codes, - "input_configs": input_configs, - "output_config": output_config, - "models": models, - "glossaries": glossaries, - } - ) - - print("Waiting for operation to complete...") - response = operation.result() - - # Display the translation for each input text provided - print("Total Characters: {}".format(response.total_characters)) - print("Translated Characters: {}".format(response.translated_characters)) - - -# [END translate_v3_batch_translate_text_with_glossary_and_model] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py deleted file mode 100644 index 9c2e5610166c..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py +++ /dev/null @@ -1,50 +0,0 @@ -# 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 -# -# 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 uuid - -from google.cloud import storage -import pytest - -import translate_v3_batch_translate_text_with_glossary_and_model - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" -MODEL_ID = "TRL251293382528204800" - - -@pytest.fixture(scope="function") -def bucket(): - """Create a temporary bucket to store annotation output.""" - bucket_name = "test-bucket-for-glossary-" + str(uuid.uuid1()) - storage_client = storage.Client() - bucket = storage_client.create_bucket(bucket_name) - - yield bucket - - bucket.delete(force=True) - - -def test_batch_translate_text_with_glossary_and_model(capsys, bucket): - 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_GLOS_MODEL_OUTPUT/".format(bucket.name), - PROJECT_ID, - MODEL_ID, - GLOSSARY_ID, - ) - - out, _ = capsys.readouterr() - assert "Total Characters: 25" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py deleted file mode 100644 index adfbd7e26b95..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py +++ /dev/null @@ -1,72 +0,0 @@ -# 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 -import uuid - -import backoff -from google.cloud import storage -import pytest - -import translate_v3_batch_translate_text_with_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" - - -def get_ephemeral_bucket(): - """Create a temporary bucket to store annotation output.""" - bucket_name = f"tmp-{uuid.uuid4().hex}" - storage_client = storage.Client() - bucket = storage_client.create_bucket(bucket_name) - - yield bucket - - bucket.delete(force=True) - - -@pytest.fixture(scope="function") -def bucket(): - """Create a bucket feature for testing""" - return next(get_ephemeral_bucket()) - - -def on_backoff(invocation_dict): - """Backoff callback; create a testing bucket for each backoff run""" - invocation_dict["kwargs"]["bucket"] = next(get_ephemeral_bucket()) - - -# If necessary, retry test function while backing off the timeout sequentially -MAX_TIMEOUT = 500 - - -@backoff.on_exception( - wait_gen=lambda: (wait_time for wait_time in [100, 250, 300, MAX_TIMEOUT]), - exception=Exception, - max_tries=5, - on_backoff=on_backoff, -) -def test_batch_translate_text_with_glossary(capsys, bucket): - - translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary( - "gs://cloud-samples-data/translation/text_with_glossary.txt", - "gs://{}/translation/BATCH_TRANSLATION_GLOS_OUTPUT/".format(bucket.name), - PROJECT_ID, - GLOSSARY_ID, - MAX_TIMEOUT, - ) - - out, _ = capsys.readouterr() - assert "Total Characters: 9" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_model.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_model.py deleted file mode 100644 index a5b94866623f..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_model.py +++ /dev/null @@ -1,69 +0,0 @@ -# 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. - - -# [START translate_v3_batch_translate_text_with_model] -from google.cloud import translate - - -def batch_translate_text_with_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", -): - """Batch translate text using Translation model. - Model can be AutoML or General[built-in] model.""" - - client = translate.TranslationServiceClient() - - # Supported file types: https://cloud.google.com/translate/docs/supported-formats - gcs_source = {"input_uri": input_uri} - location = "us-central1" - - input_configs_element = { - "gcs_source": gcs_source, - "mime_type": "text/plain", # Can be "text/plain" or "text/html". - } - gcs_destination = {"output_uri_prefix": output_uri} - output_config = {"gcs_destination": gcs_destination} - parent = f"projects/{project_id}/locations/{location}" - - model_path = "projects/{}/locations/{}/models/{}".format( - project_id, location, model_id # The location of AutoML model. - ) - - # Supported language codes: https://cloud.google.com/translate/docs/languages - models = {"ja": model_path} # takes a target lang as key. - - operation = client.batch_translate_text( - request={ - "parent": parent, - "source_language_code": "en", - "target_language_codes": ["ja"], # Up to 10 language codes here. - "input_configs": [input_configs_element], - "output_config": output_config, - "models": models, - } - ) - - print("Waiting for operation to complete...") - response = operation.result() - - # Display the translation for each input text provided. - print("Total Characters: {}".format(response.total_characters)) - print("Translated Characters: {}".format(response.translated_characters)) - - -# [END translate_v3_batch_translate_text_with_model] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_model_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_model_test.py deleted file mode 100644 index 21ca6c33d7d2..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_batch_translate_text_with_model_test.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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 -import uuid - -from google.cloud import storage -import pytest - -import translate_v3_batch_translate_text_with_model - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -MODEL_ID = "TRL251293382528204800" - - -@pytest.fixture(scope="function") -def bucket(): - """Create a temporary bucket to store annotation output.""" - bucket_name = f"tmp-{uuid.uuid4().hex}" - storage_client = storage.Client() - bucket = storage_client.create_bucket(bucket_name) - - yield bucket - - bucket.delete(force=True) - - -def test_batch_translate_text_with_model(capsys, bucket): - translate_v3_batch_translate_text_with_model.batch_translate_text_with_model( - "gs://cloud-samples-data/translation/custom_model_text.txt", - "gs://{}/translation/BATCH_TRANSLATION_MODEL_OUTPUT/".format(bucket.name), - PROJECT_ID, - MODEL_ID, - ) - out, _ = capsys.readouterr() - assert "Total Characters: 15" in out - assert "Translated Characters: 15" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_create_glossary.py b/packages/google-cloud-translate/samples/snippets/translate_v3_create_glossary.py deleted file mode 100644 index b2fd38634cd2..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_create_glossary.py +++ /dev/null @@ -1,60 +0,0 @@ -# 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. - -# [START translate_v3_create_glossary] -from google.cloud import translate_v3 as translate - - -def create_glossary( - project_id="YOUR_PROJECT_ID", - input_uri="YOUR_INPUT_URI", - glossary_id="YOUR_GLOSSARY_ID", - timeout=180, -): - """ - Create a equivalent term sets glossary. Glossary can be words or - short phrases (usually fewer than five words). - https://cloud.google.com/translate/docs/advanced/glossary#format-glossary - """ - client = translate.TranslationServiceClient() - - # Supported language codes: https://cloud.google.com/translate/docs/languages - source_lang_code = "en" - target_lang_code = "ja" - location = "us-central1" # The location of the glossary - - name = client.glossary_path(project_id, location, glossary_id) - language_codes_set = translate.types.Glossary.LanguageCodesSet( - language_codes=[source_lang_code, target_lang_code] - ) - - gcs_source = translate.types.GcsSource(input_uri=input_uri) - - input_config = translate.types.GlossaryInputConfig(gcs_source=gcs_source) - - glossary = translate.types.Glossary( - name=name, language_codes_set=language_codes_set, input_config=input_config - ) - - parent = f"projects/{project_id}/locations/{location}" - # glossary is a custom dictionary Translation API uses - # to translate the domain-specific terminology. - operation = client.create_glossary(parent=parent, glossary=glossary) - - result = operation.result(timeout) - print("Created: {}".format(result.name)) - print("Input Uri: {}".format(result.input_config.gcs_source.input_uri)) - - -# [END translate_v3_create_glossary] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_create_glossary_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_create_glossary_test.py deleted file mode 100644 index 3e4b61cf89ea..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_create_glossary_test.py +++ /dev/null @@ -1,54 +0,0 @@ -# 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 -import uuid - -import backoff -from google.api_core.exceptions import DeadlineExceeded, GoogleAPICallError -from google.cloud.exceptions import NotFound -import pytest - -import translate_v3_create_glossary -import translate_v3_delete_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_create_glossary(capsys): - try: - glossary_id = "test-{}".format(uuid.uuid4()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - out, _ = capsys.readouterr() - # assert - assert "Created:" in out - assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out - finally: - # cleanup - @backoff.on_exception( - backoff.expo, (DeadlineExceeded, GoogleAPICallError), max_time=60 - ) - def delete_glossary(): - try: - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - except NotFound as e: - # Ignoring this case. - print("Got NotFound, detail: {}".format(str(e))) - - delete_glossary() diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_delete_glossary.py b/packages/google-cloud-translate/samples/snippets/translate_v3_delete_glossary.py deleted file mode 100644 index b5c55b2ba09b..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_delete_glossary.py +++ /dev/null @@ -1,34 +0,0 @@ -# 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. - -# [START translate_v3_delete_glossary] -from google.cloud import translate_v3 as translate - - -def delete_glossary( - project_id="YOUR_PROJECT_ID", - glossary_id="YOUR_GLOSSARY_ID", - timeout=180, -): - """Delete a specific glossary based on the glossary ID.""" - client = translate.TranslationServiceClient() - - name = client.glossary_path(project_id, "us-central1", glossary_id) - - operation = client.delete_glossary(name=name) - result = operation.result(timeout) - print("Deleted: {}".format(result.name)) - - -# [END translate_v3_delete_glossary] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_delete_glossary_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_delete_glossary_test.py deleted file mode 100644 index a88c16a37a2b..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_delete_glossary_test.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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 -import uuid - -import pytest - -import translate_v3_create_glossary -import translate_v3_delete_glossary - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_delete_glossary(capsys): - # setup - glossary_id = "test-{}".format(uuid.uuid4()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - # assert - translate_v3_delete_glossary.delete_glossary(PROJECT_ID, glossary_id) - out, _ = capsys.readouterr() - assert "Deleted:" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_detect_language.py b/packages/google-cloud-translate/samples/snippets/translate_v3_detect_language.py deleted file mode 100644 index eae985c3d631..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_detect_language.py +++ /dev/null @@ -1,45 +0,0 @@ -# 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 -# -# 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. - -# [START translate_v3_detect_language] -from google.cloud import translate - - -def detect_language(project_id="YOUR_PROJECT_ID"): - """Detecting the language of a text string.""" - - client = translate.TranslationServiceClient() - - location = "global" - - parent = f"projects/{project_id}/locations/{location}" - - # Detail on supported types can be found here: - # https://cloud.google.com/translate/docs/supported-formats - response = client.detect_language( - content="Hello, world!", - parent=parent, - mime_type="text/plain", # mime types: text/plain, text/html - ) - - # Display list of detected languages sorted by detection confidence. - # The most probable language is first. - for language in response.languages: - # The language detected - print("Language code: {}".format(language.language_code)) - # Confidence of detection result for this language - print("Confidence: {}".format(language.confidence)) - - -# [END translate_v3_detect_language] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_detect_language_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_detect_language_test.py deleted file mode 100644 index 7e9540cca009..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_detect_language_test.py +++ /dev/null @@ -1,26 +0,0 @@ -# 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 - -import translate_v3_detect_language - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -def test_detect_language(capsys): - translate_v3_detect_language.detect_language(PROJECT_ID) - out, _ = capsys.readouterr() - assert "en" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_get_glossary.py b/packages/google-cloud-translate/samples/snippets/translate_v3_get_glossary.py deleted file mode 100644 index f4c2aaae4602..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_get_glossary.py +++ /dev/null @@ -1,32 +0,0 @@ -# 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 -# -# 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. - -# [START translate_v3_get_glossary] -from google.cloud import translate_v3 as translate - - -def get_glossary(project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID"): - """Get a particular glossary based on the glossary ID.""" - - client = translate.TranslationServiceClient() - - name = client.glossary_path(project_id, "us-central1", glossary_id) - - response = client.get_glossary(name=name) - print(u"Glossary name: {}".format(response.name)) - print(u"Entry count: {}".format(response.entry_count)) - print(u"Input URI: {}".format(response.input_config.gcs_source.input_uri)) - - -# [END translate_v3_get_glossary] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_get_glossary_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_get_glossary_test.py deleted file mode 100644 index 9dfa5e81c479..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_get_glossary_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# 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 - -import translate_v3_get_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" - - -def test_get_glossary(capsys): - translate_v3_get_glossary.get_glossary(PROJECT_ID, GLOSSARY_ID) - out, _ = capsys.readouterr() - assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages.py b/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages.py deleted file mode 100644 index 3802a7e1422b..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages.py +++ /dev/null @@ -1,35 +0,0 @@ -# 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 -# -# 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. - -# [START translate_v3_get_supported_languages] -from google.cloud import translate - - -def get_supported_languages(project_id="YOUR_PROJECT_ID"): - """Getting a list of supported language codes.""" - - client = translate.TranslationServiceClient() - - parent = f"projects/{project_id}" - - # Supported language codes: https://cloud.google.com/translate/docs/languages - response = client.get_supported_languages(parent=parent) - - # List language codes of supported languages. - print("Supported Languages:") - for language in response.languages: - print("Language Code: {}".format(language.language_code)) - - -# [END translate_v3_get_supported_languages] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_test.py deleted file mode 100644 index 3a1a9be4cd77..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_test.py +++ /dev/null @@ -1,26 +0,0 @@ -# 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 - -import translate_v3_get_supported_languages - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -def test_list_languages(capsys): - translate_v3_get_supported_languages.get_supported_languages(PROJECT_ID) - out, _ = capsys.readouterr() - assert "zh" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_with_target.py b/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_with_target.py deleted file mode 100644 index 856b317846a9..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_with_target.py +++ /dev/null @@ -1,38 +0,0 @@ -# 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. - -# [START translate_v3_get_supported_languages_for_target] -from google.cloud import translate - - -def get_supported_languages_with_target(project_id="YOUR_PROJECT_ID"): - """Listing supported languages with target language name.""" - - client = translate.TranslationServiceClient() - - location = "global" - - parent = f"projects/{project_id}/locations/{location}" - - # Supported language codes: https://cloud.google.com/translate/docs/languages - response = client.get_supported_languages( - display_language_code="is", parent=parent # target language code - ) - # List language codes of supported languages - for language in response.languages: - print("Language Code: {}".format(language.language_code)) - print("Display Name: {}".format(language.display_name)) - - -# [END translate_v3_get_supported_languages_for_target] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_with_target_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_with_target_test.py deleted file mode 100644 index 9688efee3b9e..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_get_supported_languages_with_target_test.py +++ /dev/null @@ -1,27 +0,0 @@ -# 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 - -import translate_v3_get_supported_languages_with_target as get_supported_langs - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -def test_list_languages_with_target(capsys): - get_supported_langs.get_supported_languages_with_target(PROJECT_ID) - out, _ = capsys.readouterr() - assert u"Language Code: sq" in out - assert u"Display Name: albanska" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary.py b/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary.py deleted file mode 100644 index 934194b4443f..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary.py +++ /dev/null @@ -1,42 +0,0 @@ -# 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 -# -# 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. - -# [START translate_v3_list_glossary] -from google.cloud import translate - - -def list_glossaries(project_id="YOUR_PROJECT_ID"): - """List Glossaries.""" - - client = translate.TranslationServiceClient() - - location = "us-central1" - - parent = f"projects/{project_id}/locations/{location}" - - # Iterate over all results - for glossary in client.list_glossaries(parent=parent): - print("Name: {}".format(glossary.name)) - print("Entry count: {}".format(glossary.entry_count)) - print("Input uri: {}".format(glossary.input_config.gcs_source.input_uri)) - - # Note: You can create a glossary using one of two modes: - # language_code_set or language_pair. When listing the information for - # a glossary, you can only get information for the mode you used - # when creating the glossary. - for language_code in glossary.language_codes_set.language_codes: - print("Language code: {}".format(language_code)) - - -# [END translate_v3_list_glossary] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary_test.py deleted file mode 100644 index 9d87f0cda9b9..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_list_glossary_test.py +++ /dev/null @@ -1,28 +0,0 @@ -# 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 - -import translate_v3_list_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" - - -def test_list_glossary(capsys): - translate_v3_list_glossary.list_glossaries(PROJECT_ID) - out, _ = capsys.readouterr() - assert GLOSSARY_ID in out - assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text.py b/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text.py deleted file mode 100644 index 095fd1460484..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text.py +++ /dev/null @@ -1,54 +0,0 @@ -# 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 -# -# 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. - -# [START translate_v3_translate_text] -# [START translate_v3_translate_text_0] -# Imports the Google Cloud Translation library -from google.cloud import translate -# [END translate_v3_translate_text_0] - - -# [START translate_v3_translate_text_1] -# Initialize Translation client -def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): - """Translating Text.""" - - client = translate.TranslationServiceClient() - - location = "global" - - parent = f"projects/{project_id}/locations/{location}" - # [END translate_v3_translate_text_1] - - # [START translate_v3_translate_text_2] - # Translate text from English to French - # Detail on supported types can be found here: - # https://cloud.google.com/translate/docs/supported-formats - response = client.translate_text( - request={ - "parent": parent, - "contents": [text], - "mime_type": "text/plain", # mime types: text/plain, text/html - "source_language_code": "en-US", - "target_language_code": "fr", - } - ) - - # Display the translation for each input text provided - for translation in response.translations: - print("Translated text: {}".format(translation.translated_text)) - # [END translate_v3_translate_text_2] - - -# [END translate_v3_translate_text] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_test.py deleted file mode 100644 index fee6e7715664..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_test.py +++ /dev/null @@ -1,26 +0,0 @@ -# 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 - -import translate_v3_translate_text - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -def test_translate_text(capsys): - translate_v3_translate_text.translate_text("Hello World!", PROJECT_ID) - out, _ = capsys.readouterr() - assert "Bonjour le monde" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_glossary.py b/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_glossary.py deleted file mode 100644 index addd737f6ad5..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_glossary.py +++ /dev/null @@ -1,53 +0,0 @@ -# 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. - -# [START translate_v3_translate_text_with_glossary] - -from google.cloud import translate - - -def translate_text_with_glossary( - text="YOUR_TEXT_TO_TRANSLATE", - project_id="YOUR_PROJECT_ID", - glossary_id="YOUR_GLOSSARY_ID", -): - """Translates a given text using a glossary.""" - - client = translate.TranslationServiceClient() - location = "us-central1" - parent = f"projects/{project_id}/locations/{location}" - - glossary = client.glossary_path( - project_id, "us-central1", glossary_id # The location of the glossary - ) - - glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary) - - # Supported language codes: https://cloud.google.com/translate/docs/languages - response = client.translate_text( - request={ - "contents": [text], - "target_language_code": "ja", - "source_language_code": "en", - "parent": parent, - "glossary_config": glossary_config, - } - ) - - print("Translated text: \n") - for translation in response.glossary_translations: - print("\t {}".format(translation.translated_text)) - - -# [END translate_v3_translate_text_with_glossary] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_glossary_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_glossary_test.py deleted file mode 100644 index ef5a90823d0e..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_glossary_test.py +++ /dev/null @@ -1,30 +0,0 @@ -# -*- encoding: utf-8 -*- -# 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 - -import translate_v3_translate_text_with_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" - - -def test_translate_text_with_glossary(capsys): - translate_v3_translate_text_with_glossary.translate_text_with_glossary( - "account", PROJECT_ID, GLOSSARY_ID - ) - out, _ = capsys.readouterr() - assert "アカウント" or "口座" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_model.py b/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_model.py deleted file mode 100644 index bd61bb769107..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_model.py +++ /dev/null @@ -1,49 +0,0 @@ -# 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. - -# [START translate_v3_translate_text_with_model] - -from google.cloud import translate - - -def translate_text_with_model( - text="YOUR_TEXT_TO_TRANSLATE", - project_id="YOUR_PROJECT_ID", - model_id="YOUR_MODEL_ID", -): - """Translates a given text using Translation custom model.""" - - client = translate.TranslationServiceClient() - - location = "us-central1" - parent = f"projects/{project_id}/locations/{location}" - model_path = f"{parent}/models/{model_id}" - - # Supported language codes: https://cloud.google.com/translate/docs/languages - response = client.translate_text( - request={ - "contents": [text], - "target_language_code": "ja", - "model": model_path, - "source_language_code": "en", - "parent": parent, - "mime_type": "text/plain", # mime types: text/plain, text/html - } - ) - # Display the translation for each input text provided - for translation in response.translations: - print("Translated text: {}".format(translation.translated_text)) - - -# [END translate_v3_translate_text_with_model] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_model_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_model_test.py deleted file mode 100644 index e547c8ce29e8..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3_translate_text_with_model_test.py +++ /dev/null @@ -1,30 +0,0 @@ -# -*- encoding: utf-8 -*- -# 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 - -import translate_v3_translate_text_with_model - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -MODEL_ID = "TRL251293382528204800" - - -def test_translate_text_with_model(capsys): - translate_v3_translate_text_with_model.translate_text_with_model( - "That' il do it.", PROJECT_ID, MODEL_ID - ) - out, _ = capsys.readouterr() - assert "それはそうだ" or "それじゃあ" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_batch_translate_document.py b/packages/google-cloud-translate/samples/snippets/translate_v3beta1_batch_translate_document.py deleted file mode 100644 index 825bda14bfbb..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_batch_translate_document.py +++ /dev/null @@ -1,62 +0,0 @@ -# Copyright 2021 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. - -# [START translate_v3beta1_batch_translate_document] - -from google.cloud import translate_v3beta1 as translate - - -def batch_translate_document( - input_uri: str, - output_uri: str, - project_id: str, - timeout=180, -): - - client = translate.TranslationServiceClient() - - # The ``global`` location is not supported for batch translation - location = "us-central1" - - # Google Cloud Storage location for the source input. This can be a single file - # (for example, ``gs://translation-test/input.docx``) or a wildcard - # (for example, ``gs://translation-test/*``). - # Supported file types: https://cloud.google.com/translate/docs/supported-formats - gcs_source = {"input_uri": input_uri} - - batch_document_input_configs = { - "gcs_source": gcs_source, - } - gcs_destination = {"output_uri_prefix": output_uri} - batch_document_output_config = {"gcs_destination": gcs_destination} - parent = f"projects/{project_id}/locations/{location}" - - # Supported language codes: https://cloud.google.com/translate/docs/language - operation = client.batch_translate_document( - request={ - "parent": parent, - "source_language_code": "en-US", - "target_language_codes": ["fr-FR"], - "input_configs": [batch_document_input_configs], - "output_config": batch_document_output_config, - } - ) - - print("Waiting for operation to complete...") - response = operation.result(timeout) - - print("Total Pages: {}".format(response.total_pages)) - - -# [END translate_v3beta1_batch_translate_document] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_batch_translate_document_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3beta1_batch_translate_document_test.py deleted file mode 100644 index 3654f85cf88d..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_batch_translate_document_test.py +++ /dev/null @@ -1,52 +0,0 @@ -# Copyright 2021 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 uuid - -from google.cloud import storage -import pytest - - -import translate_v3beta1_batch_translate_document - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - - -@pytest.fixture(scope="function") -def bucket(): - # Create a temporary bucket to store annotation output. - bucket_name = "test-{}".format(uuid.uuid4()) - storage_client = storage.Client() - bucket = storage_client.bucket(bucket_name) - bucket = storage_client.create_bucket(bucket, location="us-central1") - - yield bucket - - bucket.delete(force=True) - - -@pytest.mark.flaky(max_runs=3, min_passes=1) -def test_batch_translate_document(capsys, bucket): - translate_v3beta1_batch_translate_document.batch_translate_document( - input_uri="gs://cloud-samples-data/translation/async_invoices/*", - output_uri=f"gs://{bucket.name}/translation/BATCH_TRANSLATE_DOCUMENT_OUTPUT/", - project_id=PROJECT_ID, - timeout=1000, - ) - - out, _ = capsys.readouterr() - assert "Total Pages" in out diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_translate_document.py b/packages/google-cloud-translate/samples/snippets/translate_v3beta1_translate_document.py deleted file mode 100644 index 23b14f3f73da..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_translate_document.py +++ /dev/null @@ -1,54 +0,0 @@ -# Copyright 2021 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. - -# [START translate_v3beta1_translate_document] -from google.cloud import translate_v3beta1 as translate - - -def translate_document(project_id: str, file_path: str): - - client = translate.TranslationServiceClient() - - location = "us-central1" - - parent = f"projects/{project_id}/locations/{location}" - - # Supported file types: https://cloud.google.com/translate/docs/supported-formats - with open(file_path, "rb") as document: - document_content = document.read() - - document_input_config = { - "content": document_content, - "mime_type": "application/pdf", - } - - response = client.translate_document( - request={ - "parent": parent, - "target_language_code": "fr-FR", - "document_input_config": document_input_config, - } - ) - - # To output the translated document, uncomment the code below. - # f = open('/tmp/output', 'wb') - # f.write(response.document_translation.byte_stream_outputs[0]) - # f.close() - - # If not provided in the TranslationRequest, the translated file will only be returned through a byte-stream - # and its output mime type will be the same as the input file's mime type - print("Response: Detected Language Code - {}".format(response.document_translation.detected_language_code)) - - -# [END translate_v3beta1_translate_document] diff --git a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_translate_document_test.py b/packages/google-cloud-translate/samples/snippets/translate_v3beta1_translate_document_test.py deleted file mode 100644 index 9065702f2c97..000000000000 --- a/packages/google-cloud-translate/samples/snippets/translate_v3beta1_translate_document_test.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright 2021 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 translate_v3beta1_translate_document - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] - -FILE_PATH = "resources/fake_invoice.pdf" - - -def test_translate_document(capsys): - translate_v3beta1_translate_document.translate_document(project_id=PROJECT_ID, file_path=FILE_PATH) - out, _ = capsys.readouterr() - assert "Response" in out