Skip to content

Commit

Permalink
Avoid redundant calls to /whoami-v2 (#344)
Browse files Browse the repository at this point in the history
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
2 people authored and GitHub committed Nov 17, 2025
1 parent 4106990 commit 7e01024
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/forty-dolls-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trackio": patch
---

feat:Avoid redundant calls to /whoami-v2
5 changes: 5 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

from trackio import Run, init
from trackio.utils import _cached_whoami


class DummyClient:
Expand Down Expand Up @@ -99,6 +100,8 @@ def test_run_name_generation_with_space_id(mock_time, mock_whoami, temp_dir):
)
assert run.name == "testuser-1234567890"

_cached_whoami.cache_clear()


def test_reserved_config_keys_rejected(temp_dir):
with pytest.raises(ValueError, match="Config key '_test' is reserved"):
Expand Down Expand Up @@ -130,6 +133,8 @@ def test_automatic_username_and_timestamp_added(mock_whoami, temp_dir):
created_time = datetime.fromisoformat(run.config["_Created"])
assert created_time.tzinfo is not None

_cached_whoami.cache_clear()


def test_run_group_added(temp_dir):
run = Run(
Expand Down
4 changes: 2 additions & 2 deletions trackio/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from trackio.sqlite_storage import SQLiteStorage
from trackio.table import Table
from trackio.typehints import LogEntry, UploadEntry
from trackio.utils import _get_default_namespace

BATCH_SEND_INTERVAL = 0.5

Expand Down Expand Up @@ -62,8 +63,7 @@ def __init__(
def _get_username(self) -> str | None:
"""Get the current HuggingFace username if logged in, otherwise None."""
try:
who = huggingface_hub.whoami()
return who["name"] if who else None
return _get_default_namespace()
except Exception:
return None

Expand Down
3 changes: 3 additions & 0 deletions trackio/ui/fns.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Shared functions for the Trackio UI."""

import os
from functools import lru_cache

import gradio as gr
import huggingface_hub as hf
Expand Down Expand Up @@ -82,6 +83,7 @@ def update_navbar_value(project_dd, request: gr.Request):
)


@lru_cache(maxsize=32)
def check_hf_token_has_write_access(hf_token: str | None) -> None:
"""
Checks to see if the provided hf_token is valid and has write access to the Space
Expand Down Expand Up @@ -137,6 +139,7 @@ def check_hf_token_has_write_access(hf_token: str | None) -> None:
)


@lru_cache(maxsize=32)
def check_oauth_token_has_write_access(oauth_token: str | None) -> None:
"""
Checks to see if the oauth token provided via Gradio's OAuth is valid and has write access
Expand Down
21 changes: 18 additions & 3 deletions trackio/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import time
from datetime import datetime, timezone
from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -144,7 +145,7 @@ def generate_readable_name(used_names: list[str], space_id: str | None = None) -
If space_id is provided, generates username-timestamp format instead.
"""
if space_id is not None:
username = huggingface_hub.whoami()["name"]
username = _get_default_namespace()
timestamp = int(time.time())
return f"{username}-{timestamp}"
adjectives = [
Expand Down Expand Up @@ -418,10 +419,10 @@ def preprocess_space_and_dataset_ids(
space_id: str | None, dataset_id: str | None
) -> tuple[str | None, str | None]:
if space_id is not None and "/" not in space_id:
username = huggingface_hub.whoami()["name"]
username = _get_default_namespace()
space_id = f"{username}/{space_id}"
if dataset_id is not None and "/" not in dataset_id:
username = huggingface_hub.whoami()["name"]
username = _get_default_namespace()
dataset_id = f"{username}/{dataset_id}"
if space_id is not None and dataset_id is None:
dataset_id = f"{space_id}-dataset"
Expand Down Expand Up @@ -865,3 +866,17 @@ def get_space() -> str | None:
def ordered_subset(items: list[str], subset: list[str] | None) -> list[str]:
subset_set = set(subset or [])
return [item for item in items if item in subset_set]


def _get_default_namespace() -> str:
"""Get the default namespace (username).
This function uses caching to avoid repeated API calls to /whoami-v2.
"""
token = huggingface_hub.get_token()
return _cached_whoami(token)["name"]


@lru_cache(maxsize=32)
def _cached_whoami(token: str | None) -> dict:
return huggingface_hub.whoami(token=token)

0 comments on commit 7e01024

Please sign in to comment.