feat(stt): pass conversation context as Whisper initial_prompt to reduce mishearings
This commit is contained in:
@@ -55,8 +55,12 @@ def stt_available() -> bool:
|
||||
return _model is not None
|
||||
|
||||
|
||||
async def transcribe(audio_bytes: bytes, mime_type: str = "audio/webm") -> str:
|
||||
"""Transcribe audio bytes to text. Runs the model in a thread executor."""
|
||||
async def transcribe(audio_bytes: bytes, mime_type: str = "audio/webm", initial_prompt: str | None = None) -> str:
|
||||
"""Transcribe audio bytes to text. Runs the model in a thread executor.
|
||||
|
||||
initial_prompt: optional text to bias the model toward domain-specific vocabulary
|
||||
(e.g. recent conversation context). Reduces mishearings like "gold" for "cold".
|
||||
"""
|
||||
if _model is None:
|
||||
raise RuntimeError("STT model not loaded")
|
||||
|
||||
@@ -73,7 +77,11 @@ async def transcribe(audio_bytes: bytes, mime_type: str = "audio/webm") -> str:
|
||||
f.write(audio_bytes)
|
||||
f.flush()
|
||||
t0 = time.monotonic()
|
||||
segments, _ = _model.transcribe(f.name, beam_size=5) # type: ignore[union-attr]
|
||||
segments, _ = _model.transcribe( # type: ignore[union-attr]
|
||||
f.name,
|
||||
beam_size=5,
|
||||
initial_prompt=initial_prompt or None,
|
||||
)
|
||||
text = " ".join(seg.text.strip() for seg in segments).strip()
|
||||
logger.debug("STT transcription took %.2fs", time.monotonic() - t0)
|
||||
return text
|
||||
|
||||
Reference in New Issue
Block a user