diff --git a/translate/cloud-client/README.rst b/translate/cloud-client/README.rst index f5065ed4be2..d52323646c5 100644 --- a/translate/cloud-client/README.rst +++ b/translate/cloud-client/README.rst @@ -1,178 +1,3 @@ -.. This file is automatically generated. Do not edit this file directly. +These samples have been moved. -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 2.7 and 3.4+. - - .. 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/ \ No newline at end of file +https://github.com/googleapis/python-translate/tree/master/samples \ No newline at end of file diff --git a/translate/cloud-client/README.rst.in b/translate/cloud-client/README.rst.in deleted file mode 100644 index ba804e74de3..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/beta_snippets.py b/translate/cloud-client/beta_snippets.py deleted file mode 100644 index 96b19f5de7e..00000000000 --- a/translate/cloud-client/beta_snippets.py +++ /dev/null @@ -1,358 +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 = client.location_path(project_id, location) - - response = client.translate_text( - 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(u'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 = client.location_path(project_id, 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( - parent=parent, - source_language_code='en-US', - target_language_codes=['sr-Latn'], - input_configs=[input_config], - output_config=output_config) - - result = operation.result(timeout=240) - - print(u'Total Characters: {}'.format(result.total_characters)) - print(u'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 = client.location_path(project_id, location) - - response = client.detect_language( - parent=parent, - content=text, - mime_type='text/plain') # mime types: text/plain, text/html - - for language in response.languages: - print(u'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 = client.location_path(project_id, location) - - response = client.get_supported_languages(parent) - - print('Supported Languages:') - for language in response.languages: - print(u'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 = client.location_path(project_id, location) - - response = client.get_supported_languages( - parent=parent, - display_language_code=display_language_code) - - print('Supported Languages:') - for language in response.languages: - print(u'Language Code: {}'.format(language.language_code)) - print(u'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 = client.location_path(project_id, location) - - operation = client.create_glossary(parent=parent, glossary=glossary) - - result = operation.result(timeout=90) - print(u'Created: {}'.format(result.name)) - print(u'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 = client.location_path(project_id, location) - - for glossary in client.list_glossaries(parent): - print(u'Name: {}'.format(glossary.name)) - print(u'Entry count: {}'.format(glossary.entry_count)) - print(u'Input uri: {}'.format( - glossary.input_config.gcs_source.input_uri)) - for language_code in glossary.language_codes_set.language_codes: - print(u'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' - - parent = client.glossary_path( - project_id, - 'us-central1', # The location of the glossary - glossary_id) - - response = client.get_glossary(parent) - print(u'Name: {}'.format(response.name)) - print(u'Language Pair:') - print(u'\tSource Language Code: {}'.format( - response.language_pair.source_language_code)) - print(u'\tTarget Language Code: {}'.format( - response.language_pair.target_language_code)) - print(u'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' - - parent = client.glossary_path( - project_id, - 'us-central1', # The location of the glossary - glossary_id) - - operation = client.delete_glossary(parent) - result = operation.result(timeout=90) - print(u'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', # The location of the glossary - glossary_id) - - glossary_config = translate.types.TranslateTextGlossaryConfig( - glossary=glossary) - - parent = client.location_path(project_id, location) - - result = client.translate_text( - parent=parent, - contents=[text], - mime_type='text/plain', # mime types: text/plain, text/html - 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/translate/cloud-client/beta_snippets_test.py b/translate/cloud-client/beta_snippets_test.py deleted file mode 100644 index a42ab08ce3f..00000000000 --- a/translate/cloud-client/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_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, u'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-CN' in out - - -def test_list_languages_with_target(capsys): - beta_snippets.list_languages_with_target(PROJECT_ID, 'is') - out, _ = capsys.readouterr() - assert u'Language Code: sq' in out - assert u'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/translate/cloud-client/hybrid_glossaries/README.rst b/translate/cloud-client/hybrid_glossaries/README.rst deleted file mode 100644 index 2c9866545dc..00000000000 --- a/translate/cloud-client/hybrid_glossaries/README.rst +++ /dev/null @@ -1,97 +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 2.7 and 3.4+. - - .. 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/ \ No newline at end of file diff --git a/translate/cloud-client/hybrid_glossaries/README.rst.in b/translate/cloud-client/hybrid_glossaries/README.rst.in deleted file mode 100644 index 882f3666fc9..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py deleted file mode 100644 index 7d36174b476..00000000000 --- a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py +++ /dev/null @@ -1,256 +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.types.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 - - 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.types.Glossary.LanguageCodesSet( - language_codes=languages) - - gcs_source = translate.types.GcsSource( - input_uri=glossary_uri) - - input_config = translate.types.GlossaryInputConfig( - gcs_source=gcs_source) - - # Set glossary resource information - glossary = translate.types.Glossary( - name=name, - language_codes_set=language_codes_set, - input_config=input_config) - - parent = client.location_path(project_id, 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.types.TranslateTextGlossaryConfig( - glossary=glossary) - - parent = client.location_path(project_id, location) - - result = client.translate_text( - 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/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_test.py b/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_test.py deleted file mode 100644 index de7b62b93cf..00000000000 --- a/translate/cloud-client/hybrid_glossaries/hybrid_tutorial_test.py +++ /dev/null @@ -1,99 +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' - - 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' - - 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/translate/cloud-client/hybrid_glossaries/requirements-test.txt b/translate/cloud-client/hybrid_glossaries/requirements-test.txt deleted file mode 100644 index 7e460c8c866..00000000000 --- a/translate/cloud-client/hybrid_glossaries/requirements-test.txt +++ /dev/null @@ -1 +0,0 @@ -pytest==6.0.1 diff --git a/translate/cloud-client/hybrid_glossaries/requirements.txt b/translate/cloud-client/hybrid_glossaries/requirements.txt deleted file mode 100644 index e5a1171094c..00000000000 --- a/translate/cloud-client/hybrid_glossaries/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -google-cloud-translate==2.0.2 -google-cloud-vision==1.0.0 -google-cloud-texttospeech==2.2.0 diff --git a/translate/cloud-client/hybrid_glossaries/resources/bistro_glossary.csv b/translate/cloud-client/hybrid_glossaries/resources/bistro_glossary.csv deleted file mode 100644 index 99b45144552..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/hybrid_glossaries/resources/example.mp3 b/translate/cloud-client/hybrid_glossaries/resources/example.mp3 deleted file mode 100644 index 5b5f02512dc..00000000000 Binary files a/translate/cloud-client/hybrid_glossaries/resources/example.mp3 and /dev/null differ diff --git a/translate/cloud-client/hybrid_glossaries/resources/example.png b/translate/cloud-client/hybrid_glossaries/resources/example.png deleted file mode 100644 index a3ac25ab053..00000000000 Binary files a/translate/cloud-client/hybrid_glossaries/resources/example.png and /dev/null differ diff --git a/translate/cloud-client/hybrid_glossaries/resources/expected_standard_text.mp3 b/translate/cloud-client/hybrid_glossaries/resources/expected_standard_text.mp3 deleted file mode 100644 index c731fbd787d..00000000000 Binary files a/translate/cloud-client/hybrid_glossaries/resources/expected_standard_text.mp3 and /dev/null differ diff --git a/translate/cloud-client/hybrid_glossaries/resources/standard_format.jpeg b/translate/cloud-client/hybrid_glossaries/resources/standard_format.jpeg deleted file mode 100644 index f985a540dad..00000000000 Binary files a/translate/cloud-client/hybrid_glossaries/resources/standard_format.jpeg and /dev/null differ diff --git a/translate/cloud-client/hybrid_glossaries/resources/standard_format.txt b/translate/cloud-client/hybrid_glossaries/resources/standard_format.txt deleted file mode 100644 index d91d49e8680..00000000000 --- a/translate/cloud-client/hybrid_glossaries/resources/standard_format.txt +++ /dev/null @@ -1,2 +0,0 @@ -This is -a test! diff --git a/translate/cloud-client/pytest.ini b/translate/cloud-client/pytest.ini deleted file mode 100644 index f139880676b..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/quickstart.py b/translate/cloud-client/quickstart.py deleted file mode 100644 index 888b6691c40..00000000000 --- a/translate/cloud-client/quickstart.py +++ /dev/null @@ -1,42 +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/translate/cloud-client/quickstart_test.py b/translate/cloud-client/quickstart_test.py deleted file mode 100644 index 8def7f3e9ac..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/requirements-test.txt b/translate/cloud-client/requirements-test.txt deleted file mode 100644 index 204efbba941..00000000000 --- a/translate/cloud-client/requirements-test.txt +++ /dev/null @@ -1,3 +0,0 @@ -backoff==1.10.0 -flaky==3.7.0 -pytest==6.0.1 \ No newline at end of file diff --git a/translate/cloud-client/requirements.txt b/translate/cloud-client/requirements.txt deleted file mode 100644 index 35285126d98..00000000000 --- a/translate/cloud-client/requirements.txt +++ /dev/null @@ -1,2 +0,0 @@ -google-cloud-translate==2.0.2 -google-cloud-storage==1.31.0 diff --git a/translate/cloud-client/snippets.py b/translate/cloud-client/snippets.py deleted file mode 100644 index 24b457162fa..00000000000 --- a/translate/cloud-client/snippets.py +++ /dev/null @@ -1,158 +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 - -import six - - -def detect_language(text): - # [START translate_detect_language] - """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] - - -def list_languages(): - # [START translate_list_codes] - """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] - - -def list_languages_with_target(target): - # [START translate_list_language_names] - """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] - - -def translate_text_with_model(target, text, model='nmt'): - # [START translate_text_with_model] - """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, 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, 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] - - -def translate_text(target, text): - # [START translate_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 - """ - 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/translate/cloud-client/snippets_test.py b/translate/cloud-client/snippets_test.py deleted file mode 100644 index 713a2cfac47..00000000000 --- a/translate/cloud-client/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'나는 파인애플을 좋아한다.' - snippets.translate_text('en', text) - out, _ = capsys.readouterr() - assert u'I love pineapple' in out diff --git a/translate/cloud-client/translate_v3_batch_translate_text.py b/translate/cloud-client/translate_v3_batch_translate_text.py deleted file mode 100644 index 4454afb4074..00000000000 --- a/translate/cloud-client/translate_v3_batch_translate_text.py +++ /dev/null @@ -1,56 +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 = client.location_path(project_id, location) - - # Supported language codes: https://cloud.google.com/translate/docs/language - operation = client.batch_translate_text( - 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(u"Waiting for operation to complete...") - response = operation.result(timeout) - - print(u"Total Characters: {}".format(response.total_characters)) - print(u"Translated Characters: {}".format(response.translated_characters)) - - -# [END translate_v3_batch_translate_text] diff --git a/translate/cloud-client/translate_v3_batch_translate_text_test.py b/translate/cloud-client/translate_v3_batch_translate_text_test.py deleted file mode 100644 index 76a1200b7dd..00000000000 --- a/translate/cloud-client/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=300 - ) - out, _ = capsys.readouterr() - assert "Total Characters" in out diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary.py b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary.py deleted file mode 100644 index 97845e2a8e2..00000000000 --- a/translate/cloud-client/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=180, -): - """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 = client.location_path(project_id, 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.types.TranslateTextGlossaryConfig( - glossary=glossary_path - ) - - glossaries = {"ja": glossary_config} # target lang as key - - operation = client.batch_translate_text( - 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(u"Waiting for operation to complete...") - response = operation.result(timeout) - - print(u"Total Characters: {}".format(response.total_characters)) - print(u"Translated Characters: {}".format(response.translated_characters)) - - -# [END translate_v3_batch_translate_text_with_glossary] diff --git a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py deleted file mode 100644 index 7a14f951563..00000000000 --- a/translate/cloud-client/translate_v3_batch_translate_text_with_glossary_test.py +++ /dev/null @@ -1,80 +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 import storage -from google.cloud.exceptions import NotFound -import pytest - -import translate_v3_batch_translate_text_with_glossary -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.fixture(scope="session") -def glossary(): - """Get the ID of a glossary available to session (do not mutate/delete).""" - glossary_id = "test-{}".format(uuid.uuid4()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # 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() - - -@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.mark.flaky(max_runs=3, min_passes=1) -def test_batch_translate_text_with_glossary(capsys, bucket, glossary): - 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_OUTPUT/".format(bucket.name), - PROJECT_ID, - glossary, - 300 - ) - - out, _ = capsys.readouterr() - assert "Total Characters: 9" in out diff --git a/translate/cloud-client/translate_v3_create_glossary.py b/translate/cloud-client/translate_v3_create_glossary.py deleted file mode 100644 index 603cb4cc568..00000000000 --- a/translate/cloud-client/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 = client.location_path(project_id, 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/translate/cloud-client/translate_v3_create_glossary_test.py b/translate/cloud-client/translate_v3_create_glossary_test.py deleted file mode 100644 index a24f461e05a..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/translate_v3_delete_glossary.py b/translate/cloud-client/translate_v3_delete_glossary.py deleted file mode 100644 index e8acf76c31f..00000000000 --- a/translate/cloud-client/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() - - parent = client.glossary_path(project_id, "us-central1", glossary_id) - - operation = client.delete_glossary(parent) - result = operation.result(timeout) - print("Deleted: {}".format(result.name)) - - -# [END translate_v3_delete_glossary] diff --git a/translate/cloud-client/translate_v3_delete_glossary_test.py b/translate/cloud-client/translate_v3_delete_glossary_test.py deleted file mode 100644 index a88c16a37a2..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/translate_v3_detect_language.py b/translate/cloud-client/translate_v3_detect_language.py deleted file mode 100644 index 759eb41a4d3..00000000000 --- a/translate/cloud-client/translate_v3_detect_language.py +++ /dev/null @@ -1,43 +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() - - parent = client.location_path(project_id, "global") - - # 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(u"Language code: {}".format(language.language_code)) - # Confidence of detection result for this language - print(u"Confidence: {}".format(language.confidence)) - - -# [END translate_v3_detect_language] diff --git a/translate/cloud-client/translate_v3_detect_language_test.py b/translate/cloud-client/translate_v3_detect_language_test.py deleted file mode 100644 index 7e9540cca00..00000000000 --- a/translate/cloud-client/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/translate/cloud-client/translate_v3_get_glossary.py b/translate/cloud-client/translate_v3_get_glossary.py deleted file mode 100644 index 99753136c5b..00000000000 --- a/translate/cloud-client/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) - 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/translate/cloud-client/translate_v3_get_glossary_test.py b/translate/cloud-client/translate_v3_get_glossary_test.py deleted file mode 100644 index 96ea6b78103..00000000000 --- a/translate/cloud-client/translate_v3_get_glossary_test.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. - -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 -import translate_v3_get_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@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()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # 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() - - -def test_get_glossary(capsys, glossary): - translate_v3_get_glossary.get_glossary(PROJECT_ID, glossary) - out, _ = capsys.readouterr() - assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/translate/cloud-client/translate_v3_get_supported_languages.py b/translate/cloud-client/translate_v3_get_supported_languages.py deleted file mode 100644 index 6e15458d9cc..00000000000 --- a/translate/cloud-client/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 = client.location_path(project_id, "global") - - # 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(u"Language Code: {}".format(language.language_code)) - - -# [END translate_v3_get_supported_languages] diff --git a/translate/cloud-client/translate_v3_get_supported_languages_test.py b/translate/cloud-client/translate_v3_get_supported_languages_test.py deleted file mode 100644 index 4bdc0fa4b14..00000000000 --- a/translate/cloud-client/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-CN" in out diff --git a/translate/cloud-client/translate_v3_get_supported_languages_with_target.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target.py deleted file mode 100644 index f71138b533e..00000000000 --- a/translate/cloud-client/translate_v3_get_supported_languages_with_target.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 -# -# 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() - parent = client.location_path(project_id, "global") - - # Supported language codes: https://cloud.google.com/translate/docs/languages - response = client.get_supported_languages( - display_language_code="is", # target language code - parent=parent - ) - # List language codes of supported languages - for language in response.languages: - print(u"Language Code: {}".format(language.language_code)) - print(u"Display Name: {}".format(language.display_name)) - -# [END translate_v3_get_supported_languages_for_target] diff --git a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py deleted file mode 100644 index 8f3f52cfe54..00000000000 --- a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py +++ /dev/null @@ -1,29 +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/translate/cloud-client/translate_v3_list_glossary.py b/translate/cloud-client/translate_v3_list_glossary.py deleted file mode 100644 index 53e51d85447..00000000000 --- a/translate/cloud-client/translate_v3_list_glossary.py +++ /dev/null @@ -1,40 +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() - - parent = client.location_path(project_id, "us-central1") - - # Iterate over all results - for glossary in client.list_glossaries(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/translate/cloud-client/translate_v3_list_glossary_test.py b/translate/cloud-client/translate_v3_list_glossary_test.py deleted file mode 100644 index 8f4eaa1ac51..00000000000 --- a/translate/cloud-client/translate_v3_list_glossary_test.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. - -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 -import translate_v3_list_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@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()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # clean up - @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() - - -def test_list_glossary(capsys, glossary): - translate_v3_list_glossary.list_glossaries(PROJECT_ID) - out, _ = capsys.readouterr() - assert glossary in out - assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/translate/cloud-client/translate_v3_translate_text.py b/translate/cloud-client/translate_v3_translate_text.py deleted file mode 100644 index 086f775a37f..00000000000 --- a/translate/cloud-client/translate_v3_translate_text.py +++ /dev/null @@ -1,40 +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] -from google.cloud import translate - - -def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): - """Translating Text.""" - - client = translate.TranslationServiceClient() - - parent = client.location_path(project_id, "global") - - # Detail on supported types can be found here: - # https://cloud.google.com/translate/docs/supported-formats - response = client.translate_text( - 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(u"Translated text: {}".format(translation.translated_text)) - - -# [END translate_v3_translate_text] diff --git a/translate/cloud-client/translate_v3_translate_text_test.py b/translate/cloud-client/translate_v3_translate_text_test.py deleted file mode 100644 index c93cb91f6dd..00000000000 --- a/translate/cloud-client/translate_v3_translate_text_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_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/translate/cloud-client/translate_v3_translate_text_with_glossary.py b/translate/cloud-client/translate_v3_translate_text_with_glossary.py deleted file mode 100644 index ca0eeaccced..00000000000 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary.py +++ /dev/null @@ -1,50 +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_v3 - - -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_v3.TranslationServiceClient() - parent = client.location_path(project_id, "us-central1") - - glossary = client.glossary_path( - project_id, "us-central1", glossary_id # The location of the glossary - ) - - glossary_config = translate_v3.types.TranslateTextGlossaryConfig( - glossary=glossary) - - # Supported language codes: https://cloud.google.com/translate/docs/languages - response = client.translate_text( - 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(u"\t {}".format(translation.translated_text)) - - -# [END translate_v3_translate_text_with_glossary] diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py deleted file mode 100644 index 1caa9e6e9e0..00000000000 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py +++ /dev/null @@ -1,62 +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 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 -import translate_v3_translate_text_with_glossary - - -PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] -GLOSSARY_INPUT_URI = "gs://cloud-samples-data/translation/glossary_ja.csv" - - -@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()) - translate_v3_create_glossary.create_glossary( - PROJECT_ID, GLOSSARY_INPUT_URI, glossary_id - ) - - yield glossary_id - - # 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() - - -def test_translate_text_with_glossary(capsys, glossary): - translate_v3_translate_text_with_glossary.translate_text_with_glossary( - "account", PROJECT_ID, glossary - ) - out, _ = capsys.readouterr() - assert "アカウント" or "口座" in out diff --git a/translate/cloud-client/translate_v3_translate_text_with_model.py b/translate/cloud-client/translate_v3_translate_text_with_model.py deleted file mode 100644 index 8a0b0bb7825..00000000000 --- a/translate/cloud-client/translate_v3_translate_text_with_model.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. - -# [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() - - parent = client.location_path(project_id, "us-central1") - model_path = "projects/{}/locations/{}/models/{}".format( - project_id, "us-central1", model_id - ) - - # Supported language codes: https://cloud.google.com/translate/docs/languages - response = client.translate_text( - 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(u"Translated text: {}".format(translation.translated_text)) - - -# [END translate_v3_translate_text_with_model] diff --git a/translate/cloud-client/translate_v3_translate_text_with_model_test.py b/translate/cloud-client/translate_v3_translate_text_with_model_test.py deleted file mode 100644 index f1cd5968752..00000000000 --- a/translate/cloud-client/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 = "TRL3128559826197068699" - - -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