Skip to content

Commit

Permalink
Update v3 samples to use default library
Browse files Browse the repository at this point in the history
  • Loading branch information
nnegrey committed Oct 25, 2019
1 parent 824d83b commit 615c08e
Show file tree
Hide file tree
Showing 19 changed files with 35 additions and 37 deletions.
4 changes: 1 addition & 3 deletions translate/cloud-client/beta_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def unique_glossary_id():
def test_translate_text(capsys):
beta_snippets.translate_text(PROJECT_ID, 'Hello world')
out, _ = capsys.readouterr()
assert 'Zdravo svet' in out
assert 'Zdravo svet' in out or 'Pozdrav svijetu' in out


def test_batch_translate_text(capsys, bucket):
Expand Down Expand Up @@ -99,7 +99,6 @@ def test_create_glossary(capsys, unique_glossary_id):
beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id)
out, _ = capsys.readouterr()
assert 'Created' in out
assert PROJECT_ID in out
assert unique_glossary_id in out
assert 'gs://cloud-samples-data/translation/glossary.csv' in out

Expand Down Expand Up @@ -129,6 +128,5 @@ def test_delete_glossary(capsys, unique_glossary_id):
beta_snippets.create_glossary(PROJECT_ID, unique_glossary_id)
beta_snippets.delete_glossary(PROJECT_ID, unique_glossary_id)
out, _ = capsys.readouterr()
assert PROJECT_ID in out
assert 'us-central1' in out
assert unique_glossary_id in out
2 changes: 1 addition & 1 deletion translate/cloud-client/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
def run_quickstart():
# [START translate_quickstart]
# Imports the Google Cloud client library
from google.cloud import translate
from google.cloud import translate_v2 as translate

# Instantiates a client
translate_client = translate.Client()
Expand Down
2 changes: 1 addition & 1 deletion translate/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-translate==1.7.0
google-cloud-translate==2.0.0
google-cloud-storage==1.19.1
4 changes: 2 additions & 2 deletions translate/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import argparse

from google.cloud import translate
from google.cloud import translate_v2 as translate
import six


Expand Down Expand Up @@ -70,7 +70,7 @@ def list_languages_with_target(target):
# [END translate_list_language_names]


def translate_text_with_model(target, text, model=translate.NMT):
def translate_text_with_model(target, text, model='nmt'):
# [START translate_text_with_model]
"""Translates text into the target language.
Expand Down
4 changes: 2 additions & 2 deletions translate/cloud-client/translate_v3_batch_translate_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
# 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
from google.cloud import translate

def sample_batch_translate_text(
input_uri, output_uri, project_id, location, source_lang, target_lang
):
"""Batch translate text"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# input_uri = 'gs://cloud-samples-data/text.txt'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
# 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
from google.cloud import translate

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()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# input_uri = 'gs://cloud-samples-data/text.txt'
Expand All @@ -59,7 +59,7 @@ def sample_batch_translate_text_with_glossary(
'us-central1', # The location of the glossary
glossary_id)

