def test_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
        encoding = enums.RecognitionConfig.AudioEncoding.FLAC
        sample_rate_hertz = 44100
        language_code = 'en-US'
        config = cloud_speech_pb2.RecognitionConfig(
            encoding=encoding,
            sample_rate_hertz=sample_rate_hertz,
            language_code=language_code)
        uri = 'gs://bucket_name/file_name.flac'
        audio = cloud_speech_pb2.RecognitionAudio(uri=uri)

        # Mock response
        expected_response = cloud_speech_pb2.RecognizeResponse()
        grpc_stub.Recognize.return_value = expected_response

        response = client.recognize(config, audio)
        self.assertEqual(expected_response, response)

        grpc_stub.Recognize.assert_called_once()
        args, kwargs = grpc_stub.Recognize.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = cloud_speech_pb2.RecognizeRequest(config=config,
                                                             audio=audio)
        self.assertEqual(expected_request, actual_request)
    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 test_long_running_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
        encoding = enums.RecognitionConfig.AudioEncoding.FLAC
        sample_rate_hertz = 44100
        language_code = 'en-US'
        config = cloud_speech_pb2.RecognitionConfig(
            encoding=encoding,
            sample_rate_hertz=sample_rate_hertz,
            language_code=language_code)
        uri = 'gs://bucket_name/file_name.flac'
        audio = cloud_speech_pb2.RecognitionAudio(uri=uri)

        # Mock exception response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_long_running_recognize_exception', done=True)
        operation.error.CopyFrom(error)
        grpc_stub.LongRunningRecognize.return_value = operation

        response = client.long_running_recognize(config, audio)
        self.assertEqual(error, response.exception())
Esempio n. 4
0
    def test_long_running_recognize(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=cloud_speech_pb2.SpeechStub)
        mock_create_stub.return_value = grpc_stub

        client = speech_client.SpeechClient()

        # Mock request
        encoding = enums.RecognitionConfig.AudioEncoding.FLAC
        sample_rate_hertz = 44100
        language_code = 'en-US'
        config = cloud_speech_pb2.RecognitionConfig(encoding,
                                                    sample_rate_hertz,
                                                    language_code)
        uri = 'gs://bucket_name/file_name.flac'
        audio = cloud_speech_pb2.RecognitionAudio(uri)

        # Mock response
        expected_response = cloud_speech_pb2.LongRunningRecognizeResponse()
        operation = operations_pb2.Operation(
            'operations/test_long_running_recognize', True)
        operation.response.Pack(expected_response)
        grpc_stub.LongRunningRecognize.return_value = operation

        response = client.long_running_recognize(config, audio)
        self.assertEqual(expected_response, response.result())

        grpc_stub.LongRunningRecognize.assert_called_once()
        request = grpc_stub.LongRunningRecognize.call_args[0]

        self.assertEqual(config, request.config)
        self.assertEqual(audio, request.audio)
    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)
Esempio n. 6
0
    def _process(self):
        """sets up a streaming speech api request. And streams results into a queue."""
        self.client = speech_client.SpeechClient()
        self.config = cloud_speech_pb2.StreamingRecognitionConfig(
            config=cloud_speech_pb2.RecognitionConfig(
                encoding=self.encoding,
                sample_rate_hertz=self.rate,
                language_code=self.language),
            interim_results=True)

        requests = StreamingRequest(self.audio, self.config)
        streaming_resp = self.client.streaming_recognize(iter(requests))

        # This will block until self.audio is closed...which closes the streaming_recognize req
        for resp in streaming_resp:
            self.results.put(resp)
Esempio n. 7
0
    def test_recognize_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock(spec=cloud_speech_pb2.SpeechStub)
        mock_create_stub.return_value = grpc_stub

        client = speech_client.SpeechClient()

        # Mock request
        encoding = enums.RecognitionConfig.AudioEncoding.FLAC
        sample_rate_hertz = 44100
        language_code = 'en-US'
        config = cloud_speech_pb2.RecognitionConfig(encoding,
                                                    sample_rate_hertz,
                                                    language_code)
        uri = 'gs://bucket_name/file_name.flac'
        audio = cloud_speech_pb2.RecognitionAudio(uri)

        # Mock exception response
        grpc_stub.Recognize.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.recognize, config, audio)
FILE="podcast.flac"

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "auth.json"

print("Creating bucket")

client = storage.Client()
bucket = client.lookup_bucket(BUCKET)
if not bucket:
    bucket = client.create_bucket(BUCKET)

print("Uploading Podcast")
blob = storage.Blob(FILE, bucket)
blob.upload_from_filename(FILE)

sclient = speech_client.SpeechClient()
encoding = enums.RecognitionConfig.AudioEncoding.FLAC
sample_rate_hertz = 16000
language_code = 'en-US'
config = cloud_speech_pb2.RecognitionConfig(
    encoding=encoding,
    sample_rate_hertz=sample_rate_hertz,
    language_code=language_code)
uri = 'gs://%s/%s' % (BUCKET, FILE)
audio = cloud_speech_pb2.RecognitionAudio(uri=uri)
response = sclient.long_running_recognize(config, audio)

def callback(operation_future):
    # Handle result.
    result = operation_future.result()
    with open("google-transcript.json", "w") as f: