Spaces:
Running on Zero
Running on Zero
Download progress.py from Pabot/Content-filter-ai: direct link, hf CLI and curl.
- Browser
- Download file 4.14 kB
-
https://huggingface.co/spaces/Pabot/Content-filter-ai/resolve/main/progress.py
- Command line
-
hf download hf://spaces/Pabot/Content-filter-ai/progress.py
-
curl -L -o progress.py https://huggingface.co/spaces/Pabot/Content-filter-ai/resolve/main/progress.py
4.14 kB
| """Live scan progress for the report page: % done, the current step and the | |
| time left, written to the scan's row (throttled to one write every few seconds). | |
| Each step has an expected duration (from the video's size and length). The % | |
| and the time left come from those expectations, so the bar moves at roughly | |
| real speed; steps with real feedback (download bytes, ffmpeg time, screenshots) | |
| report exact fractions, and steps without it (Gemini) ease forward over their | |
| expected time without ever claiming to be finished. | |
| """ | |
| from __future__ import annotations | |
| import logging | |
| import math | |
| import threading | |
| import time | |
| log = logging.getLogger("progress") | |
| ORDER = ("download", "prepare", "upload", "analyze", "finish") | |
| LABELS = { | |
| "download": "Getting your video", | |
| "prepare": "Preparing your video", | |
| "upload": "Sending to the AI", | |
| "analyze": "Checking reuse, copyright and ad safety", | |
| "finish": "Capturing screenshots", | |
| } | |
| def estimates_for(size_bytes: float, minutes: float, needs_prepare: bool, from_r2: bool) -> dict: | |
| """Rough seconds per step for this video (tuned for the HF CPU worker).""" | |
| minutes = max(minutes, 0.2) | |
| prepared_bytes = minutes * 11e6 if needs_prepare else size_bytes # 720p H.264 ~ 11 MB/min | |
| return { | |
| "download": size_bytes / (30e6 if from_r2 else 15e6) + 2, | |
| "prepare": (minutes * 40 + 10) if needs_prepare else 0, | |
| "upload": prepared_bytes / 15e6 + 8, | |
| "analyze": 20 + minutes * 6, | |
| "finish": 8, | |
| } | |
| class ScanProgress: | |
| def __init__(self, client, scan_id: str, estimates: dict): | |
| self.client = client | |
| self.scan_id = scan_id | |
| self.est = {k: max(0.0, float(estimates.get(k, 0))) for k in ORDER} | |
| self.cur = ORDER[0] | |
| self.frac = 0.0 | |
| self.pct = 0.0 | |
| self._last_write = 0.0 | |
| self._last_label = None | |
| self._lock = threading.Lock() | |
| self._ramp_stop: threading.Event | None = None | |
| def set_estimate(self, key: str, seconds: float) -> None: | |
| with self._lock: | |
| self.est[key] = max(0.0, float(seconds)) | |
| def _calc(self) -> tuple[float, float]: | |
| total = sum(self.est.values()) or 1.0 | |
| i = ORDER.index(self.cur) | |
| done = sum(self.est[k] for k in ORDER[:i]) + self.frac * self.est[self.cur] | |
| return 100.0 * done / total, max(0.0, total - done) | |
| def update(self, key: str, frac: float = 0.0, force: bool = False) -> None: | |
| with self._lock: | |
| if ORDER.index(key) < ORDER.index(self.cur): | |
| return # never go back a step | |
| if key != self.cur: | |
| self.cur, self.frac = key, 0.0 | |
| self.frac = max(self.frac, min(1.0, max(0.0, frac))) | |
| pct, left = self._calc() | |
| self.pct = max(self.pct, pct) | |
| label = LABELS[key] | |
| now = time.time() | |
| if not (force or label != self._last_label or now - self._last_write >= 3): | |
| return | |
| self._last_write, self._last_label = now, label | |
| payload = {"progress": int(min(99, self.pct)), "stage": label, "eta_seconds": int(left)} | |
| try: | |
| self.client.table("scans").update(payload).eq("id", self.scan_id).execute() | |
| except Exception: # noqa: BLE001 (progress is nice to have; never break a scan) | |
| log.warning("progress update failed", exc_info=True) | |
| def ramp(self, key: str) -> None: | |
| """For steps with no real feedback: ease toward 95% of the step over its | |
| expected time (keeps moving if it runs long, never claims to be done).""" | |
| self.stop_ramp() | |
| stop = threading.Event() | |
| expected = max(1.0, self.est.get(key, 10)) | |
| self.update(key, 0.0, force=True) | |
| def run() -> None: | |
| t0 = time.time() | |
| while not stop.wait(3): | |
| self.update(key, 0.95 * (1 - math.exp(-(time.time() - t0) / expected))) | |
| threading.Thread(target=run, daemon=True).start() | |
| self._ramp_stop = stop | |
| def stop_ramp(self) -> None: | |
| if self._ramp_stop: | |
| self._ramp_stop.set() | |
| self._ramp_stop = None | |