Handle CLI request errors and backend whisper timeout
This commit is contained in:
@@ -17,6 +17,7 @@ CONTENT_TYPES = {
|
||||
}
|
||||
|
||||
app = FastAPI(title="whisper-remote-backend")
|
||||
WHISPER_PROCESS_TIMEOUT_SECONDS = 300
|
||||
|
||||
|
||||
def validate_output_format(output_format: str) -> str:
|
||||
@@ -112,12 +113,21 @@ async def transcribe(
|
||||
check=False,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=WHISPER_PROCESS_TIMEOUT_SECONDS,
|
||||
)
|
||||
except FileNotFoundError as exc:
|
||||
raise HTTPException(
|
||||
status_code=500,
|
||||
detail="The 'whisper' CLI was not found on PATH on the backend host.",
|
||||
) from exc
|
||||
except subprocess.TimeoutExpired as exc:
|
||||
raise HTTPException(
|
||||
status_code=504,
|
||||
detail=(
|
||||
"Whisper CLI timed out after "
|
||||
f"{WHISPER_PROCESS_TIMEOUT_SECONDS}s and was terminated."
|
||||
),
|
||||
) from exc
|
||||
|
||||
if completed.returncode != 0:
|
||||
detail = completed.stderr.strip() or completed.stdout.strip() or "Whisper CLI failed."
|
||||
|
||||
@@ -64,3 +64,22 @@ def test_transcriptions_maps_subprocess_failure(monkeypatch) -> None:
|
||||
|
||||
assert response.status_code == 502
|
||||
assert response.json()["detail"] == "bad whisper day"
|
||||
|
||||
|
||||
def test_transcriptions_maps_subprocess_timeout(monkeypatch) -> None:
|
||||
def fake_run(command: list[str], check: bool, capture_output: bool, text: bool, timeout: int):
|
||||
raise server.subprocess.TimeoutExpired(cmd=command, timeout=timeout)
|
||||
|
||||
monkeypatch.setattr(server.subprocess, "run", fake_run)
|
||||
|
||||
response = client.post(
|
||||
"/transcriptions",
|
||||
data={"model": "base", "output_format": "txt"},
|
||||
files={"file": ("clip.wav", b"audio", "audio/wav")},
|
||||
)
|
||||
|
||||
assert response.status_code == 504
|
||||
assert (
|
||||
response.json()["detail"]
|
||||
== f"Whisper CLI timed out after {server.WHISPER_PROCESS_TIMEOUT_SECONDS}s and was terminated."
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user