Datasets:

ArXiv:
ea-dev-pjlab-results / upload_checkpoints.sh
shulin16's picture
Upload folder using huggingface_hub
9f3bc09 verified
#!/bin/bash
# Script to upload checkpoints to HuggingFace with customizable repository name
# Usage: ./upload_checkpoints.sh <parent_folder_path> <hf_repo> [<subfolder>]
set -e
if [ $# -lt 2 ] || [ $# -gt 3 ]; then
echo "Usage: $0 <parent_folder_path> <hf_repo> [<subfolder>]"
echo " <parent_folder_path>: Local directory containing checkpoint folders"
echo " <hf_repo>: HuggingFace repository (format: username/repo-name)"
echo " <subfolder>: Optional subfolder path in the repository"
echo ""
echo "Examples:"
echo " Upload to root: $0 ./checkpoints myusername/my-model"
echo " Upload to subfolder: $0 ./checkpoints myusername/my-model checkpoints/v1"
exit 1
fi
PARENT_FOLDER="$1"
HF_REPO="$2"
SUBFOLDER="$3" # optional subfolder path in the repository
if [ ! -d "$PARENT_FOLDER" ]; then
echo "Error: Directory $PARENT_FOLDER does not exist"
exit 1
fi
# Check if huggingface-cli is available
if ! command -v huggingface-cli &> /dev/null; then
echo "Error: huggingface-cli not found. Please install it with: pip install huggingface_hub[cli]"
exit 1
fi
# Check if logged in
if ! huggingface-cli whoami &> /dev/null; then
echo "Error: Not logged in to HuggingFace. Run: huggingface-cli login"
exit 1
fi
echo "Searching for checkpoint directories in $PARENT_FOLDER..."
# Find all checkpoint directories
while IFS= read -r -d '' checkpoint_dir; do
checkpoint_name=$(basename "$checkpoint_dir")
# Extract checkpoint number (e.g., checkpoint-100 -> 100)
if [[ $checkpoint_name =~ checkpoint-([0-9]+) ]]; then
ckpt_num="${BASH_REMATCH[1]}"
# Determine the target path in the repository
if [ -n "$SUBFOLDER" ]; then
# Remove trailing slash if present
SUBFOLDER="${SUBFOLDER%/}"
target_path="$SUBFOLDER/$checkpoint_name"
else
target_path="$checkpoint_name"
fi
echo "=================================================="
echo "Uploading $checkpoint_name to $HF_REPO/$target_path"
echo "=================================================="
# Create repository if it doesn't exist
huggingface-cli repo create "$HF_REPO" --repo-type model --exist-ok
# Upload checkpoint to specific path (excluding optimizer states for smaller size)
huggingface-cli upload \
"$HF_REPO" \
"$checkpoint_dir" \
"$target_path" \
--repo-type model \
--exclude="global_step*" \
--exclude="*.pt*" \
--commit-message "Upload $checkpoint_name to $target_path"
echo "✅ Successfully uploaded to: https://huggingface.co/$HF_REPO/tree/main/$target_path"
echo ""
else
echo "Warning: Skipping $checkpoint_name (doesn't match checkpoint-* pattern)"
fi
done < <(find "$PARENT_FOLDER" -type d -name "checkpoint-*" -print0 | sort -z)
echo "All uploads completed!"