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
+8
View File
@@ -479,6 +479,7 @@ 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
""" """
with self.lock:
if self.frames_np[int((self.timestamp_offset - self.frames_offset)*self.RATE):].shape[0] > 25 * self.RATE: if self.frames_np[int((self.timestamp_offset - self.frames_offset)*self.RATE):].shape[0] > 25 * self.RATE:
duration = self.frames_np.shape[0] / self.RATE duration = self.frames_np.shape[0] / self.RATE
self.timestamp_offset = self.frames_offset + duration - 5 self.timestamp_offset = self.frames_offset + duration - 5
@@ -497,6 +498,7 @@ 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.
""" """
with self.lock:
samples_take = max(0, (self.timestamp_offset - self.frames_offset) * self.RATE) samples_take = max(0, (self.timestamp_offset - self.frames_offset) * self.RATE)
input_bytes = self.frames_np[int(samples_take):].copy() input_bytes = self.frames_np[int(samples_take):].copy()
duration = input_bytes.shape[0] / self.RATE duration = input_bytes.shape[0] / self.RATE
@@ -715,6 +717,8 @@ 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 + " "})
with self.lock():
self.timestamp_offset += duration self.timestamp_offset += duration
def speech_to_text(self): def speech_to_text(self):
@@ -1067,6 +1071,7 @@ 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_)
with self.lock:
start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end) start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end)
if start >= end: if start >= end:
@@ -1080,6 +1085,7 @@ 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
with self.lock:
last_segment = self.format_segment( last_segment = self.format_segment(
self.timestamp_offset + segments[-1].start, self.timestamp_offset + segments[-1].start,
self.timestamp_offset + min(duration, segments[-1].end), self.timestamp_offset + min(duration, segments[-1].end),
@@ -1098,6 +1104,7 @@ 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)
with self.lock:
self.transcript.append(self.format_segment( self.transcript.append(self.format_segment(
self.timestamp_offset, self.timestamp_offset,
self.timestamp_offset + duration, self.timestamp_offset + duration,
@@ -1113,6 +1120,7 @@ class ServeClientFasterWhisper(ServeClientBase):
# update offset # update offset
if offset is not None: if offset is not None:
with self.lock:
self.timestamp_offset += offset self.timestamp_offset += offset
return last_segment return last_segment