Spaces:
Running on Zero
Running on Zero
File size: 3,582 Bytes
25e4412 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | """Job progress reporting from the worker subprocess into the jobs table (read by the SSE endpoint)."""
from __future__ import annotations
import time
from dataclasses import dataclass, field
from sqlmodel import Session
from stemdeck.db import get_engine
from stemdeck.models import Job
STAGE_LABELS: dict[str, str] = {
"queued": "Waiting in queue",
"downloading": "Downloading from YouTube",
"converting": "Converting audio",
"separating_vocals": "Separating vocals",
"separating_lead_backing": "Splitting lead and backing vocals",
"separating_instruments": "Separating instruments",
"assembling": "Assembling stems",
"analysing": "Analysing beats and key",
"building_assets": "Building waveforms",
"extracting": "Extracting instrument",
"downloading_model": "Downloading model (first time only)",
"done": "Done",
"failed": "Failed",
"cancelled": "Cancelled",
}
UPDATE_INTERVAL_SEC = 0.4
@dataclass
class JobReporter:
"""Tracks weighted stages; weights are expected seconds so overall % and ETA stay meaningful."""
job_id: str
stage_weights: dict[str, float]
_current: str | None = None
_done_weight: float = 0.0
_stage_started: float = field(default_factory=time.perf_counter)
_job_started: float = field(default_factory=time.perf_counter)
_last_write: float = 0.0
_stage_fraction: float = 0.0
_message: str | None = None
@property
def total_weight(self) -> float:
return max(sum(self.stage_weights.values()), 1e-6)
def stage(self, name: str, message: str | None = None) -> None:
if self._current is not None:
self._done_weight += self.stage_weights.get(self._current, 0.0)
self._current = name
self._stage_started = time.perf_counter()
self._stage_fraction = 0.0
self._message = message
self._write(force=True)
def update(self, fraction: float, message: str | None = None) -> None:
self._stage_fraction = max(0.0, min(1.0, fraction))
if message is not None:
self._message = message
self._write(force=False)
def overall(self) -> float:
current = self.stage_weights.get(self._current or "", 0.0) * self._stage_fraction
return min(0.999, (self._done_weight + current) / self.total_weight)
def eta_seconds(self) -> float | None:
overall = self.overall()
elapsed = time.perf_counter() - self._job_started
planned_remaining = self.total_weight * (1.0 - overall)
if overall < 0.05 or elapsed < 5:
return planned_remaining
observed_total = elapsed / overall
return max(0.0, 0.5 * planned_remaining + 0.5 * (observed_total - elapsed))
def _write(self, force: bool) -> None:
now = time.perf_counter()
if not force and now - self._last_write < UPDATE_INTERVAL_SEC:
return
self._last_write = now
with Session(get_engine()) as session:
job = session.get(Job, self.job_id)
if job is None or job.status != "running":
return
job.stage = self._current or job.stage
job.stage_progress = round(self._stage_fraction, 4)
job.progress = round(self.overall(), 4)
job.message = self._message
eta = self.eta_seconds()
job.eta_sec = round(eta, 1) if eta is not None else None
session.add(job)
session.commit()
|