Skip to content

Commit

Permalink
docs(samples): a draft that refactors speech_to_storage_beta sample (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
b-loved-dreamer authored and dandhlee committed Feb 9, 2023
1 parent 9b38d3d commit 0f60e6b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
44 changes: 35 additions & 9 deletions speech/snippets/speech_to_storage_beta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
6 changes: 4 additions & 2 deletions speech/snippets/speech_to_storage_beta_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0f60e6b

Please sign in to comment.