Skip to content

Commit a68d878

Browse files
committed
fix: improve heuristic for not combining paragraphs
this changes the detection heuristic for overly long sentences from the previous "longer than 30s" to a word based approach of "more than 161 words". moreover it gets rid of the stateful approach that we would disable all recombination after the first overly long paragraph. overall this is not a perfect approach but produces saner results than before. this should temporarily fix the situation in which transcribee generates only one word per paragraph. the proper fix is resetting the whisper state from time to time and will land in the future.
1 parent 06ba6c0 commit a68d878

3 files changed

Lines changed: 31 additions & 110 deletions

File tree

worker/tests/data/test_strict_sentence_paragraphs-doest_combine_long_para.json

Lines changed: 0 additions & 90 deletions
This file was deleted.

worker/tests/test_transcribe.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import pytest
66
from pydantic import BaseModel
7-
from transcribee_proto.document import Paragraph
7+
from transcribee_proto.document import Atom, Paragraph
88
from transcribee_worker.whisper_transcribe import (
99
move_space_to_prev_token,
1010
strict_sentence_paragraphs,
@@ -43,6 +43,28 @@ def test_strict_sentence_paragraphs(data_file):
4343
assert output == test_data.expected
4444

4545

46+
def test_strict_sentence_paragraphs_dont_combine_overly_long_paras():
47+
test_data = [
48+
Paragraph(
49+
lang="en",
50+
children=[
51+
Atom(text=f"{word} ", start=0, end=0, conf=1, conf_ts=0)
52+
for word in f"this is long sentence {i} without a sentence ending".split()
53+
],
54+
)
55+
for i in range(19)
56+
]
57+
58+
output = list(doc_chain_func_to_list(strict_sentence_paragraphs)(test_data))
59+
60+
for para in output:
61+
for atom in para.children:
62+
print(atom.text, end="")
63+
print()
64+
65+
assert output == test_data
66+
67+
4668
@pytest.mark.parametrize(
4769
"data_file",
4870
glob.glob(

worker/transcribee_worker/whisper_transcribe.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,18 @@ def strict_sentence_paragraphs(
9090
) -> Iterator[Paragraph]:
9191
acc_paragraph = None
9292
acc_used_paras = []
93-
combination_active = True
9493
for paragraph in iter:
95-
if not combination_active:
96-
yield paragraph
97-
continue
98-
elif acc_paragraph is None:
94+
if acc_paragraph is not None and len(acc_paragraph.children) > 161:
95+
# it seems like whisper is on some path that does not involve sentence breaks
96+
# as a workaround we just emit the raw whisper bars
97+
yield from acc_used_paras
98+
acc_paragraph = None
99+
100+
if acc_paragraph is None:
99101
acc_paragraph = Paragraph(
100102
lang=paragraph.lang, speaker=paragraph.speaker, children=[]
101103
)
102104
acc_used_paras = []
103-
elif (
104-
(start := acc_paragraph.start()) is not None
105-
and (end := paragraph.end()) is not None
106-
and end - start > 30
107-
):
108-
# It seems like whisper doesn't produce sentence breaks. Ignore the
109-
# current `acc_paragraph` and yield the original paras instead,
110-
# disable this step until the end of the document
111-
combination_active = False
112-
for para in acc_used_paras:
113-
yield para
114-
yield paragraph
115-
continue
116105
elif (
117106
acc_paragraph.lang != paragraph.lang
118107
or acc_paragraph.speaker != paragraph.speaker
@@ -163,7 +152,7 @@ def strict_sentence_paragraphs(
163152
children=paragraph.children[acc_yield_offset:],
164153
)
165154
)
166-
if acc_paragraph is not None and acc_paragraph.children and combination_active:
155+
if acc_paragraph is not None and acc_paragraph.children:
167156
yield acc_paragraph
168157

169158

0 commit comments

Comments
 (0)