Skip to content

Commit

Permalink
Merge pull request #89 from hcljsq/main
Browse files Browse the repository at this point in the history
format segment timestamps
  • Loading branch information
makaveli10 authored Jan 3, 2024
2 parents e30286c + 32c6a56 commit 7b2f5cf
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions whisper_live/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,14 @@ def speech_to_text(self):
logging.error(f"[ERROR]: {e}")
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):
"""
Processes the segments from whisper. Appends all the segments to the list
Expand Down Expand Up @@ -437,22 +445,16 @@ def update_segments(self, segments, duration):
text_ = s.text
self.text.append(text_)
start, end = self.timestamp_offset + s.start, self.timestamp_offset + min(duration, s.end)
self.transcript.append(
{
'start': start,
'end': end,
'text': text_
}
)
self.transcript.append(self.format_segment(start, end, text_))

offset = min(duration, s.end)

self.current_out += segments[-1].text
last_segment = {
'start': self.timestamp_offset + segments[-1].start,
'end': self.timestamp_offset + min(duration, segments[-1].end),
'text': self.current_out
}
last_segment = self.format_segment(
self.timestamp_offset + segments[-1].start,
self.timestamp_offset + min(duration, segments[-1].end),
self.current_out
)

# if same incomplete segment is seen multiple times then update the offset
# and append the segment to the list
Expand All @@ -464,13 +466,11 @@ def update_segments(self, segments, duration):
if self.same_output_threshold > 5:
if not len(self.text) or self.text[-1].strip().lower()!=self.current_out.strip().lower():
self.text.append(self.current_out)
self.transcript.append(
{
'start': self.timestamp_offset,
'end': self.timestamp_offset + duration,
'text': self.current_out
}
)
self.transcript.append(self.format_segment(
self.timestamp_offset,
self.timestamp_offset + duration,
self.current_out
))
self.current_out = ''
offset = duration
self.same_output_threshold = 0
Expand Down

0 comments on commit 7b2f5cf

Please sign in to comment.