| #!/bin/bash |
|
|
| |
| |
|
|
| 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" |
|
|
| if [ ! -d "$PARENT_FOLDER" ]; then |
| echo "Error: Directory $PARENT_FOLDER does not exist" |
| exit 1 |
| fi |
|
|
| |
| 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 |
|
|
| |
| 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..." |
|
|
| |
| while IFS= read -r -d '' checkpoint_dir; do |
| checkpoint_name=$(basename "$checkpoint_dir") |
| |
| |
| if [[ $checkpoint_name =~ checkpoint-([0-9]+) ]]; then |
| ckpt_num="${BASH_REMATCH[1]}" |
| |
| |
| if [ -n "$SUBFOLDER" ]; then |
| |
| SUBFOLDER="${SUBFOLDER%/}" |
| target_path="$SUBFOLDER/$checkpoint_name" |
| else |
| target_path="$checkpoint_name" |
| fi |
| |
| echo "==================================================" |
| echo "Uploading $checkpoint_name to $HF_REPO/$target_path" |
| echo "==================================================" |
| |
| |
| huggingface-cli repo create "$HF_REPO" --repo-type model --exist-ok |
| |
| |
| 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!" |