From 81cea12e0cea812b25e688c2d19676bdcb4a5721 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Sat, 28 Feb 2026 19:39:39 -0500 Subject: [PATCH] Revert "feat: implement heuristic punctuation buffering for translations when context is enabled" --- history.md | 2 +- transcribe.py | 52 +++++++++++++-------------------------------------- 2 files changed, 14 insertions(+), 40 deletions(-) diff --git a/history.md b/history.md index 6faf380..0ca0267 100644 --- a/history.md +++ b/history.md @@ -101,4 +101,4 @@ The project now features a high-performance, Apple Silicon-optimized pipeline th - **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. - **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. diff --git a/transcribe.py b/transcribe.py index e031bec..ceb89ca 100644 --- a/transcribe.py +++ b/transcribe.py @@ -150,8 +150,6 @@ def main(): speech_started = False last_stream_time = time.time() rolling_context = "" - translation_buffer_orig = "" - translation_buffer_en = "" try: with sd.InputStream(samplerate=SAMPLERATE, channels=CHANNELS, callback=callback, blocksize=BLOCK_SIZE, device=device_index): @@ -199,6 +197,13 @@ def main(): if 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 if detected_lang != "en": bridge_kwargs = {"path_or_hf_repo": WHISPER_MODEL, "task": "translate"} @@ -206,52 +211,21 @@ def main(): bridge_kwargs["language"] = args.lang bridge_result = mlx_whisper.transcribe(current_audio, **bridge_kwargs) english_text = bridge_result['text'].strip() + if args.en: + payload["en"] = english_text + print(f"[EN]: {english_text}") else: english_text = original_text + if args.en: + payload["en"] = english_text # Update rolling context for next segment if args.context: # keep the last ~200 characters of the source language text 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 - if final_en and translation_engines: + if english_text and translation_engines: # Limit input length clean_en = english_text[:247] + "..." if len(english_text) > 250 else english_text