6
6
import sys
7
7
import time
8
8
import warnings
9
+ import wave
9
10
from pathlib import Path
10
11
from typing import Callable , Dict , Generator , Iterable , List , Optional , TextIO , Union
11
12
12
- import wave
13
+ from grpc . _channel import _MultiThreadedRendezvous
13
14
14
- import riva_api .proto .riva_asr_pb2 as rasr
15
- import riva_api .proto .riva_asr_pb2_grpc as rasr_srv
16
- from riva_api .auth import Auth
15
+ import riva . client .proto .riva_asr_pb2 as rasr
16
+ import riva . client .proto .riva_asr_pb2_grpc as rasr_srv
17
+ from riva . client .auth import Auth
17
18
18
19
19
20
def get_wav_file_parameters (input_file : Union [str , os .PathLike ]) -> Dict [str , Union [int , float ]]:
@@ -113,7 +114,7 @@ def print_streaming(
113
114
Prints streaming speech recognition results to provided files or streams.
114
115
115
116
Args:
116
- responses (:obj:`Iterable[riva_api .proto.riva_asr_pb2.StreamingRecognizeResponse]`): responses acquired during
117
+ responses (:obj:`Iterable[riva.client .proto.riva_asr_pb2.StreamingRecognizeResponse]`): responses acquired during
117
118
streaming speech recognition.
118
119
output_file (:obj:`Union[Union[os.PathLike, str, TextIO], List[Union[os.PathLike, str, TextIO]]]`, `optional`):
119
120
a path to an output file or a text stream or a list of paths/streams. If contains several elements, then
@@ -260,7 +261,7 @@ def __init__(self, auth: Auth) -> None:
260
261
Initializes an instance of the class.
261
262
262
263
Args:
263
- auth (:obj:`riva_api. auth.Auth`): an instance of :class:`riva_api .auth.Auth` which is used for
264
+ auth (:obj:`riva.client. auth.Auth`): an instance of :class:`riva.client .auth.Auth` which is used for
264
265
authentication metadata generation.
265
266
"""
266
267
self .auth = auth
@@ -286,28 +287,32 @@ def streaming_response_generator(
286
287
with wave.open(file_name, 'rb') as wav_f:
287
288
raw_audio = wav_f.readframes(n_frames)
288
289
289
- streaming_config (:obj:`riva_api .proto.riva_asr_pb2.StreamingRecognitionConfig`): a config for streaming.
290
+ streaming_config (:obj:`riva.client .proto.riva_asr_pb2.StreamingRecognitionConfig`): a config for streaming.
290
291
You may find description of config fields in message ``StreamingRecognitionConfig`` in
291
- `common repo <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
292
+ `common repo
293
+ <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
292
294
An example of creation of streaming config:
293
295
294
296
.. code-style:: python
295
297
296
- from riva_api import RecognitionConfig, StreamingRecognitionConfig
298
+ from riva.client import RecognitionConfig, StreamingRecognitionConfig
297
299
config = RecognitionConfig(enable_automatic_punctuation=True)
298
300
streaming_config = StreamingRecognitionConfig(config, interim_results=True)
299
301
300
302
Yields:
301
- :obj:`riva_api .proto.riva_asr_pb2.StreamingRecognizeResponse`: responses for audio chunks in
303
+ :obj:`riva.client .proto.riva_asr_pb2.StreamingRecognizeResponse`: responses for audio chunks in
302
304
:param:`audio_chunks`. You may find description of response fields in declaration of
303
305
``StreamingRecognizeResponse``
304
- message `here <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
306
+ message `here
307
+ <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
305
308
"""
306
309
generator = streaming_request_generator (audio_chunks , streaming_config )
307
310
for response in self .stub .StreamingRecognize (generator , metadata = self .auth .get_auth_metadata ()):
308
311
yield response
309
312
310
- def offline_recognize (self , audio_bytes : bytes , config : rasr .RecognitionConfig ) -> rasr .RecognizeResponse :
313
+ def offline_recognize (
314
+ self , audio_bytes : bytes , config : rasr .RecognitionConfig , future : bool = False
315
+ ) -> Union [rasr .RecognizeResponse , _MultiThreadedRendezvous ]:
311
316
"""
312
317
Performs speech recognition for raw audio in :param:`audio_bytes`. This method is for processing of
313
318
huge audio at once - not as it is being generated.
@@ -321,21 +326,27 @@ def offline_recognize(self, audio_bytes: bytes, config: rasr.RecognitionConfig)
321
326
with wave.open(file_name, 'rb') as wav_f:
322
327
raw_audio = wav_f.readframes(n_frames)
323
328
324
- config (:obj:`riva_api .proto.riva_asr_pb2.RecognitionConfig`): a config for offline speech recognition.
329
+ config (:obj:`riva.client .proto.riva_asr_pb2.RecognitionConfig`): a config for offline speech recognition.
325
330
You may find description of config fields in message ``RecognitionConfig`` in
326
- `common repo <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
331
+ `common repo
332
+ <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
327
333
An example of creation of config:
328
334
329
335
.. code-style:: python
330
336
331
- from riva_api import RecognitionConfig
337
+ from riva.client import RecognitionConfig
332
338
config = RecognitionConfig(enable_automatic_punctuation=True)
339
+ future (:obj:`bool`, defaults to :obj:`False`): whether to return an async result instead of usual
340
+ response. You can get a response by calling ``result()`` method of the future object.
333
341
334
342
Returns:
335
- :obj:`riva_api.proto.riva_asr_pb2.RecognizeResponse`: a response with results of :param:`audio_bytes`
336
- processing. You may find description of response fields in declaration of ``RecognizeResponse``
337
- message `here <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
343
+ :obj:`Union[riva.client.proto.riva_asr_pb2.RecognizeResponse, grpc._channel._MultiThreadedRendezvous]``: a
344
+ response with results of :param:`audio_bytes` processing. You may find description of response fields in
345
+ declaration of ``RecognizeResponse`` message `here
346
+ <https://docs.nvidia.com/deeplearning/riva/user-guide/docs/reference/protos/protos.html#riva-proto-riva-asr-proto>`_.
347
+ If :param:`future` is :obj:`True`, then a future object is returned. You may retrieve a response from a
348
+ future object by calling ``result()`` method.
338
349
"""
339
350
request = rasr .RecognizeRequest (config = config , audio = audio_bytes )
340
- response = self .stub .Recognize ( request , metadata = self .auth . get_auth_metadata ())
341
- return response
351
+ func = self .stub .Recognize . future if future else self .stub . Recognize
352
+ return func ( request , metadata = self . auth . get_auth_metadata ())
0 commit comments