Upload /mnt/data1/andrew/core_datasets/upload_dataset.py with huggingface_hub
Browse files
mnt/data1/andrew/core_datasets/upload_dataset.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from typing import List
|
| 5 |
+
|
| 6 |
+
from huggingface_hub import HfApi, login
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def upload_to_huggingface(
|
| 10 |
+
token: str,
|
| 11 |
+
repo_id: str,
|
| 12 |
+
file_path: Path,
|
| 13 |
+
repo_type: str = "dataset",
|
| 14 |
+
create_repo: bool = True,
|
| 15 |
+
exclude_suffixes: List[str] = None,
|
| 16 |
+
):
|
| 17 |
+
"""
|
| 18 |
+
Upload a file or all files in a folder to Hugging Face Hub.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
token: HuggingFace access token
|
| 22 |
+
repo_id: Repository ID (format: "username/repo-name")
|
| 23 |
+
file_path: Path to file or folder to upload
|
| 24 |
+
repo_type: Type of repository ("dataset" or "model")
|
| 25 |
+
create_repo: Whether to create the repo if it doesn't exist
|
| 26 |
+
exclude_suffixes (list[str]): List of suffixes to exclude from upload
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
# Login to Hugging Face
|
| 30 |
+
login(token=token)
|
| 31 |
+
api = HfApi()
|
| 32 |
+
|
| 33 |
+
# Create repo if it doesn't exist and create_repo is True
|
| 34 |
+
if create_repo:
|
| 35 |
+
try:
|
| 36 |
+
api.create_repo(
|
| 37 |
+
repo_id=repo_id, repo_type=repo_type, private=False, exist_ok=True
|
| 38 |
+
)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"Note: Repository creation returned: {e}")
|
| 41 |
+
|
| 42 |
+
# Function to upload individual files
|
| 43 |
+
def upload_file(file: Path):
|
| 44 |
+
try:
|
| 45 |
+
print(f"Uploading {file} to {repo_id}...")
|
| 46 |
+
api.upload_file(
|
| 47 |
+
path_or_fileobj=str(file),
|
| 48 |
+
path_in_repo=str(file),
|
| 49 |
+
repo_id=repo_id,
|
| 50 |
+
repo_type=repo_type,
|
| 51 |
+
)
|
| 52 |
+
print(f"Successfully uploaded {file.name} to {repo_id}")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Error uploading {file}: {e}")
|
| 55 |
+
|
| 56 |
+
# If file_path is a directory, upload all files recursively
|
| 57 |
+
if file_path.is_dir():
|
| 58 |
+
for file in file_path.rglob("*"):
|
| 59 |
+
if file.is_file():
|
| 60 |
+
# Skip files with specified suffixes
|
| 61 |
+
if exclude_suffixes and any(
|
| 62 |
+
file.name.endswith(suffix) for suffix in exclude_suffixes
|
| 63 |
+
):
|
| 64 |
+
print(f"Skipping {file} due to matching exclude suffix.")
|
| 65 |
+
continue
|
| 66 |
+
upload_file(file)
|
| 67 |
+
else:
|
| 68 |
+
# If file_path is a single file, upload it
|
| 69 |
+
upload_file(file_path)
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error during upload process: {e}")
|
| 73 |
+
sys.exit(1)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
if __name__ == "__main__":
|
| 77 |
+
# Example usage
|
| 78 |
+
TOKEN = os.environ.get("HUGGING_FACE_HUB_TOKEN")
|
| 79 |
+
if TOKEN is None:
|
| 80 |
+
TOKEN = input("Please enter your Hugging Face Hub token: ")
|
| 81 |
+
|
| 82 |
+
REPO_ID = "robotlearning/cpgendata" # Replace with your desired repo name
|
| 83 |
+
FILE_PATH = Path(
|
| 84 |
+
"/mnt/data1/andrew/core_datasets"
|
| 85 |
+
) # Replace with your file or folder path
|
| 86 |
+
EXCLUDE_SUFFIXES = [".mp4"] # Replace with your suffixes to exclude
|
| 87 |
+
|
| 88 |
+
upload_to_huggingface(
|
| 89 |
+
token=TOKEN,
|
| 90 |
+
repo_id=REPO_ID,
|
| 91 |
+
file_path=FILE_PATH,
|
| 92 |
+
exclude_suffixes=EXCLUDE_SUFFIXES,
|
| 93 |
+
)
|