Skip to content

Commit

Permalink
Add speech async GAPIC.
Browse files Browse the repository at this point in the history
  • Loading branch information
daspecster committed Nov 2, 2016
1 parent 1f8a79d commit 5078aa4
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 443 deletions.
2 changes: 0 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@
speech-usage
Client <speech-client>
speech-encoding
speech-metadata
speech-operation
speech-sample
speech-transcript

Expand Down
7 changes: 0 additions & 7 deletions docs/speech-metadata.rst

This file was deleted.

7 changes: 0 additions & 7 deletions docs/speech-operation.rst

This file was deleted.

7 changes: 4 additions & 3 deletions docs/speech-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ See: `Speech Asynchronous Recognize`_
>>> operation.complete
True
>>> for result in operation.results:
... print('=' * 20)
... print(result.transcript)
... print(result.confidence)
... print('=' * 20)
... for alternative in result.alternatives:
... print(alternative.transcript)
... print(alternative.confidence)
====================
'how old is the Brooklyn Bridge'
0.98267895
Expand Down
41 changes: 37 additions & 4 deletions speech/google/cloud/speech/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@

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

from google.longrunning import operations_grpc

from google.cloud.gapic.speech.v1beta1.speech_api import SpeechApi
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
AsyncRecognizeMetadata)
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
AsyncRecognizeResponse)
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import SpeechContext
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionConfig
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio
Expand All @@ -23,13 +29,23 @@
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
StreamingRecognizeRequest)


from google.cloud._helpers import make_secure_stub
from google.cloud.connection import DEFAULT_USER_AGENT
from google.cloud.speech.transcript import Transcript
from google.cloud.operation import Operation
from google.cloud.operation import register_type


OPERATIONS_API_HOST = 'speech.googleapis.com'

register_type(AsyncRecognizeMetadata)
register_type(AsyncRecognizeResponse)


class GAPICSpeechAPI(object):
"""Manage calls through GAPIC wrappers to the Speech API."""
def __init__(self):
def __init__(self, client=None):
self._client = client
self._gapic_api = SpeechApi()

def async_recognize(self, sample, language_code=None,
Expand Down Expand Up @@ -72,9 +88,26 @@ def async_recognize(self, sample, language_code=None,
and phrases. This can also be used to add new
words to the vocabulary of the recognizer.
:raises NotImplementedError: Always.
:rtype: :class:`~google.cloud.operation.Opeartion`
:returns: Instance of ``Operation`` to poll for results.
"""
raise NotImplementedError
config = RecognitionConfig(
encoding=sample.encoding, sample_rate=sample.sample_rate,
language_code=language_code, max_alternatives=max_alternatives,
profanity_filter=profanity_filter,
speech_context=SpeechContext(phrases=speech_context))

audio = RecognitionAudio(content=sample.content,
uri=sample.source_uri)
api = self._gapic_api
response = api.async_recognize(config=config, audio=audio)

self._client._operations_stub = make_secure_stub(
self._client.connection.credentials,
DEFAULT_USER_AGENT,
operations_grpc.OperationsStub,
OPERATIONS_API_HOST)
return Operation.from_pb(response, self._client)

def sync_recognize(self, sample, language_code=None, max_alternatives=None,
profanity_filter=None, speech_context=None):
Expand Down
6 changes: 3 additions & 3 deletions speech/google/cloud/speech/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from google.cloud.environment_vars import DISABLE_GRPC
from google.cloud.speech.connection import Connection
from google.cloud.speech.encoding import Encoding
from google.cloud.speech.operation import Operation
from google.cloud.operation import Operation
from google.cloud.speech.sample import Sample
from google.cloud.speech.transcript import Transcript

Expand Down Expand Up @@ -161,7 +161,7 @@ def speech_api(self):
"""Helper for speech-related API calls."""
if self._speech_api is None:
if self._use_gax:
self._speech_api = GAPICSpeechAPI()
self._speech_api = GAPICSpeechAPI(self)
else:
self._speech_api = _JSONSpeechAPI(self)
return self._speech_api
Expand Down Expand Up @@ -287,7 +287,7 @@ def async_recognize(self, sample, language_code=None,
api_response = self._connection.api_request(
method='POST', path='speech:asyncrecognize', data=data)

return Operation.from_api_repr(self, api_response)
return Operation.from_dict(api_response, self)

def sync_recognize(self, sample, language_code=None, max_alternatives=None,
profanity_filter=None, speech_context=None):
Expand Down
78 changes: 0 additions & 78 deletions speech/google/cloud/speech/metadata.py

This file was deleted.

133 changes: 0 additions & 133 deletions speech/google/cloud/speech/operation.py

This file was deleted.

2 changes: 2 additions & 0 deletions speech/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

REQUIREMENTS = [
'google-cloud-core >= 0.20.0',
'grpcio >= 1.0.0, < 2.0dev',
'google-gax >= 0.14.1, < 0.15dev',
'gapic-google-cloud-speech-v1beta1 >= 0.11.1, < 0.12.0',
'grpc-google-cloud-speech-v1beta1 >= 0.11.1, < 0.12.0',
]
Expand Down
Loading

0 comments on commit 5078aa4

Please sign in to comment.