Skip to content

Commit 04bf28d

Browse files
committed
Add Sample factory to Client.
1 parent c7b159d commit 04bf28d

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

docs/speech-usage.rst

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ See: `Speech Asynchronous Recognize`_
4545
.. code-block:: python
4646
4747
>>> import time
48-
>>> from google.cloud.speech.sample import Sample
48+
>>> from google.cloud import speech
4949
>>> from google.cloud.speech.encoding import Encoding
50-
>>> sample = Sample(source_uri='gs://my-bucket/recording.flac',
51-
... encoding=Encoding.FLAC,
52-
... sample_rate=44100)
50+
>>> client = speech.Client()
51+
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
52+
... encoding=Encoding.FLAC,
53+
... sample_rate=44100)
5354
>>> operation = client.async_recognize(sample, max_alternatives=2)
5455
>>> retry_count = 100
5556
>>> while retry_count > 0 and not operation.complete:
@@ -75,11 +76,12 @@ Great Britian.
7576

7677
.. code-block:: python
7778
78-
>>> from google.cloud.speech.sample import Sample
79+
>>> from google.cloud import speech
7980
>>> from google.cloud.speech.encoding import Encoding
80-
>>> sample = Sample(source_uri='gs://my-bucket/recording.flac',
81-
... encoding=Encoding.FLAC,
82-
... sample_rate=44100)
81+
>>> client = speech.Client()
82+
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
83+
... encoding=Encoding.FLAC,
84+
... sample_rate=44100)
8385
>>> operation = client.async_recognize(sample, max_alternatives=2)
8486
>>> alternatives = client.sync_recognize(
8587
... 'FLAC', 16000, source_uri='gs://my-bucket/recording.flac',
@@ -99,11 +101,12 @@ Example of using the profanity filter.
99101

100102
.. code-block:: python
101103
102-
>>> from google.cloud.speech.sample import Sample
104+
>>> from google.cloud import speech
103105
>>> from google.cloud.speech.encoding import Encoding
104-
>>> sample = Sample(source_uri='gs://my-bucket/recording.flac',
105-
... encoding=Encoding.FLAC,
106-
... sample_rate=44100)
106+
>>> client = speech.Client()
107+
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
108+
... encoding=Encoding.FLAC,
109+
... sample_rate=44100)
107110
>>> alternatives = client.sync_recognize(sample, max_alternatives=1,
108111
... profanity_filter=True)
109112
>>> for alternative in alternatives:
@@ -120,11 +123,12 @@ words to the vocabulary of the recognizer.
120123

121124
.. code-block:: python
122125
123-
>>> from google.cloud.speech.sample import Sample
126+
>>> from google.cloud import speech
124127
>>> from google.cloud.speech.encoding import Encoding
125-
>>> sample = Sample(source_uri='gs://my-bucket/recording.flac',
126-
... encoding=Encoding.FLAC,
127-
... sample_rate=44100)
128+
>>> client = speech.Client()
129+
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
130+
... encoding=Encoding.FLAC,
131+
... sample_rate=44100)
128132
>>> hints = ['hi', 'good afternoon']
129133
>>> alternatives = client.sync_recognize(sample, max_alternatives=2,
130134
... speech_context=hints)

speech/google/cloud/speech/client.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from google.cloud import client as client_module
2121
from google.cloud.speech.connection import Connection
2222
from google.cloud.speech.operation import Operation
23+
from google.cloud.speech.sample import Sample
2324

2425

