- Notifications
You must be signed in to change notification settings - Fork 6.6k
translate-v3: samples #3034
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
leahecole merged 21 commits into GoogleCloudPlatform:master from munkhuushmgl:moving-translate-samples Mar 16, 2020
Merged
translate-v3: samples #3034
Changes from 3 commits
Commits
Show all changes
21 commits Select commit Hold shift + click to select a range
dd3e34e translate with custom model, get supported langs
munkhuushmgl 85af59b inlined small nit
munkhuushmgl 1a22c4f added encoding to model test
munkhuushmgl 81bb4c8 added missing region tags and link to supported langs
munkhuushmgl f63a537 Merge branch 'master' into moving-translate-samples
munkhuushmgl 1fd9261 Merge branch 'master' into moving-translate-samples
leahecole 76fccaa Merge branch 'master' into moving-translate-samples
munkhuushmgl da4401c inlined text-to-translate
munkhuushmgl 70b4c8c Merge branch 'master' into moving-translate-samples
munkhuushmgl 7663ac2 Merge branch 'moving-translate-samples' of https://github.com/munkhuu…
munkhuushmgl 8f5a3d7 directly inlined contents
munkhuushmgl 3e213e7 Merge branch 'master' into moving-translate-samples
munkhuushmgl 534e469 Merge branch 'master' into moving-translate-samples
munkhuushmgl c624817 Merge branch 'master' into moving-translate-samples
munkhuushmgl 8afb5ce revert text-translate vars
munkhuushmgl bd3ef9d Merge branch 'moving-translate-samples' of https://github.com/munkhuu…
munkhuushmgl 8739c09 Merge branch 'master' into moving-translate-samples
leahecole ba63302 reversed inlined text params
munkhuushmgl 01c6c35 Merge branch 'moving-translate-samples' of https://github.com/munkhuu…
munkhuushmgl 8a317a9 small nit
munkhuushmgl 6f88140 Merge branch 'master' into moving-translate-samples
munkhuushmgl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions 32 translate/cloud-client/translate_v3_get_supported_languages_with_target.py
This file contains hidden or 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,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" | ||
munkhuushmgl marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| ): | ||
| """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)) | ||
27 changes: 27 additions & 0 deletions 27 translate/cloud-client/translate_v3_get_supported_languages_with_target_test.py
This file contains hidden or 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( | ||
| "is", PROJECT_ID | ||
| ) | ||
| out, _ = capsys.readouterr() | ||
| assert u"Language Code: sq" in out | ||
| assert u"Display Name: albanska" in out |
48 changes: 48 additions & 0 deletions 48 translate/cloud-client/translate_v3_translate_text_with_model.py
This file contains hidden or 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", | ||
munkhuushmgl marked this conversation as resolved. Show resolved Hide resolved | ||
| 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 hidden or 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 -*- | ||
munkhuushmgl marked this conversation as resolved. Show resolved Hide resolved | ||
| # 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 | ||
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.