From dd3e34e112084231833ddd14e139ff2b235163a5 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Wed, 4 Mar 2020 17:40:18 -0800 Subject: [PATCH 1/9] translate with custom model, get supported langs --- ..._v3_get_supported_languages_with_target.py | 32 ++++++++++++ ...et_supported_languages_with_target_test.py | 27 ++++++++++ .../translate_v3_translate_text_with_model.py | 49 +++++++++++++++++++ ...slate_v3_translate_text_with_model_test.py | 27 ++++++++++ 4 files changed, 135 insertions(+) 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_with_target.py b/translate/cloud-client/translate_v3_get_supported_languages_with_target.py new file mode 100644 index 000000000000..de7824306c55 --- /dev/null +++ b/translate/cloud-client/translate_v3_get_supported_languages_with_target.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 +# +# 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. + +from google.cloud import translate + + +def get_supported_languages_with_target( + language_code="YOUR_TARGET_LANG_CODE", project_id="YOUR_PROJECT_ID" +): + """Listing supported languages with target language name.""" + + client = translate.TranslationServiceClient() + parent = client.location_path(project_id, "global") + + response = client.get_supported_languages( + display_language_code=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)) 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..1a1a8e2377d8 --- /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( + "is", 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_with_model.py b/translate/cloud-client/translate_v3_translate_text_with_model.py new file mode 100644 index 000000000000..15aa0ff4e713 --- /dev/null +++ b/translate/cloud-client/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() + + contents = [text] + 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, + 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..c466e15b0100 --- /dev/null +++ b/translate/cloud-client/translate_v3_translate_text_with_model_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_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 From 85af59b2ee1d3a63e6c35b28cef1f5f39022d51d Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Wed, 4 Mar 2020 17:46:12 -0800 Subject: [PATCH 2/9] inlined small nit --- .../cloud-client/translate_v3_translate_text_with_model.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/translate/cloud-client/translate_v3_translate_text_with_model.py b/translate/cloud-client/translate_v3_translate_text_with_model.py index 15aa0ff4e713..8a0b0bb7825a 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model.py @@ -26,7 +26,6 @@ def translate_text_with_model( client = translate.TranslationServiceClient() - contents = [text] parent = client.location_path(project_id, "us-central1") model_path = "projects/{}/locations/{}/models/{}".format( project_id, "us-central1", model_id @@ -34,7 +33,7 @@ def translate_text_with_model( # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.translate_text( - contents, + contents=[text], target_language_code="ja", model=model_path, source_language_code="en", From 1a22c4fba8ca076613d5fb69331abe259d9d2dbf Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Wed, 4 Mar 2020 18:00:03 -0800 Subject: [PATCH 3/9] added encoding to model test --- .../cloud-client/translate_v3_translate_text_with_model_test.py | 1 + 1 file changed, 1 insertion(+) 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 index c466e15b0100..6e6ab37eee16 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); From 81bb4c8542b33a76a6f30f53b1f27abef16580eb Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 5 Mar 2020 08:09:14 -0800 Subject: [PATCH 4/9] added missing region tags and link to supported langs --- .../translate_v3_get_supported_languages.py | 1 + ...ranslate_v3_get_supported_languages_with_target.py | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) 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 index de7824306c55..f71138b533e4 100644 --- a/translate/cloud-client/translate_v3_get_supported_languages_with_target.py +++ b/translate/cloud-client/translate_v3_get_supported_languages_with_target.py @@ -12,21 +12,24 @@ # 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( - language_code="YOUR_TARGET_LANG_CODE", project_id="YOUR_PROJECT_ID" -): +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=language_code, parent=parent + 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] From da4401c5d88a3c83dfdc91ef6302ef123995e11e Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Mon, 9 Mar 2020 12:51:29 -0700 Subject: [PATCH 5/9] inlined text-to-translate --- .../translate_v3_translate_text_with_glossary.py | 11 +++++------ .../translate_v3_translate_text_with_glossary_test.py | 2 +- .../translate_v3_translate_text_with_model.py | 4 ++-- .../translate_v3_translate_text_with_model_test.py | 3 +-- 4 files changed, 9 insertions(+), 11 deletions(-) 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..99c6ab217039 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary.py @@ -18,26 +18,25 @@ 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] + text_to_translate = "account" 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_to_translate], target_language_code="ja", source_language_code="en", parent=parent, diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py index 72f9d64e2f8d..c4ca7a9bbbec 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py @@ -42,7 +42,7 @@ def glossary(): def test_translate_text_with_glossary(capsys, glossary): translate_v3_translate_text_with_glossary.translate_text_with_glossary( - "account", PROJECT_ID, glossary + PROJECT_ID, glossary ) out, _ = capsys.readouterr() assert "アカウント" or "口座" in out diff --git a/translate/cloud-client/translate_v3_translate_text_with_model.py b/translate/cloud-client/translate_v3_translate_text_with_model.py index 8a0b0bb7825a..c77a631d1626 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model.py @@ -18,7 +18,6 @@ def translate_text_with_model( - text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID", model_id="YOUR_MODEL_ID", ): @@ -32,8 +31,9 @@ def translate_text_with_model( ) # Supported language codes: https://cloud.google.com/translate/docs/languages + text_to_translate = "That' il do it." response = client.translate_text( - contents=[text], + contents=[text_to_translate], target_language_code="ja", model=model_path, source_language_code="en", 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 index 6e6ab37eee16..ba40d116b70d 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -1,4 +1,3 @@ -# -*- encoding: utf-8 -*- # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,7 +21,7 @@ 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 + PROJECT_ID, MODEL_ID ) out, _ = capsys.readouterr() assert "それはそうだ" or "それじゃあ" in out From 8f5a3d767cca5f0e852ef7ca9ded71fc0337a2d2 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Mon, 9 Mar 2020 13:38:27 -0700 Subject: [PATCH 6/9] directly inlined contents --- .../cloud-client/translate_v3_translate_text_with_glossary.py | 3 +-- .../cloud-client/translate_v3_translate_text_with_model.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) 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 99c6ab217039..ca34895389d9 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary.py @@ -24,7 +24,6 @@ def translate_text_with_glossary( """Translates a given text using a glossary.""" client = translate_v3.TranslationServiceClient() - text_to_translate = "account" parent = client.location_path(project_id, "us-central1") glossary = client.glossary_path( @@ -36,7 +35,7 @@ def translate_text_with_glossary( # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.translate_text( - contents=[text_to_translate], + contents=["account"], 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 index c77a631d1626..19a3fd069aa3 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model.py @@ -31,9 +31,8 @@ def translate_text_with_model( ) # Supported language codes: https://cloud.google.com/translate/docs/languages - text_to_translate = "That' il do it." response = client.translate_text( - contents=[text_to_translate], + contents=["That' il do it."], target_language_code="ja", model=model_path, source_language_code="en", From 8afb5cea29722c4dff6aec865f980c47f31e6827 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Tue, 10 Mar 2020 22:53:25 -0700 Subject: [PATCH 7/9] revert text-translate vars --- .../translate_v3_get_supported_languages_with_target_test.py | 2 +- .../cloud-client/translate_v3_translate_text_with_glossary.py | 3 ++- .../cloud-client/translate_v3_translate_text_with_model.py | 3 ++- .../translate_v3_translate_text_with_model_test.py | 1 + 4 files changed, 6 insertions(+), 3 deletions(-) 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 index 1a1a8e2377d8..aa40efe4bb7a 100644 --- 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 @@ -20,7 +20,7 @@ def test_list_languages_with_target(capsys): get_supported_langs.get_supported_languages_with_target( - "is", PROJECT_ID + PROJECT_ID ) out, _ = capsys.readouterr() assert u"Language Code: sq" 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 ca34895389d9..99c6ab217039 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary.py @@ -24,6 +24,7 @@ def translate_text_with_glossary( """Translates a given text using a glossary.""" client = translate_v3.TranslationServiceClient() + text_to_translate = "account" parent = client.location_path(project_id, "us-central1") glossary = client.glossary_path( @@ -35,7 +36,7 @@ def translate_text_with_glossary( # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.translate_text( - contents=["account"], + contents=[text_to_translate], 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 index 19a3fd069aa3..c77a631d1626 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model.py @@ -31,8 +31,9 @@ def translate_text_with_model( ) # Supported language codes: https://cloud.google.com/translate/docs/languages + text_to_translate = "That' il do it." response = client.translate_text( - contents=["That' il do it."], + contents=[text_to_translate], target_language_code="ja", model=model_path, source_language_code="en", 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 index ba40d116b70d..45cdffdb9942 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -1,3 +1,4 @@ +# -*- encoding: utf-8 -*- # Copyright 2020 Google LLC # # Licensed under the Apache License, Version 2.0 (the "License"); From ba63302a7d6059607a49ed6482665d585a512403 Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 12 Mar 2020 11:04:43 -0700 Subject: [PATCH 8/9] reversed inlined text params --- translate/cloud-client/translate_v3_translate_text.py | 2 +- translate/cloud-client/translate_v3_translate_text_test.py | 3 ++- .../cloud-client/translate_v3_translate_text_with_glossary.py | 4 ++-- .../translate_v3_translate_text_with_glossary_test.py | 2 +- .../cloud-client/translate_v3_translate_text_with_model.py | 4 ++-- .../translate_v3_translate_text_with_model_test.py | 2 +- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/translate/cloud-client/translate_v3_translate_text.py b/translate/cloud-client/translate_v3_translate_text.py index c5034c94e845..6e33955fb1e6 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() 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 99c6ab217039..ca0eeaccced9 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary.py @@ -18,13 +18,13 @@ def translate_text_with_glossary( + text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID", glossary_id="YOUR_GLOSSARY_ID", ): """Translates a given text using a glossary.""" client = translate_v3.TranslationServiceClient() - text_to_translate = "account" parent = client.location_path(project_id, "us-central1") glossary = client.glossary_path( @@ -36,7 +36,7 @@ def translate_text_with_glossary( # Supported language codes: https://cloud.google.com/translate/docs/languages response = client.translate_text( - contents=[text_to_translate], + contents=[text], target_language_code="ja", source_language_code="en", parent=parent, diff --git a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py index c4ca7a9bbbec..72f9d64e2f8d 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_glossary_test.py @@ -42,7 +42,7 @@ def glossary(): def test_translate_text_with_glossary(capsys, glossary): translate_v3_translate_text_with_glossary.translate_text_with_glossary( - PROJECT_ID, glossary + "account", PROJECT_ID, glossary ) out, _ = capsys.readouterr() assert "アカウント" or "口座" in out diff --git a/translate/cloud-client/translate_v3_translate_text_with_model.py b/translate/cloud-client/translate_v3_translate_text_with_model.py index c77a631d1626..8a0b0bb7825a 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model.py @@ -18,6 +18,7 @@ def translate_text_with_model( + text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJECT_ID", model_id="YOUR_MODEL_ID", ): @@ -31,9 +32,8 @@ def translate_text_with_model( ) # Supported language codes: https://cloud.google.com/translate/docs/languages - text_to_translate = "That' il do it." response = client.translate_text( - contents=[text_to_translate], + contents=[text], target_language_code="ja", model=model_path, source_language_code="en", 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 index 45cdffdb9942..6e6ab37eee16 100644 --- a/translate/cloud-client/translate_v3_translate_text_with_model_test.py +++ b/translate/cloud-client/translate_v3_translate_text_with_model_test.py @@ -22,7 +22,7 @@ def test_translate_text_with_model(capsys): translate_v3_translate_text_with_model.translate_text_with_model( - PROJECT_ID, MODEL_ID + "That' il do it.", PROJECT_ID, MODEL_ID ) out, _ = capsys.readouterr() assert "それはそうだ" or "それじゃあ" in out From 8a317a939314db4d6066ba42a42a1c94af7cf80d Mon Sep 17 00:00:00 2001 From: Mike Ganbold Date: Thu, 12 Mar 2020 11:08:27 -0700 Subject: [PATCH 9/9] small nit --- translate/cloud-client/translate_v3_translate_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/translate/cloud-client/translate_v3_translate_text.py b/translate/cloud-client/translate_v3_translate_text.py index 6e33955fb1e6..77fcec26deeb 100644 --- a/translate/cloud-client/translate_v3_translate_text.py +++ b/translate/cloud-client/translate_v3_translate_text.py @@ -27,7 +27,7 @@ def sample_translate_text(text="YOUR_TEXT_TO_TRANSLATE", project_id="YOUR_PROJEC # 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",