Download token_tracker.py from edward70/sitegen: direct link, hf CLI and curl.
- Browser
- Download file 2.9 kB
-
https://huggingface.co/spaces/edward70/sitegen/resolve/main/token_tracker.py
- Command line
-
hf download hf://spaces/edward70/sitegen/token_tracker.py
-
curl -L -o token_tracker.py https://huggingface.co/spaces/edward70/sitegen/resolve/main/token_tracker.py
2.9 kB
| """ | |
| Tracks daily token usage per provider. | |
| Persists to token_usage.json. Groq resets at midnight UTC daily. | |
| """ | |
| import json | |
| import re | |
| from datetime import datetime, timezone, timedelta | |
| from pathlib import Path | |
| _FILE = Path(__file__).parent / "token_usage.json" | |
| LIMITS = { | |
| "groq": 100_000, | |
| "cerebras": 1_000_000, | |
| "together": None, # pay-as-you-go | |
| } | |
| def _next_midnight_utc() -> str: | |
| now = datetime.now(timezone.utc) | |
| midnight = (now + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0) | |
| return midnight.isoformat() | |
| def _load() -> dict: | |
| if _FILE.exists(): | |
| try: | |
| return json.loads(_FILE.read_text()) | |
| except Exception: | |
| pass | |
| return {"providers": {}, "reset_at": None} | |
| def _save(data: dict): | |
| _FILE.write_text(json.dumps(data)) | |
| def _auto_reset(data: dict) -> dict: | |
| """Reset Groq counter at midnight UTC.""" | |
| reset_at = data.get("reset_at") | |
| if reset_at: | |
| try: | |
| if datetime.now(timezone.utc) >= datetime.fromisoformat(reset_at): | |
| data.setdefault("providers", {}).pop("groq", None) | |
| data["reset_at"] = _next_midnight_utc() | |
| _save(data) | |
| except Exception: | |
| pass | |
| else: | |
| # initialise reset clock on first load | |
| data["reset_at"] = _next_midnight_utc() | |
| _save(data) | |
| return data | |
| def add(tokens: int, provider: str = "groq"): | |
| data = _auto_reset(_load()) | |
| data.setdefault("providers", {}) | |
| data["providers"][provider] = data["providers"].get(provider, 0) + tokens | |
| _save(data) | |
| def parse_and_store_reset(error_msg: str) -> str | None: | |
| """Parse Groq 429 message, update used count from authoritative value.""" | |
| error_msg = str(error_msg) | |
| data = _load() | |
| data.setdefault("providers", {}) | |
| used_m = re.search(r"Used (\d+),", error_msg) | |
| if used_m: | |
| data["providers"]["groq"] = int(used_m.group(1)) | |
| _save(data) | |
| return data.get("reset_at") | |
| def mark_reset(): | |
| pass # no-op — Groq resets on midnight schedule, not per-call | |
| def status() -> dict: | |
| data = _auto_reset(_load()) | |
| providers = data.get("providers", {}) | |
| groq_used = providers.get("groq", 0) | |
| cerebras_used = providers.get("cerebras", 0) | |
| return { | |
| # legacy fields dashboard expects | |
| "used": groq_used, | |
| "limit": LIMITS["groq"], | |
| "remaining": max(0, LIMITS["groq"] - groq_used), | |
| "pct": round(groq_used / LIMITS["groq"] * 100, 1), | |
| "reset_at": data.get("reset_at"), | |
| # per-provider breakdown | |
| "providers": { | |
| "groq": {"used": groq_used, "limit": LIMITS["groq"], "pct": round(groq_used / LIMITS["groq"] * 100, 1)}, | |
| "cerebras": {"used": cerebras_used, "limit": LIMITS["cerebras"], "pct": round(cerebras_used / LIMITS["cerebras"] * 100, 1)}, | |
| }, | |
| } | |