Send completed bool with each segment

Completed bool represents if the segment is completely processed by the server

Signed-off-by: makaveli10 <suryanvineet47@gmail.com>
This commit is contained in:
makaveli10
2024-11-05 18:11:32 +05:30
parent 00f0ff1112
commit 81c57ae40c
2 changed files with 11 additions and 8 deletions
+3 -3
View File
@@ -112,9 +112,9 @@ class Client:
for i, seg in enumerate(segments): for i, seg in enumerate(segments):
if not text or text[-1] != seg["text"]: if not text or text[-1] != seg["text"]:
text.append(seg["text"]) text.append(seg["text"])
if i == len(segments) - 1: if i == len(segments) - 1 and not seg["completed"]:
self.last_segment = seg self.last_segment = seg
elif (self.server_backend == "faster_whisper" and elif (self.server_backend == "faster_whisper" and seg["completed"] and
(not self.transcript or (not self.transcript or
float(seg['start']) >= float(self.transcript[-1]['end']))): float(seg['start']) >= float(self.transcript[-1]['end']))):
self.transcript.append(seg) self.transcript.append(seg)
@@ -259,7 +259,7 @@ class Client:
""" """
if self.server_backend == "faster_whisper": if self.server_backend == "faster_whisper":
if (self.last_segment): if (self.last_segment) and self.transcript[-1]["text"] != self.last_segment["text"]:
self.transcript.append(self.last_segment) self.transcript.append(self.last_segment)
utils.create_srt_file(self.transcript, output_path) utils.create_srt_file(self.transcript, output_path)
+8 -5
View File
@@ -1001,7 +1001,7 @@ class ServeClientFasterWhisper(ServeClientBase):
logging.error(f"[ERROR]: Failed to transcribe audio chunk: {e}") logging.error(f"[ERROR]: Failed to transcribe audio chunk: {e}")
time.sleep(0.01) time.sleep(0.01)
def format_segment(self, start, end, text): def format_segment(self, start, end, text, completed=False):
""" """
Formats a transcription segment with precise start and end times alongside the transcribed text. Formats a transcription segment with precise start and end times alongside the transcribed text.
@@ -1018,7 +1018,8 @@ class ServeClientFasterWhisper(ServeClientBase):
return { return {
'start': "{:.3f}".format(start), 'start': "{:.3f}".format(start),
'end': "{:.3f}".format(end), 'end': "{:.3f}".format(end),
'text': text 'text': text,
'completed': completed
} }
def update_segments(self, segments, duration): def update_segments(self, segments, duration):
@@ -1058,7 +1059,7 @@ class ServeClientFasterWhisper(ServeClientBase):
if s.no_speech_prob > self.no_speech_thresh: if s.no_speech_prob > self.no_speech_thresh:
continue continue
self.transcript.append(self.format_segment(start, end, text_)) self.transcript.append(self.format_segment(start, end, text_, completed=True))
offset = min(duration, s.end) offset = min(duration, s.end)
# only process the segments if it satisfies the no_speech_thresh # only process the segments if it satisfies the no_speech_thresh
@@ -1067,7 +1068,8 @@ class ServeClientFasterWhisper(ServeClientBase):
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),
self.current_out self.current_out,
completed=False
) )
# if same incomplete segment is seen multiple times then update the offset # if same incomplete segment is seen multiple times then update the offset
@@ -1083,7 +1085,8 @@ class ServeClientFasterWhisper(ServeClientBase):
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,
self.current_out self.current_out,
completed=True
)) ))
self.current_out = '' self.current_out = ''
offset = duration offset = duration