diff --git a/translation/samples/snippets/beta_snippets.py b/translation/samples/snippets/beta_snippets.py index 96b19f5de7e5..10ec2dc6e648 100644 --- a/translation/samples/snippets/beta_snippets.py +++ b/translation/samples/snippets/beta_snippets.py @@ -25,14 +25,17 @@ def translate_text(project_id, text): # text = 'Text you wish to translate' location = 'global' - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{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') + request={ + "parent": parent, + "contents": [text], + "mime_type": "text/plain", # mime types: text/plain, text/html + "source_language_code": "en-US", + "target_language_code": "sr-Latn" + } + ) for translation in response.translations: print(u'Translated Text: {}'.format(translation)) @@ -49,7 +52,7 @@ def batch_translate_text(project_id, input_uri, output_uri): # output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' location = 'us-central1' - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" gcs_source = translate.types.GcsSource(input_uri=input_uri) @@ -64,11 +67,14 @@ def batch_translate_text(project_id, input_uri, output_uri): 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) + request={ + "parent": parent, + "source_language_code": "en-US", + "target_language_codes": ["sr-Latn"], + "input_configs": [input_config], + "output_config": output_config + } + ) result = operation.result(timeout=240) @@ -86,12 +92,15 @@ def detect_language(project_id, text): # text = 'Text you wish to translate' location = 'global' - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" response = client.detect_language( - parent=parent, - content=text, - mime_type='text/plain') # mime types: text/plain, text/html + request={ + "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( @@ -108,9 +117,9 @@ def list_languages(project_id): # project_id = YOUR_PROJECT_ID location = 'global' - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" - response = client.get_supported_languages(parent) + response = client.get_supported_languages(parent=parent) print('Supported Languages:') for language in response.languages: @@ -127,7 +136,7 @@ def list_languages_with_target(project_id, display_language_code): # display_language_code = 'is' location = 'global' - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" response = client.get_supported_languages( parent=parent, @@ -168,7 +177,7 @@ def create_glossary(project_id, glossary_id): language_codes_set=language_codes_set, input_config=input_config) - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" operation = client.create_glossary(parent=parent, glossary=glossary) @@ -186,9 +195,9 @@ def list_glossaries(project_id): # project_id = 'YOUR_PROJECT_ID' location = 'us-central1' # The location of the glossary - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" - for glossary in client.list_glossaries(parent): + for glossary in client.list_glossaries(parent=parent): print(u'Name: {}'.format(glossary.name)) print(u'Entry count: {}'.format(glossary.entry_count)) print(u'Input uri: {}'.format( @@ -206,12 +215,12 @@ def get_glossary(project_id, glossary_id): # project_id = 'YOUR_PROJECT_ID' # glossary_id = 'GLOSSARY_ID' - parent = client.glossary_path( + name = client.glossary_path( project_id, 'us-central1', # The location of the glossary glossary_id) - response = client.get_glossary(parent) + response = client.get_glossary(name=name) print(u'Name: {}'.format(response.name)) print(u'Language Pair:') print(u'\tSource Language Code: {}'.format( @@ -231,12 +240,12 @@ def delete_glossary(project_id, glossary_id): # project_id = 'YOUR_PROJECT_ID' # glossary_id = 'GLOSSARY_ID' - parent = client.glossary_path( + name = client.glossary_path( project_id, 'us-central1', # The location of the glossary glossary_id) - operation = client.delete_glossary(parent) + operation = client.delete_glossary(name=name) result = operation.result(timeout=90) print(u'Deleted: {}'.format(result.name)) # [END translate_delete_glossary_beta] @@ -260,15 +269,18 @@ def translate_text_with_glossary(project_id, glossary_id, text): glossary_config = translate.types.TranslateTextGlossaryConfig( glossary=glossary) - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{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) + request={ + "parent": parent, + "contents": [text], + "mime_type": "text/plain", + "source_language_code": "en", + "target_language_code": "es", + "glossary_config": glossary_config + } + ) for translation in result.glossary_translations: print(translation) @@ -355,4 +367,4 @@ def translate_text_with_glossary(project_id, glossary_id, text): 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) + args.project_id, args.glossary_id, args.text) diff --git a/translation/samples/snippets/hybrid_glossaries/README.rst b/translation/samples/snippets/hybrid_glossaries/README.rst index e68b8114ce3c..61fdc27fef72 100644 --- a/translation/samples/snippets/hybrid_glossaries/README.rst +++ b/translation/samples/snippets/hybrid_glossaries/README.rst @@ -1,4 +1,3 @@ - .. This file is automatically generated. Do not edit this file directly. Google Translation API Python Samples @@ -16,11 +15,13 @@ This directory contains samples for Google Translation API. With `Google Transla .. _Google Translation API: https://cloud.google.com/translate/docs + + + Setup ------------------------------------------------------------------------------- - Authentication ++++++++++++++ @@ -31,9 +32,6 @@ credentials for applications. .. _Authentication Getting Started Guide: https://cloud.google.com/docs/authentication/getting-started - - - Install Dependencies ++++++++++++++++++++ @@ -48,7 +46,7 @@ Install Dependencies .. _Python Development Environment Setup Guide: https://cloud.google.com/python/setup -#. Create a virtualenv. Samples are compatible with Python 3.6+. +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. .. code-block:: bash @@ -64,15 +62,9 @@ Install Dependencies .. _pip: https://pip.pypa.io/ .. _virtualenv: https://virtualenv.pypa.io/ - - - - - Samples ------------------------------------------------------------------------------- - Using glossaries with vision and text-to-speech +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -91,10 +83,6 @@ To run this sample: - - - - The client library ------------------------------------------------------------------------------- @@ -110,5 +98,4 @@ to `browse the source`_ and `report issues`_. https://github.com/GoogleCloudPlatform/google-cloud-python/issues - -.. _Google Cloud SDK: https://cloud.google.com/sdk/ +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py b/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py index 7d36174b4766..c4a8434502ba 100644 --- a/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py +++ b/translation/samples/snippets/hybrid_glossaries/hybrid_tutorial.py @@ -104,7 +104,7 @@ def create_glossary(languages, project_id, glossary_name, glossary_uri): language_codes_set=language_codes_set, input_config=input_config) - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" # Create glossary resource # Handle exception for case in which a glossary @@ -150,15 +150,18 @@ def translate_text(text, source_language_code, target_language_code, glossary_config = translate.types.TranslateTextGlossaryConfig( glossary=glossary) - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{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) + request={ + "parent": parent, + "contents": [text], + "mime_type": "text/plain", # mime types: text/plain, text/html + "source_language_code": source_language_code, + "target_language_code": target_language_code, + "glossary_config": glossary_config + } + ) # Extract translated text from API response return result.glossary_translations[0].translated_text @@ -240,7 +243,7 @@ def main(): # 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) + create_glossary(glossary_langs, PROJECT_ID, glossary_name, glossary_uri) # photo -> detected text text_to_translate = pic_to_text(infile) diff --git a/translation/samples/snippets/translate_v3_batch_translate_text.py b/translation/samples/snippets/translate_v3_batch_translate_text.py index 4454afb40746..487b0965774d 100644 --- a/translation/samples/snippets/translate_v3_batch_translate_text.py +++ b/translation/samples/snippets/translate_v3_batch_translate_text.py @@ -36,15 +36,18 @@ def batch_translate_text( } gcs_destination = {"output_uri_prefix": output_uri} output_config = {"gcs_destination": gcs_destination} - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{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) + request={ + "parent": parent, + "source_language_code": "en", + "target_language_codes": ["ja"], # Up to 10 language codes here. + "input_configs": [input_configs_element], + "output_config": output_config + } + ) print(u"Waiting for operation to complete...") response = operation.result(timeout) diff --git a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py index 97845e2a8e2b..e9c4a7c26375 100644 --- a/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py +++ b/translation/samples/snippets/translate_v3_batch_translate_text_with_glossary.py @@ -42,7 +42,7 @@ def batch_translate_text_with_glossary( gcs_destination = {"output_uri_prefix": output_uri} output_config = {"gcs_destination": gcs_destination} - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" # glossary is a custom dictionary Translation API uses # to translate the domain-specific terminology. @@ -50,19 +50,21 @@ def batch_translate_text_with_glossary( project_id, "us-central1", glossary_id # The location of the glossary ) - glossary_config = translate.types.TranslateTextGlossaryConfig( + glossary_config = translate.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, + request={ + "parent": parent, + "source_language_code": "en", + "target_language_codes": ["ja"], # Up to 10 language codes here. + "input_configs": [input_configs_element], + "glossaries": glossaries, + "output_config": output_config + } ) print(u"Waiting for operation to complete...") diff --git a/translation/samples/snippets/translate_v3_create_glossary.py b/translation/samples/snippets/translate_v3_create_glossary.py index 603cb4cc5681..2955acaa3478 100644 --- a/translation/samples/snippets/translate_v3_create_glossary.py +++ b/translation/samples/snippets/translate_v3_create_glossary.py @@ -47,7 +47,7 @@ def create_glossary( name=name, language_codes_set=language_codes_set, input_config=input_config ) - parent = client.location_path(project_id, location) + parent = f"projects/{project_id}/locations/{location}" # glossary is a custom dictionary Translation API uses # to translate the domain-specific terminology. operation = client.create_glossary(parent=parent, glossary=glossary) diff --git a/translation/samples/snippets/translate_v3_delete_glossary.py b/translation/samples/snippets/translate_v3_delete_glossary.py index e8acf76c31f4..fb9f86afe59e 100644 --- a/translation/samples/snippets/translate_v3_delete_glossary.py +++ b/translation/samples/snippets/translate_v3_delete_glossary.py @@ -24,9 +24,9 @@ def delete_glossary( """Delete a specific glossary based on the glossary ID.""" client = translate.TranslationServiceClient() - parent = client.glossary_path(project_id, "us-central1", glossary_id) + name = client.glossary_path(project_id, "us-central1", glossary_id) - operation = client.delete_glossary(parent) + operation = client.delete_glossary(name=name) result = operation.result(timeout) print("Deleted: {}".format(result.name)) diff --git a/translation/samples/snippets/translate_v3_detect_language.py b/translation/samples/snippets/translate_v3_detect_language.py index 759eb41a4d30..9e92b6079d5d 100644 --- a/translation/samples/snippets/translate_v3_detect_language.py +++ b/translation/samples/snippets/translate_v3_detect_language.py @@ -21,7 +21,9 @@ def detect_language(project_id="YOUR_PROJECT_ID"): client = translate.TranslationServiceClient() - parent = client.location_path(project_id, "global") + location = "global" + + parent = f"projects/{project_id}/locations/{location}" # Detail on supported types can be found here: # https://cloud.google.com/translate/docs/supported-formats diff --git a/translation/samples/snippets/translate_v3_get_glossary.py b/translation/samples/snippets/translate_v3_get_glossary.py index 99753136c5b0..f4c2aaae4602 100644 --- a/translation/samples/snippets/translate_v3_get_glossary.py +++ b/translation/samples/snippets/translate_v3_get_glossary.py @@ -23,7 +23,7 @@ def get_glossary(project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID"): name = client.glossary_path(project_id, "us-central1", glossary_id) - response = client.get_glossary(name) + response = client.get_glossary(name=name) print(u"Glossary name: {}".format(response.name)) print(u"Entry count: {}".format(response.entry_count)) print(u"Input URI: {}".format(response.input_config.gcs_source.input_uri)) diff --git a/translation/samples/snippets/translate_v3_get_supported_languages.py b/translation/samples/snippets/translate_v3_get_supported_languages.py index 6e15458d9cca..db7792787241 100644 --- a/translation/samples/snippets/translate_v3_get_supported_languages.py +++ b/translation/samples/snippets/translate_v3_get_supported_languages.py @@ -21,7 +21,7 @@ def get_supported_languages(project_id="YOUR_PROJECT_ID"): client = translate.TranslationServiceClient() - parent = client.location_path(project_id, "global") + parent = f"projects/{project_id}" # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.get_supported_languages(parent=parent) diff --git a/translation/samples/snippets/translate_v3_get_supported_languages_with_target.py b/translation/samples/snippets/translate_v3_get_supported_languages_with_target.py index f71138b533e4..3a164dcbd28b 100644 --- a/translation/samples/snippets/translate_v3_get_supported_languages_with_target.py +++ b/translation/samples/snippets/translate_v3_get_supported_languages_with_target.py @@ -20,7 +20,10 @@ 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") + + location = "global" + + parent = f"projects/{project_id}/locations/{location}" # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.get_supported_languages( diff --git a/translation/samples/snippets/translate_v3_list_glossary.py b/translation/samples/snippets/translate_v3_list_glossary.py index 53e51d85447f..934194b4443f 100644 --- a/translation/samples/snippets/translate_v3_list_glossary.py +++ b/translation/samples/snippets/translate_v3_list_glossary.py @@ -21,10 +21,12 @@ def list_glossaries(project_id="YOUR_PROJECT_ID"): client = translate.TranslationServiceClient() - parent = client.location_path(project_id, "us-central1") + location = "us-central1" + + parent = f"projects/{project_id}/locations/{location}" # Iterate over all results - for glossary in client.list_glossaries(parent): + for glossary in client.list_glossaries(parent=parent): print("Name: {}".format(glossary.name)) print("Entry count: {}".format(glossary.entry_count)) print("Input uri: {}".format(glossary.input_config.gcs_source.input_uri)) diff --git a/translation/samples/snippets/translate_v3_translate_text.py b/translation/samples/snippets/translate_v3_translate_text.py index 086f775a37fb..78a79b4e1072 100644 --- a/translation/samples/snippets/translate_v3_translate_text.py +++ b/translation/samples/snippets/translate_v3_translate_text.py @@ -21,17 +21,22 @@ def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): client = translate.TranslationServiceClient() - parent = client.location_path(project_id, "global") + location = "global" + + parent = f"projects/{project_id}/locations/{location}" # Detail on supported types can be found here: # https://cloud.google.com/translate/docs/supported-formats response = client.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", + request={ + "parent": parent, + "contents": [text], + "mime_type": "text/plain", # mime types: text/plain, text/html + "source_language_code": "en-US", + "target_language_code": "fr" + } ) + # Display the translation for each input text provided for translation in response.translations: print(u"Translated text: {}".format(translation.translated_text)) diff --git a/translation/samples/snippets/translate_v3_translate_text_with_glossary.py b/translation/samples/snippets/translate_v3_translate_text_with_glossary.py index ca0eeaccced9..b62d54133b20 100644 --- a/translation/samples/snippets/translate_v3_translate_text_with_glossary.py +++ b/translation/samples/snippets/translate_v3_translate_text_with_glossary.py @@ -14,7 +14,7 @@ # [START translate_v3_translate_text_with_glossary] -from google.cloud import translate_v3 +from google.cloud import translate def translate_text_with_glossary( @@ -24,24 +24,28 @@ def translate_text_with_glossary( ): """Translates a given text using a glossary.""" - client = translate_v3.TranslationServiceClient() - parent = client.location_path(project_id, "us-central1") + client = translate.TranslationServiceClient() + location = "us-central1" + parent = f"projects/{project_id}/locations/{location}" glossary = client.glossary_path( project_id, "us-central1", glossary_id # The location of the glossary ) - glossary_config = translate_v3.types.TranslateTextGlossaryConfig( + glossary_config = translate.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, + request={ + "contents": [text], + "target_language_code": "ja", + "source_language_code": "en", + "parent": parent, + "glossary_config": glossary_config + } ) + print("Translated text: \n") for translation in response.glossary_translations: print(u"\t {}".format(translation.translated_text)) diff --git a/translation/samples/snippets/translate_v3_translate_text_with_model.py b/translation/samples/snippets/translate_v3_translate_text_with_model.py index 8a0b0bb7825a..44c92f96b437 100644 --- a/translation/samples/snippets/translate_v3_translate_text_with_model.py +++ b/translation/samples/snippets/translate_v3_translate_text_with_model.py @@ -26,19 +26,21 @@ def translate_text_with_model( client = translate.TranslationServiceClient() - parent = client.location_path(project_id, "us-central1") - model_path = "projects/{}/locations/{}/models/{}".format( - project_id, "us-central1", model_id - ) + location = "us-central1" + parent = f"projects/{project_id}/locations/{location}" + model_path = f"{parent}/models/{model_id}" # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.translate_text( - 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 + request={ + "contents": [text], + "target_language_code": "ja", + "model": model_path, + "source_language_code": "en", + "parent": parent, + "mime_type": "text/plain", # mime types: text/plain, text/html + } + ) # Display the translation for each input text provided for translation in response.translations: