hf.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # 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>] [-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 --show-progress -c -O %s %s"
  26. elif has_cmd curl; then
  27. cmd="curl -C - -f -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. # parse args
  36. while [[ $# -gt 0 ]]; do
  37. case "$1" in
  38. --url)
  39. url="$2"
  40. shift 2
  41. ;;
  42. --repo)
  43. repo="$2"
  44. shift 2
  45. ;;
  46. --file)
  47. file="$2"
  48. shift 2
  49. ;;
  50. -h|--help)
  51. usage
  52. ;;
  53. *)
  54. url="$1"
  55. shift
  56. ;;
  57. esac
  58. done
  59. if [ -n "$repo" ] && [ -n "$file" ]; then
  60. url="https://huggingface.co/$repo/resolve/main/$file"
  61. fi
  62. if [ -z "$url" ]; then
  63. log "[E] missing --url"
  64. usage
  65. fi
  66. # check if the URL is a HuggingFace model, and if so, try to download it
  67. is_url=false
  68. if [[ ${#url} -gt 22 ]]; then
  69. if [[ ${url:0:22} == "https://huggingface.co" ]]; then
  70. is_url=true
  71. fi
  72. fi
  73. if [ "$is_url" = false ]; then
  74. log "[E] invalid URL, must start with https://huggingface.co"
  75. exit 0
  76. fi
  77. # replace "blob/main" with "resolve/main"
  78. url=${url/blob\/main/resolve\/main}
  79. basename=$(basename $url)
  80. log "[+] attempting to download $basename"
  81. if [ -n "$cmd" ]; then
  82. cmd=$(printf "$cmd" "$basename" "$url")
  83. log "[+] $cmd"
  84. if $cmd; then
  85. echo $basename
  86. exit 0
  87. fi
  88. fi
  89. log "[-] failed to download"
  90. exit 1