NiklasAbraham commited on
Commit
ed7b69c
·
verified ·
1 Parent(s): fe429a0

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. upload_hf.py +27 -3
upload_hf.py CHANGED
@@ -1,9 +1,33 @@
1
  import os
 
2
 
3
- from huggingface_hub import login, upload_folder
4
 
5
- # Login with your Hugging Face credentials
6
- login()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  # Get current directory (absolute path of this script)
9
  current_dir = os.path.abspath(os.path.dirname(__file__))
 
1
  import os
2
+ from pathlib import Path
3
 
4
+ from huggingface_hub import login, upload_folder, whoami
5
 
6
+ # Check if already logged in
7
+ try:
8
+ user_info = whoami()
9
+ print(f"Already logged in as: {user_info.get('name', 'Unknown')}")
10
+ except Exception:
11
+ # Not logged in, need to authenticate
12
+ print("Not logged in. Checking for saved token...")
13
+
14
+ # Try to get token from environment variable first
15
+ hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN")
16
+
17
+ # If not in environment, try to read from Hugging Face cache
18
+ if not hf_token:
19
+ cache_token_path = Path.home() / ".cache" / "huggingface" / "token"
20
+ if cache_token_path.exists():
21
+ try:
22
+ hf_token = cache_token_path.read_text().strip()
23
+ if hf_token:
24
+ print("Found token in cache.")
25
+ except Exception:
26
+ pass
27
+
28
+ # Login with token (will prompt if token is None and not found)
29
+ # add_to_git_credential=True saves it to git credential helper for future use
30
+ login(token=hf_token, add_to_git_credential=True)
31
 
32
  # Get current directory (absolute path of this script)
33
  current_dir = os.path.abspath(os.path.dirname(__file__))