Datasets:

ArXiv:
ea-dev-pjlab-results / upload_to_hf_dataset.sh
shulin16's picture
Upload folder using huggingface_hub
9f3bc09 verified
#!/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