2526
class Client(client_module.Client):
@@ -97,6 +98,41 @@ def async_recognize(self, sample, language_code=None,
9798

9899
return Operation.from_api_repr(self, api_response)
99100

101+
@staticmethod
102+
def sample(content=None, source_uri=None, encoding=None,
103+
sample_rate=None):
104+
"""Factory: construct Sample to use when making recognize requests.
105+
106+
:type content: bytes
107+
:param content: (Optional) Byte stream of audio.
108+
109+
:type source_uri: str
110+
:param source_uri: (Optional) URI that points to a file that contains
111+
audio data bytes as specified in RecognitionConfig.
112+
Currently, only Google Cloud Storage URIs are
113+
supported, which must be specified in the following
114+
format: ``gs://bucket_name/object_name``.
115+
116+
:type encoding: str
117+
:param encoding: encoding of audio data sent in all RecognitionAudio
118+
messages, can be one of: :attr:`~.Encoding.LINEAR16`,
119+
:attr:`~.Encoding.FLAC`, :attr:`~.Encoding.MULAW`,
120+
:attr:`~.Encoding.AMR`, :attr:`~.Encoding.AMR_WB`
121+
122+
:type sample_rate: int
123+
:param sample_rate: Sample rate in Hertz of the audio data sent in all
124+
requests. Valid values are: 8000-48000. For best
125+
results, set the sampling rate of the audio source
126+
to 16000 Hz. If that's not possible, use the
127+
native sample rate of the audio source (instead of
128+
re-sampling).
129+
130+
:rtype: :class:`~google.cloud.speech.sample.Sample`
131+
:returns: Instance of ``Sample``.
132+
"""
133+
return Sample(content=content, source_uri=source_uri,
134+
encoding=encoding, sample_rate=sample_rate)
135+
100136
def sync_recognize(self, sample, language_code=None,
101137
max_alternatives=None, profanity_filter=None,
102138
speech_context=None):

speech/unit_tests/test_client.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TestClient(unittest.TestCase):
1919
SAMPLE_RATE = 16000
2020
HINTS = ['hi']
2121
AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac'
22+
AUDIO_CONTENT = '/9j/4QNURXhpZgAASUkq'
2223

2324
def _getTargetClass(self):
2425
from google.cloud.speech.client import Client
@@ -37,15 +38,36 @@ def test_ctor(self):
3738
self.assertTrue(client.connection.credentials is creds)
3839
self.assertTrue(client.connection.http is http)
3940

41+
def test_create_sample_from_client(self):
42+
from google.cloud.speech.encoding import Encoding
43+
from google.cloud.speech.sample import Sample
44+
45+
credentials = _Credentials()
46+
client = self._makeOne(credentials=credentials)
47+
48+
sample = client.sample(source_uri=self.AUDIO_SOURCE_URI,
49+
encoding=Encoding.FLAC,
50+
sample_rate=self.SAMPLE_RATE)
51+
self.assertIsInstance(sample, Sample)
52+
self.assertEqual(sample.source_uri, self.AUDIO_SOURCE_URI)
53+
self.assertEqual(sample.sample_rate, self.SAMPLE_RATE)
54+
self.assertEqual(sample.encoding, Encoding.FLAC)
55+
56+
content_sample = client.sample(content=self.AUDIO_CONTENT,
57+
encoding=Encoding.FLAC,
58+
sample_rate=self.SAMPLE_RATE)
59+
self.assertEqual(content_sample.content, self.AUDIO_CONTENT)
60+
self.assertEqual(content_sample.sample_rate, self.SAMPLE_RATE)
61+
self.assertEqual(content_sample.encoding, Encoding.FLAC)
62+
4063
def test_sync_recognize_content_with_optional_parameters(self):
4164
import base64
4265
from google.cloud._helpers import _to_bytes
4366
from google.cloud.speech.encoding import Encoding
4467
from google.cloud.speech.sample import Sample
4568
from unit_tests._fixtures import SYNC_RECOGNIZE_RESPONSE
4669

47-
_AUDIO_CONTENT = _to_bytes('/9j/4QNURXhpZgAASUkq')
48-
_B64_AUDIO_CONTENT = base64.b64encode(_AUDIO_CONTENT)
70+
_B64_AUDIO_CONTENT = base64.b64encode(_to_bytes(self.AUDIO_CONTENT))
4971
RETURNED = SYNC_RECOGNIZE_RESPONSE
5072
REQUEST = {
5173
'config': {
@@ -70,7 +92,7 @@ def test_sync_recognize_content_with_optional_parameters(self):
7092

7193
encoding = Encoding.FLAC
7294

73-
sample = Sample(content=_AUDIO_CONTENT, encoding=encoding,
95+
sample = Sample(content=self.AUDIO_CONTENT, encoding=encoding,
7496
sample_rate=self.SAMPLE_RATE)
7597
response = client.sync_recognize(sample,
7698
language_code='EN',

0 commit comments

Comments
 (0)