Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions speech/cloud-client/transcribe_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,9 @@
"""

import argparse
import io
import time


def transcribe_file(speech_file):
"""Transcribe the given audio file asynchronously."""
from google.cloud import speech
speech_client = speech.Client()

with io.open(speech_file, 'rb') as audio_file:
content = audio_file.read()
audio_sample = speech_client.sample(
content,
source_uri=None,
encoding='LINEAR16',
sample_rate_hertz=16000)

operation = audio_sample.long_running_recognize('en-US')

retry_count = 100
while retry_count > 0 and not operation.complete:
retry_count -= 1
time.sleep(2)
operation.poll()

if not operation.complete:
print('Operation not complete and retry limit reached.')
return

alternatives = operation.results
for alternative in alternatives:
print('Transcript: {}'.format(alternative.transcript))
print('Confidence: {}'.format(alternative.confidence))
# [END send_request]


def transcribe_gcs(gcs_uri):
"""Asynchronously transcribes the audio file specified by the gcs_uri."""
from google.cloud import speech
Expand Down Expand Up @@ -98,5 +65,3 @@ def transcribe_gcs(gcs_uri):
args = parser.parse_args()
if args.path.startswith('gs://'):
transcribe_gcs(args.path)
else:
transcribe_file(args.path)
8 changes: 0 additions & 8 deletions speech/cloud-client/transcribe_async_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')


def test_transcribe(capsys):
transcribe_async.transcribe_file(
os.path.join(RESOURCES, 'audio.raw'))
out, err = capsys.readouterr()

assert re.search(r'how old is the Brooklyn Bridge', out, re.DOTALL | re.I)


def test_transcribe_gcs(capsys):
transcribe_async.transcribe_gcs(
'gs://python-docs-samples-tests/speech/audio.raw')
Expand Down