fix: enforce asr client timeout

This commit is contained in:
2026-05-19 09:27:47 +08:00
parent e5652c463c
commit 9a4268281e
2 changed files with 15 additions and 6 deletions

View File

@@ -2790,14 +2790,13 @@ def _transcribe_gemini_sync(wav: Path) -> list[dict]:
last_error: Exception | None = None
for attempt in range(3):
try:
resp = llm().chat.completions.create(
resp = llm().with_options(timeout=ASR_TIMEOUT_SECONDS).chat.completions.create(
model=ASR_FALLBACK_MODEL,
messages=[{"role": "user", "content": [
{"type": "text", "text": prompt},
{"type": "input_audio", "input_audio": {"data": audio_b64, "format": "wav"}},
]}],
temperature=0,
timeout=ASR_TIMEOUT_SECONDS,
)
content = (resp.choices[0].message.content or "").strip()
return _validate_asr_segments(_parse_asr_segments(content, duration), duration, "gemini audio fallback")
@@ -2814,12 +2813,11 @@ def _transcribe_sync(wav: Path) -> list[dict]:
duration = media_duration(wav)
try:
with wav.open("rb") as f:
resp = llm().audio.transcriptions.create(
resp = llm().with_options(timeout=ASR_TIMEOUT_SECONDS).audio.transcriptions.create(
file=(wav.name, f, "audio/wav"),
model=ASR_MODEL,
response_format="verbose_json",
timestamp_granularities=["segment"],
timeout=ASR_TIMEOUT_SECONDS,
)
raw = resp.model_dump() if hasattr(resp, "model_dump") else resp
segments = raw.get("segments") or []
@@ -2978,7 +2976,7 @@ def _audio_profile_model_sync(wav: Path, segments: list[TranscriptSegment], targ
last_error: Exception | None = None
for attempt in range(2):
try:
resp = llm().chat.completions.create(
resp = llm().with_options(timeout=ASR_TIMEOUT_SECONDS).chat.completions.create(
model=ASR_FALLBACK_MODEL,
messages=[{"role": "user", "content": [
{"type": "text", "text": prompt},
@@ -2987,7 +2985,6 @@ def _audio_profile_model_sync(wav: Path, segments: list[TranscriptSegment], targ
response_format={"type": "json_object"},
temperature=0.1,
max_tokens=900,
timeout=ASR_TIMEOUT_SECONDS,
)
content = (resp.choices[0].message.content or "").strip()
data = json.loads(content)