forked from louisbrulenaudet/balena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hci.py
393 lines (297 loc) · 13.5 KB
/
hci.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# -*- coding: utf-8 -*-
# Copyright (c) Louis Brulé Naudet. All Rights Reserved.
# This software may be used and distributed according to the terms of License Agreement.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import ssl
import time
import warnings
import nltk
import numpy as np
import pyaudio
import scipy.signal as signal
import torch
from embeddings import SimilaritySearch
def create_unverified_https_context():
"""
Create an unverified HTTPS context if available.
This function checks for the availability of an unverified HTTPS context and sets it as the default HTTPS context
if it's available. If not available, it maintains the default behavior.
Raises
------
RuntimeError
If an error occurs during the creation of the unverified HTTPS context.
Notes
-----
This functionality is primarily used to handle SSL certificate verification in HTTPS connections, allowing
for unverified contexts when necessary.
"""
try:
_create_unverified_https_context = ssl._create_unverified_context
ssl._create_default_https_context = _create_unverified_https_context
except AttributeError as e:
raise RuntimeError("Unable to create an unverified HTTPS context.") from e
try:
create_unverified_https_context()
nltk.download("words")
from nltk.corpus import words
except LookupError as e:
raise LookupError("Failed to download the 'words' corpus from NLTK.") from e
from nltk.metrics.distance import jaccard_distance
from nltk.util import ngrams
from sentence_transformers import SentenceTransformer
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
warnings.filterwarnings("ignore")
class Speech2Interact():
def __init__(self, actions:dict, wav2vec_model:str="facebook/wav2vec2-large-960h", sentence_similarity_model:str="sentence-transformers/all-mpnet-base-v2", auto_correct:bool=True, device:str=None):
"""
Initializes an instance of Speech2Interact with models for Wav2Vec2 and sentence similarity.
Parameters
----------
actions : dict
Dict of lists of trigger words that might signify actions
wav2vec_model : str, optional
The name or path of the Wav2Vec2 model. Default is "facebook/wav2vec2-large-960h".
sentence_similarity_model : str, optional
The name or path of the sentence similarity model. Default is "sentence-transformers/all-mpnet-base-v2".
auto_correct : bool, optional
Performs word correction based on Jaccard distance. Default is True.
device : str, optional
Device to perform the encoding (e.g., 'cpu', 'cuda'). Defaults to 'cpu'.
Notes
-----
This initialization function loads and prepares models for Wav2Vec2 and sentence similarity, using
the specified Wav2Vec2 model and sentence similarity model to process audio and text data respectively.
"""
if device:
self.device = device
else:
self.device = "cuda:0" if torch.cuda.is_available() else "cpu"
self.actions = list(actions.keys())
self.auto_correct = auto_correct
if self.auto_correct == True:
self.correct_words = set(word.lower() for word in words.words() if word.isalpha())
self.torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
self.processor = Wav2Vec2Processor.from_pretrained(wav2vec_model)
self.wav2vec_model = Wav2Vec2ForCTC.from_pretrained(wav2vec_model)
self.sentence_similarity_model = SimilaritySearch(
model=sentence_similarity_model,
device=self.device
)
if self.sentence_similarity_model.vectorstore is None:
for element in actions:
if self.device != "cpu":
embeddings = self.sentence_similarity_model.encode(
data=actions[element]
)
self.sentence_similarity_model.index(
embeddings=embeddings
)
else:
self.embeddings = []
for element in actions:
embeddings = self.sentence_similarity_model.encode(
data=actions[element]
)
self.embeddings.append(embeddings)
def stream(self, duration:int=5, sample_rate:int=16000, frames_per_buffer:int=8000, channels:int=1) -> list:
"""
Records audio for a specified duration using the microphone input and returns the recorded frames.
Parameters
----------
duration : int, optional
The duration of the audio recording in seconds. Default is 5 seconds.
sample_rate : int, optional
The sample rate of the audio. Default is 16000 Hz.
frames_per_buffer : int, optional
The number of frames per buffer for audio recording. Default is 8000 frames.
channels : int, optional
The number of audio channels. Default is 1 (mono).
Returns
-------
frames : list
A list containing the recorded audio frames.
"""
if not all(isinstance(arg, int) and arg > 0 for arg in (duration, sample_rate, frames_per_buffer, channels)):
raise ValueError("All parameters should be positive integers.")
try:
audio = pyaudio.PyAudio()
# Set up the audio stream to record for the specified duration
stream = audio.open(
format=pyaudio.paInt16,
channels=channels,
rate=sample_rate,
input=True,
frames_per_buffer=frames_per_buffer
)
frames = []
print(f"Recording for {duration} seconds...")
start_time = time.time()
while time.time() - start_time < duration:
data = stream.read(frames_per_buffer)
frames.append(data)
print("Recording stopped.")
stream.stop_stream()
stream.close()
audio.terminate()
return frames
except IOError as e:
raise IOError(f"An error occurred with the audio stream: {e}")
def high_pass_filter(self, waveform:np.array, sample_rate:int, cutoff_freq:float) -> np.array:
"""
Apply a high-pass filter to the input waveform.
Parameters
----------
waveform : array_like
Input waveform data to be filtered.
sample_rate : int or float
Sampling rate of the input waveform.
cutoff_freq : float
Cutoff frequency of the high-pass filter.
Returns
-------
filtered_waveform : array_like
The filtered waveform after applying the high-pass filter.
"""
if not isinstance(waveform, np.ndarray):
raise ValueError("waveform should be a numpy array.")
if not isinstance(sample_rate, (int, float)) or sample_rate <= 0:
raise ValueError("sample_rate should be a positive integer or float.")
if not isinstance(cutoff_freq, (int, float)) or cutoff_freq <= 0:
raise ValueError("cutoff_freq should be a positive float.")
try:
b, a = signal.butter(
5,
cutoff_freq / (0.5 * sample_rate),
btype="highpass",
analog=False
)
filtered_waveform = signal.lfilter(b, a, waveform)
except Exception as e:
raise RuntimeError(f"An error occurred during the filtering process: {e}")
return filtered_waveform
def normalize_waveform(self, frames: list) -> np.ndarray:
"""
Normalizes waveform the waveform from audio frames.
Parameters
----------
frames : list
List of audio frames captured from the input source.
Returns
-------
waveform : np.ndarray
Processed waveform ready for further operations.
"""
if not frames or not all(isinstance(frame, (bytes, bytearray)) for frame in frames):
raise ValueError("frames should be a non-empty list of bytes-like objects.")
try:
waveform = np.frombuffer(b''.join(frames), dtype=np.int16)
waveform = waveform / 32768.0 # Normalize waveform to be in the range [-1.0, 1.0]
except Exception as e:
raise ValueError(f"An error occurred while processing the waveform: {e}")
return waveform
def jaccard_correction(self, sentences: str) -> str:
"""
Performs word correction based on Jaccard distance.
Parameters
----------
sentences : str
String containing sentences to be corrected.
Returns
-------
corrected_sentence : str
The corrected sentence after applying word corrections based on Jaccard distance.
Notes
-----
This function uses Jaccard distance to correct words in the input sentences by finding similar words
based on the overlap of 2-grams. It takes a string of sentences and identifies words that are likely
misspelled or incorrect by comparing them to a list of correct words. It then replaces the incorrect
words with the most similar correct words, considering the Jaccard distance between their 2-gram sets.
"""
if not isinstance(sentences, str):
raise ValueError("Input should be a string.")
corrected_words = []
sentences_list = sentences.split() # Split the string into a list of words
for word in sentences_list:
try:
similar_words = [(jaccard_distance(set(ngrams(word, 2)), set(ngrams(w, 2))), w)
for w in self.correct_words if w[0] == word[0]]
if similar_words:
corrected_words.append(sorted(similar_words, key=lambda val: val[0])[0][1])
except (KeyError, IndexError) as e:
# Handle specific exceptions if needed
print(f"Error processing word '{word}': {e}")
pass
return " ".join(corrected_words)
def recognize_speech(self, duration:int=5, sample_rate:int=16000) -> str:
"""
Recognizes speech from audio frames using a pre-trained model.
Parameters
----------
duration : int, optional
The duration of the audio recording in seconds. Default is 5 seconds.
sample_rate : int, optional
The sample rate of the audio frames. Default is 16000 Hz.
Returns
-------
transcription : str
Transcription of the recognized speech from the input audio frames.
Notes
-----
This function assumes the availability of the 'high_pass_filter' function, a model, and a processor
to perform speech recognition. Adjustments or connections to these components need to be ensured
for the proper functionality of this function.
"""
try:
frames = self.stream(
duration=duration
)
waveform = self.normalize_waveform(
frames=frames
)
waveform = self.high_pass_filter(
waveform=waveform,
sample_rate=sample_rate,
cutoff_freq=300
)
waveform_tensor = torch.from_numpy(waveform).float()
# Perform speech recognition
input_values = self.processor(
waveform_tensor,
sampling_rate=sample_rate,
return_tensors="pt"
).input_values
with torch.no_grad(): # Ensure that we're not computing gradients
logits = self.wav2vec_model(input_values).logits
predicted_ids = torch.argmax(
logits,
dim=-1
)
transcription = self.processor.batch_decode(predicted_ids)[0]
transcription = transcription.lower()
if transcription and self.auto_correct == True:
corrected_sentences = self.jaccard_correction(
sentences=transcription
)
else:
raise RuntimeError("Missing components: transcription.")
# Check for empty embeddings or model issues
if not self.embeddings or not self.sentence_similarity_model or not self.actions:
raise RuntimeError("Missing components: embeddings, sentence_similarity_model, or actions.")
target = self.sentence_similarity_model.encode(
data=corrected_sentences
)
if self.device == "cpu":
index = self.sentence_similarity_model.cosine(
embeddings=self.embeddings,
target=target
)
else:
raise RuntimeError("Invalid device. Only 'cpu' is supported.")
return self.actions[index]
except Exception as e:
raise RuntimeError(f"An error occurred during speech recognition: {e}")