| #!/bin/bash |
|
|
| |
| |
| |
| |
| |
|
|
| |
| DEFAULT_REPO_TYPE="dataset" |
|
|
| |
| 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 |
|
|
| |
| 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." |
| } |
|
|
| |
| 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:-.}" |
| REPO_TYPE="$DEFAULT_REPO_TYPE" |
|
|
| |
| if [ ! -d "$PARENT_FOLDER_PATH" ]; then |
| echo "Error: Parent folder '$PARENT_FOLDER_PATH' does not exist or is not a directory." |
| exit 1 |
| fi |
|
|
| |
| 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 "" |
|
|
| |
| 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 "" |
|
|
| |
| SUCCESS_COUNT=0 |
| FAIL_COUNT=0 |
|
|
| |
| for SUBFOLDER in "$PARENT_FOLDER_PATH"/*; do |
| |
| if [ ! -d "$SUBFOLDER" ]; then |
| continue |
| fi |
| |
| |
| SUBFOLDER_NAME=$(basename "$SUBFOLDER") |
| |
| |
| if [[ "$SUBFOLDER_NAME" == .* ]]; then |
| echo "Skipping hidden folder: $SUBFOLDER_NAME" |
| continue |
| fi |
| |
| echo "--------------------------------------------------" |
| echo "Processing subfolder: $SUBFOLDER_NAME" |
| |
| |
| if [ "$TARGET_PATH_IN_REPO" == "." ]; then |
| FULL_TARGET_PATH="$SUBFOLDER_NAME" |
| else |
| FULL_TARGET_PATH="$TARGET_PATH_IN_REPO/$SUBFOLDER_NAME" |
| fi |
| |
| |
| if [ -z "$(ls -A "$SUBFOLDER")" ]; then |
| echo "⚠️ Skipping empty subfolder: $SUBFOLDER_NAME" |
| continue |
| fi |
| |
| |
| COMMIT_MESSAGE="Upload subfolder '$SUBFOLDER_NAME' to '$FULL_TARGET_PATH'" |
| |
| echo "Uploading to: $HF_REPO_ID/$FULL_TARGET_PATH" |
| |
| |
| huggingface-cli upload \ |
| "$HF_REPO_ID" \ |
| "$SUBFOLDER" \ |
| --repo-type "$REPO_TYPE" \ |
| --path-in-repo "$FULL_TARGET_PATH" \ |
| --commit-message "$COMMIT_MESSAGE" \ |
| --quiet |
| |
| |
| if [ $? -eq 0 ]; then |
| echo "✅ Successfully uploaded: $SUBFOLDER_NAME" |
| ((SUCCESS_COUNT++)) |
| else |
| echo "❌ Failed to upload: $SUBFOLDER_NAME" |
| ((FAIL_COUNT++)) |
| fi |
| echo "" |
| done |
|
|
| |
| echo "==========================================" |
| echo "Upload Summary" |
| echo "==========================================" |
| echo "Total subfolders processed: $((SUCCESS_COUNT + FAIL_COUNT))" |
| echo "✅ Successful uploads: $SUCCESS_COUNT" |
| echo "❌ Failed uploads: $FAIL_COUNT" |
| echo "" |
|
|
| |
| 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_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 |