def test_async_recognize(self):
        from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
        from google.cloud import speech
        from google.cloud.speech.operation import Operation
        from google.cloud.speech.sample import Sample

        RETURNED = ASYNC_RECOGNIZE_RESPONSE

        credentials = _Credentials()
        client = self._makeOne(credentials=credentials)
        client.connection = _Connection(RETURNED)

        sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
                        encoding=speech.Encoding.LINEAR16,
                        sample_rate=self.SAMPLE_RATE)
        operation = client.async_recognize(sample)
        self.assertIsInstance(operation, Operation)
        self.assertFalse(operation.complete)
        self.assertIsNone(operation.metadata)
    def test_sync_recognize_source_uri_without_optional_params_no_gax(self):
        from google.cloud._testing import _Monkey
        from google.cloud.speech import client as MUT
        from google.cloud import speech
        from google.cloud.speech.sample import Sample
        from google.cloud.speech.transcript import Transcript
        from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE

        RETURNED = SYNC_RECOGNIZE_RESPONSE
        REQUEST = {
            'config': {
                'encoding': 'FLAC',
                'sampleRate': 16000,
            },
            'audio': {
                'uri': self.AUDIO_SOURCE_URI,
            }
        }
        credentials = _Credentials()
        client = self._makeOne(credentials=credentials, use_gax=False)
        client.connection = _Connection(RETURNED)

        encoding = speech.Encoding.FLAC

        sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
                        encoding=encoding,
                        sample_rate=self.SAMPLE_RATE)
        with _Monkey(MUT, _USE_GAX=False):
            response = client.sync_recognize(sample)

        self.assertEqual(len(client.connection._requested), 1)
        req = client.connection._requested[0]
        self.assertEqual(len(req), 3)
        self.assertEqual(req['data'], REQUEST)
        self.assertEqual(req['method'], 'POST')
        self.assertEqual(req['path'], 'speech:syncrecognize')

        expected = Transcript.from_api_repr(
            SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0])
        self.assertEqual(len(response), 1)
        self.assertIsInstance(response[0], Transcript)
        self.assertEqual(response[0].transcript, expected.transcript)
        self.assertEqual(response[0].confidence, expected.confidence)
Exemple #3
0
    def test_sync_recognize_with_empty_results_gax(self):
        from google.cloud._testing import _Monkey

        from google.cloud import speech
        from google.cloud.speech import _gax
        from google.cloud.speech.sample import Sample

        credentials = _Credentials()
        client = self._make_one(credentials=credentials, use_gax=True)
        client._connection = _Connection()
        client._connection.credentials = credentials

        channel_args = []
        channel_obj = object()

        def make_channel(*args):
            channel_args.append(args)
            return channel_obj

        def speech_api(channel=None):
            return _MockGAPICSpeechAPI(response=_make_sync_response(),
                                       channel=channel)

        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)

        low_level = client.speech_api._gapic_api
        self.assertIsInstance(low_level, _MockGAPICSpeechAPI)
        self.assertIs(low_level._channel, channel_obj)
        self.assertEqual(
            channel_args,
            [(credentials, _gax.DEFAULT_USER_AGENT, host)])

        sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
                        encoding=speech.Encoding.FLAC,
                        sample_rate=self.SAMPLE_RATE)

        with self.assertRaises(ValueError):
            client.sync_recognize(sample)
    def test_ctor(self):
        from google.cloud import speech
        from google.cloud.speech.sample import Sample
        from google.cloud.proto.speech.v1.cloud_speech_pb2 import (
            RecognitionConfig, SpeechContext, StreamingRecognitionConfig,
            StreamingRecognizeRequest)

        sample = Sample(content=self.AUDIO_CONTENT,
                        encoding=speech.Encoding.FLAC,
                        sample_rate_hertz=self.SAMPLE_RATE)
        language_code = 'US-en'
        max_alternatives = 2
        profanity_filter = True
        speech_contexts = [SpeechContext(phrases=self.HINTS)]
        single_utterance = True
        interim_results = False

        streaming_request = self._call_fut(sample, language_code,
                                           max_alternatives, profanity_filter,
                                           speech_contexts, single_utterance,
                                           interim_results)
        self.assertIsInstance(streaming_request, StreamingRecognizeRequest)

        # This isn't set by _make_streaming_request().
        # The first request can only have `streaming_config` set.
        # The following requests can only have `audio_content` set.
        self.assertEqual(streaming_request.audio_content, b'')

        self.assertIsInstance(streaming_request.streaming_config,
                              StreamingRecognitionConfig)
        streaming_config = streaming_request.streaming_config
        self.assertTrue(streaming_config.single_utterance)
        self.assertFalse(streaming_config.interim_results)
        config = streaming_config.config
        self.assertIsInstance(config, RecognitionConfig)
        self.assertEqual(config.encoding, 2)  # speech.Encoding.FLAC maps to 2.
        self.assertEqual(config.sample_rate_hertz, self.SAMPLE_RATE)
        self.assertEqual(config.language_code, language_code)
        self.assertEqual(config.max_alternatives, max_alternatives)
        self.assertTrue(config.profanity_filter)
        self.assertEqual(config.speech_contexts[0].phrases, self.HINTS)
    def test_stream_requests(self):
        from io import BytesIO
        from google.cloud import speech
        from google.cloud.speech.sample import Sample
        from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
            SpeechContext)
        from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
            StreamingRecognitionConfig)
        from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
            StreamingRecognizeRequest)

        sample = Sample(content=BytesIO(self.AUDIO_CONTENT),
                        encoding=speech.Encoding.FLAC,
                        sample_rate=self.SAMPLE_RATE)
        language_code = 'US-en'
        max_alternatives = 2
        profanity_filter = True
        speech_context = SpeechContext(phrases=self.HINTS)
        single_utterance = True
        interim_results = False
        streaming_requests = self._callFUT(sample, language_code,
                                           max_alternatives, profanity_filter,
                                           speech_context, single_utterance,
                                           interim_results)
        all_requests = []
        for streaming_request in streaming_requests:
            self.assertIsInstance(streaming_request, StreamingRecognizeRequest)
            all_requests.append(streaming_request)

        self.assertEqual(len(all_requests), 2)

        config_request = all_requests[0]
        streaming_request = all_requests[1]
        # This isn't set by _make_streaming_request().
        # The first request can only have `streaming_config` set.
        # The following requests can only have `audio_content` set.
        self.assertEqual(config_request.audio_content, b'')
        self.assertEqual(streaming_request.audio_content, self.AUDIO_CONTENT)
        self.assertIsInstance(config_request.streaming_config,
                              StreamingRecognitionConfig)
