def test_streaming_closed_stream(self): from io import BytesIO from google.cloud._testing import _Monkey from google.cloud.speech import _gax from google.cloud.speech.encoding import Encoding stream = BytesIO(b'Some audio data...') credentials = _make_credentials() client = self._make_one(credentials=credentials) client._credentials = credentials channel_args = [] channel_obj = object() def make_channel(*args): channel_args.append(args) return channel_obj def speech_api(channel=None, **kwargs): return _MockGAPICSpeechAPI(channel=channel, **kwargs) host = 'foo.apis.invalid' speech_api.SERVICE_ADDRESS = host stream.close() self.assertTrue(stream.closed) sample = client.sample( stream=stream, encoding=Encoding.LINEAR16, sample_rate_hertz=self.SAMPLE_RATE) with _Monkey(_gax, SpeechClient=speech_api, make_secure_channel=make_channel): client._speech_api = _gax.GAPICSpeechAPI(client) with self.assertRaises(ValueError): list(sample.streaming_recognize(language_code='en-US'))
def test_sync_recognize_with_empty_results_gax(self): from google.cloud import _helpers from google.cloud._testing import _Monkey from google.cloud import speech from google.cloud.speech import _gax credentials = _make_credentials() client = self._make_one(credentials=credentials, _use_grpc=True) client._credentials = credentials def speech_api(channel=None, **kwargs): return _MockGAPICSpeechAPI(response=_make_sync_response(), channel=channel, **kwargs) host = 'foo.apis.invalid' speech_api.SERVICE_ADDRESS = host with _Monkey(_gax, SpeechClient=speech_api): with mock.patch.object(_helpers, 'make_secure_channel') as msc: client._speech_api = _gax.GAPICSpeechAPI(client) low_level = client.speech_api._gapic_api self.assertIsInstance(low_level, _MockGAPICSpeechAPI) self.assertIs(low_level._channel, msc.return_value) assert msc.mock_calls[0] == mock.call( credentials, _gax.DEFAULT_USER_AGENT, host, ) sample = client.sample(source_uri=self.AUDIO_SOURCE_URI, encoding=speech.Encoding.FLAC, sample_rate_hertz=self.SAMPLE_RATE) with self.assertRaises(ValueError): next(sample.recognize(language_code='en-US'))
def test_stream_recognize_no_results(self): from io import BytesIO from google.cloud._testing import _Monkey from google.cloud.speech import _gax from google.cloud.speech.encoding import Encoding stream = BytesIO(b'Some audio data...') credentials = _Credentials() client = self._make_one(credentials=credentials) client.connection = _Connection() client.connection.credentials = credentials responses = [_make_streaming_response()] channel_args = [] channel_obj = object() def make_channel(*args): channel_args.append(args) return channel_obj def speech_api(channel=None): return _MockGAPICSpeechAPI(channel=channel, response=responses) host = 'foo.apis.invalid' speech_api.SERVICE_ADDRESS = host with _Monkey(_gax, SpeechApi=speech_api, make_secure_channel=make_channel): client._speech_api = _gax.GAPICSpeechAPI(client) sample = client.sample(content=stream, encoding=Encoding.LINEAR16, sample_rate=self.SAMPLE_RATE) results = list(client.streaming_recognize(sample)) self.assertEqual(results, [])
def test_stream_recognize_interim_results(self): from io import BytesIO from google.cloud import _helpers from google.cloud._testing import _Monkey from google.cloud.speech import _gax from google.cloud.speech.encoding import Encoding from google.cloud.speech.result import StreamingSpeechResult stream = BytesIO(b'Some audio data...') credentials = _make_credentials() client = self._make_one(credentials=credentials) client._credentials = credentials alternatives = [{ 'transcript': 'testing streaming 1 2 3', 'confidence': 0.5888671875, }, { 'transcript': 'testing streaming 4 5 6', 'confidence': 0.28125, }] first_response = _make_streaming_response( _make_streaming_result([], is_final=False, stability=0.314453125)) second_response = _make_streaming_response( _make_streaming_result(alternatives, is_final=False, stability=0.28125)) last_response = _make_streaming_response( _make_streaming_result(alternatives, is_final=True, stability=0.4375)) responses = [first_response, second_response, last_response] def speech_api(channel=None, **kwargs): return _MockGAPICSpeechAPI(channel=channel, response=responses, **kwargs) host = 'foo.apis.invalid' speech_api.SERVICE_ADDRESS = host with _Monkey(_gax, SpeechClient=speech_api): with mock.patch.object(_helpers, 'make_secure_channel') as msc: client._speech_api = _gax.GAPICSpeechAPI(client) sample = client.sample(stream=stream, encoding=Encoding.LINEAR16, sample_rate_hertz=self.SAMPLE_RATE) results = list( sample.streaming_recognize( interim_results=True, language_code='en-US', )) self.assertEqual(len(results), 3) for result in results: self.assertIsInstance(result, StreamingSpeechResult) result = results[0] self.assertEqual(result.alternatives, []) self.assertFalse(result.is_final) self.assertEqual(result.stability, 0.314453125) result_two = results[1] self.assertEqual(result_two.stability, 0.28125) self.assertFalse(result_two.is_final) self.assertEqual(result_two.transcript, result_two.alternatives[0].transcript, alternatives[0]['transcript']) self.assertEqual(result_two.confidence, result_two.alternatives[0].confidence, alternatives[0]['confidence']) self.assertEqual(result_two.alternatives[1].transcript, alternatives[1]['transcript']) self.assertEqual(result_two.alternatives[1].confidence, alternatives[1]['confidence']) result_three = results[2] self.assertTrue(result_three.is_final) self.assertEqual(result_three.stability, 0.4375) self.assertEqual(result_three.transcript, result_three.alternatives[0].transcript, alternatives[0]['transcript']) self.assertEqual(result_three.confidence, result_three.alternatives[0].confidence, alternatives[0]['confidence'])
def test_sync_recognize_with_gax(self): from google.cloud._testing import _Monkey from google.cloud import speech from google.cloud.speech import _gax creds = _make_credentials() client = self._make_one(credentials=creds, _use_grpc=True) client._credentials = creds alternatives = [{ 'transcript': 'testing 1 2 3', 'confidence': 0.740234375, }, { 'transcript': 'testing 4 5 6', 'confidence': 0.6396484375, }] result = _make_result(alternatives) channel_args = [] channel_obj = object() def make_channel(*args): channel_args.append(args) return channel_obj def speech_api(channel=None, **kwargs): return _MockGAPICSpeechAPI( response=_make_sync_response(result), channel=channel, **kwargs) host = 'foo.apis.invalid' speech_api.SERVICE_ADDRESS = host sample = client.sample( source_uri=self.AUDIO_SOURCE_URI, encoding=speech.Encoding.FLAC, sample_rate_hertz=self.SAMPLE_RATE) with _Monkey(_gax, SpeechClient=speech_api, make_secure_channel=make_channel): client._speech_api = _gax.GAPICSpeechAPI(client) low_level = client.speech_api._gapic_api self.assertIsInstance(low_level, _MockGAPICSpeechAPI) self.assertIs(low_level._channel, channel_obj) self.assertEqual( channel_args, [(creds, _gax.DEFAULT_USER_AGENT, host)]) results = [i for i in sample.recognize(language_code='en-US')] self.assertEqual(len(results), 1) result = results[0] self.assertEqual(len(results[0].alternatives), 2) self.assertEqual( result.transcript, result.alternatives[0].transcript, alternatives[0]['transcript']) self.assertEqual( result.confidence, result.alternatives[0].confidence, alternatives[0]['confidence']) self.assertEqual( result.alternatives[1].transcript, alternatives[1]['transcript']) self.assertEqual( result.alternatives[1].confidence, alternatives[1]['confidence'])
def test_stream_recognize_interim_results(self): from io import BytesIO from google.cloud._testing import _Monkey from google.cloud.speech import _gax from google.cloud.speech.encoding import Encoding from google.cloud.speech.result import StreamingSpeechResult stream = BytesIO(b'Some audio data...') credentials = _make_credentials() client = self._make_one(credentials=credentials) client.connection = _Connection() client.connection.credentials = credentials alternatives = [{ 'transcript': 'testing streaming 1 2 3', 'confidence': 0.9224355, }, { 'transcript': 'testing streaming 4 5 6', 'confidence': 0.0123456, }] first_response = _make_streaming_response( _make_streaming_result([], is_final=False, stability=0.122435)) second_response = _make_streaming_response( _make_streaming_result(alternatives, is_final=False, stability=0.1432343)) last_response = _make_streaming_response( _make_streaming_result(alternatives, is_final=True, stability=0.9834534)) responses = [first_response, second_response, last_response] channel_args = [] channel_obj = object() def make_channel(*args): channel_args.append(args) return channel_obj def speech_api(channel=None): return _MockGAPICSpeechAPI(channel=channel, response=responses) host = 'foo.apis.invalid' speech_api.SERVICE_ADDRESS = host with _Monkey(_gax, SpeechClient=speech_api, make_secure_channel=make_channel): client._speech_api = _gax.GAPICSpeechAPI(client) sample = client.sample(content=stream, encoding=Encoding.LINEAR16, sample_rate=self.SAMPLE_RATE) results = list(sample.streaming_recognize(interim_results=True)) self.assertEqual(len(results), 3) self.assertIsInstance(results[0], StreamingSpeechResult) self.assertEqual(results[0].alternatives, []) self.assertFalse(results[0].is_final) self.assertEqual(results[0].stability, 0.122435) self.assertEqual(results[1].stability, 0.1432343) self.assertFalse(results[1].is_final) self.assertEqual(results[1].alternatives[0].transcript, alternatives[0]['transcript']) self.assertEqual(results[1].alternatives[0].confidence, alternatives[0]['confidence']) self.assertEqual(results[1].alternatives[1].transcript, alternatives[1]['transcript']) self.assertEqual(results[1].alternatives[1].confidence, alternatives[1]['confidence']) self.assertTrue(results[2].is_final) self.assertEqual(results[2].stability, 0.9834534) self.assertEqual(results[2].alternatives[0].transcript, alternatives[0]['transcript']) self.assertEqual(results[2].alternatives[0].confidence, alternatives[0]['confidence'])
def test_sync_recognize_with_gax(self): from google.cloud._testing import _Monkey from google.cloud import speech from google.cloud.speech import _gax creds = _Credentials() client = self._makeOne(credentials=creds, use_gax=True) client.connection = _Connection() client.connection.credentials = creds client._speech_api = None alternatives = [{ 'transcript': 'testing 1 2 3', 'confidence': 0.9224355, }, { 'transcript': 'testing 4 5 6', 'confidence': 0.0123456, }] result = self._make_result(alternatives) channel_args = [] channel_obj = object() def make_channel(*args): channel_args.append(args) return channel_obj def speech_api(channel=None): return _MockGAPICSpeechAPI( response=self._make_sync_response(result), channel=channel) host = 'foo.apis.invalid' speech_api.SERVICE_ADDRESS = host sample = client.sample(source_uri=self.AUDIO_SOURCE_URI, encoding=speech.Encoding.FLAC, sample_rate=self.SAMPLE_RATE) with _Monkey(_gax, SpeechApi=speech_api, make_secure_channel=make_channel): client._speech_api = _gax.GAPICSpeechAPI(client) low_level = client.speech_api._gapic_api self.assertIsInstance(low_level, _MockGAPICSpeechAPI) self.assertIs(low_level._channel, channel_obj) self.assertEqual( channel_args, [(creds, _gax.DEFAULT_USER_AGENT, host)]) results = client.sync_recognize(sample) self.assertEqual(len(results), 2) self.assertEqual(results[0].transcript, alternatives[0]['transcript']) self.assertEqual(results[0].confidence, alternatives[0]['confidence']) self.assertEqual(results[1].transcript, alternatives[1]['transcript']) self.assertEqual(results[1].confidence, alternatives[1]['confidence'])