-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasr.py
195 lines (161 loc) · 7.09 KB
/
asr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of NVIDIA CORPORATION nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import os
import queue
import sys
import grpc
import pyaudio
import riva_api.riva_asr_pb2 as rasr
import riva_api.riva_asr_pb2_grpc as rasr_srv
import riva_api.riva_audio_pb2 as ra
RATE = 16000
CHUNK = int(RATE / 10) # 100ms
def get_args():
parser = argparse.ArgumentParser(description="Streaming transcription via Riva AI Services")
parser.add_argument("--server", default="localhost:50051", type=str, help="URI to GRPC server endpoint")
parser.add_argument("--input-device", type=int, default=None, help="output device to use")
parser.add_argument("--list-devices", action="store_true", help="list output devices indices")
parser.add_argument("--language-code", default="en-US", type=str, help="Language code of the model to be used")
parser.add_argument("--ssl_cert", type=str, default="", help="Path to SSL client certificatates file")
parser.add_argument(
"--use_ssl", default=False, action='store_true', help="Boolean to control if SSL/TLS encryption should be used"
)
return parser.parse_args()
class MicrophoneStream(object):
"""Opens a recording stream as a generator yielding the audio chunks."""
def __init__(self, rate, chunk, device=None):
self._rate = rate
self._chunk = chunk
self._device = device
# Create a thread-safe buffer of audio data
self._buff = queue.Queue()
self.closed = True
def __enter__(self):
self._audio_interface = pyaudio.PyAudio()
self._audio_stream = self._audio_interface.open(
format=pyaudio.paInt16,
input_device_index=self._device,
channels=1,
rate=self._rate,
input=True,
frames_per_buffer=self._chunk,
stream_callback=self._fill_buffer,
)
self.closed = False
return self
def __exit__(self, type, value, traceback):
self._audio_stream.stop_stream()
self._audio_stream.close()
self.closed = True
# Signal the generator to terminate so that the client's
# streaming_recognize method will not block the process termination.
self._buff.put(None)
self._audio_interface.terminate()
def _fill_buffer(self, in_data, frame_count, time_info, status_flags):
"""Continuously collect data from the audio stream, into the buffer."""
self._buff.put(in_data)
return None, pyaudio.paContinue
def generator(self):
while not self.closed:
chunk = self._buff.get()
if chunk is None:
return
data = [chunk]
while True:
try:
chunk = self._buff.get(block=False)
if chunk is None:
return
data.append(chunk)
except queue.Empty:
break
yield b''.join(data)
def listen_print_loop(responses):
num_chars_printed = 0
for response in responses:
if not response.results:
continue
partial_transcript = ""
for result in response.results:
if not result.alternatives:
continue
transcript = result.alternatives[0].transcript
if not result.is_final:
partial_transcript += transcript
else:
overwrite_chars = ' ' * (num_chars_printed - len(transcript))
print("## " + transcript + overwrite_chars + "\n")
asr_line = transcript + overwrite_chars
num_chars_printed = 0
return asr_line
if partial_transcript != "":
overwrite_chars = ' ' * (num_chars_printed - len(partial_transcript))
sys.stdout.write(">> " + partial_transcript + overwrite_chars + '\r')
sys.stdout.flush()
num_chars_printed = len(partial_transcript) + 3
def asr():
args = get_args()
if args.list_devices:
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
info = p.get_device_info_by_index(i)
if info['maxInputChannels'] < 1:
continue
print(f"{info['index']}: {info['name']}")
sys.exit(0)
if args.ssl_cert != "" or args.use_ssl:
root_certificates = None
if args.ssl_cert != "" and os.path.exists(args.ssl_cert):
with open(args.ssl_cert, 'rb') as f:
root_certificates = f.read()
creds = grpc.ssl_channel_credentials(root_certificates)
channel = grpc.secure_channel(args.server, creds)
else:
channel = grpc.insecure_channel(args.server)
client = rasr_srv.RivaSpeechRecognitionStub(channel)
config = rasr.RecognitionConfig(
encoding=ra.AudioEncoding.LINEAR_PCM,
sample_rate_hertz=RATE,
language_code=args.language_code,
max_alternatives=1,
enable_automatic_punctuation=True,
)
streaming_config = rasr.StreamingRecognitionConfig(config=config, interim_results=True)
with MicrophoneStream(RATE, CHUNK, device=args.input_device) as stream:
audio_generator = stream.generator()
requests = (rasr.StreamingRecognizeRequest(audio_content=content) for content in audio_generator)
def build_generator(cfg, gen):
yield rasr.StreamingRecognizeRequest(streaming_config=cfg)
for x in gen:
yield x
responses = client.StreamingRecognize(build_generator(streaming_config, requests))
asr_line = listen_print_loop(responses)
if asr_line == None:
return "no input"
else:
return asr_line
asr()