Skip to content

Commit a6f61d2

Browse files
authored
Print out transcripts directly. (GoogleCloudPlatform#605)
Printing out the grpc-generated objects results in `str`s with escape sequences for unicode results, instead of the unicode characters themselves.
1 parent 84c8ce3 commit a6f61d2

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

speech/grpc/transcribe.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from grpc.beta import implementations
2323

2424
# Keep the request alive for this many seconds
25-
DEADLINE_SECS = 10
25+
DEADLINE_SECS = 60
2626
SPEECH_SCOPE = 'https://www.googleapis.com/auth/cloud-platform'
2727

2828

@@ -48,7 +48,7 @@ def make_channel(host, port):
4848
return implementations.secure_channel(host, port, composite_channel)
4949

5050

51-
def main(input_uri, encoding, sample_rate):
51+
def main(input_uri, encoding, sample_rate, language_code='en-US'):
5252
service = cloud_speech.beta_create_Speech_stub(
5353
make_channel('speech.googleapis.com', 443))
5454
# The method and parameters can be inferred from the proto from which the
@@ -60,17 +60,21 @@ def main(input_uri, encoding, sample_rate):
6060
# https://goo.gl/KPZn97 for the full list.
6161
encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB
6262
sample_rate=sample_rate, # the rate in hertz
63-
# See
64-
# https://g.co/cloud/speech/docs/best-practices#language_support
65-
# for a list of supported languages.
66-
language_code='en-US', # a BCP-47 language tag
63+
# See https://g.co/cloud/speech/docs/languages for a list of
64+
# supported languages.
65+
language_code=language_code, # a BCP-47 language tag
6766
),
6867
audio=cloud_speech.RecognitionAudio(
6968
uri=input_uri,
7069
)
7170
), DEADLINE_SECS)
72-
# Print the recognition results.
73-
print(response.results)
71+
72+
# Print the recognition result alternatives and confidence scores.
73+
for result in response.results:
74+
print('Result:')
75+
for alternative in result.alternatives:
76+
print(u' ({}): {}'.format(
77+
alternative.confidence, alternative.transcript))
7478

7579

7680
def _gcs_uri(text):

speech/grpc/transcribe_async.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -51,52 +51,54 @@ def make_channel(host, port):
5151
return implementations.secure_channel(host, port, composite_channel)
5252

5353

54-
def main(input_uri, encoding, sample_rate):
54+
def main(input_uri, encoding, sample_rate, language_code='en-US'):
5555
channel = make_channel('speech.googleapis.com', 443)
5656
service = cloud_speech_pb2.beta_create_Speech_stub(channel)
5757
# The method and parameters can be inferred from the proto from which the
5858
# grpc client lib was generated. See:
5959
# https://github.com/googleapis/googleapis/blob/master/google/cloud/speech/v1beta1/cloud_speech.proto
60-
response = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest(
60+
operation = service.AsyncRecognize(cloud_speech_pb2.AsyncRecognizeRequest(
6161
config=cloud_speech_pb2.RecognitionConfig(
6262
# There are a bunch of config options you can specify. See
6363
# https://goo.gl/KPZn97 for the full list.
6464
encoding=encoding, # one of LINEAR16, FLAC, MULAW, AMR, AMR_WB
6565
sample_rate=sample_rate, # the rate in hertz
66-
# See
67-
# https://g.co/cloud/speech/docs/best-practices#language_support
68-
# for a list of supported languages.
69-
language_code='en-US', # a BCP-47 language tag
66+
# See https://g.co/cloud/speech/docs/languages for a list of
67+
# supported languages.
68+
language_code=language_code, # a BCP-47 language tag
7069
),
7170
audio=cloud_speech_pb2.RecognitionAudio(
7271
uri=input_uri,
7372
)
7473
), DEADLINE_SECS)
7574

7675
# Print the longrunning operation handle.
77-
print(response)
76+
print(operation)
7877

7978
# Construct a long running operation endpoint.
8079
service = operations_grpc_pb2.beta_create_Operations_stub(channel)
8180

82-
name = response.name
81+
name = operation.name
8382

8483
while True:
8584
# Give the server a few seconds to process.
8685
print('Waiting for server processing...')
8786
time.sleep(1)
88-
# Get the long running operation with response.
89-
response = service.GetOperation(
87+
operation = service.GetOperation(
9088
operations_grpc_pb2.GetOperationRequest(name=name),
9189
DEADLINE_SECS)
9290

93-
if response.done:
91+
if operation.done:
9492
break
9593

96-
# Print the recognition results.
97-
results = cloud_speech_pb2.AsyncRecognizeResponse()
98-
response.response.Unpack(results)
99-
print(results)
94+
response = cloud_speech_pb2.AsyncRecognizeResponse()
95+
operation.response.Unpack(response)
96+
# Print the recognition result alternatives and confidence scores.
97+
for result in response.results:
98+
print('Result:')
99+
for alternative in result.alternatives:
100+
print(u' ({}): {}'.format(
101+
alternative.confidence, alternative.transcript))
100102

101103

102104
def _gcs_uri(text):

0 commit comments

Comments
 (0)