-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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 <[email protected]>
- Loading branch information
1 parent
e8266a9
commit b00f00d
Showing
8 changed files
with
147 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
translate/cloud-client/translate_v3_get_supported_languages_with_target.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
27 changes: 27 additions & 0 deletions
27
translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
translate/cloud-client/translate_v3_translate_text_with_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
28 changes: 28 additions & 0 deletions
28
translate/cloud-client/translate_v3_translate_text_with_model_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |