NiklasAbraham's picture
Upload folder using huggingface_hub
ed7b69c verified
import os
from pathlib import Path
from huggingface_hub import login, upload_folder, whoami
# Check if already logged in
try:
user_info = whoami()
print(f"Already logged in as: {user_info.get('name', 'Unknown')}")
except Exception:
# Not logged in, need to authenticate
print("Not logged in. Checking for saved token...")
# Try to get token from environment variable first
hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_HUB_TOKEN")
# If not in environment, try to read from Hugging Face cache
if not hf_token:
cache_token_path = Path.home() / ".cache" / "huggingface" / "token"
if cache_token_path.exists():
try:
hf_token = cache_token_path.read_text().strip()
if hf_token:
print("Found token in cache.")
except Exception:
pass
# Login with token (will prompt if token is None and not found)
# add_to_git_credential=True saves it to git credential helper for future use
login(token=hf_token, add_to_git_credential=True)
# Get current directory (absolute path of this script)
current_dir = os.path.abspath(os.path.dirname(__file__))
print(f"Current directory: {current_dir}")
# Upload the current folder (contents of the directory this script is in)
upload_folder(
folder_path=current_dir,
repo_id="NiklasAbraham/MoviePlotEmbeddingsDataset",
repo_type="dataset",
)