Skip to content

Commit

Permalink
translate-v3: samples (#3034)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
munkhuushmgl and leahecole committed Mar 16, 2020
1 parent e8266a9 commit b00f00d
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions translate/cloud-client/translate_v3_translate_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion translate/cloud-client/translate_v3_translate_text_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 48 additions & 0 deletions translate/cloud-client/translate_v3_translate_text_with_model.py
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b00f00d

Please sign in to comment.