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

Always send the cached token when user is logged in #1064

Merged
merged 24 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
49 changes: 34 additions & 15 deletions docs/source/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,16 @@ full-length hash instead of the shorter 7-character commit hash:

For more details and options, see the API reference for [`hf_hub_download`].

## Create a repository

To create and share files to the Hub, you need to have a Hugging Face account. [Create
an account](https://hf.co/join) if you don't already have one, and then sign in to find
your [User Access Token](https://huggingface.co/docs/hub/security-tokens) in
your Settings. The User Access Token is used to authenticate your identity to the Hub.

<Tip>

You can also provide your token to our functions and methods. This way you don't need to
store your token anywhere.
## Login

</Tip>
In a lot of cases, you must be logged in with a Hugging Face account to interact with
the Hub: download private repos, upload files, create PRs,...
[Create an account](https://hf.co/join) if you don't already have one, and then sign in
to get your [User Access Token](https://huggingface.co/docs/hub/security-tokens) from
your [Settings page](https://huggingface.co/settings/tokens). The User Access Token is
used to authenticate your identity to the Hub.
Wauplin marked this conversation as resolved.
Show resolved Hide resolved

1. Log in to your Hugging Face account with the following command:
Once you have your User Access Token, run the following command in your terminal:

```bash
huggingface-cli login
Expand All @@ -82,9 +77,25 @@ Or if you prefer to work from a Jupyter or Colaboratory notebook, then login wit
>>> notebook_login()
```

2. Enter your User Access Token to authenticate your identity to the Hub.
<Tip>

You can also provide your token to the functions and methods. This way you don't need to
store your token anywhere.

</Tip>

<Tip>

Once you are logged in, all requests to the Hub will use your access token by default.
If you want to disable implicit use of your token, you should set the
`HF_HUB_DISABLE_IMPLICIT_TOKEN` environment variable.

</Tip>

## Create a repository

After you've created an account, create a repository with the [`create_repo`] function:
Once you've registered and logged in, create a repository with the [`create_repo`]
function:

```py
>>> from huggingface_hub import HfApi
Expand All @@ -102,6 +113,14 @@ If you want your repository to be private, then:

Private repositories will not be visible to anyone except yourself.

<Tip>

To create a repository or to push content to the Hub, you must provide a User Access
Token that has the `write` permission. You can choose the permission when creating the
token in your [Settings page](https://huggingface.co/settings/tokens).

</Tip>

## Share files to the Hub

Use the [`upload_file`] function to add a file to your newly created repository. You
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def get_version() -> str:
extras["tensorflow"] = ["tensorflow", "pydot", "graphviz"]

extras["testing"] = extras["cli"] + [
"datasets",
"isort>=5.5.4",
"jedi",
"Jinja2",
Expand Down
4 changes: 2 additions & 2 deletions src/huggingface_hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
"CommitOperationDelete",
"DatasetSearchArguments",
"HfApi",
"HfFolder",
"ModelSearchArguments",
"change_discussion_status",
"comment_discussion",
Expand Down Expand Up @@ -169,6 +168,7 @@
"CorruptedCacheException",
"DeleteCacheStrategy",
"HFCacheInfo",
"HfFolder",
"logging",
"scan_cache_dir",
],
Expand Down Expand Up @@ -311,7 +311,6 @@ def __dir__():
from .hf_api import CommitOperationDelete # noqa: F401
from .hf_api import DatasetSearchArguments # noqa: F401
from .hf_api import HfApi # noqa: F401
from .hf_api import HfFolder # noqa: F401
from .hf_api import ModelSearchArguments # noqa: F401
from .hf_api import change_discussion_status # noqa: F401
from .hf_api import comment_discussion # noqa: F401
Expand Down Expand Up @@ -368,6 +367,7 @@ def __dir__():
from .utils import CorruptedCacheException # noqa: F401
from .utils import DeleteCacheStrategy # noqa: F401
from .utils import HFCacheInfo # noqa: F401
from .utils import HfFolder # noqa: F401
from .utils import logging # noqa: F401
from .utils import scan_cache_dir # noqa: F401
from .utils.endpoint_helpers import DatasetFilter # noqa: F401
Expand Down
4 changes: 2 additions & 2 deletions src/huggingface_hub/_commit_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from .constants import ENDPOINT
from .lfs import UploadInfo, _validate_batch_actions, lfs_upload, post_lfs_batch_info
from .utils import hf_raise_for_status, logging, validate_hf_hub_args
from .utils import build_hf_headers, hf_raise_for_status, logging, validate_hf_hub_args
from .utils._typing import Literal


Expand Down Expand Up @@ -355,7 +355,7 @@ def fetch_upload_modes(
If the Hub API returned an HTTP 400 error (bad request)
"""
endpoint = endpoint if endpoint is not None else ENDPOINT
headers = {"authorization": f"Bearer {token}"} if token is not None else None
headers = build_hf_headers(use_auth_token=token)
payload = {
"files": [
{
Expand Down
19 changes: 5 additions & 14 deletions src/huggingface_hub/_snapshot_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .constants import DEFAULT_REVISION, HUGGINGFACE_HUB_CACHE, REPO_TYPES
from .file_download import REGEX_COMMIT_HASH, hf_hub_download, repo_folder_name
from .hf_api import HfApi, HfFolder
from .hf_api import HfApi
from .utils import filter_repo_objects, logging, tqdm, validate_hf_hub_args
from .utils._deprecation import _deprecate_arguments

Expand Down Expand Up @@ -108,18 +108,6 @@ def snapshot_download(
if isinstance(cache_dir, Path):
cache_dir = str(cache_dir)

if isinstance(use_auth_token, str):
token = use_auth_token
elif use_auth_token:
token = HfFolder.get_token()
if token is None:
raise EnvironmentError(
"You specified use_auth_token=True, but a Hugging Face token was not"
" found."
)
else:
token = None

if repo_type is None:
repo_type = "model"
if repo_type not in REPO_TYPES:
Expand Down Expand Up @@ -167,7 +155,10 @@ def snapshot_download(
# if we have internet connection we retrieve the correct folder name from the huggingface api
_api = HfApi()
repo_info = _api.repo_info(
repo_id=repo_id, repo_type=repo_type, revision=revision, use_auth_token=token
repo_id=repo_id,
repo_type=repo_type,
revision=revision,
use_auth_token=use_auth_token,
)
filtered_repo_files = list(
filter_repo_objects(
Expand Down
14 changes: 8 additions & 6 deletions src/huggingface_hub/commands/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import subprocess
from argparse import ArgumentParser
from getpass import getpass
Expand All @@ -24,10 +23,10 @@
REPO_TYPES_URL_PREFIXES,
SPACES_SDK_TYPES,
)
from huggingface_hub.hf_api import HfApi, HfFolder
from huggingface_hub.hf_api import HfApi
from requests.exceptions import HTTPError

from ..utils import run_subprocess
from ..utils import HfFolder, run_subprocess
from ._cli_utils import ANSI


Expand Down Expand Up @@ -308,13 +307,16 @@ def login_token_event(t):
# Erase token and clear value to make sure it's not saved in the notebook.
token_widget.value = ""
clear_output()
_login(HfApi(), token=token)
_login(token=token)

token_finish_button.on_click(login_token_event)


def _login(hf_api, token=None):
token, name = hf_api._validate_or_retrieve_token(token)
def _login(hf_api: HfApi, token: str) -> None:
if token.startswith("api_org"):
raise ValueError("You must use your personal account token.")
if not hf_api._is_valid_token(token=token):
raise ValueError("Invalid token passed!")
hf_api.set_access_token(token)
HfFolder.save_token(token)
print("Login successful")
Expand Down
3 changes: 1 addition & 2 deletions src/huggingface_hub/fastai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from packaging import version

from huggingface_hub import hf_api, snapshot_download
from huggingface_hub import snapshot_download
from huggingface_hub.constants import CONFIG_NAME
from huggingface_hub.file_download import (
_PY_VERSION,
Expand Down Expand Up @@ -437,7 +437,6 @@ def push_to_hub_fastai(
"""

_check_fastai_fastcore_versions()
token, _ = hf_api._validate_or_retrieve_token(token)
api = HfApi(endpoint=api_endpoint)
api.create_repo(
repo_id=repo_id,
Expand Down
61 changes: 14 additions & 47 deletions src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
REPO_TYPES,
REPO_TYPES_URL_PREFIXES,
)
from .hf_api import HfFolder
from .utils import (
EntryNotFoundError,
LocalEntryNotFoundError,
build_hf_headers,
hf_raise_for_status,
http_backoff,
logging,
Expand Down Expand Up @@ -670,23 +670,12 @@ def cached_download(

os.makedirs(cache_dir, exist_ok=True)

headers = {
"user-agent": http_user_agent(
library_name=library_name,
library_version=library_version,
user_agent=user_agent,
)
}
if isinstance(use_auth_token, str):
headers["authorization"] = f"Bearer {use_auth_token}"
elif use_auth_token:
token = HfFolder.get_token()
if token is None:
raise EnvironmentError(
"You specified use_auth_token=True, but a huggingface token was not"
" found."
)
headers["authorization"] = f"Bearer {token}"
headers = build_hf_headers(use_auth_token=use_auth_token)
headers["user-agent"] = http_user_agent(
library_name=library_name,
library_version=library_version,
user_agent=user_agent,
)

url_to_download = url
etag = None
Expand Down Expand Up @@ -1115,23 +1104,12 @@ def hf_hub_download(

url = hf_hub_url(repo_id, filename, repo_type=repo_type, revision=revision)

headers = {
"user-agent": http_user_agent(
library_name=library_name,
library_version=library_version,
user_agent=user_agent,
)
}
if isinstance(use_auth_token, str):
headers["authorization"] = f"Bearer {use_auth_token}"
elif use_auth_token:
token = HfFolder.get_token()
if token is None:
raise EnvironmentError(
"You specified use_auth_token=True, but a huggingface token was not"
" found."
)
headers["authorization"] = f"Bearer {token}"
headers = build_hf_headers(use_auth_token=use_auth_token)
headers["user-agent"] = http_user_agent(
library_name=library_name,
library_version=library_version,
user_agent=user_agent,
)

url_to_download = url
etag = None
Expand Down Expand Up @@ -1433,18 +1411,7 @@ def get_hf_file_metadata(
A [`HfFileMetadata`] object containing metadata such as location, etag and
commit_hash.
"""
# TODO: helper to get headers from `use_auth_token` (copy-pasted several times)
headers = {}
if isinstance(use_auth_token, str):
headers["authorization"] = f"Bearer {use_auth_token}"
elif use_auth_token:
token = HfFolder.get_token()
if token is None:
raise EnvironmentError(
"You specified use_auth_token=True, but a huggingface token was not"
" found."
)
headers["authorization"] = f"Bearer {token}"
headers = build_hf_headers(use_auth_token=use_auth_token)

# Retrieve metadata
r = _request_wrapper(
Expand Down
Loading