| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9506476 commit facb62d
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,13 +14,14 @@ | |||
| 14 | 14 | ||
| 15 | 15 | """GAX/GAPIC module for managing Speech API requests.""" | |
| 16 | 16 | ||
| 17 | - from google.longrunning import operations_grpc | ||
| 17 | + | ||
| 18 | + from google.cloud._helpers import make_secure_stub | ||
| 19 | + from google.cloud.connection import DEFAULT_USER_AGENT | ||
| 20 | + | ||
| 21 | + from google.cloud.speech.alternative import Alternative | ||
| 22 | + from google.cloud.speech.operation import Operation | ||
| 18 | 23 | ||
| 19 | 24 | 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) | ||
| 24 | 25 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import SpeechContext | |
| 25 | 26 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionConfig | |
| 26 | 27 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import RecognitionAudio | |
@@ -29,24 +30,21 @@ | |||
| 29 | 30 | from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import ( | |
| 30 | 31 | StreamingRecognizeRequest) | |
| 31 | 32 | ||
| 32 | - from google.cloud.speech.alternative import Alternative | ||
| 33 | - from google.cloud._helpers import make_secure_stub | ||
| 34 | - from google.cloud.connection import DEFAULT_USER_AGENT | ||
| 35 | - from google.cloud.speech.operation import Operation | ||
| 36 | - from google.cloud.operation import register_type | ||
| 37 | - | ||
| 33 | + from google.longrunning import operations_grpc | ||
| 38 | 34 | ||
| 39 | 35 | OPERATIONS_API_HOST = 'speech.googleapis.com' | |
| 40 | 36 | ||
| 41 | - register_type(AsyncRecognizeMetadata) | ||
| 42 | - register_type(AsyncRecognizeResponse) | ||
| 43 | - | ||
| 44 | 37 | ||
| 45 | 38 | class GAPICSpeechAPI(object): | |
| 46 | 39 | """Manage calls through GAPIC wrappers to the Speech API.""" | |
| 47 | 40 | def __init__(self, client=None): | |
| 48 | 41 | self._client = client | |
| 49 | 42 | self._gapic_api = SpeechApi() | |
| 43 | + self._operations_stub = make_secure_stub( | ||
| 44 | + self._client.connection.credentials, | ||
| 45 | + DEFAULT_USER_AGENT, | ||
| 46 | + operations_grpc.OperationsStub, | ||
| 47 | + OPERATIONS_API_HOST) | ||
| 50 | 48 | ||
| 51 | 49 | def async_recognize(self, sample, language_code=None, | |
| 52 | 50 | max_alternatives=None, profanity_filter=None, | |
@@ -88,7 +86,7 @@ def async_recognize(self, sample, language_code=None, | |||
| 88 | 86 | and phrases. This can also be used to add new | |
| 89 | 87 | words to the vocabulary of the recognizer. | |
| 90 | 88 | ||
| 91 | - :rtype: :class:`~google.cloud.operation.Opeartion` | ||
| 89 | + :rtype: :class:`~google.cloud.speech.operation.Operation` | ||
| 92 | 90 | :returns: Instance of ``Operation`` to poll for results. | |
| 93 | 91 | """ | |
| 94 | 92 | config = RecognitionConfig( | |
@@ -102,12 +100,7 @@ def async_recognize(self, sample, language_code=None, | |||
| 102 | 100 | api = self._gapic_api | |
| 103 | 101 | response = api.async_recognize(config=config, audio=audio) | |
| 104 | 102 | ||
| 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) | ||
| 103 | + return Operation.from_pb(response, self) | ||
| 111 | 104 | ||
| 112 | 105 | def sync_recognize(self, sample, language_code=None, max_alternatives=None, | |
| 113 | 106 | profanity_filter=None, speech_context=None): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -21,6 +21,26 @@ class TestClient(unittest.TestCase): | |||
| 21 | 21 | AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac' | |
| 22 | 22 | AUDIO_CONTENT = '/9j/4QNURXhpZgAASUkq' | |
| 23 | 23 | ||
| 24 | + @staticmethod | ||
| 25 | + def _make_result(transcript, confidence): | ||
| 26 | + from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 | ||
| 27 | + return cloud_speech_pb2.SpeechRecognitionResult( | ||
| 28 | + alternatives=[ | ||
| 29 | + cloud_speech_pb2.SpeechRecognitionAlternative( | ||
| 30 | + transcript=transcript, | ||
| 31 | + confidence=confidence, | ||
| 32 | + ), | ||
| 33 | + ], | ||
| 34 | + ) | ||
| 35 | + | ||
| 36 | + def _make_sync_response(self, *results): | ||
| 37 | + from google.cloud.grpc.speech.v1beta1 import cloud_speech_pb2 | ||
| 38 | + response = cloud_speech_pb2.SyncRecognizeResponse( | ||
| 39 | + results=results, | ||
| 40 | + ) | ||
| 41 | + | ||
| 42 | + return response | ||
| 43 | + | ||
| 24 | 44 | def _getTargetClass(self): | |
| 25 | 45 | from google.cloud.speech.client import Client | |
| 26 | 46 | ||
@@ -199,7 +219,9 @@ def test_sync_recognize_with_empty_results_gax(self): | |||
| 199 | 219 | credentials = _Credentials() | |
| 200 | 220 | client = self._makeOne(credentials=credentials, use_gax=True) | |
| 201 | 221 | client.connection = _Connection() | |
| 202 | - _MockGAPICSpeechAPI._results = [] | ||
| 222 | + client.connection.credentials = credentials | ||
| 223 | + | ||
| 224 | + _MockGAPICSpeechAPI._response = self._make_sync_response() | ||
| 203 | 225 | ||
| 204 | 226 | with self.assertRaises(ValueError): | |
| 205 | 227 | with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI): | |
@@ -216,18 +238,20 @@ def test_sync_recognize_with_gax(self): | |||
| 216 | 238 | creds = _Credentials() | |
| 217 | 239 | client = self._makeOne(credentials=creds, use_gax=True) | |
| 218 | 240 | client.connection = _Connection() | |
| 241 | + client.connection.credentials = creds | ||
| 219 | 242 | client._speech_api = None | |
| 220 | - _MockGAPICSpeechAPI._results = [_MockGAPICSyncResult] | ||
| 243 | + transcript = 'testing 1 2 3' | ||
| 244 | + confidence = 0.9224355 | ||
| 245 | + result = self._make_result(transcript, confidence) | ||
| 246 | + _MockGAPICSpeechAPI._response = self._make_sync_response(result) | ||
| 221 | 247 | ||
| 222 | 248 | with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI): | |
| 223 | 249 | sample = client.sample(source_uri=self.AUDIO_SOURCE_URI, | |
| 224 | 250 | encoding=speech.Encoding.FLAC, | |
| 225 | 251 | sample_rate=self.SAMPLE_RATE) | |
| 226 | 252 | results = client.sync_recognize(sample) | |
| 227 | - self.assertEqual(results[0].transcript, | ||
| 228 | - _MockGAPICAlternative.transcript) | ||
| 229 | - self.assertEqual(results[0].confidence, | ||
| 230 | - _MockGAPICAlternative.confidence) | ||
| 253 | + self.assertEqual(results[0].transcript, transcript) | ||
| 254 | + self.assertEqual(results[0].confidence, confidence) | ||
| 231 | 255 | ||
| 232 | 256 | def test_async_supported_encodings(self): | |
| 233 | 257 | from google.cloud import speech | |
@@ -270,6 +294,7 @@ def test_async_recognize_with_gax(self): | |||
| 270 | 294 | from google.cloud.speech import _gax as MUT | |
| 271 | 295 | from google.cloud._testing import _Monkey | |
| 272 | 296 | from google.cloud import speech | |
| 297 | + from google.cloud.speech.operation import Operation | ||
| 273 | 298 | ||
| 274 | 299 | credentials = _Credentials() | |
| 275 | 300 | client = self._makeOne(credentials=credentials) | |
@@ -282,6 +307,7 @@ def test_async_recognize_with_gax(self): | |||
| 282 | 307 | with _Monkey(MUT, SpeechApi=_MockGAPICSpeechAPI): | |
| 283 | 308 | operation = client.async_recognize(sample) | |
| 284 | 309 | ||
| 310 | + self.assertIsInstance(operation, Operation) | ||
| 285 | 311 | self.assertFalse(operation.complete) | |
| 286 | 312 | self.assertIsNone(operation.response) | |
| 287 | 313 | ||
@@ -316,32 +342,28 @@ def test_speech_api_preset(self): | |||
| 316 | 342 | self.assertIs(client.speech_api, fake_api) | |
| 317 | 343 | ||
| 318 | 344 | ||
| 319 | - class _MockGAPICAlternative(object): | ||
| 320 | - transcript = 'testing 1 2 3' | ||
| 321 | - confidence = 0.95234356 | ||
| 322 | - | ||
| 323 | - | ||
| 324 | - class _MockGAPICMetadata(object): | ||
| 325 | - type_url = None | ||
| 326 | - | ||
| 327 | - | ||
| 328 | - class _MockGAPICSyncResult(object): | ||
| 329 | - alternatives = [_MockGAPICAlternative()] | ||
| 330 | - | ||
| 331 | - | ||
| 332 | - class _MockGAPICSpeechResponse(object): | ||
| 333 | - error = None | ||
| 334 | - endpointer_type = None | ||
| 335 | - name = None | ||
| 336 | - metadata = _MockGAPICMetadata() | ||
| 337 | - results = [] | ||
| 338 | - result_index = 0 | ||
| 345 | + # class _MockGAPICAlternative(object): | ||
| 346 | + # transcript = 'testing 1 2 3' | ||
| 347 | + # confidence = 0.95234356 | ||
| 348 | + # | ||
| 349 | + # | ||
| 350 | + # class _MockGAPICSyncResult(object): | ||
| 351 | + # alternatives = [_MockGAPICAlternative()] | ||
| 352 | + # | ||
| 353 | + # | ||
| 354 | + # class _MockGAPICSpeechResponse(object): | ||
| 355 | + # error = None | ||
| 356 | + # endpointer_type = None | ||
| 357 | + # name = None | ||
| 358 | + # metadata = _MockGAPICMetadata() | ||
| 359 | + # results = [] | ||
| 360 | + # result_index = 0 | ||
| 339 | 361 | ||
| 340 | 362 | ||
| 341 | 363 | class _MockGAPICSpeechAPI(object): | |
| 342 | 364 | _requests = None | |
| 343 | - _response = _MockGAPICSpeechResponse | ||
| 344 | - _results = [_MockGAPICSyncResult()] | ||
| 365 | + _response = None | ||
| 366 | + _results = None | ||
| 345 | 367 | ||
| 346 | 368 | def async_recognize(self, config, audio): | |
| 347 | 369 | from google.longrunning.operations_pb2 import Operation | |
@@ -353,7 +375,7 @@ def async_recognize(self, config, audio): | |||
| 353 | 375 | def sync_recognize(self, config, audio): | |
| 354 | 376 | self.config = config | |
| 355 | 377 | self.audio = audio | |
| 356 | - self._response.results = self._results | ||
| 378 | + # self._response.results = self._results | ||
| 357 | 379 | return self._response | |
| 358 | 380 | ||
| 359 | 381 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments