mirror of
https://github.com/debpalash/VoiceStudio.git
synced 2026-12-04 01:18:16 +00:00
fix(batch): check the speech model inside the transcribe step, not ahead of the whole pipeline (#4625)
* fix(batch): check the speech model when restoring, not in every pipeline run #4623 added the missing-model check to the batch transcribe stage. Every batch pipeline test that does not stub it then failed in CI's model-free environment, leaving main red. Restored jobs are the only ones that skip enqueue's check, so restore now checks once and shows such a job as failed with Retry, leaving the pipeline as it was. Also confines queue record paths to the batch folder after resolving them (the CodeQL path alerts on the record writer) and reads test files as UTF-8. * fix(batch): check the speech model inside the transcribe step itself Review found that a job queued before its speech model was removed could still download it, and that checking at restore can run before startup swaps a missing saved model for an installed one. The check now runs in the transcribe function, right before the backend loads, for every job: queued, retried or restored. Pipeline tests that stub the guarded transcribe call never reach it; the one test that runs it with a stubbed model opts out. Queue record paths are checked after resolving but replaced and removed by their own directory entry, so a link can't make a cleanup delete another job's record.
This commit is contained in:
1 parent
d6bd82844c
commit
ce3827b613
3 files changed
+43
-18
No files matched your search
@@ -95,7 +95,14 @@ def _batch_root() -> str:
|
||||
def _record_path(job_id: str) -> Optional[str]:
|
||||
if not _JOB_ID_RE.match(job_id or ""):
|
||||
return None
|
||||
return os.path.join(_batch_root(), os.path.basename(f"{job_id}{_RECORD_SUFFIX}"))
|
||||
root = _batch_root()
|
||||
path = os.path.join(root, f"{job_id}{_RECORD_SUFFIX}")
|
||||
# Ids reach here from request paths: the record must stay in the batch
|
||||
# folder. The resolved path is only checked; replacing or removing works on
|
||||
# the entry itself, so a link never redirects them to another job's record.
|
||||
if not os.path.realpath(path).startswith(root + os.sep):
|
||||
return None
|
||||
return path
|
||||
|
||||
|
||||
def _write_record(path: str, record: dict) -> None:
|
||||
@@ -539,15 +546,14 @@ async def _run_batch_pipeline_leased(job_id: str, job: dict, lease: voice_leases
|
||||
segment_transcript, assign_speakers_heuristic,
|
||||
)
|
||||
|
||||
# A job queued earlier, or restored after a restart, may find the speech
|
||||
# model removed since it was accepted: fail it with the message enqueue
|
||||
# gives, instead of letting the transcriber download the weights unasked.
|
||||
from services.asr_backend import asr_model_missing_detail, asr_model_missing_error
|
||||
missing = await asyncio.to_thread(asr_model_missing_error)
|
||||
if missing is not None:
|
||||
raise RuntimeError(asr_model_missing_detail(missing))
|
||||
|
||||
def _transcribe():
|
||||
# Enqueue checked the speech model, but it may have been removed since
|
||||
# (or while the app was closed, for a restored job). Loading it now
|
||||
# would download the weights without the user asking.
|
||||
from services.asr_backend import asr_model_missing_detail, asr_model_missing_error
|
||||
missing = asr_model_missing_error()
|
||||
if missing is not None:
|
||||
raise RuntimeError(asr_model_missing_detail(missing))
|
||||
# `load_*`, not `get_*`: the plain selector returns engines whose
|
||||
# shallow probe passed but whose deep import chain is broken, failing
|
||||
# the whole batch job at `.transcribe()` instead of degrading (#1185).
|
||||
|
||||
@@ -8,7 +8,8 @@ import json
|
||||
|
||||
import pytest
|
||||
|
||||
pytestmark = pytest.mark.asyncio
|
||||
# The hermetic test env has no speech model; restore checks for one.
|
||||
pytestmark = [pytest.mark.asyncio, pytest.mark.usefixtures("asr_model_installed")]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -111,7 +112,7 @@ async def test_a_record_without_its_upload_or_outside_the_folder_is_dropped(batc
|
||||
async def test_records_hold_only_the_upload_name_and_survive_a_moved_data_folder(
|
||||
batch, tmp_path, monkeypatch):
|
||||
_queued(batch, tmp_path, "moved", created_at=1)
|
||||
record = json.loads((tmp_path / "batch" / "moved.queued.json").read_text())
|
||||
record = json.loads((tmp_path / "batch" / "moved.queued.json").read_text(encoding="utf-8"))
|
||||
assert record["video_path"] == "moved.mp4"
|
||||
|
||||
# The user moves the data folder; the copied record still finds its upload.
|
||||
@@ -150,10 +151,9 @@ async def test_a_record_that_cannot_be_removed_is_marked_finished(batch, tmp_pat
|
||||
def refuse(_path):
|
||||
raise batch.FileCleanupError("locked")
|
||||
|
||||
monkeypatch.setattr(batch, "unlink_if_present", refuse)
|
||||
batch._discard_record("stuck") # the worker's terminal path
|
||||
monkeypatch.undo()
|
||||
monkeypatch.setattr(batch, "DATA_DIR", str(tmp_path))
|
||||
with monkeypatch.context() as patch:
|
||||
patch.setattr(batch, "unlink_if_present", refuse)
|
||||
batch._discard_record("stuck") # the worker's terminal path
|
||||
assert await _restart(batch, monkeypatch) == 0
|
||||
# The finished job's results are left alone.
|
||||
assert (tmp_path / "batch" / "stuck" / "dubbed_es.mp4").exists()
|
||||
@@ -168,9 +168,9 @@ async def test_one_malformed_record_is_dropped_without_blocking_the_rest(
|
||||
_queued(batch, tmp_path, "good", created_at=5)
|
||||
_queued(batch, tmp_path, "bad", created_at=1)
|
||||
path = tmp_path / "batch" / "bad.queued.json"
|
||||
record = json.loads(path.read_text())
|
||||
record = json.loads(path.read_text(encoding="utf-8"))
|
||||
record[field] = value
|
||||
path.write_text(json.dumps(record))
|
||||
path.write_text(json.dumps(record), encoding="utf-8")
|
||||
|
||||
assert await _restart(batch, monkeypatch) == 1
|
||||
assert list(batch._jobs) == ["good"]
|
||||
@@ -202,6 +202,22 @@ async def test_a_job_finding_its_speech_model_removed_fails_instead_of_downloadi
|
||||
job = {"id": "nomodel", "status": "running", "filename": "in.mp4", "video_path": str(video),
|
||||
"langs": ["es"], "voice_id": None, "preserve_bg": False,
|
||||
"translation_provider": "argos", "created_at": 1, "attempts": 1}
|
||||
with pytest.raises(RuntimeError, match="(?i)model"):
|
||||
with pytest.raises(RuntimeError, match="speech-to-text"):
|
||||
await batch._run_batch_pipeline("nomodel", job)
|
||||
assert loaded == []
|
||||
|
||||
|
||||
async def test_a_record_linked_to_another_jobs_record_never_removes_that_record(
|
||||
batch, tmp_path, monkeypatch):
|
||||
_queued(batch, tmp_path, "real", created_at=1)
|
||||
link = tmp_path / "batch" / "alias.queued.json"
|
||||
try:
|
||||
link.symlink_to(tmp_path / "batch" / "real.queued.json")
|
||||
except OSError:
|
||||
pytest.skip("symlinks need extra rights on this host")
|
||||
|
||||
assert await _restart(batch, monkeypatch) == 1
|
||||
assert list(batch._jobs) == ["real"]
|
||||
# The mismatched link is dropped; the record it pointed at stays.
|
||||
assert not link.is_symlink()
|
||||
assert (tmp_path / "batch" / "real.queued.json").is_file()
|
||||
@@ -54,6 +54,9 @@ def test_reported_language_is_preserved():
|
||||
assert parse_transcript({'result': {'language': 'nl'}}, word_timestamps=False)['language'] == 'nl'
|
||||
|
||||
|
||||
# The transcribe stage checks for an installed speech model first; this test
|
||||
# stubs the model itself, so it opts out of that check.
|
||||
@pytest.mark.usefixtures('asr_model_installed')
|
||||
@pytest.mark.parametrize('language', [None, 'unknown', 'und', ' '])
|
||||
def test_batch_stops_before_translation_when_language_unknown(tmp_path, monkeypatch, m, language):
|
||||
from api.routers import batch
|
||||
|
||||
Reference in new issue
Block a user