diff --git a/translate/automl/README.rst b/translate/automl/README.rst deleted file mode 100644 index 9de68506aa2..00000000000 --- a/translate/automl/README.rst +++ /dev/null @@ -1,3 +0,0 @@ -These samples have been moved. - -https://github.com/googleapis/python-automl/tree/main/samples diff --git a/translate/cloud-client/README.rst b/translate/cloud-client/README.rst deleted file mode 100644 index bc654b1f223..00000000000 --- a/translate/cloud-client/README.rst +++ /dev/null @@ -1,3 +0,0 @@ -These samples have been moved. - -https://github.com/googleapis/python-translate/tree/main/samples diff --git a/translate/samples/AUTHORING_GUIDE.md b/translate/samples/AUTHORING_GUIDE.md new file mode 100644 index 00000000000..8249522ffc2 --- /dev/null +++ b/translate/samples/AUTHORING_GUIDE.md @@ -0,0 +1 @@ +See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md \ No newline at end of file diff --git a/translate/samples/CONTRIBUTING.md b/translate/samples/CONTRIBUTING.md new file mode 100644 index 00000000000..f5fe2e6baf1 --- /dev/null +++ b/translate/samples/CONTRIBUTING.md @@ -0,0 +1 @@ +See https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/CONTRIBUTING.md \ No newline at end of file diff --git a/translate/samples/cloud-nebulous-serverless/README.md b/translate/samples/cloud-nebulous-serverless/README.md new file mode 100644 index 00000000000..e2c82823a7d --- /dev/null +++ b/translate/samples/cloud-nebulous-serverless/README.md @@ -0,0 +1 @@ +This sample, demonstrating how to access the [Cloud Translation API](https://cloud.google.com/translate) from [Google Cloud serverless platforms](https://cloud.google.com/serverless) (App Engine, Cloud Functions, Cloud Run) can be found at . Versions in Python (2.7 and 3.7+) and Node.js (10+) are available along with hands-on tutorials. diff --git a/translate/samples/snippets/README.rst b/translate/samples/snippets/README.rst new file mode 100644 index 00000000000..559b57e2a50 --- /dev/null +++ b/translate/samples/snippets/README.rst @@ -0,0 +1,202 @@ + +.. This file is automatically generated. Do not edit this file directly. + +Google Translation API Python Samples +=============================================================================== + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/README.rst + + +This directory contains samples for Google Translation API. With `Google Translation API`, you can dynamically translate text between thousands of language pairs. + + + + +.. _Google Translation API: https://cloud.google.com/translate/docs + + +Setup +------------------------------------------------------------------------------- + + + +Authentication +++++++++++++++ + +This sample requires you to have authentication setup. Refer to the +`Authentication Getting Started Guide`_ for instructions on setting up +credentials for applications. + +.. _Authentication Getting Started Guide: + https://cloud.google.com/docs/authentication/getting-started + + + + +Install Dependencies +++++++++++++++++++++ + +#. Clone python-docs-samples and change directory to the sample directory you want to use. + + .. code-block:: bash + + $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. + + .. _Python Development Environment Setup Guide: + https://cloud.google.com/python/setup + +#. Create a virtualenv. Samples are compatible with Python 3.6+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + + + + + + +Samples +------------------------------------------------------------------------------- + + +Quickstart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/quickstart.py,translate/cloud-client/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python quickstart.py + + + + +Snippets ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/snippets.py,translate/cloud-client/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python snippets.py + + + usage: snippets.py [-h] + {detect-language,list-languages,list-languages-with-target,translate-text} + ... + + This application demonstrates how to perform basic operations with the + Google Cloud Translate API + + For more information, the documentation at + https://cloud.google.com/translate/docs. + + positional arguments: + {detect-language,list-languages,list-languages-with-target,translate-text} + detect-language Detects the text's language. + list-languages Lists all available languages. + list-languages-with-target + Lists all available languages and localizes them to + the target language. Target must be an ISO 639-1 + language code. See + https://g.co/cloud/translate/v2/translate- + reference#supported_languages + translate-text Translates text into the target language. Target must + be an ISO 639-1 language code. See + https://g.co/cloud/translate/v2/translate- + reference#supported_languages + + optional arguments: + -h, --help show this help message and exit + + + + + +Beta Snippets ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=translate/cloud-client/beta_snippets.py,translate/cloud-client/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python beta_snippets.py + + + usage: beta_snippets.py [-h] + {translate-text,batch-translate-text,detect-language,list-languages,list-languages-with-target,create-glossary,get-glossary,list-glossaries,delete-glossary,translate-with-glossary} + ... + + positional arguments: + {translate-text,batch-translate-text,detect-language,list-languages,list-languages-with-target,create-glossary,get-glossary,list-glossaries,delete-glossary,translate-with-glossary} + translate-text + batch-translate-text + detect-language + list-languages + list-languages-with-target + create-glossary + get-glossary + list-glossaries + delete-glossary + translate-with-glossary + + optional arguments: + -h, --help show this help message and exit + + + + + + + + + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. _Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. _browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. _report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ diff --git a/translate/samples/snippets/README.rst.in b/translate/samples/snippets/README.rst.in new file mode 100644 index 00000000000..ba804e74de3 --- /dev/null +++ b/translate/samples/snippets/README.rst.in @@ -0,0 +1,27 @@ +# 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/samples/snippets/hybrid_glossaries/README.rst b/translate/samples/snippets/hybrid_glossaries/README.rst new file mode 100644 index 00000000000..e68b8114ce3 --- /dev/null +++ b/translate/samples/snippets/hybrid_glossaries/README.rst @@ -0,0 +1,114 @@ + +.. This file is automatically generated. Do not edit this file directly. + +Google Translation API Python Samples +=============================================================================== + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/README.rst + + +This directory contains samples for Google Translation API. With `Google Translation API`, you can dynamically translate text between thousands of language pairs. + + + + +.. _Google Translation API: https://cloud.google.com/translate/docs + + +Setup +------------------------------------------------------------------------------- + + + +Authentication +++++++++++++++ + +This sample requires you to have authentication setup. Refer to the +`Authentication Getting Started Guide`_ for instructions on setting up +credentials for applications. + +.. _Authentication Getting Started Guide: + https://cloud.google.com/docs/authentication/getting-started + + + + +Install Dependencies +++++++++++++++++++++ + +#. Clone python-docs-samples and change directory to the sample directory you want to use. + + .. code-block:: bash + + $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. + + .. _Python Development Environment Setup Guide: + https://cloud.google.com/python/setup + +#. Create a virtualenv. Samples are compatible with Python 3.6+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + + + + + + +Samples +------------------------------------------------------------------------------- + + +Using glossaries with vision and text-to-speech ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +.. image:: https://gstatic.com/cloudssh/images/open-btn.png + :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=/hybrid_tutorial.py,/README.rst + + + + +To run this sample: + +.. code-block:: bash + + $ python hybrid_tutorial.py + + + + + + + + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. _Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. _browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. _report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ diff --git a/translate/samples/snippets/hybrid_glossaries/README.rst.in b/translate/samples/snippets/hybrid_glossaries/README.rst.in new file mode 100644 index 00000000000..882f3666fc9 --- /dev/null +++ b/translate/samples/snippets/hybrid_glossaries/README.rst.in @@ -0,0 +1,22 @@ + + +# 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/samples/snippets/hybrid_glossaries/hybrid_tutorial.py b/translate/samples/snippets/hybrid_glossaries/hybrid_tutorial.py new file mode 100644 index 00000000000..9eae168ada3 --- /dev/null +++ b/translate/samples/snippets/hybrid_glossaries/hybrid_tutorial.py @@ -0,0 +1,257 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# [START translate_hybrid_imports] +import html +import io +import os + +# Imports the Google Cloud client libraries +from google.api_core.exceptions import AlreadyExists +from google.cloud import texttospeech +from google.cloud import translate_v3beta1 as translate +from google.cloud import vision + +# [END translate_hybrid_imports] + + +# [START translate_hybrid_project_id] +# extract GCP project id +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +# [END translate_hybrid_project_id] + + +# [START translate_hybrid_vision] +def pic_to_text(infile): + """Detects text in an image file + + ARGS + infile: path to image file + + RETURNS + String of text detected in image + """ + + # Instantiates a client + client = vision.ImageAnnotatorClient() + + # Opens the input image file + with io.open(infile, "rb") as image_file: + content = image_file.read() + + image = vision.Image(content=content) + + # For dense text, use document_text_detection + # For less dense text, use text_detection + response = client.document_text_detection(image=image) + text = response.full_text_annotation.text + print("Detected text: {}".format(text)) + + return text + # [END translate_hybrid_vision] + + +# [START translate_hybrid_create_glossary] +def create_glossary(languages, project_id, glossary_name, glossary_uri): + """Creates a GCP glossary resource + Assumes you've already manually uploaded a glossary to Cloud Storage + + ARGS + languages: list of languages in the glossary + project_id: GCP project id + glossary_name: name you want to give this glossary resource + glossary_uri: the uri of the glossary you uploaded to Cloud Storage + + RETURNS + nothing + """ + + # Instantiates a client + client = translate.TranslationServiceClient() + + # Designates the data center location that you want to use + location = "us-central1" + + # Set glossary resource name + name = client.glossary_path(project_id, location, glossary_name) + + # Set language codes + language_codes_set = translate.Glossary.LanguageCodesSet( + language_codes=languages + ) + + gcs_source = translate.GcsSource(input_uri=glossary_uri) + + input_config = translate.GlossaryInputConfig(gcs_source=gcs_source) + + # Set glossary resource information + glossary = translate.Glossary( + name=name, language_codes_set=language_codes_set, input_config=input_config + ) + + parent = f"projects/{project_id}/locations/{location}" + + # Create glossary resource + # Handle exception for case in which a glossary + # with glossary_name already exists + try: + operation = client.create_glossary(parent=parent, glossary=glossary) + operation.result(timeout=90) + print("Created glossary " + glossary_name + ".") + except AlreadyExists: + print( + "The glossary " + + glossary_name + + " already exists. No new glossary was created." + ) + # [END translate_hybrid_create_glossary] + + +# [START translate_hybrid_translate] +def translate_text( + text, source_language_code, target_language_code, project_id, glossary_name +): + """Translates text to a given language using a glossary + + ARGS + text: String of text to translate + source_language_code: language of input text + target_language_code: language of output text + project_id: GCP project id + glossary_name: name you gave your project's glossary + resource when you created it + + RETURNS + String of translated text + """ + + # Instantiates a client + client = translate.TranslationServiceClient() + + # Designates the data center location that you want to use + location = "us-central1" + + glossary = client.glossary_path(project_id, location, glossary_name) + + glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary) + + parent = f"projects/{project_id}/locations/{location}" + + result = client.translate_text( + request={ + "parent": parent, + "contents": [text], + "mime_type": "text/plain", # mime types: text/plain, text/html + "source_language_code": source_language_code, + "target_language_code": target_language_code, + "glossary_config": glossary_config, + } + ) + + # Extract translated text from API response + return result.glossary_translations[0].translated_text + # [END translate_hybrid_translate] + + +# [START translate_hybrid_tts] +def text_to_speech(text, outfile): + """Converts plaintext to SSML and + generates synthetic audio from SSML + + ARGS + text: text to synthesize + outfile: filename to use to store synthetic audio + + RETURNS + nothing + """ + + # Replace special characters with HTML Ampersand Character Codes + # These Codes prevent the API from confusing text with + # SSML commands + # For example, '<' --> '<' and '&' --> '&' + escaped_lines = html.escape(text) + + # Convert plaintext to SSML in order to wait two seconds + # between each line in synthetic speech + ssml = "{}".format( + escaped_lines.replace("\n", '\n') + ) + + # Instantiates a client + client = texttospeech.TextToSpeechClient() + + # Sets the text input to be synthesized + synthesis_input = texttospeech.SynthesisInput(ssml=ssml) + + # Builds the voice request, selects the language code ("en-US") and + # the SSML voice gender ("MALE") + voice = texttospeech.VoiceSelectionParams( + language_code="en-US", ssml_gender=texttospeech.SsmlVoiceGender.MALE + ) + + # Selects the type of audio file to return + audio_config = texttospeech.AudioConfig( + audio_encoding=texttospeech.AudioEncoding.MP3 + ) + + # Performs the text-to-speech request on the text input with the selected + # voice parameters and audio file type + + request = texttospeech.SynthesizeSpeechRequest( + input=synthesis_input, voice=voice, audio_config=audio_config + ) + + response = client.synthesize_speech(request=request) + + # Writes the synthetic audio to the output file. + with open(outfile, "wb") as out: + out.write(response.audio_content) + print("Audio content written to file " + outfile) + # [END translate_hybrid_tts] + + +# [START translate_hybrid_integration] +def main(): + + # Photo from which to extract text + infile = "resources/example.png" + # Name of file that will hold synthetic speech + outfile = "resources/example.mp3" + + # Defines the languages in the glossary + # This list must match the languages in the glossary + # Here, the glossary includes French and English + glossary_langs = ["fr", "en"] + # Name that will be assigned to your project's glossary resource + glossary_name = "bistro-glossary" + # uri of .csv file uploaded to Cloud Storage + glossary_uri = "gs://cloud-samples-data/translation/bistro_glossary.csv" + + create_glossary(glossary_langs, PROJECT_ID, glossary_name, glossary_uri) + + # photo -> detected text + text_to_translate = pic_to_text(infile) + # detected text -> translated text + text_to_speak = translate_text( + text_to_translate, "fr", "en", PROJECT_ID, glossary_name + ) + # translated text -> synthetic audio + text_to_speech(text_to_speak, outfile) + # [END translate_hybrid_integration] + + +if __name__ == "__main__": + main() diff --git a/translate/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py b/translate/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py new file mode 100644 index 00000000000..ac3044ae186 --- /dev/null +++ b/translate/samples/snippets/hybrid_glossaries/hybrid_tutorial_test.py @@ -0,0 +1,105 @@ +# 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 translate_v3_delete_glossary import delete_glossary + + languages = ["fr", "en"] + glossary_name = f"test-glossary-{uuid.uuid4()}" + glossary_uri = "gs://cloud-samples-data/translation/bistro_glossary.csv" + + # create_glossary will raise an exception if creation fails + create_glossary(languages, PROJECT_ID, glossary_name, glossary_uri) + + # Delete glossary so that future tests will pass + # delete_glossary will raise an exception if deletion fails + delete_glossary(PROJECT_ID, glossary_name) + + +def test_translate_standard(): + + expected_text = "Hello" + + # attempt to create glossary, fails if it already exists + languages = ["fr", "en"] + glossary_name = "bistro-glossary" + glossary_uri = f"gs://cloud-samples-data/translation/{glossary_name}.csv" + create_glossary(languages, PROJECT_ID, glossary_name, glossary_uri) + + text = translate_text("Bonjour", "fr", "en", PROJECT_ID, "bistro-glossary") + + assert text == expected_text + + +def test_translate_glossary(): + + expected_text = "I eat goat cheese" + input_text = "Je mange du chevre" + + # attempt to create glossary, fails if it already exists + languages = ["fr", "en"] + glossary_name = "bistro-glossary" + glossary_uri = f"gs://cloud-samples-data/translation/{glossary_name}.csv" + create_glossary(languages, PROJECT_ID, glossary_name, glossary_uri) + + text = translate_text(input_text, "fr", "en", PROJECT_ID, "bistro-glossary") + + assert text == expected_text + + +# TEXT-TO-SPEECH TESTS + + +def test_tts_standard(capsys): + outfile = "resources/test_standard_text.mp3" + text = "this is\na test!" + + 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/samples/snippets/hybrid_glossaries/requirements-test.txt b/translate/samples/snippets/hybrid_glossaries/requirements-test.txt new file mode 100644 index 00000000000..49780e03569 --- /dev/null +++ b/translate/samples/snippets/hybrid_glossaries/requirements-test.txt @@ -0,0 +1 @@ +pytest==7.2.0 diff --git a/translate/samples/snippets/hybrid_glossaries/requirements.txt b/translate/samples/snippets/hybrid_glossaries/requirements.txt new file mode 100644 index 00000000000..fee36e967ed --- /dev/null +++ b/translate/samples/snippets/hybrid_glossaries/requirements.txt @@ -0,0 +1,3 @@ +google-cloud-translate==3.8.4 +google-cloud-vision==3.1.4 +google-cloud-texttospeech==2.12.3 diff --git a/translate/samples/snippets/hybrid_glossaries/resources/bistro_glossary.csv b/translate/samples/snippets/hybrid_glossaries/resources/bistro_glossary.csv new file mode 100644 index 00000000000..99b45144552 --- /dev/null +++ b/translate/samples/snippets/hybrid_glossaries/resources/bistro_glossary.csv @@ -0,0 +1,13 @@ +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/samples/snippets/hybrid_glossaries/resources/example.mp3 b/translate/samples/snippets/hybrid_glossaries/resources/example.mp3 new file mode 100644 index 00000000000..5b5f02512dc Binary files /dev/null and b/translate/samples/snippets/hybrid_glossaries/resources/example.mp3 differ diff --git a/translate/samples/snippets/hybrid_glossaries/resources/example.png b/translate/samples/snippets/hybrid_glossaries/resources/example.png new file mode 100644 index 00000000000..a3ac25ab053 Binary files /dev/null and b/translate/samples/snippets/hybrid_glossaries/resources/example.png differ diff --git a/translate/samples/snippets/hybrid_glossaries/resources/expected_standard_text.mp3 b/translate/samples/snippets/hybrid_glossaries/resources/expected_standard_text.mp3 new file mode 100644 index 00000000000..c731fbd787d Binary files /dev/null and b/translate/samples/snippets/hybrid_glossaries/resources/expected_standard_text.mp3 differ diff --git a/translate/samples/snippets/hybrid_glossaries/resources/standard_format.jpeg b/translate/samples/snippets/hybrid_glossaries/resources/standard_format.jpeg new file mode 100644 index 00000000000..f985a540dad Binary files /dev/null and b/translate/samples/snippets/hybrid_glossaries/resources/standard_format.jpeg differ diff --git a/translate/samples/snippets/pytest.ini b/translate/samples/snippets/pytest.ini new file mode 100644 index 00000000000..f139880676b --- /dev/null +++ b/translate/samples/snippets/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +# don't recurse into hybrid_glossaries directory +norecursedirs = * \ No newline at end of file diff --git a/translate/samples/snippets/requirements-test.txt b/translate/samples/snippets/requirements-test.txt new file mode 100644 index 00000000000..6c0e08f306e --- /dev/null +++ b/translate/samples/snippets/requirements-test.txt @@ -0,0 +1,3 @@ +backoff==2.2.1 +flaky==3.7.0 +pytest==7.2.0 diff --git a/translate/samples/snippets/requirements.txt b/translate/samples/snippets/requirements.txt new file mode 100644 index 00000000000..8526082bd8f --- /dev/null +++ b/translate/samples/snippets/requirements.txt @@ -0,0 +1,3 @@ +google-cloud-translate==3.8.4 +google-cloud-storage==2.5.0 +google-cloud-automl==2.8.3 diff --git a/translate/samples/snippets/resources/fake_invoice.pdf b/translate/samples/snippets/resources/fake_invoice.pdf new file mode 100644 index 00000000000..b9107fba129 Binary files /dev/null and b/translate/samples/snippets/resources/fake_invoice.pdf differ diff --git a/translate/samples/snippets/snippets.py b/translate/samples/snippets/snippets.py new file mode 100644 index 00000000000..e0be2e1b065 --- /dev/null +++ b/translate/samples/snippets/snippets.py @@ -0,0 +1,168 @@ +#!/usr/bin/env python + +# Copyright 2016 Google, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""This application demonstrates how to perform basic operations with the +Google Cloud Translate API + +For more information, the documentation at +https://cloud.google.com/translate/docs. +""" + +import argparse + + +# [START translate_detect_language] +def detect_language(text): + """Detects the text's language.""" + from google.cloud import translate_v2 as translate + + translate_client = translate.Client() + + # Text can also be a sequence of strings, in which case this method + # will return a sequence of results for each text. + result = translate_client.detect_language(text) + + print("Text: {}".format(text)) + print("Confidence: {}".format(result["confidence"])) + print("Language: {}".format(result["language"])) + + +# [END translate_detect_language] + +# [START translate_list_codes] +def list_languages(): + """Lists all available languages.""" + from google.cloud import translate_v2 as translate + + translate_client = translate.Client() + + results = translate_client.get_languages() + + for language in results: + print(u"{name} ({language})".format(**language)) + + +# [END translate_list_codes] + +# [START translate_list_language_names] +def list_languages_with_target(target): + """Lists all available languages and localizes them to the target language. + + Target must be an ISO 639-1 language code. + See https://g.co/cloud/translate/v2/translate-reference#supported_languages + """ + from google.cloud import translate_v2 as translate + + translate_client = translate.Client() + + results = translate_client.get_languages(target_language=target) + + for language in results: + print(u"{name} ({language})".format(**language)) + + +# [END translate_list_language_names] + +# [START translate_text_with_model] +def translate_text_with_model(target, text, model="nmt"): + """Translates text into the target language. + + Make sure your project is allowlisted. + + Target must be an ISO 639-1 language code. + See https://g.co/cloud/translate/v2/translate-reference#supported_languages + """ + from google.cloud import translate_v2 as translate + + translate_client = translate.Client() + + if isinstance(text, bytes): + text = text.decode("utf-8") + + # Text can also be a sequence of strings, in which case this method + # will return a sequence of results for each text. + result = translate_client.translate(text, target_language=target, model=model) + + print(u"Text: {}".format(result["input"])) + print(u"Translation: {}".format(result["translatedText"])) + print(u"Detected source language: {}".format(result["detectedSourceLanguage"])) + + +# [END translate_text_with_model] + +# [START translate_translate_text] +def translate_text(target, text): + """Translates text into the target language. + + Target must be an ISO 639-1 language code. + See https://g.co/cloud/translate/v2/translate-reference#supported_languages + """ + import six + from google.cloud import translate_v2 as translate + + translate_client = translate.Client() + + if isinstance(text, six.binary_type): + text = text.decode("utf-8") + + # Text can also be a sequence of strings, in which case this method + # will return a sequence of results for each text. + result = translate_client.translate(text, target_language=target) + + print(u"Text: {}".format(result["input"])) + print(u"Translation: {}".format(result["translatedText"])) + print(u"Detected source language: {}".format(result["detectedSourceLanguage"])) + + +# [END translate_translate_text] + + +if __name__ == "__main__": + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) + subparsers = parser.add_subparsers(dest="command") + + detect_langage_parser = subparsers.add_parser( + "detect-language", help=detect_language.__doc__ + ) + detect_langage_parser.add_argument("text") + + list_languages_parser = subparsers.add_parser( + "list-languages", help=list_languages.__doc__ + ) + + list_languages_with_target_parser = subparsers.add_parser( + "list-languages-with-target", help=list_languages_with_target.__doc__ + ) + list_languages_with_target_parser.add_argument("target") + + translate_text_parser = subparsers.add_parser( + "translate-text", help=translate_text.__doc__ + ) + translate_text_parser.add_argument("target") + translate_text_parser.add_argument("text") + + args = parser.parse_args() + + if args.command == "detect-language": + detect_language(args.text) + elif args.command == "list-languages": + list_languages() + elif args.command == "list-languages-with-target": + list_languages_with_target(args.target) + elif args.command == "translate-text": + translate_text(args.target, args.text) diff --git a/translate/samples/snippets/snippets_test.py b/translate/samples/snippets/snippets_test.py new file mode 100644 index 00000000000..b5fe362cafc --- /dev/null +++ b/translate/samples/snippets/snippets_test.py @@ -0,0 +1,49 @@ +# -*- coding: utf-8 -*- + +# Copyright 2016 Google, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import snippets + + +def test_detect_language(capsys): + snippets.detect_language("Hæ sæta") + out, _ = capsys.readouterr() + assert "is" in out + + +def test_list_languages(capsys): + snippets.list_languages() + out, _ = capsys.readouterr() + assert "Icelandic (is)" in out + + +def test_list_languages_with_target(capsys): + snippets.list_languages_with_target("is") + out, _ = capsys.readouterr() + assert u"íslenska (is)" in out + + +def test_translate_text(capsys): + snippets.translate_text("is", "Hello world") + out, _ = capsys.readouterr() + assert u"Halló heimur" in out + + +def test_translate_utf8(capsys): + text = u"파인애플 13개" + snippets.translate_text("en", text) + out, _ = capsys.readouterr() + assert u"13 pineapples" in out diff --git a/translate/samples/snippets/translate_v3_batch_translate_text.py b/translate/samples/snippets/translate_v3_batch_translate_text.py new file mode 100644 index 00000000000..514eae2c59d --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text.py @@ -0,0 +1,59 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_batch_translate_text] +from google.cloud import translate + + +def batch_translate_text( + input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", + output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", + project_id="YOUR_PROJECT_ID", + timeout=180, +): + """Translates a batch of texts on GCS and stores the result in a GCS location.""" + + client = translate.TranslationServiceClient() + + location = "us-central1" + # Supported file types: https://cloud.google.com/translate/docs/supported-formats + gcs_source = {"input_uri": input_uri} + + input_configs_element = { + "gcs_source": gcs_source, + "mime_type": "text/plain", # Can be "text/plain" or "text/html". + } + gcs_destination = {"output_uri_prefix": output_uri} + output_config = {"gcs_destination": gcs_destination} + parent = f"projects/{project_id}/locations/{location}" + + # Supported language codes: https://cloud.google.com/translate/docs/languages + operation = client.batch_translate_text( + request={ + "parent": parent, + "source_language_code": "en", + "target_language_codes": ["ja"], # Up to 10 language codes here. + "input_configs": [input_configs_element], + "output_config": output_config, + } + ) + + print("Waiting for operation to complete...") + response = operation.result(timeout) + + print("Total Characters: {}".format(response.total_characters)) + print("Translated Characters: {}".format(response.translated_characters)) + + +# [END translate_v3_batch_translate_text] diff --git a/translate/samples/snippets/translate_v3_batch_translate_text_test.py b/translate/samples/snippets/translate_v3_batch_translate_text_test.py new file mode 100644 index 00000000000..ad637cd8938 --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text_test.py @@ -0,0 +1,48 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import uuid + +from google.cloud import storage +import pytest + +import translate_v3_batch_translate_text + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] + + +@pytest.fixture(scope="function") +def bucket(): + """Create a temporary bucket to store annotation output.""" + bucket_name = "test-{}".format(uuid.uuid4()) + storage_client = storage.Client() + bucket = storage_client.create_bucket(bucket_name) + + yield bucket + + bucket.delete(force=True) + + +@pytest.mark.flaky(max_runs=3, min_passes=1) +def test_batch_translate_text(capsys, bucket): + translate_v3_batch_translate_text.batch_translate_text( + "gs://cloud-samples-data/translation/text.txt", + "gs://{}/translation/BATCH_TRANSLATION_OUTPUT/".format(bucket.name), + PROJECT_ID, + timeout=320, + ) + out, _ = capsys.readouterr() + assert "Total Characters" in out diff --git a/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary.py b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary.py new file mode 100644 index 00000000000..574b001c665 --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary.py @@ -0,0 +1,75 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# [START translate_v3_batch_translate_text_with_glossary] +from google.cloud import translate + + +def batch_translate_text_with_glossary( + input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", + output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", + project_id="YOUR_PROJECT_ID", + glossary_id="YOUR_GLOSSARY_ID", + timeout=320, +): + """Translates a batch of texts on GCS and stores the result in a GCS location. + Glossary is applied for translation.""" + + client = translate.TranslationServiceClient() + + # Supported language codes: https://cloud.google.com/translate/docs/languages + location = "us-central1" + + # Supported file types: https://cloud.google.com/translate/docs/supported-formats + gcs_source = {"input_uri": input_uri} + + input_configs_element = { + "gcs_source": gcs_source, + "mime_type": "text/plain", # Can be "text/plain" or "text/html". + } + gcs_destination = {"output_uri_prefix": output_uri} + output_config = {"gcs_destination": gcs_destination} + + parent = f"projects/{project_id}/locations/{location}" + + # glossary is a custom dictionary Translation API uses + # to translate the domain-specific terminology. + glossary_path = client.glossary_path( + project_id, "us-central1", glossary_id # The location of the glossary + ) + + glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary_path) + + glossaries = {"ja": glossary_config} # target lang as key + + operation = client.batch_translate_text( + request={ + "parent": parent, + "source_language_code": "en", + "target_language_codes": ["ja"], # Up to 10 language codes here. + "input_configs": [input_configs_element], + "glossaries": glossaries, + "output_config": output_config, + } + ) + + print("Waiting for operation to complete...") + response = operation.result(timeout) + + print("Total Characters: {}".format(response.total_characters)) + print("Translated Characters: {}".format(response.translated_characters)) + + +# [END translate_v3_batch_translate_text_with_glossary] diff --git a/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py new file mode 100644 index 00000000000..3110dcee53c --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model.py @@ -0,0 +1,77 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_batch_translate_text_with_glossary_and_model] +from google.cloud import translate + + +def batch_translate_text_with_glossary_and_model( + input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", + output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", + project_id="YOUR_PROJECT_ID", + model_id="YOUR_MODEL_ID", + glossary_id="YOUR_GLOSSARY_ID", +): + """ + Batch translate text with Glossary and Translation model + """ + + client = translate.TranslationServiceClient() + + # Supported language codes: https://cloud.google.com/translate/docs/languages + location = "us-central1" + + target_language_codes = ["ja"] + gcs_source = {"input_uri": input_uri} + + # Optional. Can be "text/plain" or "text/html". + mime_type = "text/plain" + input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type} + input_configs = [input_configs_element] + gcs_destination = {"output_uri_prefix": output_uri} + output_config = {"gcs_destination": gcs_destination} + parent = f"projects/{project_id}/locations/{location}" + model_path = "projects/{}/locations/{}/models/{}".format( + project_id, "us-central1", model_id + ) + models = {"ja": model_path} + + glossary_path = client.glossary_path( + project_id, "us-central1", glossary_id # The location of the glossary + ) + + glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary_path) + glossaries = {"ja": glossary_config} # target lang as key + + operation = client.batch_translate_text( + request={ + "parent": parent, + "source_language_code": "en", + "target_language_codes": target_language_codes, + "input_configs": input_configs, + "output_config": output_config, + "models": models, + "glossaries": glossaries, + } + ) + + print("Waiting for operation to complete...") + response = operation.result() + + # Display the translation for each input text provided + print("Total Characters: {}".format(response.total_characters)) + print("Translated Characters: {}".format(response.translated_characters)) + + +# [END translate_v3_batch_translate_text_with_glossary_and_model] diff --git a/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py new file mode 100644 index 00000000000..9c2e5610166 --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_and_model_test.py @@ -0,0 +1,50 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import uuid + +from google.cloud import storage +import pytest + +import translate_v3_batch_translate_text_with_glossary_and_model + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" +MODEL_ID = "TRL251293382528204800" + + +@pytest.fixture(scope="function") +def bucket(): + """Create a temporary bucket to store annotation output.""" + bucket_name = "test-bucket-for-glossary-" + str(uuid.uuid1()) + storage_client = storage.Client() + bucket = storage_client.create_bucket(bucket_name) + + yield bucket + + bucket.delete(force=True) + + +def test_batch_translate_text_with_glossary_and_model(capsys, bucket): + translate_v3_batch_translate_text_with_glossary_and_model.batch_translate_text_with_glossary_and_model( + "gs://cloud-samples-data/translation/text_with_custom_model_and_glossary.txt", + "gs://{}/translation/BATCH_TRANSLATION_GLOS_MODEL_OUTPUT/".format(bucket.name), + PROJECT_ID, + MODEL_ID, + GLOSSARY_ID, + ) + + out, _ = capsys.readouterr() + assert "Total Characters: 25" in out diff --git a/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py new file mode 100644 index 00000000000..adfbd7e26b9 --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text_with_glossary_test.py @@ -0,0 +1,72 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import uuid + +import backoff +from google.cloud import storage +import pytest + +import translate_v3_batch_translate_text_with_glossary + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" + + +def get_ephemeral_bucket(): + """Create a temporary bucket to store annotation output.""" + bucket_name = f"tmp-{uuid.uuid4().hex}" + storage_client = storage.Client() + bucket = storage_client.create_bucket(bucket_name) + + yield bucket + + bucket.delete(force=True) + + +@pytest.fixture(scope="function") +def bucket(): + """Create a bucket feature for testing""" + return next(get_ephemeral_bucket()) + + +def on_backoff(invocation_dict): + """Backoff callback; create a testing bucket for each backoff run""" + invocation_dict["kwargs"]["bucket"] = next(get_ephemeral_bucket()) + + +# If necessary, retry test function while backing off the timeout sequentially +MAX_TIMEOUT = 500 + + +@backoff.on_exception( + wait_gen=lambda: (wait_time for wait_time in [100, 250, 300, MAX_TIMEOUT]), + exception=Exception, + max_tries=5, + on_backoff=on_backoff, +) +def test_batch_translate_text_with_glossary(capsys, bucket): + + translate_v3_batch_translate_text_with_glossary.batch_translate_text_with_glossary( + "gs://cloud-samples-data/translation/text_with_glossary.txt", + "gs://{}/translation/BATCH_TRANSLATION_GLOS_OUTPUT/".format(bucket.name), + PROJECT_ID, + GLOSSARY_ID, + MAX_TIMEOUT, + ) + + out, _ = capsys.readouterr() + assert "Total Characters: 9" in out diff --git a/translate/samples/snippets/translate_v3_batch_translate_text_with_model.py b/translate/samples/snippets/translate_v3_batch_translate_text_with_model.py new file mode 100644 index 00000000000..a5b94866623 --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text_with_model.py @@ -0,0 +1,69 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# [START translate_v3_batch_translate_text_with_model] +from google.cloud import translate + + +def batch_translate_text_with_model( + input_uri="gs://YOUR_BUCKET_ID/path/to/your/file.txt", + output_uri="gs://YOUR_BUCKET_ID/path/to/save/results/", + project_id="YOUR_PROJECT_ID", + model_id="YOUR_MODEL_ID", +): + """Batch translate text using Translation model. + Model can be AutoML or General[built-in] model.""" + + client = translate.TranslationServiceClient() + + # Supported file types: https://cloud.google.com/translate/docs/supported-formats + gcs_source = {"input_uri": input_uri} + location = "us-central1" + + input_configs_element = { + "gcs_source": gcs_source, + "mime_type": "text/plain", # Can be "text/plain" or "text/html". + } + gcs_destination = {"output_uri_prefix": output_uri} + output_config = {"gcs_destination": gcs_destination} + parent = f"projects/{project_id}/locations/{location}" + + model_path = "projects/{}/locations/{}/models/{}".format( + project_id, location, model_id # The location of AutoML model. + ) + + # Supported language codes: https://cloud.google.com/translate/docs/languages + models = {"ja": model_path} # takes a target lang as key. + + operation = client.batch_translate_text( + request={ + "parent": parent, + "source_language_code": "en", + "target_language_codes": ["ja"], # Up to 10 language codes here. + "input_configs": [input_configs_element], + "output_config": output_config, + "models": models, + } + ) + + print("Waiting for operation to complete...") + response = operation.result() + + # Display the translation for each input text provided. + print("Total Characters: {}".format(response.total_characters)) + print("Translated Characters: {}".format(response.translated_characters)) + + +# [END translate_v3_batch_translate_text_with_model] diff --git a/translate/samples/snippets/translate_v3_batch_translate_text_with_model_test.py b/translate/samples/snippets/translate_v3_batch_translate_text_with_model_test.py new file mode 100644 index 00000000000..21ca6c33d7d --- /dev/null +++ b/translate/samples/snippets/translate_v3_batch_translate_text_with_model_test.py @@ -0,0 +1,49 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import uuid + +from google.cloud import storage +import pytest + +import translate_v3_batch_translate_text_with_model + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +MODEL_ID = "TRL251293382528204800" + + +@pytest.fixture(scope="function") +def bucket(): + """Create a temporary bucket to store annotation output.""" + bucket_name = f"tmp-{uuid.uuid4().hex}" + storage_client = storage.Client() + bucket = storage_client.create_bucket(bucket_name) + + yield bucket + + bucket.delete(force=True) + + +def test_batch_translate_text_with_model(capsys, bucket): + translate_v3_batch_translate_text_with_model.batch_translate_text_with_model( + "gs://cloud-samples-data/translation/custom_model_text.txt", + "gs://{}/translation/BATCH_TRANSLATION_MODEL_OUTPUT/".format(bucket.name), + PROJECT_ID, + MODEL_ID, + ) + out, _ = capsys.readouterr() + assert "Total Characters: 15" in out + assert "Translated Characters: 15" in out diff --git a/translate/samples/snippets/translate_v3_create_glossary.py b/translate/samples/snippets/translate_v3_create_glossary.py new file mode 100644 index 00000000000..b2fd38634cd --- /dev/null +++ b/translate/samples/snippets/translate_v3_create_glossary.py @@ -0,0 +1,60 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_create_glossary] +from google.cloud import translate_v3 as translate + + +def create_glossary( + project_id="YOUR_PROJECT_ID", + input_uri="YOUR_INPUT_URI", + glossary_id="YOUR_GLOSSARY_ID", + timeout=180, +): + """ + Create a equivalent term sets glossary. Glossary can be words or + short phrases (usually fewer than five words). + https://cloud.google.com/translate/docs/advanced/glossary#format-glossary + """ + client = translate.TranslationServiceClient() + + # Supported language codes: https://cloud.google.com/translate/docs/languages + source_lang_code = "en" + target_lang_code = "ja" + location = "us-central1" # The location of the glossary + + name = client.glossary_path(project_id, location, glossary_id) + language_codes_set = translate.types.Glossary.LanguageCodesSet( + language_codes=[source_lang_code, target_lang_code] + ) + + gcs_source = translate.types.GcsSource(input_uri=input_uri) + + input_config = translate.types.GlossaryInputConfig(gcs_source=gcs_source) + + glossary = translate.types.Glossary( + name=name, language_codes_set=language_codes_set, input_config=input_config + ) + + parent = f"projects/{project_id}/locations/{location}" + # glossary is a custom dictionary Translation API uses + # to translate the domain-specific terminology. + operation = client.create_glossary(parent=parent, glossary=glossary) + + result = operation.result(timeout) + print("Created: {}".format(result.name)) + print("Input Uri: {}".format(result.input_config.gcs_source.input_uri)) + + +# [END translate_v3_create_glossary] diff --git a/translate/samples/snippets/translate_v3_create_glossary_test.py b/translate/samples/snippets/translate_v3_create_glossary_test.py new file mode 100644 index 00000000000..3e4b61cf89e --- /dev/null +++ b/translate/samples/snippets/translate_v3_create_glossary_test.py @@ -0,0 +1,54 @@ +# 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/samples/snippets/translate_v3_delete_glossary.py b/translate/samples/snippets/translate_v3_delete_glossary.py new file mode 100644 index 00000000000..b5c55b2ba09 --- /dev/null +++ b/translate/samples/snippets/translate_v3_delete_glossary.py @@ -0,0 +1,34 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_delete_glossary] +from google.cloud import translate_v3 as translate + + +def delete_glossary( + project_id="YOUR_PROJECT_ID", + glossary_id="YOUR_GLOSSARY_ID", + timeout=180, +): + """Delete a specific glossary based on the glossary ID.""" + client = translate.TranslationServiceClient() + + name = client.glossary_path(project_id, "us-central1", glossary_id) + + operation = client.delete_glossary(name=name) + result = operation.result(timeout) + print("Deleted: {}".format(result.name)) + + +# [END translate_v3_delete_glossary] diff --git a/translate/samples/snippets/translate_v3_delete_glossary_test.py b/translate/samples/snippets/translate_v3_delete_glossary_test.py new file mode 100644 index 00000000000..a88c16a37a2 --- /dev/null +++ b/translate/samples/snippets/translate_v3_delete_glossary_test.py @@ -0,0 +1,38 @@ +# 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/samples/snippets/translate_v3_detect_language.py b/translate/samples/snippets/translate_v3_detect_language.py new file mode 100644 index 00000000000..eae985c3d63 --- /dev/null +++ b/translate/samples/snippets/translate_v3_detect_language.py @@ -0,0 +1,45 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_detect_language] +from google.cloud import translate + + +def detect_language(project_id="YOUR_PROJECT_ID"): + """Detecting the language of a text string.""" + + client = translate.TranslationServiceClient() + + location = "global" + + parent = f"projects/{project_id}/locations/{location}" + + # Detail on supported types can be found here: + # https://cloud.google.com/translate/docs/supported-formats + response = client.detect_language( + content="Hello, world!", + parent=parent, + mime_type="text/plain", # mime types: text/plain, text/html + ) + + # Display list of detected languages sorted by detection confidence. + # The most probable language is first. + for language in response.languages: + # The language detected + print("Language code: {}".format(language.language_code)) + # Confidence of detection result for this language + print("Confidence: {}".format(language.confidence)) + + +# [END translate_v3_detect_language] diff --git a/translate/samples/snippets/translate_v3_detect_language_test.py b/translate/samples/snippets/translate_v3_detect_language_test.py new file mode 100644 index 00000000000..7e9540cca00 --- /dev/null +++ b/translate/samples/snippets/translate_v3_detect_language_test.py @@ -0,0 +1,26 @@ +# 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/samples/snippets/translate_v3_get_glossary.py b/translate/samples/snippets/translate_v3_get_glossary.py new file mode 100644 index 00000000000..f4c2aaae460 --- /dev/null +++ b/translate/samples/snippets/translate_v3_get_glossary.py @@ -0,0 +1,32 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_get_glossary] +from google.cloud import translate_v3 as translate + + +def get_glossary(project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID"): + """Get a particular glossary based on the glossary ID.""" + + client = translate.TranslationServiceClient() + + name = client.glossary_path(project_id, "us-central1", glossary_id) + + response = client.get_glossary(name=name) + print(u"Glossary name: {}".format(response.name)) + print(u"Entry count: {}".format(response.entry_count)) + print(u"Input URI: {}".format(response.input_config.gcs_source.input_uri)) + + +# [END translate_v3_get_glossary] diff --git a/translate/samples/snippets/translate_v3_get_glossary_test.py b/translate/samples/snippets/translate_v3_get_glossary_test.py new file mode 100644 index 00000000000..9dfa5e81c47 --- /dev/null +++ b/translate/samples/snippets/translate_v3_get_glossary_test.py @@ -0,0 +1,27 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import translate_v3_get_glossary + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" + + +def test_get_glossary(capsys): + translate_v3_get_glossary.get_glossary(PROJECT_ID, GLOSSARY_ID) + out, _ = capsys.readouterr() + assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/translate/samples/snippets/translate_v3_get_supported_languages.py b/translate/samples/snippets/translate_v3_get_supported_languages.py new file mode 100644 index 00000000000..3802a7e1422 --- /dev/null +++ b/translate/samples/snippets/translate_v3_get_supported_languages.py @@ -0,0 +1,35 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_get_supported_languages] +from google.cloud import translate + + +def get_supported_languages(project_id="YOUR_PROJECT_ID"): + """Getting a list of supported language codes.""" + + client = translate.TranslationServiceClient() + + parent = f"projects/{project_id}" + + # Supported language codes: https://cloud.google.com/translate/docs/languages + response = client.get_supported_languages(parent=parent) + + # List language codes of supported languages. + print("Supported Languages:") + for language in response.languages: + print("Language Code: {}".format(language.language_code)) + + +# [END translate_v3_get_supported_languages] diff --git a/translate/samples/snippets/translate_v3_get_supported_languages_test.py b/translate/samples/snippets/translate_v3_get_supported_languages_test.py new file mode 100644 index 00000000000..3a1a9be4cd7 --- /dev/null +++ b/translate/samples/snippets/translate_v3_get_supported_languages_test.py @@ -0,0 +1,26 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import translate_v3_get_supported_languages + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] + + +def test_list_languages(capsys): + translate_v3_get_supported_languages.get_supported_languages(PROJECT_ID) + out, _ = capsys.readouterr() + assert "zh" in out diff --git a/translate/samples/snippets/translate_v3_get_supported_languages_with_target.py b/translate/samples/snippets/translate_v3_get_supported_languages_with_target.py new file mode 100644 index 00000000000..856b317846a --- /dev/null +++ b/translate/samples/snippets/translate_v3_get_supported_languages_with_target.py @@ -0,0 +1,38 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_get_supported_languages_for_target] +from google.cloud import translate + + +def get_supported_languages_with_target(project_id="YOUR_PROJECT_ID"): + """Listing supported languages with target language name.""" + + client = translate.TranslationServiceClient() + + location = "global" + + parent = f"projects/{project_id}/locations/{location}" + + # Supported language codes: https://cloud.google.com/translate/docs/languages + response = client.get_supported_languages( + display_language_code="is", parent=parent # target language code + ) + # List language codes of supported languages + for language in response.languages: + print("Language Code: {}".format(language.language_code)) + print("Display Name: {}".format(language.display_name)) + + +# [END translate_v3_get_supported_languages_for_target] diff --git a/translate/samples/snippets/translate_v3_get_supported_languages_with_target_test.py b/translate/samples/snippets/translate_v3_get_supported_languages_with_target_test.py new file mode 100644 index 00000000000..9688efee3b9 --- /dev/null +++ b/translate/samples/snippets/translate_v3_get_supported_languages_with_target_test.py @@ -0,0 +1,27 @@ +# 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/samples/snippets/translate_v3_list_glossary.py b/translate/samples/snippets/translate_v3_list_glossary.py new file mode 100644 index 00000000000..934194b4443 --- /dev/null +++ b/translate/samples/snippets/translate_v3_list_glossary.py @@ -0,0 +1,42 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_list_glossary] +from google.cloud import translate + + +def list_glossaries(project_id="YOUR_PROJECT_ID"): + """List Glossaries.""" + + client = translate.TranslationServiceClient() + + location = "us-central1" + + parent = f"projects/{project_id}/locations/{location}" + + # Iterate over all results + for glossary in client.list_glossaries(parent=parent): + print("Name: {}".format(glossary.name)) + print("Entry count: {}".format(glossary.entry_count)) + print("Input uri: {}".format(glossary.input_config.gcs_source.input_uri)) + + # Note: You can create a glossary using one of two modes: + # language_code_set or language_pair. When listing the information for + # a glossary, you can only get information for the mode you used + # when creating the glossary. + for language_code in glossary.language_codes_set.language_codes: + print("Language code: {}".format(language_code)) + + +# [END translate_v3_list_glossary] diff --git a/translate/samples/snippets/translate_v3_list_glossary_test.py b/translate/samples/snippets/translate_v3_list_glossary_test.py new file mode 100644 index 00000000000..9d87f0cda9b --- /dev/null +++ b/translate/samples/snippets/translate_v3_list_glossary_test.py @@ -0,0 +1,28 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import translate_v3_list_glossary + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" + + +def test_list_glossary(capsys): + translate_v3_list_glossary.list_glossaries(PROJECT_ID) + out, _ = capsys.readouterr() + assert GLOSSARY_ID in out + assert "gs://cloud-samples-data/translation/glossary_ja.csv" in out diff --git a/translate/samples/snippets/translate_v3_translate_text.py b/translate/samples/snippets/translate_v3_translate_text.py new file mode 100644 index 00000000000..5ea69a73e84 --- /dev/null +++ b/translate/samples/snippets/translate_v3_translate_text.py @@ -0,0 +1,48 @@ +# 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] +# Imports the Google Cloud Translation library +from google.cloud import translate + + +# Initialize Translation client +def translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): + """Translating Text.""" + + client = translate.TranslationServiceClient() + + location = "global" + + parent = f"projects/{project_id}/locations/{location}" + + # Translate text from English to French + # Detail on supported types can be found here: + # https://cloud.google.com/translate/docs/supported-formats + response = client.translate_text( + request={ + "parent": parent, + "contents": [text], + "mime_type": "text/plain", # mime types: text/plain, text/html + "source_language_code": "en-US", + "target_language_code": "fr", + } + ) + + # Display the translation for each input text provided + for translation in response.translations: + print("Translated text: {}".format(translation.translated_text)) + + +# [END translate_v3_translate_text] diff --git a/translate/samples/snippets/translate_v3_translate_text_test.py b/translate/samples/snippets/translate_v3_translate_text_test.py new file mode 100644 index 00000000000..fee6e771566 --- /dev/null +++ b/translate/samples/snippets/translate_v3_translate_text_test.py @@ -0,0 +1,26 @@ +# 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/samples/snippets/translate_v3_translate_text_with_glossary.py b/translate/samples/snippets/translate_v3_translate_text_with_glossary.py new file mode 100644 index 00000000000..addd737f6ad --- /dev/null +++ b/translate/samples/snippets/translate_v3_translate_text_with_glossary.py @@ -0,0 +1,53 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_translate_text_with_glossary] + +from google.cloud import translate + + +def translate_text_with_glossary( + text="YOUR_TEXT_TO_TRANSLATE", + project_id="YOUR_PROJECT_ID", + glossary_id="YOUR_GLOSSARY_ID", +): + """Translates a given text using a glossary.""" + + client = translate.TranslationServiceClient() + location = "us-central1" + parent = f"projects/{project_id}/locations/{location}" + + glossary = client.glossary_path( + project_id, "us-central1", glossary_id # The location of the glossary + ) + + glossary_config = translate.TranslateTextGlossaryConfig(glossary=glossary) + + # Supported language codes: https://cloud.google.com/translate/docs/languages + response = client.translate_text( + request={ + "contents": [text], + "target_language_code": "ja", + "source_language_code": "en", + "parent": parent, + "glossary_config": glossary_config, + } + ) + + print("Translated text: \n") + for translation in response.glossary_translations: + print("\t {}".format(translation.translated_text)) + + +# [END translate_v3_translate_text_with_glossary] diff --git a/translate/samples/snippets/translate_v3_translate_text_with_glossary_test.py b/translate/samples/snippets/translate_v3_translate_text_with_glossary_test.py new file mode 100644 index 00000000000..ef5a90823d0 --- /dev/null +++ b/translate/samples/snippets/translate_v3_translate_text_with_glossary_test.py @@ -0,0 +1,30 @@ +# -*- encoding: utf-8 -*- +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import translate_v3_translate_text_with_glossary + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +GLOSSARY_ID = "DO_NOT_DELETE_TEST_GLOSSARY" + + +def test_translate_text_with_glossary(capsys): + translate_v3_translate_text_with_glossary.translate_text_with_glossary( + "account", PROJECT_ID, GLOSSARY_ID + ) + out, _ = capsys.readouterr() + assert "アカウント" or "口座" in out diff --git a/translate/samples/snippets/translate_v3_translate_text_with_model.py b/translate/samples/snippets/translate_v3_translate_text_with_model.py new file mode 100644 index 00000000000..bd61bb76910 --- /dev/null +++ b/translate/samples/snippets/translate_v3_translate_text_with_model.py @@ -0,0 +1,49 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_translate_text_with_model] + +from google.cloud import translate + + +def translate_text_with_model( + text="YOUR_TEXT_TO_TRANSLATE", + project_id="YOUR_PROJECT_ID", + model_id="YOUR_MODEL_ID", +): + """Translates a given text using Translation custom model.""" + + client = translate.TranslationServiceClient() + + location = "us-central1" + parent = f"projects/{project_id}/locations/{location}" + model_path = f"{parent}/models/{model_id}" + + # Supported language codes: https://cloud.google.com/translate/docs/languages + response = client.translate_text( + request={ + "contents": [text], + "target_language_code": "ja", + "model": model_path, + "source_language_code": "en", + "parent": parent, + "mime_type": "text/plain", # mime types: text/plain, text/html + } + ) + # Display the translation for each input text provided + for translation in response.translations: + print("Translated text: {}".format(translation.translated_text)) + + +# [END translate_v3_translate_text_with_model] diff --git a/translate/samples/snippets/translate_v3_translate_text_with_model_test.py b/translate/samples/snippets/translate_v3_translate_text_with_model_test.py new file mode 100644 index 00000000000..e547c8ce29e --- /dev/null +++ b/translate/samples/snippets/translate_v3_translate_text_with_model_test.py @@ -0,0 +1,30 @@ +# -*- encoding: utf-8 -*- +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os + +import translate_v3_translate_text_with_model + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] +MODEL_ID = "TRL251293382528204800" + + +def test_translate_text_with_model(capsys): + translate_v3_translate_text_with_model.translate_text_with_model( + "That' il do it.", PROJECT_ID, MODEL_ID + ) + out, _ = capsys.readouterr() + assert "それはそうだ" or "それじゃあ" in out diff --git a/translate/samples/snippets/translate_v3beta1_batch_translate_document.py b/translate/samples/snippets/translate_v3beta1_batch_translate_document.py new file mode 100644 index 00000000000..825bda14bfb --- /dev/null +++ b/translate/samples/snippets/translate_v3beta1_batch_translate_document.py @@ -0,0 +1,62 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3beta1_batch_translate_document] + +from google.cloud import translate_v3beta1 as translate + + +def batch_translate_document( + input_uri: str, + output_uri: str, + project_id: str, + timeout=180, +): + + client = translate.TranslationServiceClient() + + # The ``global`` location is not supported for batch translation + location = "us-central1" + + # Google Cloud Storage location for the source input. This can be a single file + # (for example, ``gs://translation-test/input.docx``) or a wildcard + # (for example, ``gs://translation-test/*``). + # Supported file types: https://cloud.google.com/translate/docs/supported-formats + gcs_source = {"input_uri": input_uri} + + batch_document_input_configs = { + "gcs_source": gcs_source, + } + gcs_destination = {"output_uri_prefix": output_uri} + batch_document_output_config = {"gcs_destination": gcs_destination} + parent = f"projects/{project_id}/locations/{location}" + + # Supported language codes: https://cloud.google.com/translate/docs/language + operation = client.batch_translate_document( + request={ + "parent": parent, + "source_language_code": "en-US", + "target_language_codes": ["fr-FR"], + "input_configs": [batch_document_input_configs], + "output_config": batch_document_output_config, + } + ) + + print("Waiting for operation to complete...") + response = operation.result(timeout) + + print("Total Pages: {}".format(response.total_pages)) + + +# [END translate_v3beta1_batch_translate_document] diff --git a/translate/samples/snippets/translate_v3beta1_batch_translate_document_test.py b/translate/samples/snippets/translate_v3beta1_batch_translate_document_test.py new file mode 100644 index 00000000000..3654f85cf88 --- /dev/null +++ b/translate/samples/snippets/translate_v3beta1_batch_translate_document_test.py @@ -0,0 +1,52 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import os +import uuid + +from google.cloud import storage +import pytest + + +import translate_v3beta1_batch_translate_document + + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] + + +@pytest.fixture(scope="function") +def bucket(): + # Create a temporary bucket to store annotation output. + bucket_name = "test-{}".format(uuid.uuid4()) + storage_client = storage.Client() + bucket = storage_client.bucket(bucket_name) + bucket = storage_client.create_bucket(bucket, location="us-central1") + + yield bucket + + bucket.delete(force=True) + + +@pytest.mark.flaky(max_runs=3, min_passes=1) +def test_batch_translate_document(capsys, bucket): + translate_v3beta1_batch_translate_document.batch_translate_document( + input_uri="gs://cloud-samples-data/translation/async_invoices/*", + output_uri=f"gs://{bucket.name}/translation/BATCH_TRANSLATE_DOCUMENT_OUTPUT/", + project_id=PROJECT_ID, + timeout=1000, + ) + + out, _ = capsys.readouterr() + assert "Total Pages" in out diff --git a/translate/samples/snippets/translate_v3beta1_translate_document.py b/translate/samples/snippets/translate_v3beta1_translate_document.py new file mode 100644 index 00000000000..23b14f3f73d --- /dev/null +++ b/translate/samples/snippets/translate_v3beta1_translate_document.py @@ -0,0 +1,54 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3beta1_translate_document] +from google.cloud import translate_v3beta1 as translate + + +def translate_document(project_id: str, file_path: str): + + client = translate.TranslationServiceClient() + + location = "us-central1" + + parent = f"projects/{project_id}/locations/{location}" + + # Supported file types: https://cloud.google.com/translate/docs/supported-formats + with open(file_path, "rb") as document: + document_content = document.read() + + document_input_config = { + "content": document_content, + "mime_type": "application/pdf", + } + + response = client.translate_document( + request={ + "parent": parent, + "target_language_code": "fr-FR", + "document_input_config": document_input_config, + } + ) + + # To output the translated document, uncomment the code below. + # f = open('/tmp/output', 'wb') + # f.write(response.document_translation.byte_stream_outputs[0]) + # f.close() + + # If not provided in the TranslationRequest, the translated file will only be returned through a byte-stream + # and its output mime type will be the same as the input file's mime type + print("Response: Detected Language Code - {}".format(response.document_translation.detected_language_code)) + + +# [END translate_v3beta1_translate_document] diff --git a/translate/samples/snippets/translate_v3beta1_translate_document_test.py b/translate/samples/snippets/translate_v3beta1_translate_document_test.py new file mode 100644 index 00000000000..9065702f2c9 --- /dev/null +++ b/translate/samples/snippets/translate_v3beta1_translate_document_test.py @@ -0,0 +1,28 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import os + +import translate_v3beta1_translate_document + +PROJECT_ID = os.environ["GOOGLE_CLOUD_PROJECT"] + +FILE_PATH = "resources/fake_invoice.pdf" + + +def test_translate_document(capsys): + translate_v3beta1_translate_document.translate_document(project_id=PROJECT_ID, file_path=FILE_PATH) + out, _ = capsys.readouterr() + assert "Response" in out