refactor(server): add format_segment helper to standardize timestamp output

This commit is contained in:
Chen Hua
2024-01-03 12:25:25 +08:00
parent e30286c046
commit 32c6a565d7
+19 -19
View File
@@ -407,6 +407,14 @@ class ServeClient:
logging.error(f"[ERROR]: {e}") logging.error(f"[ERROR]: {e}")
time.sleep(0.01) time.sleep(0.01)
def format_segment(self, start, end, text):
"""Helper function to format a segment with string timestamps."""
return {
'start': "{:.3f}".format(start),
'end': "{:.3f}".format(end),
'text': text
}
def update_segments(self, segments, duration): def update_segments(self, segments, duration):
""" """
Processes the segments from whisper. Appends all the segments to the list Processes the segments from whisper. Appends all the segments to the list
@@ -437,22 +445,16 @@ class ServeClient:
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) start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end)
self.transcript.append( self.transcript.append(self.format_segment(start, end, text_))
{
'start': start,
'end': end,
'text': text_
}
)
offset = min(duration, s.end) offset = min(duration, s.end)
self.current_out += segments[-1].text self.current_out += segments[-1].text
last_segment = { last_segment = self.format_segment(
'start': self.timestamp_offset + segments[-1].start, self.timestamp_offset + segments[-1].start,
'end': self.timestamp_offset + min(duration, segments[-1].end), self.timestamp_offset + min(duration, segments[-1].end),
'text': self.current_out self.current_out
} )
# if same incomplete segment is seen multiple times then update the offset # if same incomplete segment is seen multiple times then update the offset
# and append the segment to the list # and append the segment to the list
@@ -464,13 +466,11 @@ class ServeClient:
if self.same_output_threshold > 5: if self.same_output_threshold > 5:
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.transcript.append(self.format_segment(
{ self.timestamp_offset,
'start': self.timestamp_offset, self.timestamp_offset + duration,
'end': self.timestamp_offset + duration, self.current_out
'text': self.current_out ))
}
)
self.current_out = '' self.current_out = ''
offset = duration offset = duration
self.same_output_threshold = 0 self.same_output_threshold = 0