Spaces:
Running
Running
| # ml_module/utils/helpers.py | |
| from __future__ import annotations | |
| import datetime | |
| import hashlib | |
| import json | |
| import uuid | |
| from typing import Any | |
| def generate_project_id() -> str: | |
| """Generates a unique identifier for a new project.""" | |
| return f"proj_{uuid.uuid4().hex[:12]}" | |
| def compute_checksum(data: bytes) -> str: | |
| """Return a stable SHA-256 checksum for the provided bytes.""" | |
| return hashlib.sha256(data).hexdigest() | |
| def json_bytes(payload: Any) -> bytes: | |
| """Serialize payload to canonical JSON bytes for hashing/persistence.""" | |
| if isinstance(payload, (bytes, bytearray)): | |
| return bytes(payload) | |
| if isinstance(payload, str): | |
| return payload.encode("utf-8") | |
| return json.dumps(payload, sort_keys=True, separators=(",", ":")).encode("utf-8") | |
| def ensure_bytes(value: Any) -> bytes: | |
| if isinstance(value, (bytes, bytearray)): | |
| return bytes(value) | |
| if isinstance(value, str): | |
| return value.encode("utf-8") | |
| raise TypeError("Expected bytes or string value for ensure_bytes") | |
| def utc_now() -> datetime.datetime: | |
| return datetime.datetime.now(datetime.timezone.utc) | |
| def utc_now_iso() -> str: | |
| return utc_now().isoformat() |