|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Shortcut for downloading HF models |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./main -m $(./examples/hf.sh https://huggingface.co/TheBloke/Mixtral-8x7B-v0.1-GGUF/resolve/main/mixtral-8x7b-v0.1.Q4_K_M.gguf) |
| 7 | +# ./main -m $(./examples/hf.sh --url https://huggingface.co/TheBloke/Mixtral-8x7B-v0.1-GGUF/blob/main/mixtral-8x7b-v0.1.Q4_K_M.gguf) |
| 8 | +# ./main -m $(./examples/hf.sh --repo TheBloke/Mixtral-8x7B-v0.1-GGUF --file mixtral-8x7b-v0.1.Q4_K_M.gguf) |
| 9 | +# |
| 10 | + |
| 11 | +# all logs go to stderr |
| 12 | +function log { |
| 13 | + echo "$@" 1>&2 |
| 14 | +} |
| 15 | + |
| 16 | +function usage { |
| 17 | + log "Usage: $0 [[--url] <url>] [--repo <repo>] [--file <file>] [-h|--help]" |
| 18 | + exit 1 |
| 19 | +} |
| 20 | + |
| 21 | +# check for curl or wget |
| 22 | +function has_cmd { |
| 23 | + if ! [ -x "$(command -v $1)" ]; then |
| 24 | + return 1 |
| 25 | + fi |
| 26 | +} |
| 27 | + |
| 28 | +if has_cmd wget; then |
| 29 | + cmd="wget -q --show-progress -c -O %s %s" |
| 30 | +elif has_cmd curl; then |
| 31 | + cmd="curl -C - -f -o %s -L %s" |
| 32 | +else |
| 33 | + log "[E] curl or wget not found" |
| 34 | + exit 1 |
| 35 | +fi |
| 36 | + |
| 37 | +url="" |
| 38 | +repo="" |
| 39 | +file="" |
| 40 | + |
| 41 | +# parse args |
| 42 | +while [[ $# -gt 0 ]]; do |
| 43 | + case "$1" in |
| 44 | + --url) |
| 45 | + url="$2" |
| 46 | + shift 2 |
| 47 | + ;; |
| 48 | + --repo) |
| 49 | + repo="$2" |
| 50 | + shift 2 |
| 51 | + ;; |
| 52 | + --file) |
| 53 | + file="$2" |
| 54 | + shift 2 |
| 55 | + ;; |
| 56 | + -h|--help) |
| 57 | + usage |
| 58 | + ;; |
| 59 | + *) |
| 60 | + url="$1" |
| 61 | + shift |
| 62 | + ;; |
| 63 | + esac |
| 64 | +done |
| 65 | + |
| 66 | +if [ -n "$repo" ] && [ -n "$file" ]; then |
| 67 | + url="https://huggingface.co/$repo/resolve/main/$file" |
| 68 | +fi |
| 69 | + |
| 70 | +if [ -z "$url" ]; then |
| 71 | + log "[E] missing --url" |
| 72 | + usage |
| 73 | +fi |
| 74 | + |
| 75 | +# check if the URL is a HuggingFace model, and if so, try to download it |
| 76 | +is_url=false |
| 77 | + |
| 78 | +if [[ ${#url} -gt 22 ]]; then |
| 79 | + if [[ ${url:0:22} == "https://huggingface.co" ]]; then |
| 80 | + is_url=true |
| 81 | + fi |
| 82 | +fi |
| 83 | + |
| 84 | +if [ "$is_url" = false ]; then |
| 85 | + log "[E] invalid URL, must start with https://huggingface.co" |
| 86 | + exit 0 |
| 87 | +fi |
| 88 | + |
| 89 | +# replace "blob/main" with "resolve/main" |
| 90 | +url=${url/blob\/main/resolve\/main} |
| 91 | + |
| 92 | +basename=$(basename $url) |
| 93 | + |
| 94 | +log "[+] attempting to download $basename" |
| 95 | + |
| 96 | +if [ -n "$cmd" ]; then |
| 97 | + cmd=$(printf "$cmd" "$basename" "$url") |
| 98 | + log "[+] $cmd" |
| 99 | + if $cmd; then |
| 100 | + echo $basename |
| 101 | + exit 0 |
| 102 | + fi |
| 103 | +fi |
| 104 | + |
| 105 | +log "[-] failed to download" |
| 106 | + |
| 107 | +exit 1 |
0 commit comments