18
18
19
19
from google .cloud import pubsub_v1
20
20
from google .cloud import storage
21
- from google .cloud import translate
21
+ from google .cloud import translate_v2 as translate
22
22
from google .cloud import vision
23
23
24
24
vision_client = vision .ImageAnnotatorClient ()
27
27
storage_client = storage .Client ()
28
28
29
29
project_id = os .environ ['GCP_PROJECT' ]
30
-
31
- with open ('config.json' ) as f :
32
- data = f .read ()
33
- config = json .loads (data )
34
30
# [END functions_ocr_setup]
35
31
36
32
@@ -55,10 +51,11 @@ def detect_text(bucket, filename):
55
51
print ('Detected language {} for text {}.' .format (src_lang , text ))
56
52
57
53
# Submit a message to the bus for each target language
58
- for target_lang in config .get ('TO_LANG' , []):
59
- topic_name = config ['TRANSLATE_TOPIC' ]
54
+ to_langs = os .environ ['TO_LANG' ].split (',' )
55
+ for target_lang in to_langs :
56
+ topic_name = os .environ ['TRANSLATE_TOPIC' ]
60
57
if src_lang == target_lang or src_lang == 'und' :
61
- topic_name = config ['RESULT_TOPIC' ]
58
+ topic_name = os . environ ['RESULT_TOPIC' ]
62
59
message = {
63
60
'text' : text ,
64
61
'filename' : filename ,
@@ -120,7 +117,7 @@ def translate_text(event, context):
120
117
translated_text = translate_client .translate (text ,
121
118
target_language = target_lang ,
122
119
source_language = src_lang )
123
- topic_name = config ['RESULT_TOPIC' ]
120
+ topic_name = os . environ ['RESULT_TOPIC' ]
124
121
message = {
125
122
'text' : translated_text ['translatedText' ],
126
123
'filename' : filename ,
@@ -147,7 +144,7 @@ def save_result(event, context):
147
144
148
145
print ('Received request to save file {}.' .format (filename ))
149
146
150
- bucket_name = config ['RESULT_BUCKET' ]
147
+ bucket_name = os . environ ['RESULT_BUCKET' ]
151
148
result_filename = '{}_{}.txt' .format (filename , lang )
152
149
bucket = storage_client .get_bucket (bucket_name )
153
150
blob = bucket .blob (result_filename )
0 commit comments