From 5e4522c45b79b04876c4378218f93b149b0982ba Mon Sep 17 00:00:00 2001 From: Franklin Nunez <69214580+b-loved-dreamer@users.noreply.github.com> Date: Mon, 15 Nov 2021 09:36:25 -0800 Subject: [PATCH] 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 --- speech/snippets/speech_to_storage_beta.py | 44 +++++++++++++++---- .../snippets/speech_to_storage_beta_test.py | 6 ++- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/speech/snippets/speech_to_storage_beta.py b/speech/snippets/speech_to_storage_beta.py index d6adcc8ae51b..a807a72af4e2 100644 --- a/speech/snippets/speech_to_storage_beta.py +++ b/speech/snippets/speech_to_storage_beta.py @@ -15,11 +15,19 @@ # [START speech_transcribe_with_speech_to_storage_beta] -from google.cloud import speech_v1p1beta1 as speech +from google.cloud import speech +from google.cloud import storage +from google.cloud.speech_v1 import types def export_transcript_to_storage_beta( - input_storage_uri, output_storage_uri, encoding, sample_rate_hertz, language_code + input_storage_uri, + output_storage_uri, + encoding, + sample_rate_hertz, + language_code, + bucket_name, + object_name, ): # input_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE] @@ -40,20 +48,38 @@ def export_transcript_to_storage_beta( audio=audio, config=config, output_config=output_config ) - # Create the speech client + # create the speech client speech_client = speech.SpeechClient() + # create the storage client + storage_client = storage.Client() + + # run the recognizer to export transcript operation = speech_client.long_running_recognize(request=request) print("Waiting for operation to complete...") - response = operation.result(timeout=90) + operation.result(timeout=90) + + # get bucket with name + bucket = storage_client.get_bucket(bucket_name) + + # get blob from bucket + blob = bucket.get_blob(object_name) + + # get content as string + results_string = blob.download_as_string() + + # get transcript exported in storage bucket + storage_transcript = types.LongRunningRecognizeResponse.from_json( + results_string, ignore_unknown_fields=True + ) # 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: + for result in storage_transcript.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)) + print(f"Transcript: {result.alternatives[0].transcript}") + print(f"Confidence: {result.alternatives[0].confidence}") - # [END speech_transcribe_with_speech_to_storage_beta] - return response.results[0].alternatives[0].transcript +# [END speech_transcribe_with_speech_to_storage_beta] + return storage_transcript.results diff --git a/speech/snippets/speech_to_storage_beta_test.py b/speech/snippets/speech_to_storage_beta_test.py index af90719e7845..88a8017d98ff 100644 --- a/speech/snippets/speech_to_storage_beta_test.py +++ b/speech/snippets/speech_to_storage_beta_test.py @@ -38,14 +38,16 @@ def test_export_transcript_to_storage_beta(bucket, capsys): - transcript = speech_to_storage_beta.export_transcript_to_storage_beta( + results = speech_to_storage_beta.export_transcript_to_storage_beta( INPUT_STORAGE_URI, OUTPUT_STORAGE_URI, encoding, sample_rate_hertz, language_code, + BUCKET_NAME, + BUCKET_PREFIX, ) - assert "I'm here" in transcript + assert len(results) > 0 @pytest.fixture