-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add Speech GAPIC for sync_recognize. #2615
Conversation
There was a problem hiding this 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.
This comment was marked as spam.
Sorry, something went wrong.
def from_pb(cls, transcript): | ||
"""Factory: construct ``Transcript`` from protobuf response. | ||
|
||
:type transcript: :class:`SpeechRecognitionAlternative` |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
:param transcript: Instance of ``SpeechRecognitionAlternative`` | ||
from protobuf. | ||
|
||
:rtype: :class:`~Transcript` |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
max_alternatives=2, | ||
profanity_filter=True, | ||
speech_context=self.HINTS) | ||
with _Monkey(MUT, _USE_GAX=False): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this 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.
This comment was marked as spam.
Sorry, something went wrong.
_USE_GAX = _HAVE_GAX and not _DISABLE_GAX | ||
|
||
|
||
class Client(JSONClient): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
try: | ||
from google.cloud.speech._gax import GAPICSpeechAPI | ||
except ImportError: # pragma: NO COVER | ||
_HAVE_GAX = False |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
"""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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
api_response = self.connection.api_request( | ||
class JSONSpeechAPI(object): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
This comment was marked as spam.
Sorry, something went wrong.
api = client.speech_api | ||
self.assertIsInstance(api, _MockGAPICSpeechAPI) | ||
|
||
def test_speech_api_without_gapic(self): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
api = client.speech_api | ||
self.assertIsInstance(api, _MockJSONSpeechAPI) | ||
|
||
def test_speech_api_preset(self): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
|
||
class GAPICSpeechAPI(object): | ||
"""Object to manage calls through GAPIC wrappers to the Speech API.""" |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
7e2e8f4
to
e24e96a
Compare
It looks like So I @dhermes I think I got everything you covered there. LMKWYT. |
Yeah I was trying something with testing. I just pushed that back up. |
Ok yeah, that causes an error. |
d3d3ced
to
9c3a578
Compare
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', |
LGTM. |
29cb827
to
9031a0f
Compare
Add Speech GAPIC for sync_recognize.
Add Speech GAPIC for sync_recognize.
Add Speech GAPIC for sync_recognize.
Add Speech GAPIC for sync_recognize.
This adds GAPIC support to speech sync_recognize().