Skip to content

Commit b2a92cc

Browse files
author
Elizabeth Crowdus
committed
finished hybrid glossaries tutorial sample code
1 parent 86a781b commit b2a92cc

File tree

5 files changed

+34
-25
lines changed

5 files changed

+34
-25
lines changed

translate/cloud-client/hybrid_glossaries/hybrid_tutorial.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ def pic_to_text(infile):
5353

5454
# [START hybrid_create_glossary]
5555
def create_glossary(languages, project_id, glossary_name, glossary_uri):
56-
# Creates a GCP glossary resources
57-
# Assumes you've already uploaded a glossary to Cloud Storage
56+
# Creates a GCP glossary resource
57+
# Assumes you've already manually uploaded a glossary to Cloud Storage
5858
#
5959
# ARGS
6060
# languages: list of languages in the glossary
@@ -70,7 +70,7 @@ def create_glossary(languages, project_id, glossary_name, glossary_uri):
7070

7171
# Defines the languages in the glossary
7272
# This list must match the languages in the glossary
73-
# Here, the glossary includes French and English
73+
# Here, the glossary includes French and English
7474
languages = ['fr', 'en']
7575
# Set information to access
7676
glossary_uri = 'gs://cloud-samples-data/translation/bistro_glossary.csv'
@@ -87,11 +87,9 @@ def create_glossary(languages, project_id, glossary_name, glossary_uri):
8787
language_codes_set = translate.types.Glossary.LanguageCodesSet(
8888
language_codes=languages)
8989

90-
# todo
9190
gcs_source = translate.types.GcsSource(
9291
input_uri=glossary_uri)
9392

94-
# todo
9593
input_config = translate.types.GlossaryInputConfig(
9694
gcs_source=gcs_source)
9795

@@ -101,10 +99,10 @@ def create_glossary(languages, project_id, glossary_name, glossary_uri):
10199
language_codes_set=language_codes_set,
102100
input_config=input_config)
103101

104-
parent = client.location_path(project_id, location)
102+
resource = client.location_path(project_id, location)
105103

106104
# Create glossary resource
107-
operation = client.create_glossary(parent=parent, glossary=glossary)
105+
operation = client.create_glossary(parent=resource, glossary=glossary)
108106

109107
return operation.result(timeout=90)
110108

@@ -160,11 +158,12 @@ def delete_glossary(project_id, glossary_name):
160158
# ARGS
161159
# project_id: GCP project id
162160
# glossary_name: name you gave your project's glossary
163-
# resource when you created it
161+
# resource when you created it
164162
#
165163
# RETURNS
166164
# nothing
167165

166+
# Designates the data center location that you want to use
168167
location = 'us-central1'
169168

170169
# Instantiates a client
@@ -184,6 +183,14 @@ def delete_glossary(project_id, glossary_name):
184183

185184
# [START hybrid_tts]
186185
def text_to_speech(text, outfile):
186+
# Generates synthetic audio from plaintext
187+
#
188+
# ARGS
189+
# text: text to synthesize
190+
# outfile: filename to use to store synthetic audio
191+
#
192+
# RETURNS
193+
# nothing
187194

188195
# Instantiates a client
189196
client = texttospeech.TextToSpeechClient()
@@ -216,27 +223,30 @@ def text_to_speech(text, outfile):
216223
def main():
217224

218225
# GCP project id
219-
project_id = 'ec-gcp'
226+
# project_id = [TODO(developer): INSERT PROJECT ID HERE]
227+
220228
# Photo from which to extract text
221229
infile = "resources/example.png"
222230
# Name of file that will hold synthetic speech
223231
outfile = "resources/example.mp3"
232+
224233
# Name that will be assigned to your project's glossary resource
225234
glossary_name = 'bistro-glossary'
226235
# URI of glossary uploaded to Cloud Storage
227236
glossary_uri = 'gs://cloud-samples-data/translation/bistro_glossary.csv'
237+
# List of languages in the glossary
238+
glossary_langs = ['fr', 'en']
228239

229240
# delete_glossary(project_id, glossary_name)
230-
create_glossary(['fr', 'en'], project_id, glossary_name, glossary_uri)
241+
create_glossary(glossary_langs, project_id, glossary_name, glossary_uri)
231242

243+
# photo -> detected text
232244
text_to_translate = pic_to_text(infile)
245+
# detected text -> translated text
233246
text_to_speak = translate_text(text_to_translate, 'fr', 'en',
234247
project_id, glossary_name)
248+
# translated text -> synthetic audio
235249
text_to_speech(text_to_speak, outfile)
236-
print(text_to_translate)
237-
print("____")
238-
print(text_to_speak)
239-
240250
# [END hybrid_integration]
241251

242252

translate/cloud-client/hybrid_glossaries/hybrid_tutorial_tests.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,20 @@
2121
import filecmp
2222
import os
2323

24-
PROJECT_ID = 'ec-gcp'
24+
# PROJECT_ID = [TODO(developer): INSERT GCP PROJECT ID HERE]
2525

2626
# VISION TESTS
2727

2828

2929
def test_vision_standard_format():
3030

31-
# Generate text
32-
text = pic_to_text('resources/standard_format.jpeg')
31+
expected_text = 'This is\na test!\n'
32+
alt_expected_text = 'This\nis\na test!\n'
3333

34-
# Read expected text
35-
with open('resources/standard_format.txt') as f:
36-
expected_text = f.read()
34+
# Generate text using Vision API
35+
text = pic_to_text('resources/standard_format.jpeg')
3736

38-
assert text == expected_text
37+
assert (text == expected_text) or (text == alt_expected_text)
3938

4039

4140
def test_vision_non_standard_format():
@@ -79,8 +78,8 @@ def test_translate_standard():
7978

8079
def test_translate_glossary():
8180

82-
expected_text = "I eat goat cheese"
83-
input_text = "Je mange du chevre"
81+
expected_text = 'I eat goat cheese'
82+
input_text = 'Je mange du chevre'
8483

8584
text = translate_text(input_text, 'fr', 'en', PROJECT_ID,
8685
'bistro-glossary')

translate/cloud-client/hybrid_glossaries/resources/bistro_glossary.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
fr,en,
22
chevre,goat cheese,
33
Chevre,Goat cheese,
4+
chèvre,goat cheese,
5+
Chèvre,Goat cheese,
46
crème brulée,crème brulée,
57
Crème brulée,Crème brulée,
68
bouillabaisse,fish stew,
10.9 KB
Loading

translate/cloud-client/hybrid_glossaries/resources/standard_format.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)