-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gather HF_TOKEN internally when calling
Distiset.push_to_hub
if tok…
…en is None. (#707) * Add a way to automatically gather the HF_TOKEN when calling distiset.push_to_hub and mode constant value to distilabel.utils module * Update src/distilabel/distiset.py Co-authored-by: Gabriel Martín Blázquez <[email protected]> * Refactor function to obtain huggingface token and move it to it's module --------- Co-authored-by: Gabriel Martín Blázquez <[email protected]>
- Loading branch information
1 parent
1d53ee8
commit a0d7e93
Showing
3 changed files
with
65 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2023-present, Argilla, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# 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 os | ||
from pathlib import Path | ||
from typing import Final | ||
|
||
from huggingface_hub import constants | ||
|
||
_INFERENCE_ENDPOINTS_API_KEY_ENV_VAR_NAME: Final[str] = "HF_TOKEN" | ||
|
||
|
||
def get_hf_token(cls_name: str, token_arg: str) -> str: | ||
"""Get the token for the hugging face API. | ||
Tries to extract it from the environment variable, if it is not found | ||
it tries to read it from the file using 'huggingface_hub', | ||
and if not possible raises a ValueError. | ||
Args: | ||
cls_name: Name of the class/function that requires the token. | ||
token_arg: Argument name to use in the error message, normally | ||
is "token" or "api_key". | ||
Raises: | ||
ValueError: If the token is not found in the file. | ||
Returns: | ||
The token for the hugging face API. | ||
""" | ||
token = os.getenv(_INFERENCE_ENDPOINTS_API_KEY_ENV_VAR_NAME) | ||
if token is None: | ||
if not Path(constants.HF_TOKEN_PATH).exists(): | ||
raise ValueError( | ||
f"To use `{cls_name}` an API key must be provided via" | ||
f" `{token_arg}`, set the environment variable" | ||
f" `{_INFERENCE_ENDPOINTS_API_KEY_ENV_VAR_NAME}` or use the `huggingface-hub` CLI to login" | ||
" with `huggingface-cli login`." | ||
) | ||
with open(constants.HF_TOKEN_PATH) as f: | ||
token = f.read().strip() | ||
return token |