-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Translation V3 GA samples #2478
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
nnegrey
merged 18 commits into
GoogleCloudPlatform:tmp-generated-samples
from
munkhuushmgl:tmp-generated-samples
Oct 18, 2019
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
7110494
Translation V3 GA samples
munkhuushmgl 24b08e2
renamed v3 files
munkhuushmgl 7ddccff
fixed tests
munkhuushmgl cd6dfc7
renamed region tags
munkhuushmgl 4acc297
renamed region tags
munkhuushmgl 84e39bf
reformatted v2 samples
munkhuushmgl 123ef82
fixed small nits on batch_translate_text with model
munkhuushmgl a9314f5
made requested changes
munkhuushmgl 21e101c
renamed methods names in main
munkhuushmgl 7a961da
added or case tests
munkhuushmgl d701172
added lang code pair for list_glossary
munkhuushmgl 87148c9
changed GOOGLE_PROJECT--> GCLOUD_PROJETC
munkhuushmgl 8a2636c
tweaked tests --> changed target lang from es to ja
munkhuushmgl 539df0b
removed v2 samples
munkhuushmgl 19e6489
added missing features batch_translate_with_glossary & model
munkhuushmgl 9188861
hardcoded model_id
munkhuushmgl 5ed2e3a
fixed wrong model_id
munkhuushmgl 3454256
updated input_uri
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
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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
google-cloud-translate==1.4.0 | ||
google-cloud-storage==1.14.0 | ||
google-cloud-translate==1.7.0 | ||
google-cloud-storage==1.19.1 |
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
99 changes: 99 additions & 0 deletions
99
translate/cloud-client/translate_v3_batch_translate_text.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,99 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2019 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 | ||
# | ||
# https://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. | ||
|
||
# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "translate_v3_batch_translate_text") | ||
|
||
# To install the latest published package dependency, execute the following: | ||
# pip install google-cloud-translate | ||
|
||
# sample-metadata | ||
# title: Batch translate text | ||
# description: Batch translate text | ||
# usage: python3 translate_v3_batch_translate_text.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project_id "[Google Cloud Project ID]"] [--location "us-central1"] [--source_lang en] [--target_lang ja] | ||
|
||
# [START translate_v3_batch_translate_text] | ||
from google.cloud import translate_v3 | ||
|
||
def sample_batch_translate_text( | ||
input_uri, output_uri, project_id, location, source_lang, target_lang | ||
): | ||
"""Batch translate text""" | ||
|
||
client = translate_v3.TranslationServiceClient() | ||
|
||
# TODO(developer): Uncomment and set the following variables | ||
# input_uri = 'gs://cloud-samples-data/text.txt' | ||
# output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' | ||
# project_id = '[Google Cloud Project ID]' | ||
# location = 'us-central1' | ||
# source_lang = 'en' | ||
# target_lang = 'ja' | ||
target_language_codes = [target_lang] | ||
gcs_source = {"input_uri": input_uri} | ||
|
||
# Optional. Can be "text/plain" or "text/html". | ||
mime_type = "text/plain" | ||
input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type} | ||
input_configs = [input_configs_element] | ||
gcs_destination = {"output_uri_prefix": output_uri} | ||
output_config = {"gcs_destination": gcs_destination} | ||
parent = client.location_path(project_id, location) | ||
|
||
operation = client.batch_translate_text( | ||
parent=parent, | ||
source_language_code=source_lang, | ||
target_language_codes=target_language_codes, | ||
input_configs=input_configs, | ||
output_config=output_config) | ||
|
||
print(u"Waiting for operation to complete...") | ||
response = operation.result(90) | ||
|
||
print(u"Total Characters: {}".format(response.total_characters)) | ||
print(u"Translated Characters: {}".format(response.translated_characters)) | ||
|
||
|
||
# [END translate_v3_batch_translate_text] | ||
|
||
|
||
def main(): | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--input_uri", type=str, default="gs://cloud-samples-data/text.txt" | ||
) | ||
parser.add_argument( | ||
"--output_uri", type=str, default="gs://YOUR_BUCKET_ID/path_to_store_results/" | ||
) | ||
parser.add_argument("--project_id", type=str, default="[Google Cloud Project ID]") | ||
parser.add_argument("--location", type=str, default="us-central1") | ||
parser.add_argument("--source_lang", type=str, default="en") | ||
parser.add_argument("--target_lang", type=str, default="ja") | ||
args = parser.parse_args() | ||
|
||
sample_batch_translate_text( | ||
args.input_uri, | ||
args.output_uri, | ||
args.project_id, | ||
args.location, | ||
args.source_lang, | ||
args.target_lang, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
44 changes: 44 additions & 0 deletions
44
translate/cloud-client/translate_v3_batch_translate_text_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,44 @@ | ||
# Copyright 2019 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 pytest | ||
import uuid | ||
import translate_v3_batch_translate_text | ||
from google.cloud import storage | ||
|
||
PROJECT_ID = os.environ['GCLOUD_PROJECT'] | ||
|
||
@pytest.fixture(scope='function') | ||
def bucket(): | ||
"""Create a temporary bucket to store annotation output.""" | ||
bucket_name = str(uuid.uuid1()) | ||
storage_client = storage.Client() | ||
bucket = storage_client.create_bucket(bucket_name) | ||
|
||
yield bucket | ||
|
||
bucket.delete(force=True) | ||
|
||
def test_batch_translate_text(capsys, bucket): | ||
translate_v3_batch_translate_text.sample_batch_translate_text( | ||
'gs://cloud-samples-data/translation/text.txt', | ||
'gs://{}/translation/BATCH_TRANSLATION_OUTPUT/'.format(bucket.name), | ||
PROJECT_ID, | ||
'us-central1', | ||
'en', | ||
'es' | ||
) | ||
out, _ = capsys.readouterr() | ||
assert 'Total Characters: 13' in out |
118 changes: 118 additions & 0 deletions
118
translate/cloud-client/translate_v3_batch_translate_text_with_glossary.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,118 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2019 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 | ||
# | ||
# https://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. | ||
|
||
# DO NOT EDIT! This is a generated sample ("LongRunningPromise", "batch_translate_text_with_glossary") | ||
|
||
# To install the latest published package dependency, execute the following: | ||
# pip install google-cloud-translate | ||
|
||
# sample-metadata | ||
# title: Batch Translate Text with Glossary | ||
# description: Batch Translate Text with Glossary a given URI using a glossary. | ||
# usage: python3 translate_v3_batch_translate_text_with_glossary.py [--input_uri "gs://cloud-samples-data/text.txt"] [--output_uri "gs://YOUR_BUCKET_ID/path_to_store_results/"] [--project "[Google Cloud Project ID]"] [--location "us-central1"] [--glossary_id "[YOUR_GLOSSARY_ID]"] [--target_language en] [--source_language de] | ||
|
||
# [START translate_v3_batch_translate_text_with_glossary] | ||
from google.cloud import translate_v3 | ||
|
||
def sample_batch_translate_text_with_glossary( | ||
input_uri, output_uri, project_id, location, source_lang, target_lang, glossary_id | ||
): | ||
"""Batch translate text with Glossary""" | ||
|
||
client = translate_v3.TranslationServiceClient() | ||
|
||
# TODO(developer): Uncomment and set the following variables | ||
# input_uri = 'gs://cloud-samples-data/text.txt' | ||
# output_uri = 'gs://YOUR_BUCKET_ID/path_to_store_results/' | ||
# project_id = '[Google Cloud Project ID]' | ||
# location = 'us-central1' | ||
# source_lang = 'en' | ||
# target_lang = 'ja' | ||
# glossary_id = '[YOUR_GLOSSARY_ID]' | ||
target_language_codes = [target_lang] | ||
gcs_source = {"input_uri": input_uri} | ||
|
||
# Optional. Can be "text/plain" or "text/html". | ||
mime_type = "text/plain" | ||
input_configs_element = {"gcs_source": gcs_source, "mime_type": mime_type} | ||
input_configs = [input_configs_element] | ||
gcs_destination = {"output_uri_prefix": output_uri} | ||
output_config = {"gcs_destination": gcs_destination} | ||
input_uri, output_uri, project_id, location, source_lang, target_lang, glossary_id | ||
parent = client.location_path(project_id, location) | ||
|
||
glossary_path = client.glossary_path( | ||
project_id, | ||
'us-central1', # The location of the glossary | ||
glossary_id) | ||
|
||
glossary_config = translate_v3.types.TranslateTextGlossaryConfig( | ||
glossary=glossary_path) | ||
|
||
glossaries = {"ja": glossary_config} #target lang as key | ||
|
||
operation = client.batch_translate_text( | ||
parent=parent, | ||
source_language_code=source_lang, | ||
target_language_codes=target_language_codes, | ||
input_configs=input_configs, | ||
glossaries=glossaries, | ||
output_config=output_config) | ||
|
||
print(u"Waiting for operation to complete...") | ||
response = operation.result(90) | ||
|
||
print(u"Total Characters: {}".format(response.total_characters)) | ||
print(u"Translated Characters: {}".format(response.translated_characters)) | ||
|
||
|
||
# [END translate_v3_batch_translate_text_with_glossary] | ||
|
||
|
||
def main(): | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--input_uri", type=str, default="gs://cloud-samples-data/translation/text_with_glossary.txt" | ||
) | ||
parser.add_argument( | ||
"--output_uri", type=str, default="gs://YOUR_BUCKET_ID/path_to_store_results/" | ||
) | ||
parser.add_argument("--project_id", type=str, default="[Google Cloud Project ID]") | ||
parser.add_argument("--location", type=str, default="us-central1") | ||
parser.add_argument("--source_lang", type=str, default="en") | ||
parser.add_argument("--target_lang", type=str, default="ja") | ||
parser.add_argument( | ||
"--glossary_id", | ||
type=str, | ||
default="[YOUR_GLOSSARY_ID]", | ||
) | ||
args = parser.parse_args() | ||
|
||
sample_batch_translate_text_with_glossary( | ||
args.input_uri, | ||
args.output_uri, | ||
args.project_id, | ||
args.location, | ||
args.source_lang, | ||
args.target_lang, | ||
args.glossary_id | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.
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.