Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Speech GAPIC for sync_recognize. #2615

Merged
merged 1 commit into from
Oct 26, 2016

Conversation

daspecster
Copy link
Contributor

This adds GAPIC support to speech sync_recognize().

@daspecster daspecster added the api: speech Issues related to the Speech-to-Text API. label Oct 25, 2016
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Oct 25, 2016
@daspecster daspecster mentioned this pull request Oct 25, 2016
14 tasks
Copy link
Contributor

@dhermes dhermes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments, as discussed in chat, the impl. should resemble the Pub/Sub and Logging implementations, i.e. separation of concerns by having HTTP-only and GAX-only API objects.

@@ -40,6 +40,19 @@ def from_api_repr(cls, transcript):
"""
return cls(transcript['transcript'], transcript['confidence'])

@classmethod
def from_pb(cls, transcript):

This comment was marked as spam.

def from_pb(cls, transcript):
"""Factory: construct ``Transcript`` from protobuf response.

:type transcript: :class:`SpeechRecognitionAlternative`

This comment was marked as spam.

:param transcript: Instance of ``SpeechRecognitionAlternative``
from protobuf.

:rtype: :class:`~Transcript`

This comment was marked as spam.

max_alternatives=2,
profanity_filter=True,
speech_context=self.HINTS)
with _Monkey(MUT, _USE_GAX=False):

This comment was marked as spam.

@@ -142,7 +161,8 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):

sample = Sample(source_uri=self.AUDIO_SOURCE_URI, encoding=encoding,
sample_rate=self.SAMPLE_RATE)
response = client.sync_recognize(sample)
with _Monkey(MUT, _USE_GAX=False):

This comment was marked as spam.

encoding=speech.Encoding.FLAC,
sample_rate=self.SAMPLE_RATE)
client.sync_recognize(sample)
with self.assertRaises(IndexError):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

sample_rate=self.SAMPLE_RATE)
client.sync_recognize(sample)
with self.assertRaises(IndexError):
with _Monkey(MUT, _USE_GAX=False):

This comment was marked as spam.

@@ -207,6 +248,46 @@ def test_async_recognize(self):
self.assertIsNone(operation.metadata)


class _RecognitionConfig(object):
def __init__(self, *args, **kwargs):

This comment was marked as spam.

Copy link
Contributor

@dhermes dhermes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big 👍 on the refactor

@@ -0,0 +1,76 @@

This comment was marked as spam.

_USE_GAX = _HAVE_GAX and not _DISABLE_GAX


class Client(JSONClient):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

try:
from google.cloud.speech._gax import GAPICSpeechAPI
except ImportError: # pragma: NO COVER
_HAVE_GAX = False

This comment was marked as spam.

"""Helper for speech-related API calls."""
if self._speech_api is None:
if self._use_gax:
self._speech_api = GAPICSpeechAPI()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


api_response = self.connection.api_request(
class JSONSpeechAPI(object):

This comment was marked as spam.

@@ -206,6 +244,90 @@ def test_async_recognize(self):
self.assertFalse(operation.complete)
self.assertIsNone(operation.metadata)

def test_speech_api_with_gapic(self):

This comment was marked as spam.

api = client.speech_api
self.assertIsInstance(api, _MockGAPICSpeechAPI)

def test_speech_api_without_gapic(self):

This comment was marked as spam.

api = client.speech_api
self.assertIsInstance(api, _MockJSONSpeechAPI)

def test_speech_api_preset(self):

This comment was marked as spam.



class GAPICSpeechAPI(object):
"""Object to manage calls through GAPIC wrappers to the Speech API."""

This comment was marked as spam.

uri=sample.source_uri)
api = self._gapic_api
api_response = api.sync_recognize(config=config, audio=audio)
results = api_response.results.pop()

This comment was marked as spam.

@@ -51,6 +51,8 @@

REQUIREMENTS = [
'google-cloud-core >= 0.20.0',
'gapic-google-cloud-speech-v1beta1==0.11.1',
'grpc-google-cloud-speech-v1beta1==0.11.1',

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@@ -91,18 +100,19 @@ def test_sync_recognize_content_with_optional_parameters(self):
}
}
credentials = _Credentials()
client = self._makeOne(credentials=credentials)
client = self._makeOne(credentials=credentials, use_gax=False)
client.connection = _Connection(RETURNED)

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@daspecster
Copy link
Contributor Author

It looks like SpeechApi() does some credential digging.
https://travis-ci.org/GoogleCloudPlatform/google-cloud-python/builds/170660357#L201-L212

So I _Monkey'd SpeechApi back.

@dhermes I think I got everything you covered there. LMKWYT.

@daspecster
Copy link
Contributor Author

Yeah I was trying something with testing. I just pushed that back up.

@daspecster
Copy link
Contributor Author

daspecster commented Oct 26, 2016

@daspecster daspecster force-pushed the add-speech-sync-gapic branch 2 times, most recently from d3d3ced to 9c3a578 Compare October 26, 2016 04:38
@daspecster
Copy link
Contributor Author

Ok that should be resolved now.

    'gapic-google-cloud-speech-v1beta1>=0.11.1,<0.12.0',
    'grpc-google-cloud-speech-v1beta1>=0.11.1,<0.12.0',

@dhermes
Copy link
Contributor

dhermes commented Oct 26, 2016

LGTM.

@daspecster daspecster merged commit 2e6ac38 into googleapis:master Oct 26, 2016
@daspecster daspecster deleted the add-speech-sync-gapic branch October 26, 2016 05:25
richkadel pushed a commit to richkadel/google-cloud-python that referenced this pull request May 6, 2017
atulep pushed a commit that referenced this pull request Apr 3, 2023
atulep pushed a commit that referenced this pull request Apr 18, 2023
parthea pushed a commit that referenced this pull request Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: speech Issues related to the Speech-to-Text API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants