Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d0e0042

Browse filesBrowse files
authored
Print the actual transcript. (GoogleCloudPlatform#601)
* Print the actual transcript. Also - print interim results as they come, overwriting them until you get the final result. * Fix test
1 parent 4127784 commit d0e0042
Copy full SHA for d0e0042

File tree

Expand file treeCollapse file tree

2 files changed

+36
-14
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+36
-14
lines changed

‎speech/grpc/transcribe_streaming.py

Copy file name to clipboardExpand all lines: speech/grpc/transcribe_streaming.py
+35-13Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import contextlib
2020
import re
2121
import signal
22+
import sys
2223
import threading
2324

2425
from google.cloud import credentials
@@ -131,13 +132,15 @@ def record_audio(rate, chunk):
131132
# [END audio_stream]
132133

133134

134-
def request_stream(data_stream, rate):
135+
def request_stream(data_stream, rate, interim_results=True):
135136
"""Yields `StreamingRecognizeRequest`s constructed from a recording audio
136137
stream.
137138
138139
Args:
139140
data_stream: A generator that yields raw audio data to send.
140141
rate: The sampling rate in hertz.
142+
interim_results: Whether to return intermediate results, before the
143+
transcription is finalized.
141144
"""
142145
# The initial request must contain metadata about the stream, so the
143146
# server knows how to interpret it.
@@ -146,12 +149,12 @@ def request_stream(data_stream, rate):
146149
# https://goo.gl/KPZn97 for the full list.
147150
encoding='LINEAR16', # raw 16-bit signed LE samples
148151
sample_rate=rate, # the rate in hertz
149-
# See
150-
# https://g.co/cloud/speech/docs/best-practices#language_support
152+
# See http://g.co/cloud/speech/docs/languages
151153
# for a list of supported languages.
152154
language_code='en-US', # a BCP-47 language tag
153155
)
154156
streaming_config = cloud_speech.StreamingRecognitionConfig(
157+
interim_results=interim_results,
155158
config=recognition_config,
156159
)
157160

@@ -164,21 +167,40 @@ def request_stream(data_stream, rate):
164167

165168

166169
def listen_print_loop(recognize_stream):
170+
num_chars_printed = 0
167171
for resp in recognize_stream:
168172
if resp.error.code != code_pb2.OK:
169173
raise RuntimeError('Server error: ' + resp.error.message)
170174

171-
# Display the transcriptions & their alternatives
172-
for result in resp.results:
173-
print(result.alternatives)
175+
if not resp.results:
176+
continue
174177

175-
# Exit recognition if any of the transcribed phrases could be
176-
# one of our keywords.
177-
if any(re.search(r'\b(exit|quit)\b', alt.transcript, re.I)
178-
for result in resp.results
179-
for alt in result.alternatives):
180-
print('Exiting..')
181-
break
178+
# Display the top transcription
179+
result = resp.results[0]
180+
transcript = result.alternatives[0].transcript
181+
182+
# Display interim results, but with a carriage return at the end of the
183+
# line, so subsequent lines will overwrite them.
184+
if not result.is_final:
185+
# If the previous result was longer than this one, we need to print
186+
# some extra spaces to overwrite the previous result
187+
overwrite_chars = ' ' * max(0, num_chars_printed - len(transcript))
188+
189+
sys.stdout.write(transcript + overwrite_chars + '\r')
190+
sys.stdout.flush()
191+
192+
num_chars_printed = len(transcript)
193+
194+
else:
195+
print(transcript)
196+
197+
# Exit recognition if any of the transcribed phrases could be
198+
# one of our keywords.
199+
if re.search(r'\b(exit|quit)\b', transcript, re.I):
200+
print('Exiting..')
201+
break
202+
203+
num_chars_printed = 0
182204

183205

184206
def main():

‎speech/grpc/transcribe_streaming_test.py

Copy file name to clipboardExpand all lines: speech/grpc/transcribe_streaming_test.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,4 @@ def test_main(resource, monkeypatch, capsys):
6262
transcribe_streaming.main()
6363
out, err = capsys.readouterr()
6464

65-
assert re.search(r'transcript.*"quit"', out, re.DOTALL | re.I)
65+
assert re.search(r'quit', out, re.DOTALL | re.I)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.