Skip to content
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 #2549

Merged
merged 20 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions docs/source/en/guides/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Once you have your token, run the following command in your terminal:
>>> huggingface-cli login
```

This command will prompt you for a token. Copy-paste yours and press *Enter*. Then you'll be asked if the token should also be saved as a git credential. Press *Enter* again (default to yes) if you plan to use `git` locally. Finally, it will call the Hub to check that your token is valid and save it locally.
This command will prompt you for a token. Copy-paste yours and press *Enter*. After that, it will prompt you for a profile name (optional, defaults to `default`). Then, you'll be asked if the token should also be saved as a git credential. Press *Enter* again (default to yes) if you plan to use `git` locally. Finally, it will call the Hub to check that your token is valid and save it locally.

```
_| _| _| _| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _|_|_|_| _|_| _|_|_| _|_|_|_|
Expand All @@ -102,7 +102,8 @@ _| _| _| _| _| _| _| _| _| _| _|_| _| _| _|
_| _| _|_| _|_|_| _|_|_| _|_|_| _| _| _|_|_| _| _| _| _|_|_| _|_|_|_|

To log in, `huggingface_hub` requires a token generated from https://huggingface.co/settings/tokens .
Token:
Enter your token (input will not be visible):
Enter profile name (default: 'default'):
Add token as git credential? (Y/n)
Token is valid (permission: write).
Your token has been saved in your configured git credential helpers (store).
Expand All @@ -116,9 +117,11 @@ Alternatively, if you want to log-in without being prompted, you can pass the to
# Or using an environment variable
>>> huggingface-cli login --token $HUGGINGFACE_TOKEN --add-to-git-credential
Token is valid (permission: write).
The profile `myprofile` has been saved to /home/wauplin/.cache/huggingface/profiles
Your token has been saved in your configured git credential helpers (store).
Your token has been saved to /home/wauplin/.cache/huggingface/token
Login successful
The current active profile is: `myprofile`
```

For more details about authentication, check out [this section](../quick-start#authentication).
Expand All @@ -137,7 +140,7 @@ If you are not logged in, an error message will be printed.

## huggingface-cli logout

This commands logs you out. In practice, it will delete the token saved on your machine.
This commands logs you out. In practice, it will delete all profiles and the corresponding tokens saved on your machine. If you want to remove a specific token profile, you can specify the profile name as an argument.

This command will not log you out if you are logged in using the `HF_TOKEN` environment variable (see [reference](../package_reference/environment_variables#hftoken)). If that is the case, you must unset the environment variable in your machine configuration.

Expand Down Expand Up @@ -431,25 +434,25 @@ https://huggingface.co/Wauplin/my-cool-model/tree/main

## huggingface-cli repo-files

If you want to delete files from a Hugging Face repository, use the `huggingface-cli repo-files` command.
If you want to delete files from a Hugging Face repository, use the `huggingface-cli repo-files` command.

### Delete files

The `huggingface-cli repo-files <repo_id> delete` sub-command allows you to delete files from a repository. Here are some usage examples.

Delete a folder :
```bash
>>> huggingface-cli repo-files Wauplin/my-cool-model delete folder/
>>> huggingface-cli repo-files Wauplin/my-cool-model delete folder/
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...
```

Delete multiple files:
Delete multiple files:
```bash
>>> huggingface-cli repo-files Wauplin/my-cool-model delete file.txt folder/pytorch_model.bin
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...
```

Use Unix-style wildcards to delete sets of files:
Use Unix-style wildcards to delete sets of files:
```bash
>>> huggingface-cli repo-files Wauplin/my-cool-model delete "*.txt" "folder/*.bin"
Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-cool-mo...
Expand All @@ -460,7 +463,7 @@ Files correctly deleted from repo. Commit: https://huggingface.co/Wauplin/my-coo
To delete files from a repo you must be authenticated and authorized. By default, the token saved locally (using `huggingface-cli login`) will be used. If you want to authenticate explicitly, use the `--token` option:

```bash
>>> huggingface-cli repo-files --token=hf_**** Wauplin/my-cool-model delete file.txt
>>> huggingface-cli repo-files --token=hf_**** Wauplin/my-cool-model delete file.txt
```

## huggingface-cli scan-cache
Expand Down
12 changes: 10 additions & 2 deletions docs/source/en/package_reference/login.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
rendered properly in your Markdown viewer.
-->

# Login and logout
# Login, Logout, and Token Management
hanouticelina marked this conversation as resolved.
Show resolved Hide resolved

The `huggingface_hub` library allows users to programmatically login and logout the machine to the Hub.
The `huggingface_hub` library allows users to programmatically manage authentication to the Hub. This includes logging in, logging out, switching between tokens, and listing available tokens.

For more details about authentication, check out [this section](../quick-start#authentication).

Expand All @@ -23,3 +23,11 @@ For more details about authentication, check out [this section](../quick-start#a
## logout

[[autodoc]] logout

## auth_switch

[[autodoc]] auth_switch

## auth_list

[[autodoc]] auth_list
17 changes: 16 additions & 1 deletion docs/source/en/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The easiest way to authenticate is to save the token on your machine. You can do
huggingface-cli login
```

The command will tell you if you are already logged in and prompt you for your token. The token is then validated and saved in your `HF_HOME` directory (defaults to `~/.cache/huggingface/token`). Any script or library interacting with the Hub will use this token when sending requests.
The command will tell you if you are already logged in and prompt you for your token and a profile name (optional, defaults to `default`). The token is then validated and saved in your `HF_HOME` directory (defaults to `~/.cache/huggingface/token`). The profile is saved in your `HF_HOME` directory as well (defaults to `~/.cache/huggingface/profiles`). Any script or library interacting with the Hub will use this token when sending requests.

Alternatively, you can programmatically login using [`login`] in a notebook or a script:

Expand All @@ -93,6 +93,21 @@ Once logged in, all requests to the Hub - even methods that don't necessarily re

</Tip>

### Multiple token profiles

If you need to switch between different tokens locally, you can save multiple tokens on your machine by assigning a profile name to each token when logging in.

To switch between these tokens, you can use the [`auth switch`] command:

```bash
huggingface-cli auth switch
```

This command will prompt you to select a token profile from a list of saved profiles. Once selected, the chosen profile becomes the _active_ profile, and its corresponding token will be used for all interactions with the Hub.


You can list all available token profiles on your machine with `huggingface-cli auth list`.

### Environment variable

The environment variable `HF_TOKEN` can also be used to authenticate yourself. This is especially useful in a Space where you can set `HF_TOKEN` as a [Space secret](https://huggingface.co/docs/hub/spaces-overview#managing-secrets).
Expand Down
4 changes: 4 additions & 0 deletions src/huggingface_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"InferenceEndpointType",
],
"_login": [
"auth_list",
"auth_switch",
"interpreter_login",
"login",
"logout",
Expand Down Expand Up @@ -577,6 +579,8 @@ def __dir__():
InferenceEndpointType, # noqa: F401
)
from ._login import (
auth_list, # noqa: F401
auth_switch, # noqa: F401
interpreter_login, # noqa: F401
login, # noqa: F401
logout, # noqa: F401
Expand Down
Loading
Loading