feat(stt): pass conversation context as Whisper initial_prompt to reduce mishearings
This commit is contained in:
@@ -80,10 +80,12 @@ async def transcribe_audio():
|
||||
return jsonify({"error": "Audio file too large (max 25 MB)"}), 413
|
||||
|
||||
mime_type = audio_file.content_type or "audio/webm"
|
||||
form = await request.form
|
||||
context = (form.get("context") or "").strip() or None
|
||||
|
||||
t0 = time.monotonic()
|
||||
try:
|
||||
transcript = await transcribe(audio_bytes, mime_type)
|
||||
transcript = await transcribe(audio_bytes, mime_type, initial_prompt=context)
|
||||
except Exception:
|
||||
logger.exception("STT transcription failed")
|
||||
return jsonify({"error": "Transcription failed"}), 500
|
||||
|
||||
@@ -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