-
Notifications
You must be signed in to change notification settings - Fork 530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support multiple tokens locally #2446
Comments
Hey @Wauplin, i would love to work on this one! To be aligned this would be my approach:
Does this sound in general correct? Looking forward to your feedback and having finally time to work on another hf issue! |
Hi @lappemic, nice to hear back from you :) Thanks for the suggestions. Unfortunately we've decided internally to move forward on it with @hanouticelina who joined us last week and will maintain core parts of the library. She's already done a comparison work with @lappemic in the meantime, would you be interested to work on the
Would you like to look into it and work on some improvements? Ofc, not everything has to be done at once. If yes, let's continue the discussion in a #1997 :) |
Hi 👋 As discussed internally, we drafted a first version of the CLI usage documentation. The idea is to keep using UsageLoginhuggingface-cli login [--token TOKEN] [--profile PROFILE_NAME]
List Profileshuggingface-cli auth list
Logouthuggingface-cli logout [--profile PROFILE_NAME --all]
Switching the current active profilehuggingface-cli auth switch PROFILE_NAME
How it WorksToken storageTokens will be stored in [default]
hf_token = hf_XXXXXXX
[profile1]
hf_token = hf_YYYYYYY
[profile2]
hf_token = hf_ZZZZZZZ Token Selection PriorityWe will keep the same token retrieval priority order
Implementation PlanI already started some experimentation locally with the following implementation (still need to be refined and discussed in the PR):
def _login(
token: str,
add_to_git_credential: bool,
write_permission: bool = False,
profile: Optional[str] = None,
) -> None:
# Existing validation code...
profile_name = profile or "default"
# Save token to profiles
_save_token_to_profiles(token, profile_name)
# Set active profile
_set_active_profile(profile_name)
logger.info("Login successful")
def _save_token_to_profiles(token: str, profile_name: str) -> None:
"""
Save the token to the profile. Parses the ini file, if the profile does not exist,
it creates it. Otherwise, it updates the token.
"""
profiles_path = Path(constants.HF_PROFILES_PATH)
...
def _get_token_from_profiles(profile_name: str) -> str:
"""
Get the token from the profile. Parses the ini file and returns the token.
If the token is not found, it raises an error.
Otherwise, it returns the token.
"""
...
def _set_active_profile(profile_name: str) -> None:
token = _get_token_from_profiles(profile_name)
# Write token to HF_TOKEN_PATH
path = Path(constants.HF_TOKEN_PATH)
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(token)
print(f"Your token has been saved to {constants.HF_TOKEN_PATH}") Other function helpers will be added to write/parse the profiles file.
def logout(profile_name: Optional[str] = None, all: bool = False) -> None:
profiles = ... # Read profiles from file
profile_name = profile_name or "default"
if all:
# delete profiles file and token file
return
if profile_name in profiles:
# Remove profile from profiles file
# If the active token matches this profile's token, delete it + Add a warning to the user that the active token will be deleted
if profiles[profile_name] == get_token():
warnings.warn(f"Active Profile '{profile_name}' will be deleted.")
print(f"Profile '{profile_name}' has been deleted.")
else:
# Raise error profile '{profile_name}' does not exist.
def auth_switch(profile_name: str) -> None:
"""Switch to the specified profile."""
token = _get_token_from_profiles(profile_name)
if token:
# Write token to ~/.cache/huggingface/token
_set_active_profile(profile_name)
print(f"Switched to profile '{profile_name}'.")
# Warn if HF_TOKEN environment variable is set
if _get_token_from_environment():
warnings.warn(
"The environment variable 'HF_TOKEN' is set and will override " "the token from the profile."
)
else:
# Raise error profile '{profile_name}' does not exist.
def auth_list():
"""List all saved profiles. The formatting still needs to be defined"""
...
|
I'd say, if |
Otherwise code looks good but next time don't hesitate to open the PR directly even if the code is not complete (draft PRs exist for that^^). It makes it easier to try things and suggest small changes. |
Originally from @osanseviero on slack (private)
Originally from @julien-c in reply
Originally from @osanseviero as well
We would need:
login
Google Colab secret management is a separate topic but still relevant.
The text was updated successfully, but these errors were encountered: