Add lock to thread shared variables updates/reads

Signed-off-by: makaveli10 <vineet.suryan@collabora.com>
This commit is contained in:
makaveli10
2025-01-06 06:34:02 +00:00
parent 18de63c649
commit c936e5f727
+28 -20
View File
@@ -479,9 +479,10 @@ class ServeClientBase(object):
Clip audio if the current chunk exceeds 30 seconds, this basically implies that Clip audio if the current chunk exceeds 30 seconds, this basically implies that
no valid segment for the last 30 seconds from whisper no valid segment for the last 30 seconds from whisper
""" """
if self.frames_np[int((self.timestamp_offset - self.frames_offset)*self.RATE):].shape[0] > 25 * self.RATE: with self.lock:
duration = self.frames_np.shape[0] / self.RATE if self.frames_np[int((self.timestamp_offset - self.frames_offset)*self.RATE):].shape[0] > 25 * self.RATE:
self.timestamp_offset = self.frames_offset + duration - 5 duration = self.frames_np.shape[0] / self.RATE
self.timestamp_offset = self.frames_offset + duration - 5
def get_audio_chunk_for_processing(self): def get_audio_chunk_for_processing(self):
""" """
@@ -497,8 +498,9 @@ class ServeClientBase(object):
- input_bytes (np.ndarray): The next chunk of audio data to be processed. - input_bytes (np.ndarray): The next chunk of audio data to be processed.
- duration (float): The duration of the audio chunk in seconds. - duration (float): The duration of the audio chunk in seconds.
""" """
samples_take = max(0, (self.timestamp_offset - self.frames_offset) * self.RATE) with self.lock:
input_bytes = self.frames_np[int(samples_take):].copy() samples_take = max(0, (self.timestamp_offset - self.frames_offset) * self.RATE)
input_bytes = self.frames_np[int(samples_take):].copy()
duration = input_bytes.shape[0] / self.RATE duration = input_bytes.shape[0] / self.RATE
return input_bytes, duration return input_bytes, duration
@@ -715,7 +717,9 @@ class ServeClientTensorRT(ServeClientBase):
self.transcript.append({"text": last_segment + " "}) self.transcript.append({"text": last_segment + " "})
elif self.transcript[-1]["text"].strip() != last_segment: elif self.transcript[-1]["text"].strip() != last_segment:
self.transcript.append({"text": last_segment + " "}) self.transcript.append({"text": last_segment + " "})
self.timestamp_offset += duration
with self.lock():
self.timestamp_offset += duration
def speech_to_text(self): def speech_to_text(self):
""" """
@@ -1067,7 +1071,8 @@ class ServeClientFasterWhisper(ServeClientBase):
for i, s in enumerate(segments[:-1]): for i, s in enumerate(segments[:-1]):
text_ = s.text text_ = s.text
self.text.append(text_) self.text.append(text_)
start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end) with self.lock:
start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end)
if start >= end: if start >= end:
continue continue
@@ -1080,12 +1085,13 @@ class ServeClientFasterWhisper(ServeClientBase):
# only process the last segment if it satisfies the no_speech_thresh # only process the last segment if it satisfies the no_speech_thresh
if segments[-1].no_speech_prob <= self.no_speech_thresh: if segments[-1].no_speech_prob <= self.no_speech_thresh:
self.current_out += segments[-1].text self.current_out += segments[-1].text
last_segment = self.format_segment( with self.lock:
self.timestamp_offset + segments[-1].start, last_segment = self.format_segment(
self.timestamp_offset + min(duration, segments[-1].end), self.timestamp_offset + segments[-1].start,
self.current_out, self.timestamp_offset + min(duration, segments[-1].end),
completed=False self.current_out,
) completed=False
)
if self.current_out.strip() == self.prev_out.strip() and self.current_out != '': if self.current_out.strip() == self.prev_out.strip() and self.current_out != '':
self.same_output_count += 1 self.same_output_count += 1
@@ -1098,12 +1104,13 @@ class ServeClientFasterWhisper(ServeClientBase):
if self.same_output_count > self.same_output_threshold: if self.same_output_count > self.same_output_threshold:
if not len(self.text) or self.text[-1].strip().lower() != self.current_out.strip().lower(): if not len(self.text) or self.text[-1].strip().lower() != self.current_out.strip().lower():
self.text.append(self.current_out) self.text.append(self.current_out)
self.transcript.append(self.format_segment( with self.lock:
self.timestamp_offset, self.transcript.append(self.format_segment(
self.timestamp_offset + duration, self.timestamp_offset,
self.current_out, self.timestamp_offset + duration,
completed=True self.current_out,
)) completed=True
))
self.current_out = '' self.current_out = ''
offset = duration offset = duration
self.same_output_count = 0 self.same_output_count = 0
@@ -1113,6 +1120,7 @@ class ServeClientFasterWhisper(ServeClientBase):
# update offset # update offset
if offset is not None: if offset is not None:
self.timestamp_offset += offset with self.lock:
self.timestamp_offset += offset
return last_segment return last_segment