feat(annotate): use cached HF token from huggingface-cli login

Fall back to huggingface_hub.get_token() when HF_TOKEN/HUGGINGFACE_API_KEY
env vars aren't set. That picks up the token cached by
'huggingface-cli login' so users don't need to export it on every shell.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Pepijn
2026-04-28 19:36:24 +02:00
parent cd495a3a9d
commit c06c8d594a
@@ -283,10 +283,17 @@ def _make_openai_client(config: VlmConfig) -> VlmClient:
if config.use_hf_inference_providers:
api_base = "https://router.huggingface.co/v1"
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_API_KEY") or ""
if not token:
try:
from huggingface_hub import get_token # noqa: PLC0415
token = get_token() or ""
except Exception: # noqa: BLE001
token = ""
if not token:
raise RuntimeError(
"use_hf_inference_providers=True requires HF_TOKEN (or "
"HUGGINGFACE_API_KEY) in the environment."
"use_hf_inference_providers=True needs an HF token. Either set "
"HF_TOKEN in the environment, or run `huggingface-cli login` once."
)
api_key = token
auto_serve = False