Voice et bot modif

This commit is contained in:
pi 2026-06-16 17:09:34 +00:00
parent 189d56026b
commit 7333a22bcd
10774 changed files with 634644 additions and 933308 deletions

View file

@ -30,17 +30,17 @@ Usage:
hf auth whoami
"""
from typing import Annotated, Optional
from typing import Annotated
import typer
from huggingface_hub.constants import ENDPOINT
from huggingface_hub.errors import HfHubHTTPError
from huggingface_hub.hf_api import whoami
from .._login import auth_list, auth_switch, login, logout
from ..utils import ANSI, get_stored_tokens, get_token, logging
from ..utils import get_stored_tokens, get_token, logging
from ._cli_utils import TokenOpt, typer_factory
from ._output import out
logger = logging.get_logger(__name__)
@ -49,7 +49,15 @@ logger = logging.get_logger(__name__)
auth_cli = typer_factory(help="Manage authentication (login, logout, etc.).")
@auth_cli.command("login", help="Login using a token from huggingface.co/settings/tokens")
@auth_cli.command(
"login",
examples=[
"hf auth login",
"hf auth login --token $HF_TOKEN",
"hf auth login --token $HF_TOKEN --add-to-git-credential",
"hf auth login --force",
],
)
def auth_login(
token: TokenOpt = None,
add_to_git_credential: Annotated[
@ -58,23 +66,32 @@ def auth_login(
help="Save to git credential helper. Useful only if you plan to run git commands directly.",
),
] = False,
force: Annotated[
bool,
typer.Option(
help="Force re-login even if already logged in.",
),
] = False,
) -> None:
login(token=token, add_to_git_credential=add_to_git_credential)
"""Login using a token from huggingface.co/settings/tokens."""
login(token=token, add_to_git_credential=add_to_git_credential, skip_if_logged_in=not force)
@auth_cli.command("logout", help="Logout from a specific token")
@auth_cli.command(
"logout",
examples=["hf auth logout", "hf auth logout --token-name my-token"],
)
def auth_logout(
token_name: Annotated[
Optional[str],
typer.Option(
help="Name of token to logout",
),
str | None,
typer.Option(help="Name of token to logout"),
] = None,
) -> None:
"""Logout from a specific token."""
logout(token_name=token_name)
def _select_token_name() -> Optional[str]:
def _select_token_name() -> str | None:
token_names = list(get_stored_tokens().keys())
if not token_names:
@ -98,10 +115,13 @@ def _select_token_name() -> Optional[str]:
print("Invalid input. Please enter a number or 'q' to quit.")
@auth_cli.command("switch", help="Switch between access tokens")
@auth_cli.command(
"switch",
examples=["hf auth switch", "hf auth switch --token-name my-token"],
)
def auth_switch_cmd(
token_name: Annotated[
Optional[str],
str | None,
typer.Option(
help="Name of the token to switch to",
),
@ -113,6 +133,7 @@ def auth_switch_cmd(
),
] = False,
) -> None:
"""Switch between access tokens."""
if token_name is None:
token_name = _select_token_name()
if token_name is None:
@ -121,27 +142,33 @@ def auth_switch_cmd(
auth_switch(token_name, add_to_git_credential=add_to_git_credential)
@auth_cli.command("list", help="List all stored access tokens")
@auth_cli.command("list | ls", examples=["hf auth list"])
def auth_list_cmd() -> None:
"""List all stored access tokens."""
auth_list()
@auth_cli.command("whoami", help="Find out which huggingface.co account you are logged in as.")
def auth_whoami() -> None:
@auth_cli.command("token", examples=["hf auth token", "hf auth token | xargs curl -H 'Authorization: Bearer {}'"])
def auth_token() -> None:
"""Print the current access token to stdout."""
token = get_token()
if token is None:
print("Not logged in")
raise typer.Exit()
try:
info = whoami(token)
print(ANSI.bold("user: "), info["name"])
orgs = [org["name"] for org in info["orgs"]]
if orgs:
print(ANSI.bold("orgs: "), ",".join(orgs))
if ENDPOINT != "https://huggingface.co":
print(f"Authenticated through private endpoint: {ENDPOINT}")
except HfHubHTTPError as e:
print(e)
print(ANSI.red(e.response.text))
out.error("Not logged in. Run `hf auth login` first.")
raise typer.Exit(code=1)
print(token)
out.hint("Run `hf auth whoami` to see which account this token belongs to.")
@auth_cli.command("whoami", examples=["hf auth whoami", "hf auth whoami --format json"])
def auth_whoami() -> None:
"""Find out which huggingface.co account you are logged in as."""
token = get_token()
if token is None:
out.error("Not logged in")
raise typer.Exit(code=1)
info = whoami(token)
orgs = ",".join(org["name"] for org in info["orgs"]) or None
endpoint = ENDPOINT if ENDPOINT != "https://huggingface.co" else None
out.result("Logged in", user=info["name"], orgs=orgs, endpoint=endpoint)