File size: 5,073 Bytes
9f3bc09 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
#!/bin/bash
# Script to upload all subfolders under a directory to a Hugging Face dataset repository.
# Each subfolder will be uploaded to the repository maintaining its folder structure.
# Usage
# chmod +x upload_to_hf_dataset.sh
# ./upload_to_hf_dataset.sh <parent_folder_path> <huggingface_repo_id> [target_path_in_repo]
# --- Configuration ---
DEFAULT_REPO_TYPE="dataset"
# --- Helper Functions ---
check_dependencies() {
if ! command -v huggingface-cli &> /dev/null; then
echo "Error: huggingface-cli is not installed."
echo "Please install it by running: pip install -U huggingface_hub"
exit 1
fi
# Check if logged in
if ! huggingface-cli whoami &> /dev/null; then
echo "Error: You are not logged in to Hugging Face CLI."
echo "Please run 'huggingface-cli login' and follow the instructions."
exit 1
fi
echo "huggingface-cli found and user is logged in."
}
# --- Argument Parsing ---
if [ "$#" -lt 2 ] || [ "$#" -gt 3 ]; then
echo "Usage: $0 <parent_folder_path> <huggingface_repo_id> [target_path_in_repo]"
echo " <parent_folder_path>: Path to the parent folder containing subfolders to upload."
echo " <huggingface_repo_id>: The ID of the Hugging Face repository (e.g., username/my_dataset_repo)."
echo " [target_path_in_repo]: Optional. The base path within the repository (defaults to '.')."
echo ""
echo "Example: $0 ./data your_username/my_dataset"
echo "Example: $0 ./experiments your_username/my_dataset results/v1"
exit 1
fi
PARENT_FOLDER_PATH="$1"
HF_REPO_ID="$2"
TARGET_PATH_IN_REPO="${3:-.}" # Default to root if not specified
REPO_TYPE="$DEFAULT_REPO_TYPE"
# --- Validation ---
if [ ! -d "$PARENT_FOLDER_PATH" ]; then
echo "Error: Parent folder '$PARENT_FOLDER_PATH' does not exist or is not a directory."
exit 1
fi
# --- Main Logic ---
check_dependencies
echo ""
echo "Starting batch upload process..."
echo "--------------------------------------------------"
echo "Parent folder: $PARENT_FOLDER_PATH"
echo "Target Hugging Face Repo ID: $HF_REPO_ID"
echo "Base target path in repo: $TARGET_PATH_IN_REPO"
echo "Repository type: $REPO_TYPE"
echo "--------------------------------------------------"
echo ""
# Count subfolders
SUBFOLDER_COUNT=$(find "$PARENT_FOLDER_PATH" -mindepth 1 -maxdepth 1 -type d | wc -l)
if [ "$SUBFOLDER_COUNT" -eq 0 ]; then
echo "No subfolders found in '$PARENT_FOLDER_PATH'."
exit 0
fi
echo "Found $SUBFOLDER_COUNT subfolder(s) to upload."
echo ""
# Initialize counters
SUCCESS_COUNT=0
FAIL_COUNT=0
# Process each subfolder
for SUBFOLDER in "$PARENT_FOLDER_PATH"/*; do
# Check if it's a directory
if [ ! -d "$SUBFOLDER" ]; then
continue
fi
# Get the subfolder name
SUBFOLDER_NAME=$(basename "$SUBFOLDER")
# Skip hidden folders unless explicitly included
if [[ "$SUBFOLDER_NAME" == .* ]]; then
echo "Skipping hidden folder: $SUBFOLDER_NAME"
continue
fi
echo "--------------------------------------------------"
echo "Processing subfolder: $SUBFOLDER_NAME"
# Determine the target path for this subfolder
if [ "$TARGET_PATH_IN_REPO" == "." ]; then
FULL_TARGET_PATH="$SUBFOLDER_NAME"
else
FULL_TARGET_PATH="$TARGET_PATH_IN_REPO/$SUBFOLDER_NAME"
fi
# Check if subfolder is empty
if [ -z "$(ls -A "$SUBFOLDER")" ]; then
echo "⚠️ Skipping empty subfolder: $SUBFOLDER_NAME"
continue
fi
# Construct commit message
COMMIT_MESSAGE="Upload subfolder '$SUBFOLDER_NAME' to '$FULL_TARGET_PATH'"
echo "Uploading to: $HF_REPO_ID/$FULL_TARGET_PATH"
# Upload the subfolder
huggingface-cli upload \
"$HF_REPO_ID" \
"$SUBFOLDER" \
--repo-type "$REPO_TYPE" \
--path-in-repo "$FULL_TARGET_PATH" \
--commit-message "$COMMIT_MESSAGE" \
--quiet
# Check the exit status
if [ $? -eq 0 ]; then
echo "✅ Successfully uploaded: $SUBFOLDER_NAME"
((SUCCESS_COUNT++))
else
echo "❌ Failed to upload: $SUBFOLDER_NAME"
((FAIL_COUNT++))
fi
echo ""
done
# Final summary
echo "=========================================="
echo "Upload Summary"
echo "=========================================="
echo "Total subfolders processed: $((SUCCESS_COUNT + FAIL_COUNT))"
echo "✅ Successful uploads: $SUCCESS_COUNT"
echo "❌ Failed uploads: $FAIL_COUNT"
echo ""
# Construct the repository URL
BASE_URL="https://huggingface.co"
if [ "$REPO_TYPE" == "dataset" ]; then
BASE_URL="$BASE_URL/datasets"
elif [ "$REPO_TYPE" == "space" ]; then
BASE_URL="$BASE_URL/spaces"
fi
if [ "$TARGET_PATH_IN_REPO" == "." ]; then
echo "View the repository at: $BASE_URL/$HF_REPO_ID/tree/main"
else
# Clean up the path for URL
CLEAN_PATH=$(echo "$TARGET_PATH_IN_REPO" | sed 's:^/*::; s:/*$::')
echo "View uploaded folders at: $BASE_URL/$HF_REPO_ID/tree/main/$CLEAN_PATH"
fi
if [ $FAIL_COUNT -gt 0 ]; then
exit 1
else
exit 0
fi |