-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: adds speech export to gcs sample (#176)
- Loading branch information
1 parent
2b0cf74
commit d45ed1f
Showing
6 changed files
with
166 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/google-cloud-python-speech/samples/snippets/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
google-cloud-speech==2.4.0 | ||
google-cloud-storage==1.38.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/google-cloud-python-speech/samples/snippets/speech_to_storage_beta.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
# [START speech_transcribe_with_speech_to_storage_beta] | ||
|
||
from google.cloud import speech_v1p1beta1 as speech | ||
|
||
|
||
def export_transcript_to_storage_beta( | ||
input_storage_uri, output_storage_uri, encoding, sample_rate_hertz, language_code | ||
): | ||
|
||
# input_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE] | ||
audio = speech.RecognitionAudio(uri=input_storage_uri) | ||
|
||
# Pass in the URI of the Cloud Storage bucket to hold the transcription | ||
output_config = speech.TranscriptOutputConfig(gcs_uri=output_storage_uri) | ||
|
||
# Speech configuration object | ||
config = speech.RecognitionConfig( | ||
encoding=encoding, | ||
sample_rate_hertz=sample_rate_hertz, | ||
language_code=language_code, | ||
) | ||
|
||
# Compose the long-running request | ||
request = speech.LongRunningRecognizeRequest( | ||
audio=audio, config=config, output_config=output_config | ||
) | ||
|
||
# Create the speech client | ||
speech_client = speech.SpeechClient() | ||
|
||
operation = speech_client.long_running_recognize(request=request) | ||
|
||
print("Waiting for operation to complete...") | ||
response = operation.result(timeout=90) | ||
|
||
# Each result is for a consecutive portion of the audio. Iterate through | ||
# them to get the transcripts for the entire audio file. | ||
for result in response.results: | ||
# The first alternative is the most likely one for this portion. | ||
print("Transcript: {}".format(result.alternatives[0].transcript)) | ||
print("Confidence: {}".format(result.alternatives[0].confidence)) | ||
|
||
# [END speech_transcribe_with_speech_to_storage_beta] | ||
return response.results[0].alternatives[0].transcript |
66 changes: 66 additions & 0 deletions
66
packages/google-cloud-python-speech/samples/snippets/speech_to_storage_beta_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2021 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import uuid | ||
|
||
from google.cloud import speech_v1p1beta1 as speech | ||
from google.cloud import storage | ||
import pytest | ||
|
||
import speech_to_storage_beta | ||
|
||
STORAGE_URI = "gs://cloud-samples-data/speech/brooklyn_bridge.raw" | ||
|
||
|
||
storage_client = storage.Client() | ||
|
||
BUCKET_UUID = str(uuid.uuid4())[:8] | ||
BUCKET_NAME = f"speech-{BUCKET_UUID}" | ||
BUCKET_PREFIX = "export-transcript-output-test" | ||
DELIMETER = None | ||
|
||
INPUT_STORAGE_URI = "gs://cloud-samples-data/speech/commercial_mono.wav" | ||
OUTPUT_STORAGE_URI = f"gs://{BUCKET_NAME}/{BUCKET_PREFIX}" | ||
encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16 | ||
sample_rate_hertz = 8000 | ||
language_code = "en-US" | ||
|
||
|
||
def test_export_transcript_to_storage_beta(bucket, capsys): | ||
transcript = speech_to_storage_beta.export_transcript_to_storage_beta( | ||
INPUT_STORAGE_URI, | ||
OUTPUT_STORAGE_URI, | ||
encoding, | ||
sample_rate_hertz, | ||
language_code, | ||
) | ||
assert "I'm here" in transcript | ||
|
||
|
||
@pytest.fixture | ||
def bucket(): | ||
"""Yields a bucket that is deleted after the test completes.""" | ||
bucket = None | ||
while bucket is None or bucket.exists(): | ||
bucket = storage_client.bucket(BUCKET_NAME) | ||
bucket.storage_class = "COLDLINE" | ||
storage_client.create_bucket(bucket, location="us") | ||
yield bucket | ||
|
||
blobs = storage_client.list_blobs(BUCKET_NAME, prefix=BUCKET_PREFIX) | ||
|
||
for blob in blobs: | ||
blob.delete() | ||
|
||
bucket.delete(force=True) |