Revert "feat: implement heuristic punctuation buffering for translations when context is enabled"

This commit is contained in:
Adolfo Reyna
2026-02-28 19:39:39 -05:00
parent f68111123d
commit 81cea12e0c
2 changed files with 14 additions and 40 deletions
+1 -1
View File
@@ -101,4 +101,4 @@ The project now features a high-performance, Apple Silicon-optimized pipeline th
- **Approach:** - **Approach:**
- **Prompt Caching (`-c`):** Implemented a rolling context buffer that feeds the last 200 characters of previously transcribed text back into Whisper as an `initial_prompt`, maintaining sentence continuity. - **Prompt Caching (`-c`):** Implemented a rolling context buffer that feeds the last 200 characters of previously transcribed text back into Whisper as an `initial_prompt`, maintaining sentence continuity.
- **Language Bypassing (`--lang`):** Added the ability to hardcode the source language to skip the Whisper language identification phase on every chunk. - **Language Bypassing (`--lang`):** Added the ability to hardcode the source language to skip the Whisper language identification phase on every chunk.
- **Heuristic Punctuation Buffering:** When `-c` is enabled, English translations are buffered until a definitive punctuation mark (`.`, `?`, `!`) is reached, preventing grammatical errors caused by translating partial sentences. - **Heuristic Punctuation Buffering (Reverted):** Briefly implemented a system to hold English translations until a definitive punctuation mark was reached to prevent grammatical errors. This was reverted because Whisper's punctuation generation is not 100% reliable, leading to translations getting "stuck" in the buffer indefinitely if no period was generated.
+13 -39
View File
@@ -150,8 +150,6 @@ def main():
speech_started = False speech_started = False
last_stream_time = time.time() last_stream_time = time.time()
rolling_context = "" rolling_context = ""
translation_buffer_orig = ""
translation_buffer_en = ""
try: try:
with sd.InputStream(samplerate=SAMPLERATE, channels=CHANNELS, callback=callback, blocksize=BLOCK_SIZE, device=device_index): with sd.InputStream(samplerate=SAMPLERATE, channels=CHANNELS, callback=callback, blocksize=BLOCK_SIZE, device=device_index):
@@ -199,6 +197,13 @@ def main():
if original_text: if original_text:
print(f"\n[{detected_lang.upper()}]: {original_text}") print(f"\n[{detected_lang.upper()}]: {original_text}")
# Prepare payload
payload = {"original": original_text}
# Include detected language if requested or if it's the bridge
if (detected_lang in active_target_langs) or (detected_lang == "en" and args.en):
payload[detected_lang] = original_text
# 2. Bridge to English if not already English # 2. Bridge to English if not already English
if detected_lang != "en": if detected_lang != "en":
bridge_kwargs = {"path_or_hf_repo": WHISPER_MODEL, "task": "translate"} bridge_kwargs = {"path_or_hf_repo": WHISPER_MODEL, "task": "translate"}
@@ -206,52 +211,21 @@ def main():
bridge_kwargs["language"] = args.lang bridge_kwargs["language"] = args.lang
bridge_result = mlx_whisper.transcribe(current_audio, **bridge_kwargs) bridge_result = mlx_whisper.transcribe(current_audio, **bridge_kwargs)
english_text = bridge_result['text'].strip() english_text = bridge_result['text'].strip()
if args.en:
payload["en"] = english_text
print(f"[EN]: {english_text}")
else: else:
english_text = original_text english_text = original_text
if args.en:
payload["en"] = english_text
# Update rolling context for next segment # Update rolling context for next segment
if args.context: if args.context:
# keep the last ~200 characters of the source language text # keep the last ~200 characters of the source language text
rolling_context = (rolling_context + " " + original_text)[-200:].strip() rolling_context = (rolling_context + " " + original_text)[-200:].strip()
# Heuristic Punctuation Buffering for better translation
if args.context:
translation_buffer_orig = (translation_buffer_orig + " " + original_text).strip()
translation_buffer_en = (translation_buffer_en + " " + english_text).strip()
if not translation_buffer_en.endswith(('.', '?', '!', '', '', '')) and len(translation_buffer_en) < 200:
if args.en:
print(f"[EN (partial)]: {english_text}")
audio_buffer = []
speech_started = False
last_stream_time = time.time()
continue # Wait for more audio before translating
final_orig = translation_buffer_orig
final_en = translation_buffer_en
translation_buffer_orig = ""
translation_buffer_en = ""
else:
final_orig = original_text
final_en = english_text
# Prepare payload
payload = {"original": final_orig}
# Include detected language if requested or if it's the bridge
if (detected_lang in active_target_langs) or (detected_lang == "en" and args.en):
payload[detected_lang] = final_orig
if args.en:
payload["en"] = final_en
if args.context:
print(f"[EN (buffered)]: {final_en}")
else:
print(f"[EN]: {final_en}")
# 3. Translate from English to other languages # 3. Translate from English to other languages
if final_en and translation_engines: if english_text and translation_engines:
# Limit input length # Limit input length
clean_en = english_text[:247] + "..." if len(english_text) > 250 else english_text clean_en = english_text[:247] + "..." if len(english_text) > 250 else english_text