Clause-level chunking: first audible chunk capped at ~60 chars (turn-2+ latency fix)
Browse files- freestream/bot.py +20 -0
freestream/bot.py
CHANGED
|
@@ -133,6 +133,26 @@ class BotSession:
|
|
| 133 |
sentences = [s.strip() for s in re.split(r"(?<=[.!?])\s+", text) if s.strip()]
|
| 134 |
if not sentences:
|
| 135 |
sentences = [text]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
t0 = timing["t0"]
|
| 137 |
first_audio = None
|
| 138 |
total_audio = 0.0
|
|
|
|
| 133 |
sentences = [s.strip() for s in re.split(r"(?<=[.!?])\s+", text) if s.strip()]
|
| 134 |
if not sentences:
|
| 135 |
sentences = [text]
|
| 136 |
+
# Clause-level chunking: the first audible chunk pays full synth cost
|
| 137 |
+
# (measured 7.47s for a 99-char first sentence), so cap chunk length at
|
| 138 |
+
# comma boundaries — later chunks pipeline behind playback anyway.
|
| 139 |
+
MAX_CHUNK = 60
|
| 140 |
+
chunks = []
|
| 141 |
+
for sent in sentences:
|
| 142 |
+
if len(sent) <= MAX_CHUNK:
|
| 143 |
+
chunks.append(sent)
|
| 144 |
+
continue
|
| 145 |
+
buf = ""
|
| 146 |
+
for part in re.split(r"(?<=[,;:])\s+", sent):
|
| 147 |
+
cand = f"{buf} {part}".strip()
|
| 148 |
+
if buf and len(cand) > MAX_CHUNK:
|
| 149 |
+
chunks.append(buf)
|
| 150 |
+
buf = part
|
| 151 |
+
else:
|
| 152 |
+
buf = cand
|
| 153 |
+
if buf:
|
| 154 |
+
chunks.append(buf)
|
| 155 |
+
sentences = chunks or [text]
|
| 156 |
t0 = timing["t0"]
|
| 157 |
first_audio = None
|
| 158 |
total_audio = 0.0
|