Download services/refine/requests.py from modventures/room-visualizer-v2: direct link, hf CLI and curl.
- Browser
- Download file 2.07 kB
-
https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/services/refine/requests.py
- Command line
-
hf download hf://spaces/modventures/room-visualizer-v2/services/refine/requests.py
-
curl -L -o requests.py https://huggingface.co/spaces/modventures/room-visualizer-v2/resolve/main/services/refine/requests.py
2.07 kB
| """Identity and admission helpers for refinement requests.""" | |
| from __future__ import annotations | |
| import hashlib | |
| import json | |
| from datetime import datetime, timezone | |
| from pathlib import Path | |
| from config.paths import JOB_DIR | |
| from enums import JobStatus | |
| ACTIVE_REFINEMENT_STATUSES = {JobStatus.QUEUED, JobStatus.PROCESSING} | |
| ACTIVE_REFINEMENT_MAX_AGE_SECONDS = 20 * 60 | |
| def refinement_request_fingerprint(composite: bytes, surfaces: list[dict]) -> str: | |
| """Return a stable identity for the pixels and material arrangement.""" | |
| digest = hashlib.sha256() | |
| digest.update(composite) | |
| digest.update(b"\0") | |
| digest.update(json.dumps(surfaces, sort_keys=True, separators=(",", ":")).encode()) | |
| return digest.hexdigest() | |
| def active_refinement_jobs( | |
| job_id: str, | |
| *, | |
| job_dir: Path = JOB_DIR, | |
| now: datetime | None = None, | |
| ) -> list[dict]: | |
| """Return recent active refinements for a conversion, newest first. | |
| A process restart can leave a PROCESSING JSON file behind even though no | |
| task exists to finish it. Ignore records older than the frontend timeout so | |
| they cannot permanently block a new request. | |
| """ | |
| current_time = now or datetime.now(timezone.utc) | |
| active: list[dict] = [] | |
| for path in job_dir.glob(f"{job_id}.refine-*.json"): | |
| try: | |
| candidate = json.loads(path.read_text()) | |
| created_at = datetime.fromisoformat(str(candidate["createdAt"]).replace("Z", "+00:00")) | |
| if created_at.tzinfo is None: | |
| created_at = created_at.replace(tzinfo=timezone.utc) | |
| except (KeyError, OSError, TypeError, ValueError, json.JSONDecodeError): | |
| continue | |
| if candidate.get("jobId") != job_id: | |
| continue | |
| if candidate.get("status") not in ACTIVE_REFINEMENT_STATUSES: | |
| continue | |
| if (current_time - created_at).total_seconds() > ACTIVE_REFINEMENT_MAX_AGE_SECONDS: | |
| continue | |
| active.append(candidate) | |
| active.sort(key=lambda item: str(item.get("createdAt") or ""), reverse=True) | |
| return active | |