1717"""Google Cloud Speech API sample application using the REST API for async
1818batch processing.
1919
20- Example usage: python transcribe_async.py resources/audio.raw
20+ Example usage:
21+ python transcribe_async.py resources/audio.raw
22+ python transcribe_async.py gs://cloud-samples-tests/speech/brooklyn.flac
2123"""
2224
23- # [START import_libraries]
2425import argparse
2526import io
2627import time
27- # [END import_libraries]
2828
2929
30- def main (speech_file ):
31- """Transcribe the given audio file asynchronously.
32-
33- Args:
34- speech_file: the name of the audio file.
35- """
36- # [START authenticating]
37- # Application default credentials provided by env variable
38- # GOOGLE_APPLICATION_CREDENTIALS
30+ def transcribe_file (speech_file ):
31+ """Transcribe the given audio file asynchronously."""
3932 from google .cloud import speech
4033 speech_client = speech .Client ()
41- # [END authenticating]
4234
43- # [START construct_request]
44- # Loads the audio into memory
4535 with io .open (speech_file , 'rb' ) as audio_file :
4636 content = audio_file .read ()
4737 audio_sample = speech_client .sample (
4838 content ,
4939 source_uri = None ,
5040 encoding = 'LINEAR16' ,
5141 sample_rate = 16000 )
52- # [END construct_request]
5342
54- # [START send_request]
5543 operation = speech_client .speech_api .async_recognize (audio_sample )
5644
5745 retry_count = 100
@@ -61,7 +49,7 @@ def main(speech_file):
6149 operation .poll ()
6250
6351 if not operation .complete :
64- print (" Operation not complete and retry limit reached." )
52+ print (' Operation not complete and retry limit reached.' )
6553 return
6654
6755 alternatives = operation .results
@@ -71,13 +59,44 @@ def main(speech_file):
7159 # [END send_request]
7260
7361
74- # [START run_application]
62+ def transcribe_gcs (gcs_uri ):
63+ """Asynchronously transcribes the audio file specified by the gcs_uri."""
64+ from google .cloud import speech
65+ speech_client = speech .Client ()
66+
67+ audio_sample = speech_client .sample (
68+ content = None ,
69+ source_uri = gcs_uri ,
70+ encoding = 'FLAC' ,
71+ sample_rate = 16000 )
72+
73+ operation = speech_client .speech_api .async_recognize (audio_sample )
74+
75+ retry_count = 100
76+ while retry_count > 0 and not operation .complete :
77+ retry_count -= 1
78+ time .sleep (2 )
79+ operation .poll ()
80+
81+ if not operation .complete :
82+ print ('Operation not complete and retry limit reached.' )
83+ return
84+
85+ alternatives = operation .results
86+ for alternative in alternatives :
87+ print ('Transcript: {}' .format (alternative .transcript ))
88+ print ('Confidence: {}' .format (alternative .confidence ))
89+ # [END send_request_gcs]
90+
91+
7592if __name__ == '__main__' :
7693 parser = argparse .ArgumentParser (
7794 description = __doc__ ,
7895 formatter_class = argparse .RawDescriptionHelpFormatter )
7996 parser .add_argument (
80- 'speech_file ' , help = 'Full path of audio file to be recognized' )
97+ 'path ' , help = 'File or GCS path for audio file to be recognized' )
8198 args = parser .parse_args ()
82- main (args .speech_file )
83- # [END run_application]
99+ if args .path .startswith ('gs://' ):
100+ transcribe_gcs (args .path )
101+ else :
102+ transcribe_file (args .path )
0 commit comments