From fa7dfd611d9df423a934c50c0b04d8efa30222ee Mon Sep 17 00:00:00 2001 From: Rebecca Taylor Date: Sat, 6 Apr 2019 11:15:58 -0700 Subject: [PATCH] Translation v3beta1 samples [(#2084)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2084) * Add in progress beta snippets Change-Id: I2cd8ddc2307a8e40d56ce7e493749dc05c34d164 * Add google-cloud-storage dependency Change-Id: Iff7bc9b2c82b1e829580a3d4ad628087dbeee8be * Non-'global' location required for BatchTranslateText Change-Id: I5198aa6368a088e8f5ee295dc55a5e9e4ca8f494 * Upgrade google-cloud-translate to 1.4.0 1.4.0 includes the new v3beta1 alongside V2 Change-Id: I5adfe78ea7e78d84678db343cd84516e3d05491f * Update Translate samples You can now provide your own glossary ID The tests now run within a randomly created bucket (deleted after each test) Change-Id: I5cb2680cd0e9e43c85932a6a0dc19e6fab5008c5 * pytest.fixture for random test bucket Change-Id: I8e816ed4c95a6235347a29849044b4cab02d40a8 * flake8 spec fixes Change-Id: I4932bcf856a9498b01d9661c90c6b45ee2958ee1 * Added pytest fixture for creating glossary (WIP) Change-Id: Iddb5ecbf0eefb9efd2243dc4bc56b585102e9351 * Add assertions, remove placeholder TODOs Change-Id: If1eb20bca5bfcc87dd0652d5488b2188afa626af --- .../samples/snippets/README.rst | 37 ++ .../samples/snippets/README.rst.in | 3 + .../samples/snippets/beta_snippets.py | 357 ++++++++++++++++++ .../samples/snippets/beta_snippets_test.py | 134 +++++++ .../samples/snippets/requirements.txt | 3 +- 5 files changed, 533 insertions(+), 1 deletion(-) create mode 100644 packages/google-cloud-translate/samples/snippets/beta_snippets.py create mode 100644 packages/google-cloud-translate/samples/snippets/beta_snippets_test.py diff --git a/packages/google-cloud-translate/samples/snippets/README.rst b/packages/google-cloud-translate/samples/snippets/README.rst index 4935e3c8b067..f5065ed4be20 100644 --- a/packages/google-cloud-translate/samples/snippets/README.rst +++ b/packages/google-cloud-translate/samples/snippets/README.rst @@ -121,6 +121,43 @@ To run this sample: +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 diff --git a/packages/google-cloud-translate/samples/snippets/README.rst.in b/packages/google-cloud-translate/samples/snippets/README.rst.in index a2483218cc40..ba804e74de3d 100644 --- a/packages/google-cloud-translate/samples/snippets/README.rst.in +++ b/packages/google-cloud-translate/samples/snippets/README.rst.in @@ -18,6 +18,9 @@ samples: - name: Snippets file: snippets.py show_help: true +- name: Beta Snippets + file: beta_snippets.py + show_help: true cloud_client_library: true diff --git a/packages/google-cloud-translate/samples/snippets/beta_snippets.py b/packages/google-cloud-translate/samples/snippets/beta_snippets.py new file mode 100644 index 000000000000..7cd94aed59a0 --- /dev/null +++ b/packages/google-cloud-translate/samples/snippets/beta_snippets.py @@ -0,0 +1,357 @@ +# 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('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(90) + + print('Total Characters: {}'.format(result.total_characters)) + print('Translated Characters: {}'.format(result.translated_characters)) + # [END translate_batch_translate_text_beta] + + +def detect_language(project_id, text): + # [START translate_detect_language_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = YOUR_PROJECT_ID + # text = 'Text you wish to translate' + location = 'global' + + parent = 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('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('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('Language Code: {}'.format(language.language_code)) + print('Display Name: {}\n'.format(language.display_name)) + # [END translate_list_language_names_beta] + + +def create_glossary(project_id, glossary_id): + # [START translate_create_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + # glossary_id = 'glossary-id' + location = 'us-central1' # The location of the glossary + + name = client.glossary_path( + project_id, + location, + glossary_id) + + language_codes_set = translate.types.Glossary.LanguageCodesSet( + language_codes=['en', 'es']) + + gcs_source = translate.types.GcsSource( + input_uri='gs://cloud-samples-data/translation/glossary.csv') + + input_config = translate.types.GlossaryInputConfig( + gcs_source=gcs_source) + + glossary = translate.types.Glossary( + name=name, + language_codes_set=language_codes_set, + input_config=input_config) + + parent = client.location_path(project_id, location) + + operation = client.create_glossary(parent=parent, glossary=glossary) + + result = operation.result(timeout=90) + print('Created: {}'.format(result.name)) + print('Input Uri: {}'.format(result.input_config.gcs_source.input_uri)) + # [END translate_create_glossary_beta] + + +def list_glossaries(project_id): + # [START translate_list_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + location = 'us-central1' # The location of the glossary + + parent = client.location_path(project_id, location) + + 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)) + for language_code in glossary.language_codes_set.language_codes: + print('Language code: {}'.format(language_code)) + # [END translate_list_glossary_beta] + + +def get_glossary(project_id, glossary_id): + # [START translate_get_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + # glossary_id = 'GLOSSARY_ID' + + parent = client.glossary_path( + project_id, + 'us-central1', # The location of the glossary + glossary_id) + + response = client.get_glossary(parent) + print('Name: {}'.format(response.name)) + print('Language Pair:') + print('\tSource Language Code: {}'.format( + response.language_pair.source_language_code)) + print('\tTarget Language Code: {}'.format( + response.language_pair.target_language_code)) + print('Input Uri: {}'.format( + response.input_config.gcs_source.input_uri)) + # [END translate_get_glossary_beta] + + +def delete_glossary(project_id, glossary_id): + # [START translate_delete_glossary_beta] + from google.cloud import translate_v3beta1 as translate + client = translate.TranslationServiceClient() + + # project_id = 'YOUR_PROJECT_ID' + # glossary_id = 'GLOSSARY_ID' + + 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('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.translations: + print(translation) + # [END translate_translate_text_with_glossary_beta] + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description=__doc__, + formatter_class=argparse.RawDescriptionHelpFormatter) + + subparsers = parser.add_subparsers(dest='command') + + translate_text_parser = subparsers.add_parser( + 'translate-text', help=translate_text.__doc__) + translate_text_parser.add_argument('project_id') + translate_text_parser.add_argument('text') + + batch_translate_text_parser = subparsers.add_parser( + 'batch-translate-text', help=translate_text.__doc__) + batch_translate_text_parser.add_argument('project_id') + batch_translate_text_parser.add_argument('gcs_source') + batch_translate_text_parser.add_argument('gcs_destination') + + detect_langage_parser = subparsers.add_parser( + 'detect-language', help=detect_language.__doc__) + detect_langage_parser.add_argument('project_id') + detect_langage_parser.add_argument('text') + + list_languages_parser = subparsers.add_parser( + 'list-languages', help=list_languages.__doc__) + list_languages_parser.add_argument('project_id') + + list_languages_with_target_parser = subparsers.add_parser( + 'list-languages-with-target', help=list_languages_with_target.__doc__) + list_languages_with_target_parser.add_argument('project_id') + list_languages_with_target_parser.add_argument('display_language_code') + + create_glossary_parser = subparsers.add_parser( + 'create-glossary', help=create_glossary.__doc__) + create_glossary_parser.add_argument('project_id') + create_glossary_parser.add_argument('glossary_id') + + get_glossary_parser = subparsers.add_parser( + 'get-glossary', help=get_glossary.__doc__) + get_glossary_parser.add_argument('project_id') + get_glossary_parser.add_argument('glossary_id') + + list_glossary_parser = subparsers.add_parser( + 'list-glossaries', help=list_glossaries.__doc__) + list_glossary_parser.add_argument('project_id') + + delete_glossary_parser = subparsers.add_parser( + 'delete-glossary', help=delete_glossary.__doc__) + delete_glossary_parser.add_argument('project_id') + delete_glossary_parser.add_argument('glossary_id') + + translate_with_glossary_parser = subparsers.add_parser( + 'translate-with-glossary', help=translate_text_with_glossary.__doc__) + translate_with_glossary_parser.add_argument('project_id') + translate_with_glossary_parser.add_argument('glossary_id') + translate_with_glossary_parser.add_argument('text') + + args = parser.parse_args() + + if args.command == 'translate-text': + translate_text(args.project_id, args.text) + elif args.command == 'batch-translate-text': + batch_translate_text( + args.project_id, args.gcs_source, args.gcs_destination) + elif args.command == 'detect-language': + detect_language(args.project_id, args.text) + elif args.command == 'list-languages': + list_languages(args.project_id) + elif args.command == 'list-languages-with-target': + list_languages_with_target(args.project_id, args.display_language_code) + elif args.command == 'create-glossary': + create_glossary(args.project_id, args.glossary_id) + elif args.command == 'get-glossary': + get_glossary(args.project_id, args.glossary_id) + elif args.command == 'list-glossaries': + list_glossaries(args.project_id) + elif args.command == 'delete-glossary': + delete_glossary(args.project_id, args.glossary_id) + elif args.command == 'translate-with-glossary': + translate_text_with_glossary( + args.project_id, args.glossary_id, args.text) diff --git a/packages/google-cloud-translate/samples/snippets/beta_snippets_test.py b/packages/google-cloud-translate/samples/snippets/beta_snippets_test.py new file mode 100644 index 000000000000..f7099a27cae1 --- /dev/null +++ b/packages/google-cloud-translate/samples/snippets/beta_snippets_test.py @@ -0,0 +1,134 @@ +# 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 pytest +import uuid +import beta_snippets +from google.cloud import storage + +PROJECT_ID = os.environ['GCLOUD_PROJECT'] + + +@pytest.fixture(scope='function') +def bucket(): + """Create a temporary bucket to store annotation output.""" + bucket_name = str(uuid.uuid1()) + 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 'Zdravo svet' in out + + +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, '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 + + +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 PROJECT_ID 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, 'directions') + out, _ = capsys.readouterr() + assert 'direcciones' in out + + +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 PROJECT_ID in out + assert 'us-central1' in out + assert unique_glossary_id in out diff --git a/packages/google-cloud-translate/samples/snippets/requirements.txt b/packages/google-cloud-translate/samples/snippets/requirements.txt index 62b23909bf67..318e3485aa01 100644 --- a/packages/google-cloud-translate/samples/snippets/requirements.txt +++ b/packages/google-cloud-translate/samples/snippets/requirements.txt @@ -1 +1,2 @@ -google-cloud-translate==1.3.3 +google-cloud-translate==1.4.0 +google-cloud-storage==1.14.0