1#!/usr/bin/env 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
12function log {
13 echo "$@" 1>&2
14}
15
16function 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
22function has_cmd {
23 if ! [ -x "$(command -v $1)" ]; then
24 return 1
25 fi
26}
27
28if has_cmd wget; then
29 cmd="wget -q -c -O %s/%s %s"
30elif has_cmd curl; then
31 cmd="curl -C - -f --output-dir %s -o %s -L %s"
32else
33 log "[E] curl or wget not found"
34 exit 1
35fi
36
37url=""
38repo=""
39file=""
40outdir="."
41
42# parse args
43while [[ $# -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
69done
70
71if [ -n "$repo" ] && [ -n "$file" ]; then
72 url="https://huggingface.co/$repo/resolve/main/$file"
73fi
74
75if [ -z "$url" ]; then
76 log "[E] missing --url"
77 usage
78fi
79
80# check if the URL is a HuggingFace model, and if so, try to download it
81is_url=false
82
83if [[ ${#url} -gt 22 ]]; then
84 if [[ ${url:0:22} == "https://huggingface.co" ]]; then
85 is_url=true
86 fi
87fi
88
89if [ "$is_url" = false ]; then
90 log "[E] invalid URL, must start with https://huggingface.co"
91 exit 0
92fi
93
94# replace "blob/main" with "resolve/main"
95url=${url/blob\/main/resolve\/main}
96
97basename=$(basename $url)
98
99log "[+] attempting to download $basename"
100
101if [ -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
108fi
109
110log "[-] failed to download"
111
112exit 1