Skip to content

Commit 0cd0e1c

Browse files
committed
Isolate stream property.
1 parent 723c7c7 commit 0cd0e1c

File tree

7 files changed

+46
-39
lines changed

7 files changed

+46
-39
lines changed

speech/google/cloud/speech/_gax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def streaming_recognize(self, sample, language_code=None,
182182
.cloud_speech_pb2.StreamingRecognizeResponse`
183183
:returns: ``StreamingRecognizeResponse`` instances.
184184
"""
185-
if sample.stream.closed:
185+
if sample.stream is None or sample.stream.closed:
186186
raise ValueError('Stream is closed.')
187187

188188
requests = _stream_requests(sample, language_code=language_code,

speech/google/cloud/speech/client.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,12 @@ def __init__(self, credentials=None, http=None, use_gax=None):
6666
else:
6767
self._use_gax = use_gax
6868

69-
def sample(self, content=None, source_uri=None, encoding=None,
69+
def sample(self, content=None, source_uri=None, stream=None, encoding=None,
7070
sample_rate=None):
7171
"""Factory: construct Sample to use when making recognize requests.
7272
73-
:type content: bytes or file
74-
:param content: (Optional) Bytes object containing audio or file-like
75-
object which yields bytes.
73+
:type content: bytes
74+
:param content: (Optional) Bytes object containing audio data.
7675
7776
:type source_uri: str
7877
:param source_uri: (Optional) URI that points to a file that contains
@@ -81,6 +80,9 @@ def sample(self, content=None, source_uri=None, encoding=None,
8180
supported, which must be specified in the following
8281
format: ``gs://bucket_name/object_name``.
8382
83+
:type stream: file
84+
:param stream: (Optional) File like object to stream.
85+
8486
:type encoding: str
8587
:param encoding: encoding of audio data sent in all RecognitionAudio
8688
messages, can be one of: :attr:`~.Encoding.LINEAR16`,
@@ -98,7 +100,7 @@ def sample(self, content=None, source_uri=None, encoding=None,
98100
:rtype: :class:`~google.cloud.speech.sample.Sample`
99101
:returns: Instance of ``Sample``.
100102
"""
101-
return Sample(content=content, source_uri=source_uri,
103+
return Sample(content=content, source_uri=source_uri, stream=stream,
102104
encoding=encoding, sample_rate=sample_rate, client=self)
103105

104106
@property

speech/google/cloud/speech/sample.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
class Sample(object):
2323
"""Representation of an audio sample to be used with Google Speech API.
2424
25-
:type content: bytes or file
26-
:param content: (Optional) Bytes object containing audio or file-like
27-
object which yields bytes.
25+
:type content: bytes
26+
:param content: (Optional) Bytes object containing audio data.
2827
2928
:type source_uri: str
3029
:param source_uri: (Optional) URI that points to a file that contains
@@ -33,6 +32,9 @@ class Sample(object):
3332
supported, which must be specified in the following
3433
format: ``gs://bucket_name/object_name``.
3534
35+
:type stream: file
36+
:param stream: (Optional) File like object to stream.
37+
3638
:type encoding: str
3739
:param encoding: encoding of audio data sent in all RecognitionAudio
3840
messages, can be one of: :attr:`~.Encoding.LINEAR16`,
@@ -53,17 +55,21 @@ class Sample(object):
5355
default_encoding = Encoding.FLAC
5456
default_sample_rate = 16000
5557

56-
def __init__(self, content=None, source_uri=None,
58+
def __init__(self, content=None, source_uri=None, stream=None,
5759
encoding=None, sample_rate=None, client=None):
5860
self._client = client
5961

60-
no_source = content is None and source_uri is None
61-
both_source = content is not None and source_uri is not None
62+
no_source = content is None and source_uri is None and stream is None
63+
both_source = (content is not None
64+
and source_uri is not None
65+
and stream is not None)
6266
if no_source or both_source:
63-
raise ValueError('Supply one of \'content\' or \'source_uri\'')
67+
raise ValueError('Supply one of '
68+
'\'content\', \'source_uri\', \'stream\'')
6469

6570
self._content = content
6671
self._source_uri = source_uri
72+
self._stream = stream
6773

6874
if sample_rate is not None and not 8000 <= sample_rate <= 48000:
6975
raise ValueError('The value of sample_rate must be between 8000'
@@ -97,13 +103,10 @@ def source_uri(self):
97103
def content(self):
98104
"""Bytes of audio content.
99105
100-
:rtype: bytes or file
101-
:returns: Byte stream of audio content or file like object.
106+
:rtype: bytes
107+
:returns: Byte stream of audio content.
102108
"""
103-
try:
104-
return self._content.read()
105-
except AttributeError:
106-
return self._content
109+
return self._content
107110

108111
@property
109112
def sample_rate(self):
@@ -121,10 +124,9 @@ def stream(self):
121124
:rtype: file
122125
:returns: File like object to stream.
123126
"""
124-
stream = self._content
125-
if getattr(stream, 'read', None) is None:
127+
if getattr(self._stream, 'read', None) is None:
126128
return None
127-
return stream
129+
return self._stream
128130

129131
@property
130132
def encoding(self):

speech/unit_tests/test__gax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def test_stream_requests(self):
106106
from google.cloud.grpc.speech.v1beta1.cloud_speech_pb2 import (
107107
StreamingRecognizeRequest)
108108

109-
sample = Sample(content=BytesIO(self.AUDIO_CONTENT),
109+
sample = Sample(stream=BytesIO(self.AUDIO_CONTENT),
110110
encoding=speech.Encoding.FLAC,
111111
sample_rate=self.SAMPLE_RATE)
112112
language_code = 'US-en'

speech/unit_tests/test_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def speech_api(channel=None):
520520
make_secure_channel=make_channel):
521521
client._speech_api = _gax.GAPICSpeechAPI(client)
522522

523-
sample = client.sample(content=stream,
523+
sample = client.sample(stream=stream,
524524
encoding=Encoding.LINEAR16,
525525
sample_rate=self.SAMPLE_RATE)
526526

@@ -593,7 +593,7 @@ def speech_api(channel=None):
593593
make_secure_channel=make_channel):
594594
client._speech_api = _gax.GAPICSpeechAPI(client)
595595

596-
sample = client.sample(content=stream,
596+
sample = client.sample(stream=stream,
597597
encoding=Encoding.LINEAR16,
598598
sample_rate=self.SAMPLE_RATE)
599599

@@ -637,7 +637,7 @@ def speech_api(channel=None):
637637
make_secure_channel=make_channel):
638638
client._speech_api = _gax.GAPICSpeechAPI(client)
639639

640-
sample = client.sample(content=stream,
640+
sample = client.sample(stream=stream,
641641
encoding=Encoding.LINEAR16,
642642
sample_rate=self.SAMPLE_RATE)
643643

speech/unit_tests/test_sample.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,31 @@ def test_initialize_sample(self):
3838
self.assertEqual(sample.sample_rate, self.SAMPLE_RATE)
3939

4040
def test_content_and_source_uri(self):
41+
from io import BytesIO
42+
4143
with self.assertRaises(ValueError):
4244
self._make_one(content='awefawagaeragere',
4345
source_uri=self.AUDIO_SOURCE_URI)
4446

45-
def test_content_is_file_like(self):
46-
from io import BytesIO
47-
from google.cloud.speech.encoding import Encoding
47+
with self.assertRaises(ValueError):
48+
self._make_one(stream=BytesIO(b'awefawagaeragere'),
49+
source_uri=self.AUDIO_SOURCE_URI)
4850

49-
test_bytes = b'testing 1 2 3 4'
50-
content = BytesIO(test_bytes)
51-
sample = self._make_one(content=content,
52-
encoding=Encoding.FLAC,
53-
sample_rate=self.SAMPLE_RATE)
54-
self.assertEqual(sample.content, test_bytes)
51+
with self.assertRaises(ValueError):
52+
self._make_one(content='awefawagaeragere',
53+
stream=BytesIO(b'awefawagaeragere'),
54+
source_uri=self.AUDIO_SOURCE_URI)
5555

5656
def test_stream_property(self):
57+
from io import BytesIO
5758
from google.cloud.speech.encoding import Encoding
5859

59-
content = b'abc 1 2 3'
60-
sample = self._make_one(content=content, encoding=Encoding.FLAC,
60+
data = b'abc 1 2 3 4'
61+
stream = BytesIO(data)
62+
sample = self._make_one(stream=stream, encoding=Encoding.FLAC,
6163
sample_rate=self.SAMPLE_RATE)
62-
self.assertIsNone(sample.stream)
64+
self.assertEqual(sample.stream, stream)
65+
self.assertEqual(sample.stream.read(), data)
6366

6467
def test_bytes_converts_to_file_like_object(self):
6568
from google.cloud import speech

system_tests/speech.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def _make_async_request(self, content=None, source_uri=None,
118118
def _make_streaming_request(self, file_obj, single_utterance=True,
119119
interim_results=False):
120120
client = Config.CLIENT
121-
sample = client.sample(content=file_obj,
121+
sample = client.sample(stream=file_obj,
122122
encoding=speech.Encoding.LINEAR16,
123123
sample_rate=16000)
124124
return sample.streaming_recognize(single_utterance=single_utterance,

0 commit comments

Comments
 (0)