Skip to content

Commit 3976433

Browse files
docs(samples): a draft that refactors speech_to_storage_beta sample (#276)
* docs(samples): a draft that refactors speech_to_storage_beta sample * docs(samples): applied changes * docs(samples): file name changed * docs(samples): added new line eof * docs(samples): updated to LongRunningRecognizeResponse * docs(samples): fixed lint * docs(samples): removed unwanted line * docs(samples): removed unwanted line
1 parent f0f2b00 commit 3976433

File tree

2 files changed

+39
-11
lines changed

2 files changed

+39
-11
lines changed

speech/snippets/speech_to_storage_beta.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,19 @@
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

2123
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,
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

speech/snippets/speech_to_storage_beta_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@
3838

3939

4040
def test_export_transcript_to_storage_beta(bucket, capsys):
41-
transcript = speech_to_storage_beta.export_transcript_to_storage_beta(
41+
results = speech_to_storage_beta.export_transcript_to_storage_beta(
4242
INPUT_STORAGE_URI,
4343
OUTPUT_STORAGE_URI,
4444
encoding,
4545
sample_rate_hertz,
4646
language_code,
47+
BUCKET_NAME,
48+
BUCKET_PREFIX,
4749
)
48-
assert "I'm here" in transcript
50+
assert len(results) > 0
4951

5052

5153
@pytest.fixture

0 commit comments

Comments
 (0)