1515
1616# [START speech_transcribe_with_speech_to_storage_beta]
1717
18- from google .cloud import speech_v1p1beta1 as speech
18+ from google .cloud import speech
19+ from google .cloud import storage
20+ from google .cloud .speech_v1 import types
1921
2022
2123def export_transcript_to_storage_beta (
22- input_storage_uri , output_storage_uri , encoding , sample_rate_hertz , language_code
24+ input_storage_uri ,
25+ output_storage_uri ,
26+ encoding ,
27+ sample_rate_hertz ,
28+ language_code ,
29+ bucket_name ,
30+ object_name ,
2331):
2432
2533 # input_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
@@ -40,20 +48,38 @@ def export_transcript_to_storage_beta(
4048 audio = audio , config = config , output_config = output_config
4149 )
4250
43- # Create the speech client
51+ # create the speech client
4452 speech_client = speech .SpeechClient ()
4553
54+ # create the storage client
55+ storage_client = storage .Client ()
56+
57+ # run the recognizer to export transcript
4658 operation = speech_client .long_running_recognize (request = request )
4759
4860 print ("Waiting for operation to complete..." )
49- response = operation .result (timeout = 90 )
61+ operation .result (timeout = 90 )
62+
63+ # get bucket with name
64+ bucket = storage_client .get_bucket (bucket_name )
65+
66+ # get blob from bucket
67+ blob = bucket .get_blob (object_name )
68+
69+ # get content as string
70+ results_string = blob .download_as_string ()
71+
72+ # get transcript exported in storage bucket
73+ storage_transcript = types .LongRunningRecognizeResponse .from_json (
74+ results_string , ignore_unknown_fields = True
75+ )
5076
5177 # Each result is for a consecutive portion of the audio. Iterate through
5278 # them to get the transcripts for the entire audio file.
53- for result in response .results :
79+ for result in storage_transcript .results :
5480 # The first alternative is the most likely one for this portion.
55- print ("Transcript: {}" . format ( result .alternatives [0 ].transcript ) )
56- print ("Confidence: {}" . format ( result .alternatives [0 ].confidence ) )
81+ print (f "Transcript: { result .alternatives [0 ].transcript } " )
82+ print (f "Confidence: { result .alternatives [0 ].confidence } " )
5783
58- # [END speech_transcribe_with_speech_to_storage_beta]
59- return response .results [ 0 ]. alternatives [ 0 ]. transcript
84+ # [END speech_transcribe_with_speech_to_storage_beta]
85+ return storage_transcript .results
0 commit comments