Skip to content

Commit f29bef3

Browse files
Speech v1 (googleapis#3266)
This updates our manual client library to the Speech v1 API. This entails several **backwards incompatible changes**: * The `language_code` parameter is no longer optional anywhere. It must be explicitly specified, and does _not_ default to `'en-US'`. * The `sync_recognize` method has been renamed to `recognize` on every class where it appears. * The `async_recognize` method has been renamed to `long_running_recognize` on every class where it appears. * The `sample_rate` parameter and property has been renamed to `sample_rate_hertz` everywhere it appears. Additionally, the backend API contains a backwards incompatible change which does not require a code change in the client library, but will likely require one downstream: The `START_OF_SPEECH`, `END_OF_SPEECH`, and `END_OF_AUDIO` events have been removed.
1 parent ff9ff61 commit f29bef3

18 files changed

+381
-317
lines changed

docs/speech-usage.rst

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ create an instance of :class:`~google.cloud.speech.client.Client`.
3434
Asynchronous Recognition
3535
------------------------
3636

37-
The :meth:`~google.cloud.speech.Client.async_recognize` sends audio data to the
38-
Speech API and initiates a Long Running Operation. Using this operation, you
39-
can periodically poll for recognition results. Use asynchronous requests for
40-
audio data of any duration up to 80 minutes.
37+
The :meth:`~google.cloud.speech.Client.long_running_recognize` sends audio
38+
data to the Speech API and initiates a Long Running Operation. Using this
39+
operation, you can periodically poll for recognition results. Use asynchronous
40+
requests for audio data of any duration up to 80 minutes.
4141

4242
.. note::
4343

@@ -54,8 +54,11 @@ See: `Speech Asynchronous Recognize`_
5454
>>> client = speech.Client()
5555
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
5656
... encoding=speech.Encoding.LINEAR16,
57-
... sample_rate=44100)
58-
>>> operation = sample.async_recognize(max_alternatives=2)
57+
... sample_rate_hertz=44100)
58+
>>> operation = sample.long_running_recognize(
59+
... language_code='en-US',
60+
... max_alternatives=2,
61+
... )
5962
>>> retry_count = 100
6063
>>> while retry_count > 0 and not operation.complete:
6164
... retry_count -= 1
@@ -76,7 +79,7 @@ See: `Speech Asynchronous Recognize`_
7679
Synchronous Recognition
7780
-----------------------
7881

79-
The :meth:`~google.cloud.speech.Client.sync_recognize` method converts speech
82+
The :meth:`~google.cloud.speech.Client.recognize` method converts speech
8083
data to text and returns alternative text transcriptions.
8184

8285
This example uses ``language_code='en-GB'`` to better recognize a dialect from
@@ -88,8 +91,8 @@ Great Britain.
8891
>>> client = speech.Client()
8992
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
9093
... encoding=speech.Encoding.FLAC,
91-
... sample_rate=44100)
92-
>>> results = sample.sync_recognize(
94+
... sample_rate_hertz=44100)
95+
>>> results = sample.recognize(
9396
... language_code='en-GB', max_alternatives=2)
9497
>>> for result in results:
9598
... for alternative in result.alternatives:
@@ -111,9 +114,12 @@ Example of using the profanity filter.
111114
>>> client = speech.Client()
112115
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
113116
... encoding=speech.Encoding.FLAC,
114-
... sample_rate=44100)
115-
>>> results = sample.sync_recognize(max_alternatives=1,
116-
... profanity_filter=True)
117+
... sample_rate_hertz=44100)
118+
>>> results = sample.recognize(
119+
... language_code='en-US',
120+
... max_alternatives=1,
121+
... profanity_filter=True,
122+
... )
117123
>>> for result in results:
118124
... for alternative in result.alternatives:
119125
... print('=' * 20)
@@ -133,10 +139,13 @@ words to the vocabulary of the recognizer.
133139
>>> client = speech.Client()
134140
>>> sample = client.sample(source_uri='gs://my-bucket/recording.flac',
135141
... encoding=speech.Encoding.FLAC,
136-
... sample_rate=44100)
142+
... sample_rate_hertz=44100)
137143
>>> hints = ['hi', 'good afternoon']
138-
>>> results = sample.sync_recognize(max_alternatives=2,
139-
... speech_context=hints)
144+
>>> results = sample.recognize(
145+
... language_code='en-US',
146+
... max_alternatives=2,
147+
... speech_context=hints,
148+
... )
140149
>>> for result in results:
141150
... for alternative in result.alternatives:
142151
... print('=' * 20)
@@ -165,8 +174,8 @@ speech data to possible text alternatives on the fly.
165174
>>> with open('./hello.wav', 'rb') as stream:
166175
... sample = client.sample(stream=stream,
167176
... encoding=speech.Encoding.LINEAR16,
168-
... sample_rate=16000)
169-
... results = sample.streaming_recognize()
177+
... sample_rate_hertz=16000)
178+
... results = sample.streaming_recognize(language_code='en-US')
170179
... for result in results:
171180
... for alternative in result.alternatives:
172181
... print('=' * 20)
@@ -192,8 +201,11 @@ See: `Single Utterance`_
192201
>>> with open('./hello_pause_goodbye.wav', 'rb') as stream:
193202
... sample = client.sample(stream=stream,
194203
... encoding=speech.Encoding.LINEAR16,
195-
... sample_rate=16000)
196-
... results = sample.streaming_recognize(single_utterance=True)
204+
... sample_rate_hertz=16000)
205+
... results = sample.streaming_recognize(
206+
... language_code='en-US',
207+
... single_utterance=True,
208+
... )
197209
... for result in results:
198210
... for alternative in result.alternatives:
199211
... print('=' * 20)
@@ -214,7 +226,10 @@ If ``interim_results`` is set to :data:`True`, interim results
214226
... sample = client.sample(stream=stream,
215227
... encoding=speech.Encoding.LINEAR16,
216228
... sample_rate=16000)
217-
... results = sample.streaming_recognize(interim_results=True):
229+
... results = sample.streaming_recognize(
230+
... interim_results=True,
231+
... language_code='en-US',
232+
... )
218233
... for result in results:
219234
... for alternative in result.alternatives:
220235
... print('=' * 20)

0 commit comments

Comments
 (0)