glossary_config = translate_v3.types.TranslateTextGlossaryConfig(
glossary_config = translate.types.TranslateTextGlossaryConfig(
glossary=glossary_path)

glossaries = {"ja": glossary_config} #target lang as key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_batch_translate_text_with_glossary_and_model.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"] [--target_language en] [--source_language de] [--model_id "{your-model-id}"] [--glossary_id "{your-glossary-id}"]

# [START translate_v3_batch_translate_text_with_glossary_and_model]
from google.cloud import translate_v3
from google.cloud import translate

def sample_batch_translate_text_with_glossary_and_model(
input_uri,
Expand All @@ -41,7 +41,7 @@ def sample_batch_translate_text_with_glossary_and_model(
Batch translate text with Glossary and Translation model
"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# input_uri = 'gs://cloud-samples-data/text.txt'
Expand Down Expand Up @@ -70,7 +70,7 @@ def sample_batch_translate_text_with_glossary_and_model(
'us-central1', # The location of the glossary
glossary_id)

glossary_config = translate_v3.types.TranslateTextGlossaryConfig(
glossary_config = translate.types.TranslateTextGlossaryConfig(
glossary=glossary_path)
glossaries = {"ja": glossary_config} #target lang as key

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_batch_translate_text_with_model.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"] [--target_language en] [--source_language de] [--model_id "{your-model-id}"]

# [START translate_v3_batch_translate_text_with_model]
from google.cloud import translate_v3
from google.cloud import translate


def sample_batch_translate_text_with_model(
Expand All @@ -47,7 +47,7 @@ def sample_batch_translate_text_with_model(
code.
"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# input_uri = 'gs://cloud-samples-data/text.txt'
Expand Down
2 changes: 1 addition & 1 deletion translate/cloud-client/translate_v3_create_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_create_glossary.py [--project "[Google Cloud Project ID]"] [--glossary_id "my_glossary_id_123"]

# [START translate_v3_create_glossary]
from google.cloud import translate_v3 as translate
from google.cloud import translate

def sample_create_glossary(project_id, input_uri, glossary_id):
"""Create Glossary"""
Expand Down
2 changes: 1 addition & 1 deletion translate/cloud-client/translate_v3_delete_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_delete_glossary.py [--project_id "[Google Cloud Project ID]"] [--glossary_id "[Glossary ID]"]

# [START translate_v3_delete_glossary]
from google.cloud import translate_v3 as translate
from google.cloud import translate

def sample_delete_glossary(project_id, glossary_id):
"""Delete Glossary"""
Expand Down
4 changes: 2 additions & 2 deletions translate/cloud-client/translate_v3_detect_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_detect_language.py [--text "Hello, world!"] [--project_id "[Google Cloud project_id ID]"]

# [START translate_v3_detect_language]
from google.cloud import translate_v3
from google.cloud import translate


def sample_detect_language(text, project_id):
Expand All @@ -36,7 +36,7 @@ def sample_detect_language(text, project_id):
text The text string for performing language detection
"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# text = 'Hello, world!'
Expand Down
4 changes: 2 additions & 2 deletions translate/cloud-client/translate_v3_get_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
# usage: python3 translate_v3_get_glossary.py [--project_id "[Google Cloud Project ID]"] [--glossary_id "[Glossary ID]"]

# [START translate_v3_get_glossary]
from google.cloud import translate_v3
from google.cloud import translate

def sample_get_glossary(project_id, glossary_id):
"""Get Glossary"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# project = '[Google Cloud Project ID]'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
# usage: python3 translate_v3_get_supported_languages.py [--project_id "[Google Cloud Project ID]"]

# [START translate_v3_get_supported_languages]
from google.cloud import translate_v3
from google.cloud import translate

def sample_get_supported_languages(project_id):
"""Getting a list of supported language codes"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# project = '[Google Cloud Project ID]'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_get_supported_languages_for_target.py [--language_code en] [--project_id "[Google Cloud Project ID]"]

# [START translate_v3_get_supported_languages_for_target]
from google.cloud import translate_v3
from google.cloud import translate


def sample_get_supported_languages_with_target(language_code, project_id):
Expand All @@ -37,7 +37,7 @@ def sample_get_supported_languages_with_target(language_code, project_id):
human readable names of supported languages.
"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# language_code = 'en'
Expand Down
4 changes: 2 additions & 2 deletions translate/cloud-client/translate_v3_list_glossary.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
# usage: python3 translate_v3_list_glossary.py [--project_id "[Google Cloud Project ID]"]

# [START translate_v3_list_glossary]
from google.cloud import translate_v3
from google.cloud import translate

def sample_list_glossaries(project_id):
"""List Glossaries"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# project_id = '[Google Cloud Project ID]'
Expand Down
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 @@ -25,7 +25,7 @@
# usage: python3 translate_v3_translate_text.py [--text "Hello, world!"] [--target_language fr] [--project_id "[Google Cloud Project ID]"]

# [START translate_v3_translate_text]
from google.cloud import translate_v3
from google.cloud import translate


def sample_translate_text(text, target_language, project_id):
Expand All @@ -37,7 +37,7 @@ def sample_translate_text(text, target_language, project_id):
target_language Required. The BCP-47 language code to use for translation.
"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# text = 'Text you wish to translate'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_translate_text_with_glossary.py [--text "Hello, world!"] [--source_language en] [--target_language fr] [--project_id "[Google Cloud Project ID]"] [--glossary_id "[YOUR_GLOSSARY_ID]"]

# [START translate_v3_translate_text_with_glossary]
from google.cloud import translate_v3 as translate
from google.cloud import translate

def sample_translate_text_with_glossary(
text, source_language, target_language, project_id, glossary_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_translate_text_with_glossary_and_model.py [--model_id "[MODEL ID]"] [--glossary_id "[YOUR_GLOSSARY_ID]"] [--text "Hello, world!"] [--target_language fr] [--source_language en] [--project_id "[Google Cloud Project ID]"] [--location global]

# [START translate_v3_translate_text_with_glossary_and_model]
from google.cloud import translate_v3
from google.cloud import translate


def sample_translate_text_glossary_and_model(
Expand All @@ -42,7 +42,7 @@ def sample_translate_text_glossary_and_model(
source_language Optional. The BCP-47 language code of the input text.
"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# model_id = '[MODEL ID]'
Expand All @@ -61,7 +61,7 @@ def sample_translate_text_glossary_and_model(
'us-central1', # The location of the glossary
glossary_id)

glossary_config = translate_v3.types.TranslateTextGlossaryConfig(
glossary_config = translate.types.TranslateTextGlossaryConfig(
glossary=glossary)

response = client.translate_text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# usage: python3 translate_v3_translate_text_with_model.py [--model_id "[MODEL ID]"] [--text "Hello, world!"] [--target_language fr] [--source_language en] [--project_id "[Google Cloud Project ID]"] [--location global]

# [START translate_v3_translate_text_with_model]
from google.cloud import translate_v3
from google.cloud import translate


def sample_translate_text_with_model(
Expand All @@ -41,7 +41,7 @@ def sample_translate_text_with_model(
source_language Optional. The BCP-47 language code of the input text.
"""

client = translate_v3.TranslationServiceClient()
client = translate.TranslationServiceClient()

# TODO(developer): Uncomment and set the following variables
# model_id = '[MODEL ID]'
Expand Down

0 comments on commit 615c08e

Please sign in to comment.