Skip to content

Commit 8db931d

Browse files
Simplified 5 speech samples (set-1) (GoogleCloudPlatform#12110)
* Simplified 5 samples
1 parent ec870b1 commit 8db931d

10 files changed

Lines changed: 86 additions & 133 deletions

speech/snippets/transcribe_batch_dynamic_batching_v2.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@
1313
# limitations under the License.
1414

1515

16-
import argparse
17-
1816
# [START speech_transcribe_batch_dynamic_batching_v2]
17+
import os
18+
1919
from google.cloud.speech_v2 import SpeechClient
2020
from google.cloud.speech_v2.types import cloud_speech
2121

22+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
23+
2224

2325
def transcribe_batch_dynamic_batching_v2(
24-
project_id: str,
25-
gcs_uri: str,
26+
audio_uri: str,
2627
) -> cloud_speech.BatchRecognizeResults:
27-
"""Transcribes audio from a Google Cloud Storage URI.
28-
28+
"""Transcribes audio from a Google Cloud Storage URI using dynamic batching.
2929
Args:
30-
project_id: The Google Cloud project ID.
31-
gcs_uri: The Google Cloud Storage URI.
32-
30+
audio_uri (str): The Cloud Storage URI of the input audio.
31+
E.g., gs://[BUCKET]/[FILE]
3332
Returns:
34-
The RecognizeResponse.
33+
cloud_speech.BatchRecognizeResults: The response containing the transcription results.
3534
"""
3635
# Instantiates a client
3736
client = SpeechClient()
@@ -42,10 +41,10 @@ def transcribe_batch_dynamic_batching_v2(
4241
model="long",
4342
)
4443

45-
file_metadata = cloud_speech.BatchRecognizeFileMetadata(uri=gcs_uri)
44+
file_metadata = cloud_speech.BatchRecognizeFileMetadata(uri=audio_uri)
4645

4746
request = cloud_speech.BatchRecognizeRequest(
48-
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
47+
recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
4948
config=config,
5049
files=[file_metadata],
5150
recognition_output_config=cloud_speech.RecognitionOutputConfig(
@@ -60,20 +59,15 @@ def transcribe_batch_dynamic_batching_v2(
6059
print("Waiting for operation to complete...")
6160
response = operation.result(timeout=120)
6261

63-
for result in response.results[gcs_uri].transcript.results:
62+
for result in response.results[audio_uri].transcript.results:
6463
print(f"Transcript: {result.alternatives[0].transcript}")
6564

66-
return response.results[gcs_uri].transcript
65+
return response.results[audio_uri].transcript
6766

6867

6968
# [END speech_transcribe_batch_dynamic_batching_v2]
7069

7170

7271
if __name__ == "__main__":
73-
parser = argparse.ArgumentParser(
74-
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
75-
)
76-
parser.add_argument("project_id", help="GCP Project ID")
77-
parser.add_argument("gcs_uri", help="URI to GCS file")
78-
args = parser.parse_args()
79-
transcribe_batch_dynamic_batching_v2(args.project_id, args.gcs_uri)
72+
audio_uri = "gs://cloud-samples-data/speech/audio.flac"
73+
transcribe_batch_dynamic_batching_v2(audio_uri)

speech/snippets/transcribe_batch_dynamic_batching_v2_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import re
1716

1817
from flaky import flaky
@@ -29,11 +28,9 @@
2928
def test_transcribe_batch_dynamic_batching_v2(
3029
capsys: pytest.CaptureFixture,
3130
) -> None:
32-
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
33-
3431
response = (
3532
transcribe_batch_dynamic_batching_v2.transcribe_batch_dynamic_batching_v2(
36-
project_id, _TEST_AUDIO_FILE_PATH
33+
_TEST_AUDIO_FILE_PATH
3734
)
3835
)
3936

speech/snippets/transcribe_batch_gcs_input_gcs_output_v2.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
16-
import argparse
17-
1815
# [START speech_transcribe_batch_gcs_input_gcs_output_v2]
16+
import os
17+
1918
import re
2019

2120
from google.cloud import storage
2221
from google.cloud.speech_v2 import SpeechClient
2322
from google.cloud.speech_v2.types import cloud_speech
2423

24+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
25+
2526

2627
def transcribe_batch_gcs_input_gcs_output_v2(
27-
project_id: str,
28-
gcs_uri: str,
28+
audio_uri: str,
2929
gcs_output_path: str,
3030
) -> cloud_speech.BatchRecognizeResults:
31-
"""Transcribes audio from a Google Cloud Storage URI.
32-
31+
"""Transcribes audio from a Google Cloud Storage URI using the Google Cloud Speech-to-Text API.
32+
The transcription results are stored in another Google Cloud Storage bucket.
3333
Args:
34-
project_id: The Google Cloud project ID.
35-
gcs_uri: The Google Cloud Storage URI.
36-
gcs_output_path: The Cloud Storage URI to which to write the transcript.
37-
34+
audio_uri (str): The Google Cloud Storage URI of the input audio file.
35+
E.g., gs://[BUCKET]/[FILE]
36+
gcs_output_path (str): The Google Cloud Storage bucket URI where the output transcript will be stored.
37+
E.g., gs://[BUCKET]
3838
Returns:
39-
The BatchRecognizeResults message.
39+
cloud_speech.BatchRecognizeResults: The response containing the URI of the transcription results.
4040
"""
4141
# Instantiates a client
4242
client = SpeechClient()
@@ -47,10 +47,10 @@ def transcribe_batch_gcs_input_gcs_output_v2(
4747
model="long",
4848
)
4949

50-
file_metadata = cloud_speech.BatchRecognizeFileMetadata(uri=gcs_uri)
50+
file_metadata = cloud_speech.BatchRecognizeFileMetadata(uri=audio_uri)
5151

5252
request = cloud_speech.BatchRecognizeRequest(
53-
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
53+
recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
5454
config=config,
5555
files=[file_metadata],
5656
recognition_output_config=cloud_speech.RecognitionOutputConfig(
@@ -66,7 +66,7 @@ def transcribe_batch_gcs_input_gcs_output_v2(
6666
print("Waiting for operation to complete...")
6767
response = operation.result(timeout=120)
6868

69-
file_results = response.results[gcs_uri]
69+
file_results = response.results[audio_uri]
7070

7171
print(f"Operation finished. Fetching results from {file_results.uri}...")
7272
output_bucket, output_object = re.match(
@@ -94,15 +94,6 @@ def transcribe_batch_gcs_input_gcs_output_v2(
9494

9595

9696
if __name__ == "__main__":
97-
parser = argparse.ArgumentParser(
98-
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
99-
)
100-
parser.add_argument("project_id", help="GCP Project ID")
101-
parser.add_argument("gcs_uri", help="URI to GCS file")
102-
parser.add_argument(
103-
"gcs_output_path", help="GCS URI to which to write the transcript"
104-
)
105-
args = parser.parse_args()
106-
transcribe_batch_gcs_input_gcs_output_v2(
107-
args.project_id, args.gcs_uri, args.gcs_output_path
108-
)
97+
audio_uri = "gs://cloud-samples-data/speech/audio.flac"
98+
output_bucket_name = "gs://your-bucket-unique-name"
99+
transcribe_batch_gcs_input_gcs_output_v2(audio_uri, output_bucket_name)

speech/snippets/transcribe_batch_gcs_input_gcs_output_v2_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import re
1716
from uuid import uuid4
1817

@@ -43,10 +42,8 @@ def test_transcribe_batch_gcs_input_gcs_output_v2(
4342
gcs_bucket: pytest.CaptureFixture,
4443
capsys: pytest.CaptureFixture,
4544
) -> None:
46-
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
47-
4845
response = transcribe_batch_gcs_input_gcs_output_v2.transcribe_batch_gcs_input_gcs_output_v2(
49-
project_id, _TEST_AUDIO_FILE_PATH, f"gs://{gcs_bucket}"
46+
_TEST_AUDIO_FILE_PATH, f"gs://{gcs_bucket}"
5047
)
5148

5249
assert re.search(

speech/snippets/transcribe_batch_gcs_input_inline_output_v2.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
16-
import argparse
17-
1815
# [START speech_transcribe_batch_gcs_input_inline_output_v2]
16+
import os
17+
1918
from google.cloud.speech_v2 import SpeechClient
2019
from google.cloud.speech_v2.types import cloud_speech
2120

21+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
22+
2223

2324
def transcribe_batch_gcs_input_inline_output_v2(
24-
project_id: str,
25-
gcs_uri: str,
25+
audio_uri: str,
2626
) -> cloud_speech.BatchRecognizeResults:
27-
"""Transcribes audio from a Google Cloud Storage URI.
28-
27+
"""Transcribes audio from a Google Cloud Storage URI using the Google Cloud Speech-to-Text API.
28+
The transcription results are returned inline in the response.
2929
Args:
30-
project_id: The Google Cloud project ID.
31-
gcs_uri: The Google Cloud Storage URI.
32-
30+
audio_uri (str): The Google Cloud Storage URI of the input audio file.
31+
E.g., gs://[BUCKET]/[FILE]
3332
Returns:
34-
The RecognizeResponse.
33+
cloud_speech.BatchRecognizeResults: The response containing the transcription results.
3534
"""
3635
# Instantiates a client
3736
client = SpeechClient()
@@ -42,10 +41,10 @@ def transcribe_batch_gcs_input_inline_output_v2(
4241
model="long",
4342
)
4443

45-
file_metadata = cloud_speech.BatchRecognizeFileMetadata(uri=gcs_uri)
44+
file_metadata = cloud_speech.BatchRecognizeFileMetadata(uri=audio_uri)
4645

4746
request = cloud_speech.BatchRecognizeRequest(
48-
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
47+
recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
4948
config=config,
5049
files=[file_metadata],
5150
recognition_output_config=cloud_speech.RecognitionOutputConfig(
@@ -59,20 +58,15 @@ def transcribe_batch_gcs_input_inline_output_v2(
5958
print("Waiting for operation to complete...")
6059
response = operation.result(timeout=120)
6160

62-
for result in response.results[gcs_uri].transcript.results:
61+
for result in response.results[audio_uri].transcript.results:
6362
print(f"Transcript: {result.alternatives[0].transcript}")
6463

65-
return response.results[gcs_uri].transcript
64+
return response.results[audio_uri].transcript
6665

6766

6867
# [END speech_transcribe_batch_gcs_input_inline_output_v2]
6968

7069

7170
if __name__ == "__main__":
72-
parser = argparse.ArgumentParser(
73-
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
74-
)
75-
parser.add_argument("project_id", help="GCP Project ID")
76-
parser.add_argument("gcs_uri", help="URI to GCS file")
77-
args = parser.parse_args()
78-
transcribe_batch_gcs_input_inline_output_v2(args.project_id, args.gcs_uri)
71+
audio_uri = "gs://cloud-samples-data/speech/audio.flac"
72+
transcribe_batch_gcs_input_inline_output_v2(audio_uri)

speech/snippets/transcribe_batch_gcs_input_inline_output_v2_test.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
1615
import re
1716

1817
from flaky import flaky
@@ -21,18 +20,15 @@
2120

2221
import transcribe_batch_gcs_input_inline_output_v2
2322

24-
2523
_TEST_AUDIO_FILE_PATH = "gs://cloud-samples-data/speech/audio.flac"
2624

2725

2826
@flaky(max_runs=10, min_passes=1)
2927
def test_transcribe_batch_gcs_input_inline_output_v2(
3028
capsys: pytest.CaptureFixture,
3129
) -> None:
32-
project_id = os.getenv("GOOGLE_CLOUD_PROJECT")
33-
3430
response = transcribe_batch_gcs_input_inline_output_v2.transcribe_batch_gcs_input_inline_output_v2(
35-
project_id, _TEST_AUDIO_FILE_PATH
31+
_TEST_AUDIO_FILE_PATH
3632
)
3733

3834
assert re.search(

speech/snippets/transcribe_batch_multiple_files_v2.py

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
16-
import argparse
17-
1815
# [START speech_transcribe_batch_multiple_files_v2]
16+
import os
1917
import re
2018
from typing import List
2119

2220
from google.cloud import storage
2321
from google.cloud.speech_v2 import SpeechClient
2422
from google.cloud.speech_v2.types import cloud_speech
2523

24+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
25+
2626

2727
def transcribe_batch_multiple_files_v2(
28-
project_id: str,
29-
gcs_uris: List[str],
28+
audio_uris: List[str],
3029
gcs_output_path: str,
3130
) -> cloud_speech.BatchRecognizeResponse:
32-
"""Transcribes audio from a Google Cloud Storage URI.
33-
31+
"""Transcribes audio from multiple Google Cloud Storage URIs using the Google Cloud Speech-to-Text API.
32+
The transcription results are stored in another Google Cloud Storage bucket.
3433
Args:
35-
project_id: The Google Cloud project ID.
36-
gcs_uris: The Google Cloud Storage URIs to transcribe.
37-
gcs_output_path: The Cloud Storage URI to which to write the transcript.
38-
34+
audio_uris (List[str]): The list of Google Cloud Storage URIs of the input audio files.
35+
E.g., ["gs://[BUCKET]/[FILE]", "gs://[BUCKET]/[FILE]"]
36+
gcs_output_path (str): The Google Cloud Storage bucket URI where the output transcript will be stored.
37+
E.g., gs://[BUCKET]
3938
Returns:
40-
The BatchRecognizeResponse message.
39+
cloud_speech.BatchRecognizeResponse: The response containing the URIs of the transcription results.
4140
"""
4241
# Instantiates a client
4342
client = SpeechClient()
@@ -48,10 +47,10 @@ def transcribe_batch_multiple_files_v2(
4847
model="long",
4948
)
5049

51-
files = [cloud_speech.BatchRecognizeFileMetadata(uri=uri) for uri in gcs_uris]
50+
files = [cloud_speech.BatchRecognizeFileMetadata(uri=uri) for uri in audio_uris]
5251

5352
request = cloud_speech.BatchRecognizeRequest(
54-
recognizer=f"projects/{project_id}/locations/global/recognizers/_",
53+
recognizer=f"projects/{PROJECT_ID}/locations/global/recognizers/_",
5554
config=config,
5655
files=files,
5756
recognition_output_config=cloud_speech.RecognitionOutputConfig(
@@ -68,7 +67,7 @@ def transcribe_batch_multiple_files_v2(
6867
response = operation.result(timeout=120)
6968

7069
print("Operation finished. Fetching results from:")
71-
for uri in gcs_uris:
70+
for uri in audio_uris:
7271
file_results = response.results[uri]
7372
print(f" {file_results.uri}...")
7473
output_bucket, output_object = re.match(
@@ -96,15 +95,8 @@ def transcribe_batch_multiple_files_v2(
9695

9796

9897
if __name__ == "__main__":
99-
parser = argparse.ArgumentParser(
100-
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
101-
)
102-
parser.add_argument("project_id", help="GCP Project ID")
103-
parser.add_argument("gcs_uri", nargs="+", help="URI to GCS file")
104-
parser.add_argument(
105-
"gcs_output_path", help="GCS URI to which to write the transcript"
106-
)
107-
args = parser.parse_args()
108-
transcribe_batch_multiple_files_v2(
109-
args.project_id, args.gcs_uri, args.gcs_output_path
110-
)
98+
audio1 = "gs://cloud-samples-data/speech/audio.flac"
99+
audio2 = "gs://cloud-samples-data/speech/corbeau_renard.flac"
100+
uris_list = [audio1, audio2]
101+
output_bucket_name = "gs://your-bucket-name"
102+
transcribe_batch_multiple_files_v2(uris_list, output_bucket_name)

0 commit comments

Comments
 (0)