Exemple #6
0
    def sample(self,
               content=None,
               source_uri=None,
               encoding=None,
               sample_rate=None):
        """Factory: construct Sample to use when making recognize requests.

        :type content: bytes
        :param content: (Optional) Byte stream of audio.

        :type source_uri: str
        :param source_uri: (Optional) URI that points to a file that contains
                           audio data bytes as specified in RecognitionConfig.
                           Currently, only Google Cloud Storage URIs are
                           supported, which must be specified in the following
                           format: ``gs://bucket_name/object_name``.

        :type encoding: str
        :param encoding: encoding of audio data sent in all RecognitionAudio
                         messages, can be one of: :attr:`~.Encoding.LINEAR16`,
                         :attr:`~.Encoding.FLAC`, :attr:`~.Encoding.MULAW`,
                         :attr:`~.Encoding.AMR`, :attr:`~.Encoding.AMR_WB`

        :type sample_rate: int
        :param sample_rate: Sample rate in Hertz of the audio data sent in all
                            requests. Valid values are: 8000-48000. For best
                            results, set the sampling rate of the audio source
                            to 16000 Hz. If that's not possible, use the
                            native sample rate of the audio source (instead of
                            re-sampling).

        :rtype: :class:`~google.cloud.speech.sample.Sample`
        :returns: Instance of ``Sample``.
        """
        return Sample(content=content,
                      source_uri=source_uri,
                      encoding=encoding,
                      sample_rate=sample_rate,
                      client=self)
Exemple #7
0
    def test_async_recognize_no_gax(self):
        from google.cloud import speech
        from google.cloud.speech.operation import Operation
        from google.cloud.speech.sample import Sample
        from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE

        RETURNED = ASYNC_RECOGNIZE_RESPONSE

        credentials = _Credentials()
        client = self._make_one(credentials=credentials, use_gax=False)
        client._connection = _Connection(RETURNED)

        sample = Sample(source_uri=self.AUDIO_SOURCE_URI,
                        encoding=speech.Encoding.LINEAR16,
                        sample_rate=self.SAMPLE_RATE)
        operation = client.async_recognize(sample)
        self.assertIsInstance(operation, Operation)
        self.assertIs(operation.client, client)
        self.assertEqual(operation.caller_metadata,
                         {'request_type': 'AsyncRecognize'})
        self.assertFalse(operation.complete)
        self.assertIsNone(operation.metadata)
    def test_sync_recognize_content_with_optional_params_no_gax(self):
        from base64 import b64encode
        from google.cloud._helpers import _to_bytes
        from google.cloud._helpers import _bytes_to_unicode

        from google.cloud._testing import _Monkey
        from google.cloud.speech import client as MUT
        from google.cloud import speech
        from google.cloud.speech.sample import Sample
        from google.cloud.speech.transcript import Transcript
        from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE

        _AUDIO_CONTENT = _to_bytes(self.AUDIO_CONTENT)
        _B64_AUDIO_CONTENT = _bytes_to_unicode(b64encode(_AUDIO_CONTENT))
        RETURNED = SYNC_RECOGNIZE_RESPONSE
        REQUEST = {
            'config': {
                'encoding': 'FLAC',
                'maxAlternatives': 2,
                'sampleRate': 16000,
                'speechContext': {
                    'phrases': [
                        'hi',
                    ]
                },
                'languageCode': 'EN',
                'profanityFilter': True,
            },
            'audio': {
                'content': _B64_AUDIO_CONTENT,
            }
        }
        credentials = _Credentials()
        client = self._makeOne(credentials=credentials, use_gax=False)
        client.connection = _Connection(RETURNED)

        encoding = speech.Encoding.FLAC

        sample = Sample(content=self.AUDIO_CONTENT,
                        encoding=encoding,
                        sample_rate=self.SAMPLE_RATE)
        with _Monkey(MUT, _USE_GAX=False):
            response = client.sync_recognize(sample,
                                             language_code='EN',
                                             max_alternatives=2,
                                             profanity_filter=True,
                                             speech_context=self.HINTS)

        self.assertEqual(len(client.connection._requested), 1)
        req = client.connection._requested[0]
        self.assertEqual(len(req), 3)
        self.assertEqual(req['data'], REQUEST)
        self.assertEqual(req['method'], 'POST')
        self.assertEqual(req['path'], 'speech:syncrecognize')

        alternative = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives'][0]
        expected = Transcript.from_api_repr(alternative)
        self.assertEqual(len(response), 1)
        self.assertIsInstance(response[0], Transcript)
        self.assertEqual(response[0].transcript, expected.transcript)
        self.assertEqual(response[0].confidence, expected.confidence)