1818
1919from google .cloud import pubsub_v1
2020from google .cloud import storage
21- from google .cloud import translate
21+ from google .cloud import translate_v2 as translate
2222from google .cloud import vision
2323
2424vision_client = vision .ImageAnnotatorClient ()
2727storage_client = storage .Client ()
2828
2929project_id = os .environ ['GCP_PROJECT' ]
30-
31- with open ('config.json' ) as f :
32- data = f .read ()
33- config = json .loads (data )
3430# [END functions_ocr_setup]
3531
3632
@@ -55,10 +51,11 @@ def detect_text(bucket, filename):
5551 print ('Detected language {} for text {}.' .format (src_lang , text ))
5652
5753 # 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' ]
6057 if src_lang == target_lang or src_lang == 'und' :
61- topic_name = config ['RESULT_TOPIC' ]
58+ topic_name = os . environ ['RESULT_TOPIC' ]
6259 message = {
6360 'text' : text ,
6461 'filename' : filename ,
@@ -120,7 +117,7 @@ def translate_text(event, context):
120117 translated_text = translate_client .translate (text ,
121118 target_language = target_lang ,
122119 source_language = src_lang )
123- topic_name = config ['RESULT_TOPIC' ]
120+ topic_name = os . environ ['RESULT_TOPIC' ]
124121 message = {
125122 'text' : translated_text ['translatedText' ],
126123 'filename' : filename ,
@@ -147,7 +144,7 @@ def save_result(event, context):
147144
148145 print ('Received request to save file {}.' .format (filename ))
149146
150- bucket_name = config ['RESULT_BUCKET' ]
147+ bucket_name = os . environ ['RESULT_BUCKET' ]
151148 result_filename = '{}_{}.txt' .format (filename , lang )
152149 bucket = storage_client .get_bucket (bucket_name )
153150 blob = bucket .blob (result_filename )
0 commit comments