From b00f00d734b89edae8ae6876d6261e19dc82cd34 Mon Sep 17 00:00:00 2001 From: Mike <45373284+munkhuushmgl@users.noreply.github.com> Date: Mon, 16 Mar 2020 12:14:19 -0700 Subject: [PATCH] translate-v3: samples (#3034) * translate with custom model, get supported langs * inlined small nit * added encoding to model test * added missing region tags and link to supported langs * inlined text-to-translate * directly inlined contents * revert text-translate vars * reversed inlined text params * small nit Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com> --- .../translate_v3_get_supported_languages.py | 1 + ..._v3_get_supported_languages_with_target.py | 35 ++++++++++++++ ...et_supported_languages_with_target_test.py | 27 +++++++++++ .../translate_v3_translate_text.py | 4 +- .../translate_v3_translate_text_test.py | 3 +- ...anslate_v3_translate_text_with_glossary.py | 9 ++-- .../translate_v3_translate_text_with_model.py | 48 +++++++++++++++++++ ...slate_v3_translate_text_with_model_test.py | 28 +++++++++++ 8 files changed, 147 insertions(+), 8 deletions(-) create mode 100644 translate/cloud-client/translate_v3_get_supported_languages_with_target.py create mode 100644 translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py create mode 100644 translate/cloud-client/translate_v3_translate_text_with_model.py create mode 100644 translate/cloud-client/translate_v3_translate_text_with_model_test.py diff --git a/translate/cloud-client/translate_v3_get_supported_languages.py b/translate/cloud-client/translate_v3_get_supported_languages.py index 9a2df58dacce..eb7d83666a43 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages.py +++ b/translate/cloud-client/translate_v3_get_supported_languages.py @@ -23,6 +23,7 @@ def sample_get_supported_languages(project_id="YOUR_PROJECT_ID"): parent = client.location_path(project_id, "global") + # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.get_supported_languages(parent=parent) # List language codes of supported languages. diff --git a/translate/cloud-client/translate_v3_get_supported_languages_with_target.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target.py new file mode 100644 index 000000000000..f71138b533e4 --- /dev/null +++ b/translate/cloud-client/translate_v3_get_supported_languages_with_target.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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START translate_v3_get_supported_languages_for_target] +from google.cloud import translate + + +def get_supported_languages_with_target(project_id="YOUR_PROJECT_ID"): + """Listing supported languages with target language name.""" + + client = translate.TranslationServiceClient() + parent = client.location_path(project_id, "global") + + # Supported language codes: https://cloud.google.com/translate/docs/languages + response = client.get_supported_languages( + display_language_code="is", # target language code + parent=parent + ) + # List language codes of supported languages + for language in response.languages: + print(u"Language Code: {}".format(language.language_code)) + print(u"Display Name: {}".format(language.display_name)) + +# [END translate_v3_get_supported_languages_for_target] diff --git a/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py new file mode 100644 index 000000000000..aa40efe4bb7a --- /dev/null +++ b/translate/cloud-client/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["GCLOUD_PROJECT"] + + +def test_list_languages_with_target(capsys): + get_supported_langs.get_supported_languages_with_target( + PROJECT_ID + ) + out, _ = capsys.readouterr() + assert u"Language Code: sq" in out + assert u"Display Name: albanska" in out diff --git a/translate/cloud-client/translate_v3_translate_text.py b/translate/cloud-client/translate_v3_translate_text.py index c5034c94e845..77fcec26deeb 100644 --- a/translate/cloud-client/translate_v3_translate_text.py +++ b/translate/cloud-client/translate_v3_translate_text.py @@ -16,7 +16,7 @@ from google.cloud import translate -def sample_translate_text(project_id="YOUR_PROJECT_ID"): +def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID"): """Translating Text.""" client = translate.TranslationServiceClient() @@ -27,7 +27,7 @@ def sample_translate_text(project_id="YOUR_PROJECT_ID"): # https://cloud.google.com/translate/docs/supported-formats response = client.translate_text( parent=parent, - contents=["Hello, world!"], + contents=[text], mime_type="text/plain", # mime types: text/plain, text/html source_language_code="en-US", target_language_code="fr", diff --git a/translate/cloud-client/translate_v3_translate_text_test.py b/translate/cloud-client/translate_v3_translate_text_test.py index e4f8acfb14aa..a63190197818 100644 --- a/translate/cloud-client/translate_v3_translate_text_test.py +++ b/translate/cloud-client/translate_v3_translate_text_test.py @@ -19,6 +19,7 @@ def test_translate_text(capsys): - translate_v3_translate_text.sample_translate_text(PROJECT_ID) + translate_v3_translate_text.sample_translate_text( + "Hello World!", PROJECT_ID) out, _ = capsys.readouterr() assert "Bonjour le monde" in out diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary.py b/translate/cloud-client/translate_v3_translate_text_with_glossary.py index cc4da00576e3..ca0eeaccced9 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary.py @@ -20,24 +20,23 @@ def translate_text_with_glossary( text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID", - glossary_id="YOUR_GLOSSARY_ID" + glossary_id="YOUR_GLOSSARY_ID", ): """Translates a given text using a glossary.""" client = translate_v3.TranslationServiceClient() - - contents = [text] parent = client.location_path(project_id, "us-central1") glossary = client.glossary_path( project_id, "us-central1", glossary_id # The location of the glossary ) - glossary_config = translate_v3.types.TranslateTextGlossaryConfig(glossary=glossary) + glossary_config = translate_v3.types.TranslateTextGlossaryConfig( + glossary=glossary) # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.translate_text( - contents, + contents=[text], target_language_code="ja", source_language_code="en", parent=parent, diff --git a/translate/cloud-client/translate_v3_translate_text_with_model.py b/translate/cloud-client/translate_v3_translate_text_with_model.py new file mode 100644 index 000000000000..8a0b0bb7825a --- /dev/null +++ b/translate/cloud-client/translate_v3_translate_text_with_model.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. + +# [START translate_v3_translate_text_with_model] + +from google.cloud import translate + + +def translate_text_with_model( + text="YOUR_TEXT_TO_TRANSLATE", + project_id="YOUR_PROJECT_ID", + model_id="YOUR_MODEL_ID", +): + """Translates a given text using Translation custom model.""" + + client = translate.TranslationServiceClient() + + parent = client.location_path(project_id, "us-central1") + model_path = "projects/{}/locations/{}/models/{}".format( + project_id, "us-central1", model_id + ) + + # Supported language codes: https://cloud.google.com/translate/docs/languages + response = client.translate_text( + contents=[text], + target_language_code="ja", + model=model_path, + source_language_code="en", + parent=parent, + mime_type="text/plain", # mime types: text/plain, text/html + ) + # Display the translation for each input text provided + for translation in response.translations: + print(u"Translated text: {}".format(translation.translated_text)) + + +# [END translate_v3_translate_text_with_model] diff --git a/translate/cloud-client/translate_v3_translate_text_with_model_test.py b/translate/cloud-client/translate_v3_translate_text_with_model_test.py new file mode 100644 index 000000000000..6e6ab37eee16 --- /dev/null +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -0,0 +1,28 @@ +# -*- 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["GCLOUD_PROJECT"] +MODEL_ID = "TRL3128559826197068699" + + +def test_translate_text_with_model(capsys): + translate_v3_translate_text_with_model.translate_text_with_model( + "That' il do it.", PROJECT_ID, MODEL_ID + ) + out, _ = capsys.readouterr() + assert "それはそうだ" or "それじゃあ" in out