-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: persist hf token between uploads #148
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
Open
red40maxxer
wants to merge
22
commits into
p-e-w:master
Choose a base branch
from
red40maxxer:persist-hf-token
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−20
Open
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
674d54b
fix: persist hf token between uploads
red40maxxer 9cb806f
doc: fix comment
red40maxxer 8155767
feat: prefill prompt with token if it already exists
red40maxxer 3ccd69a
style: ruff
red40maxxer 07bda90
feat: improve token prompting behaviour
red40maxxer f48fc7e
Merge branch 'master' into persist-hf-token
red40maxxer 446ac06
fix: wrong identifier
red40maxxer 0ac1272
refactor: clean up code
red40maxxer 6fc8619
fix: missing whoami call
red40maxxer cbd91c0
fix: dont print token
red40maxxer 88ff356
fix: tokenizer upload
red40maxxer 6777111
refactor: clean up code
red40maxxer 6b7657c
refactor: create validate_hf_token helper
red40maxxer f3e3766
chore: tighten token helper type
red40maxxer cfddcde
refactor: simplify error handling
red40maxxer c7d3942
refactor: simplify code
red40maxxer 59a3c3b
style: ruff
red40maxxer 4a13925
chore: add type
red40maxxer 184622d
refactor: enhance token auth error handling
red40maxxer 87f95f7
chore: catch hf specific exceptions
red40maxxer 8141315
chore: return None if user cancels
red40maxxer 3c10801
refactor: dry
red40maxxer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| from importlib.metadata import version | ||
| from os.path import commonprefix | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import huggingface_hub | ||
| import optuna | ||
|
|
@@ -171,6 +172,12 @@ def run(): | |
| ) | ||
| return | ||
|
|
||
| # Keep Hugging Face credentials in memory for this process only. | ||
| # We don't use huggingface_hub.login() because that stores the token on disk. | ||
| # Since this program will often be run on rented or shared GPU servers, | ||
| # it is better to not persist credentials. | ||
| hf_token = huggingface_hub.get_token() | ||
|
|
||
| # Adapted from https://github.com/huggingface/accelerate/blob/main/src/accelerate/commands/env.py | ||
| if torch.cuda.is_available(): | ||
| count = torch.cuda.device_count() | ||
|
|
@@ -598,6 +605,43 @@ def count_completed_trials() -> int: | |
| if count_completed_trials() == settings.n_trials: | ||
| study.set_user_attr("finished", True) | ||
|
|
||
| def print_hf_user_info(user: dict[str, Any]): | ||
| fullname = user.get( | ||
| "fullname", | ||
| user.get("name", "unknown user"), | ||
| ) | ||
| email = user.get("email", "no email found") | ||
| print(f"Logged in as [bold]{fullname} ({email})[/]") | ||
|
|
||
| def authenticate_hf(token: str | None) -> tuple[dict[str, Any], str]: | ||
|
red40maxxer marked this conversation as resolved.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should not accept |
||
| # If there is already an existing HF token, | ||
| # (either externally through hf auth login or a previous upload), | ||
| # allow the user to switch accounts if they want | ||
|
red40maxxer marked this conversation as resolved.
Outdated
|
||
| if token: | ||
| try: | ||
| user = huggingface_hub.whoami(token) | ||
| print_hf_user_info(user) | ||
| if ( | ||
| prompt_select( | ||
| "How do you want to proceed?", | ||
| ["Use this account", "Switch account"], | ||
| ) | ||
| == "Use this account" | ||
| ): | ||
| return user, token | ||
| except Exception as error: | ||
| print(f"[red]Failed to validate the Hugging Face token: ({error})[/]") | ||
|
red40maxxer marked this conversation as resolved.
Outdated
|
||
|
|
||
| # Otherwise, keep prompting the user for a valid token until they cancel. | ||
| while True: | ||
| token = prompt_password("Hugging Face access token:") | ||
| try: | ||
| user = huggingface_hub.whoami(token) | ||
| print_hf_user_info(user) | ||
| return user, token | ||
| except Exception as error: | ||
| print(f"[red]Failed to validate the Hugging Face token: ({error})[/]") | ||
|
red40maxxer marked this conversation as resolved.
Outdated
|
||
|
|
||
| while True: | ||
| # If no trials at all have been evaluated, the study must have been stopped | ||
| # by pressing Ctrl+C while the first trial was running. In this case, we just | ||
|
|
@@ -765,22 +809,10 @@ def count_completed_trials() -> int: | |
| print(f"Model saved to [bold]{save_directory}[/].") | ||
|
|
||
| case "Upload the model to Hugging Face": | ||
| # We don't use huggingface_hub.login() because that stores the token on disk, | ||
| # and since this program will often be run on rented or shared GPU servers, | ||
| # it's better to not persist credentials. | ||
| token = huggingface_hub.get_token() | ||
| if not token: | ||
| token = prompt_password("Hugging Face access token:") | ||
| if not token: | ||
| continue | ||
|
|
||
| user = huggingface_hub.whoami(token) | ||
| fullname = user.get( | ||
| "fullname", | ||
| user.get("name", "unknown user"), | ||
| ) | ||
| email = user.get("email", "no email found") | ||
| print(f"Logged in as [bold]{fullname} ({email})[/]") | ||
| try: | ||
| user, hf_token = authenticate_hf(hf_token) | ||
| except KeyboardInterrupt: | ||
| break | ||
|
|
||
| repo_id = prompt_text( | ||
| "Name of repository:", | ||
|
|
@@ -805,22 +837,22 @@ def count_completed_trials() -> int: | |
| model.model.push_to_hub( | ||
| repo_id, | ||
| private=private, | ||
| token=token, | ||
| token=hf_token, | ||
| ) | ||
| else: | ||
| print("Uploading merged model...") | ||
| merged_model = model.get_merged_model() | ||
| merged_model.push_to_hub( | ||
| repo_id, | ||
| private=private, | ||
| token=token, | ||
| token=hf_token, | ||
| ) | ||
| del merged_model | ||
| empty_cache() | ||
| model.tokenizer.push_to_hub( | ||
| repo_id, | ||
| private=private, | ||
| token=token, | ||
| token=hf_token, | ||
| ) | ||
|
red40maxxer marked this conversation as resolved.
|
||
|
|
||
| # If the model path exists locally and includes the | ||
|
|
@@ -857,7 +889,7 @@ def count_completed_trials() -> int: | |
| ) | ||
| + card.text | ||
| ) | ||
| card.push_to_hub(repo_id, token=token) | ||
| card.push_to_hub(repo_id, token=hf_token) | ||
|
|
||
| print(f"Model uploaded to [bold]{repo_id}[/].") | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.