annotation-studio / reporting.py
pepijn223's picture
pepijn223 HF Staff
Brand Annotation Studio and copy only successfully annotated episodes
764e398 verified
Raw History Blame
2.2 kB
"""Usage-derived cost reporting and links to materialized annotations."""
import json
import math
from datetime import UTC, datetime
from studio import COPY
def usage_costs(job, report):
usage = report.get("usage") or {}
manifest = report.get("manifest") or {}
if not manifest:
try:
manifest = json.loads((getattr(job, "environment", None) or {}).get("ANNOTATION_MANIFEST", "{}"))
except (TypeError, ValueError):
manifest = {}
rate = manifest.get("costs", {}).get("cpu_rate")
seconds = getattr(getattr(job, "durations", None), "running_secs", None)
if seconds is None and getattr(job, "started_at", None):
end = getattr(job, "finished_at", None) or datetime.now(UTC)
seconds = max(0, (end - job.started_at).total_seconds())
cpu = None
if isinstance(seconds, (int, float)) and math.isfinite(seconds) and rate is not None:
cpu = math.ceil(max(0, seconds) / 60) * rate
inference = usage.get("measured_inference_usd")
return {
"inference_usd": inference,
"cpu_usd": cpu,
"total_usd": inference + cpu if inference is not None and cpu is not None else None,
"uncertain_inference_usd": max(
0, usage.get("reserved_including_uncertain_usd", 0) - (inference or 0)
),
"running_seconds": seconds,
"basis": "Reported tokens × published rates, plus HF job runtime rounded to minutes. Final HF billing may differ.",
"billing_url": "https://huggingface.co/settings/billing",
}
def annotation_url(report):
manifest = report.get("manifest", {})
written = report.get("written_episode_ids", [])
# Old completed copy reports predate the explicit materialization checkpoint.
if (
not written
and report.get("status") == "completed"
and not (report.get("copy_scope") or manifest.get("copy_scope"))
):
written = [e["episode_index"] for e in report.get("episodes", []) if e["status"] == "completed"]
if manifest.get("mode") != COPY or not written:
return None
return f"https://lerobot-visualize-dataset.hf.space/{manifest['output']}/{min(written)}?tab=annotations"