Skip to content

Commit 0e14c9d

Browse files
committed
Add speech async GAPIC.
1 parent 0418c88 commit 0e14c9d

12 files changed

Lines changed: 61 additions & 561 deletions

File tree

docs/index.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@
176176
speech-usage
177177
Client <speech-client>
178178
speech-encoding
179-
speech-metadata
180-
speech-operation
181179
speech-sample
182180
speech-transcript
183181

docs/speech-metadata.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/speech-operation.rst

Lines changed: 0 additions & 7 deletions
This file was deleted.

docs/speech-usage.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ See: `Speech Asynchronous Recognize`_
7070
>>> operation.complete
7171
True
7272
>>> for result in operation.results:
73-
... print('=' * 20)
74-
... print(result.transcript)
75-
... print(result.confidence)
73+
... print('=' * 20)
74+
... for alternative in result.alternatives:
75+
... print(alternative.transcript)
76+
... print(alternative.confidence)
7677
====================
7778
'how old is the Brooklyn Bridge'
7879
0.98267895

speech/google/cloud/speech/_gax.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from google.longrunning import operations_grpc
1818

1919
from google.cloud.gapic.speech.v1beta1.speech_api import SpeechApi
20+
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
21+
AsyncRecognizeMetadata)
22+
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
23+
AsyncRecognizeResponse)
2024
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import SpeechContext
2125
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionConfig
2226
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio
@@ -25,14 +29,18 @@
2529
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
2630
StreamingRecognizeRequest)
2731

28-
2932
from google.cloud._helpers import make_secure_stub
3033
from google.cloud.connection import DEFAULT_USER_AGENT
3134
from google.cloud.speech.transcript import Transcript
3235
from google.cloud.operation import Operation
36+
from google.cloud.operation import register_type
37+
3338

3439
OPERATIONS_API_HOST = 'speech.googleapis.com'
3540

41+
register_type(AsyncRecognizeMetadata)
42+
register_type(AsyncRecognizeResponse)
43+
3644

3745
class GAPICSpeechAPI(object):
3846
"""Manage calls through GAPIC wrappers to the Speech API."""
@@ -80,7 +88,8 @@ def async_recognize(self, sample, language_code=None,
8088
and phrases. This can also be used to add new
8189
words to the vocabulary of the recognizer.
8290
83-
:raises NotImplementedError: Always.
91+
:rtype: :class:`~google.cloud.operation.Opeartion`
92+
:returns: Instance of ``Operation`` to poll for results.
8493
"""
8594
config = RecognitionConfig(
8695
encoding=sample.encoding, sample_rate=sample.sample_rate,
@@ -90,15 +99,15 @@ def async_recognize(self, sample, language_code=None,
9099

91100
audio = RecognitionAudio(content=sample.content,
92101
uri=sample.source_uri)
102+
api = self._gapic_api
103+
response = api.async_recognize(config=config, audio=audio)
104+
93105
self._client._operations_stub = make_secure_stub(
94106
self._client.connection.credentials,
95107
DEFAULT_USER_AGENT,
96108
operations_grpc.OperationsStub,
97109
OPERATIONS_API_HOST)
98-
99-
api = self._gapic_api
100-
operation = api.sync_recognize(config=config, audio=audio)
101-
return Operation.from_pb(operation, self._client)
110+
return Operation.from_pb(response, self._client)
102111

103112
def sync_recognize(self, sample, language_code=None, max_alternatives=None,
104113
profanity_filter=None, speech_context=None):

speech/google/cloud/speech/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from google.cloud.environment_vars import DISABLE_GRPC
2424
from google.cloud.speech.connection import Connection
2525
from google.cloud.speech.encoding import Encoding
26-
from google.cloud.speech.operation import Operation
26+
from google.cloud.operation import Operation
2727
from google.cloud.speech.sample import Sample
2828
from google.cloud.speech.transcript import Transcript
2929

@@ -287,7 +287,7 @@ def async_recognize(self, sample, language_code=None,
287287
api_response = self._connection.api_request(
288288
method='POST', path='speech:asyncrecognize', data=data)
289289

290-
return Operation.from_api_repr(self, api_response)
290+
return Operation.from_dict(api_response, self)
291291

292292
def sync_recognize(self, sample, language_code=None, max_alternatives=None,
293293
profanity_filter=None, speech_context=None):

speech/google/cloud/speech/metadata.py

Lines changed: 0 additions & 94 deletions
This file was deleted.

0 commit comments

Comments
 (0)