#!/usr/bin/env bash # Color definitions RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m'# No Color
trap'printf "${YELLOW}\nDownload interrupted. If you re-run the command, you can resume the download from the breakpoint.\n${NC}"; exit 1' INT
display_help() { cat << EOF Usage: hfd <repo_id> [--include include_pattern1 include_pattern2 ...] [--exclude exclude_pattern1 exclude_pattern2 ...] [--hf_username username] [--hf_token token] [--tool aria2c|wget] [-x threads] [--dataset] [--local-dir path] Description: Downloads a model or dataset from Hugging Face using the provided repo ID. Parameters: repo_id The Hugging Face repo ID in the format 'org/repo_name'. --include (Optional) Flag to specify string patterns to include files for downloading. Supports multiple patterns. --exclude (Optional) Flag to specify string patterns to exclude files from downloading. Supports multiple patterns. include/exclude_pattern The patterns to match against filenames, supports wildcard characters. e.g., '--exclude *.safetensor *.txt', '--include vae/*'. --hf_username (Optional) Hugging Face username for authentication. **NOT EMAIL**. --hf_token (Optional) Hugging Face token for authentication. --tool (Optional) Download tool to use. Can be aria2c (default) or wget. -x (Optional) Number of download threads for aria2c. Defaults to 4. --dataset (Optional) Flag to indicate downloading a dataset. --local-dir (Optional) Local directory path where the model or dataset will be stored. Example: hfd bigscience/bloom-560m --exclude *.safetensors hfd meta-llama/Llama-2-7b --hf_username myuser --hf_token mytoken -x 4 hfd lavita/medical-qa-shared-task-v1-toy --dataset EOF exit 1 }
# Check if aria2, wget, curl, git, and git-lfs are installed check_command() { if ! command -v $1 &>/dev/null; then echo -e "${RED}$1 is not installed. Please install it first.${NC}" exit 1 fi }
# Mark current repo safe when using shared file system like samba or nfs ensure_ownership() { if git status 2>&1 | grep "fatal: detected dubious ownership in repository at" > /dev/null; then git config --global --add safe.directory "${PWD}" printf"${YELLOW}Detected dubious ownership in repository, mark ${PWD} safe using git, edit ~/.gitconfig if you want to reverse this.\n${NC}" fi }
file_matches_include_patterns() { local file="$1" for pattern in"${INCLUDE_PATTERNS[@]}"; do if [[ "$file" == $pattern ]]; then return 0 fi done return 1 }
file_matches_exclude_patterns() { local file="$1" for pattern in"${EXCLUDE_PATTERNS[@]}"; do if [[ "$file" == $pattern ]]; then return 0 fi done return 1 }