|
46 | 46 | raise Exception(f"Missing module: {e}") |
47 | 47 |
|
48 | 48 | SAMPLE_RATE = 24000 |
| 49 | +# Seconds to wait after a "flushed" message for trailing text tokens to arrive |
| 50 | +# before finalizing the transcription. |
| 51 | +TRANSCRIPT_AGGREGATION_DELAY = 0.1 |
49 | 52 |
|
50 | 53 |
|
51 | 54 | def language_to_gradium_language(language: Language) -> Optional[str]: |
@@ -202,7 +205,6 @@ def __init__( |
202 | 205 | # and pushed as a TranscriptionFrame. |
203 | 206 | self._accumulated_text: list[str] = [] |
204 | 207 | self._flush_counter = 0 |
205 | | - self._transcript_aggregation_delay = 0.1 # seconds to wait after flushed |
206 | 208 | self._transcript_aggregation_task: Optional[asyncio.Task] = None |
207 | 209 |
|
208 | 210 | def can_generate_metrics(self) -> bool: |
@@ -384,6 +386,9 @@ async def _disconnect(self): |
384 | 386 | await self.cancel_task(self._transcript_aggregation_task) |
385 | 387 | self._transcript_aggregation_task = None |
386 | 388 |
|
| 389 | + self._accumulated_text.clear() |
| 390 | + self._flush_counter = 0 |
| 391 | + |
387 | 392 | if self._receive_task: |
388 | 393 | await self.cancel_task(self._receive_task) |
389 | 394 | self._receive_task = None |
@@ -457,7 +462,7 @@ async def _handle_flushed(self): |
457 | 462 |
|
458 | 463 | async def _transcript_aggregation_handler(self): |
459 | 464 | """Wait for trailing tokens then finalize the accumulated transcription.""" |
460 | | - await asyncio.sleep(self._transcript_aggregation_delay) |
| 465 | + await asyncio.sleep(TRANSCRIPT_AGGREGATION_DELAY) |
461 | 466 | await self._finalize_accumulated_text() |
462 | 467 |
|
463 | 468 | async def _finalize_accumulated_text(self): |
|
0 commit comments