File size: 1,532 Bytes
5c18fd7 edc52b4 5c18fd7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#!/usr/bin/env bash
# NOTE: `huggingface-cli download` already exists to do this
# fills huggingfaces cache by downloading safetensors files in parallel
# usage: $ bash download_weights.bash SOME/MODEL_ID COMMIT_HASH -j4
#PV="pv -cpteabm $((60*60*24))"
PV="pv -cpteab"
model_id="$1"
revision="$2"
shift 2
repo_url=https://huggingface.co/"$model_id"
clonepath="$(basename "$repo_url")"
cachepath="${HOME}/.cache/huggingface/hub/models--${model_id//\//--}/blobs"
if ! [ -e ${HOME}/.cache/huggingface/token ]
then
huggingface-cli login || exit -1
fi
HF_TOKEN="$(<${HOME}/.cache/huggingface/token)"
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/"$model_id" "$clonepath"
mkdir -p "$cachepath"
cd "$clonepath"
GIT_LFS_SKIP_SMUDGE=1 git checkout "$revision" || exit -1
filecount=$(ls *.safetensors | wc -l)
export repo_url revision cachepath HF_TOKEN PV
# troubleshot by adding -vx before -c in the bash command
parallel "$@" -i bash -c \
'file_url="$repo_url"/resolve/"$revision"/"{}";\
export $(cat "{}" | tr \ =);\
blobname=${oid##*:};\
outfile="$cachepath"/"$blobname";\
set pipefail;\
if [ "$(($(stat -c %s "$outfile" 2>/dev/null)))" != "$(($size))" ]; then\
curl --silent -L --header "Authorization: Bearer $HF_TOKEN" "$file_url" |\
$PV -s "$size" -N "{}" > "$outfile" 2>/dev/tty || continue;\
fi;\
echo "$blobname" "$outfile"'\
-- *.safetensors |
$PV -l -s "$filecount" -N "$clonepath" 2>/dev/tty |
sha256sum -c && echo SUCCESS || { echo ERROR try again; false; }
|