hf.sh 2.2 KB

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