15
15
16
16
# [START speech_transcribe_with_speech_to_storage_beta]
17
17
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
19
21
20
22
21
23
def 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 ,
23
31
):
24
32
25
33
# input_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
@@ -40,20 +48,38 @@ def export_transcript_to_storage_beta(
40
48
audio = audio , config = config , output_config = output_config
41
49
)
42
50
43
- # Create the speech client
51
+ # create the speech client
44
52
speech_client = speech .SpeechClient ()
45
53
54
+ # create the storage client
55
+ storage_client = storage .Client ()
56
+
57
+ # run the recognizer to export transcript
46
58
operation = speech_client .long_running_recognize (request = request )
47
59
48
60
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
+ )
50
76
51
77
# Each result is for a consecutive portion of the audio. Iterate through
52
78
# them to get the transcripts for the entire audio file.
53
- for result in response .results :
79
+ for result in storage_transcript .results :
54
80
# 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 } " )
57
83
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