Skip to content

Commit 5078aa4

Browse files
committed
Add speech async GAPIC.
1 parent 1f8a79d commit 5078aa4

File tree

13 files changed

+86
-443
lines changed

13 files changed

+86
-443
lines changed

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: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@
1414

1515
"""GAX/GAPIC module for managing Speech API requests."""
1616

17+
from google.longrunning import operations_grpc
18+
1719
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)
1824
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import SpeechContext
1925
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionConfig
2026
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio
@@ -23,13 +29,23 @@
2329
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
2430
StreamingRecognizeRequest)
2531

26-
32+
from google.cloud._helpers import make_secure_stub
33+
from google.cloud.connection import DEFAULT_USER_AGENT
2734
from google.cloud.speech.transcript import Transcript
35+
from google.cloud.operation import Operation
36+
from google.cloud.operation import register_type
37+
38+
39+
OPERATIONS_API_HOST = 'speech.googleapis.com'
40+
41+
register_type(AsyncRecognizeMetadata)
42+
register_type(AsyncRecognizeResponse)
2843

2944

3045
class GAPICSpeechAPI(object):
3146
"""Manage calls through GAPIC wrappers to the Speech API."""
32-
def __init__(self):
47+
def __init__(self, client=None):
48+
self._client = client
3349
self._gapic_api = SpeechApi()
3450

3551
def async_recognize(self, sample, language_code=None,
@@ -72,9 +88,26 @@ def async_recognize(self, sample, language_code=None,
7288
and phrases. This can also be used to add new
7389
words to the vocabulary of the recognizer.
7490
75-
:raises NotImplementedError: Always.
91+
:rtype: :class:`~google.cloud.operation.Opeartion`
92+
:returns: Instance of ``Operation`` to poll for results.
7693
"""
77-
raise NotImplementedError
94+
config = RecognitionConfig(
95+
encoding=sample.encoding, sample_rate=sample.sample_rate,
96+
language_code=language_code, max_alternatives=max_alternatives,
97+
profanity_filter=profanity_filter,
98+
speech_context=SpeechContext(phrases=speech_context))
99+
100+
audio = RecognitionAudio(content=sample.content,
101+
uri=sample.source_uri)
102+
api = self._gapic_api
103+
response = api.async_recognize(config=config, audio=audio)
104+
105+
self._client._operations_stub = make_secure_stub(
106+
self._client.connection.credentials,
107+
DEFAULT_USER_AGENT,
108+
operations_grpc.OperationsStub,
109+
OPERATIONS_API_HOST)
110+
return Operation.from_pb(response, self._client)
78111

79112
def sync_recognize(self, sample, language_code=None, max_alternatives=None,
80113
profanity_filter=None, speech_context=None):

speech/google/cloud/speech/client.py

Lines changed: 3 additions & 3 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

@@ -161,7 +161,7 @@ def speech_api(self):
161161
"""Helper for speech-related API calls."""
162162
if self._speech_api is None:
163163
if self._use_gax:
164-
self._speech_api = GAPICSpeechAPI()
164+
self._speech_api = GAPICSpeechAPI(self)
165165
else:
166166
self._speech_api = _JSONSpeechAPI(self)
167167
return self._speech_api
@@ -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 & 78 deletions
This file was deleted.

speech/google/cloud/speech/operation.py

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

speech/setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
REQUIREMENTS = [
5353
'google-cloud-core >= 0.20.0',
54+
'grpcio >= 1.0.0, < 2.0dev',
55+
'google-gax >= 0.14.1, < 0.15dev',
5456
'gapic-google-cloud-speech-v1beta1 >= 0.11.1, < 0.12.0',
5557
'grpc-google-cloud-speech-v1beta1 >= 0.11.1, < 0.12.0',
5658
]

0 commit comments

Comments
 (0)