def request_stream(data_stream, rate, interim_results=False): """Yields `StreamingRecognizeRequest`s constructed from a recording audio stream. Args: data_stream: A generator that yields raw audio data to send. rate: The sampling rate in hertz. interim_results: Whether to return intermediate results, before the transcription is finalized. """ # The initial request must contain metadata about the stream, so the # server knows how to interpret it. recognition_config = cloud_speech_pb2.RecognitionConfig( # There are a bunch of config options you can specify. See # https://goo.gl/KPZn97 for the full list. encoding='LINEAR16', # raw 16-bit signed LE samples sample_rate=rate, # the rate in hertz # See http://g.co/cloud/speech/docs/languages # for a list of supported languages. language_code='en-US', # a BCP-47 language tag ) streaming_config = cloud_speech_pb2.StreamingRecognitionConfig( interim_results=interim_results, config=recognition_config, ) yield cloud_speech_pb2.StreamingRecognizeRequest( streaming_config=streaming_config) for data in data_stream: # Subsequent requests can all just have the content yield cloud_speech_pb2.StreamingRecognizeRequest(audio_content=data)
def request_stream(data_stream, rate): recognition_config = cloud_speech.RecognitionConfig( encoding='LINEAR16', sample_rate_hertz=rate, language_code='ko-KR' ) streaming_config = cloud_speech.StreamingRecognitionConfig( config=recognition_config ) yield cloud_speech.StreamingRecognizeRequest( streaming_config=streaming_config) for data in data_stream: yield cloud_speech.StreamingRecognizeRequest(audio_content=data)
def test_streaming_recognize(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = speech_client.SpeechClient() # Mock request request = cloud_speech_pb2.StreamingRecognizeRequest() requests = [request] # Mock response expected_response = cloud_speech_pb2.StreamingRecognizeResponse() grpc_stub.StreamingRecognize.return_value = iter([expected_response]) response = client.streaming_recognize(requests) resources = list(response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response, resources[0]) grpc_stub.StreamingRecognize.assert_called_once() args, kwargs = grpc_stub.StreamingRecognize.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_requests = args[0] self.assertEqual(1, len(actual_requests)) actual_request = list(actual_requests)[0] self.assertEqual(request, actual_request)
def next(self): """Generate the next gRPC streaming api request.""" if self.audio_stream.closed: return None if self.is_first: self.is_first = False return cloud_speech_pb2.StreamingRecognizeRequest( streaming_config=self.config) # block until read some data or until stream closed data = self.audio_stream.read() while not self.audio_stream.closed and len(data) == 0: data = self.audio_stream.read() return cloud_speech_pb2.StreamingRecognizeRequest(audio_content=data)
def test_streaming_recognize_exception(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = speech_client.SpeechClient() # Mock request request = cloud_speech_pb2.StreamingRecognizeRequest() requests = [request] # Mock exception response grpc_stub.StreamingRecognize.side_effect = CustomException() self.assertRaises(errors.GaxError, client.streaming_recognize, requests)