|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Shortcut for downloading HF models |
| 4 | +# |
| 5 | +# Usage: |
| 6 | +# ./llama-cli -m $(./scripts/hf.sh https://huggingface.co/TheBloke/Mixtral-8x7B-v0.1-GGUF/resolve/main/mixtral-8x7b-v0.1.Q4_K_M.gguf) |
| 7 | +# ./llama-cli -m $(./scripts/hf.sh --url https://huggingface.co/TheBloke/Mixtral-8x7B-v0.1-GGUF/blob/main/mixtral-8x7b-v0.1.Q4_K_M.gguf) |
| 8 | +# ./llama-cli -m $(./scripts/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>] [--outdir <dir> [-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 -c -O %s/%s %s" |
| 30 | +elif has_cmd curl; then |
| 31 | + cmd="curl -C - -f --output-dir %s -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 | +outdir="." |
| 41 | + |
| 42 | +# parse args |
| 43 | +while [[ $# -gt 0 ]]; do |
| 44 | + case "$1" in |
| 45 | + --url) |
| 46 | + url="$2" |
| 47 | + shift 2 |
| 48 | + ;; |
| 49 | + --repo) |
| 50 | + repo="$2" |
| 51 | + shift 2 |
| 52 | + ;; |
| 53 | + --file) |
| 54 | + file="$2" |
| 55 | + shift 2 |
| 56 | + ;; |
| 57 | + --outdir) |
| 58 | + outdir="$2" |
| 59 | + shift 2 |
| 60 | + ;; |
| 61 | + -h|--help) |
| 62 | + usage |
| 63 | + ;; |
| 64 | + *) |
| 65 | + url="$1" |
| 66 | + shift |
| 67 | + ;; |
| 68 | + esac |
| 69 | +done |
| 70 | + |
| 71 | +if [ -n "$repo" ] && [ -n "$file" ]; then |
| 72 | + url="https://huggingface.co/$repo/resolve/main/$file" |
| 73 | +fi |
| 74 | + |
| 75 | +if [ -z "$url" ]; then |
| 76 | + log "[E] missing --url" |
| 77 | + usage |
| 78 | +fi |
| 79 | + |
| 80 | +# check if the URL is a HuggingFace model, and if so, try to download it |
| 81 | +is_url=false |
| 82 | + |
| 83 | +if [[ ${#url} -gt 22 ]]; then |
| 84 | + if [[ ${url:0:22} == "https://huggingface.co" ]]; then |
| 85 | + is_url=true |
| 86 | + fi |
| 87 | +fi |
| 88 | + |
| 89 | +if [ "$is_url" = false ]; then |
| 90 | + log "[E] invalid URL, must start with https://huggingface.co" |
| 91 | + exit 0 |
| 92 | +fi |
| 93 | + |
| 94 | +# replace "blob/main" with "resolve/main" |
| 95 | +url=${url/blob\/main/resolve\/main} |
| 96 | + |
| 97 | +basename=$(basename $url) |
| 98 | + |
| 99 | +log "[+] attempting to download $basename" |
| 100 | + |
| 101 | +if [ -n "$cmd" ]; then |
| 102 | + cmd=$(printf "$cmd" "$outdir" "$basename" "$url") |
| 103 | + log "[+] $cmd" |
| 104 | + if $cmd; then |
| 105 | + echo $outdir/$basename |
| 106 | + exit 0 |
| 107 | + fi |
| 108 | +fi |
| 109 | + |
| 110 | +log "[-] failed to download" |
| 111 | + |
| 112 | +exit 1 |
0 commit comments