-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Translation V3 GA samples * renamed v3 files * fixed tests * renamed region tags * renamed region tags * reformatted v2 samples * fixed small nits on batch_translate_text with model * made requested changes * renamed methods names in main * added or case tests * added lang code pair for list_glossary * changed GOOGLE_PROJECT--> GCLOUD_PROJETC * tweaked tests --> changed target lang from es to ja * removed v2 samples * added missing features batch_translate_with_glossary & model * hardcoded model_id * fixed wrong model_id * updated input_uri
- Loading branch information
1 parent
67bd83e
commit 824d83b
Showing
31 changed files
with
1,931 additions
and
3 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
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 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 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 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 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.