国内快速高效下载 HuggingFace上的各种大语言模型

预先安装:

apt install aria2
# sudo apt install aria2

apt install git-lfs
# sudo apt install git-lfs
  1. 下载hfd
wget https://hf-mirror.com/hfd/hfd.sh
chmod a+x hfd.sh
  1. 设置环境变量

Linux

export HF_ENDPOINT=https://hf-mirror.com

Windows

$env:HF_ENDPOINT = "https://hf-mirror.com"
  1. 下载模型
./hfd.sh gpt2 --tool aria2c -x 4
  1. 下载数据集
./hfd.sh wikitext --dataset --tool aria2c -x 4

本文参考:https://hf-mirror.com/

自己保存hfd脚本:hfd.sh

#!/usr/bin/env bash
# Color definitions
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

trap 'printf "${YELLOW}\nDownload interrupted. If you re-run the command, you can resume the download from the breakpoint.\n${NC}"; exit 1' INT

display_help() {
   
    cat << EOF
Usage:
  hfd <repo_id> [--include include_pattern] [--exclude exclude_pattern] [--hf_username username] [--hf_token token] [--tool aria2c|wget] [-x threads] [--dataset] [--local-dir path]    

Description:
  Downloads a model or dataset from Hugging Face using the provided repo ID.

Parameters:
  repo_id        The Hugging Face repo ID in the format 'org/repo_name'.
  --include       (Optional) Flag to specify a string pattern to include files for downloading.
  --exclude       (Optional) Flag to specify a string pattern to exclude files from downloading.
  include/exclude_pattern The pattern to match against filenames, supports wildcard characters. e.g., '--exclude *.safetensor', '--include vae/*'.
  --hf_username   (Optional) Hugging Face username for authentication. **NOT EMAIL**.
  --hf_token      (Optional) Hugging Face token for authentication.
  --tool          (Optional) Download tool to use. Can be aria2c (default) or wget.
  -x              (Optional) Number of download threads for aria2c. Defaults to 4.
  --dataset       (Optional) Flag to indicate downloading a dataset.
  --local-dir     (Optional) Local directory path where the model or dataset will be stored.

Example:
  hfd bigscience/bloom-560m --exclude *.safetensors
  hfd meta-llama/Llama-2-7b --hf_username myuser --hf_token mytoken -x 4
  hfd lavita/medical-qa-shared-task-v1-toy --dataset
EOF
    exit 1
}

MODEL_ID=$1
shift

# Default values
TOOL="aria2c"
THREADS=4
HF_ENDPOINT=${HF_ENDPOINT:-"https://huggingface.co"}

while [[ $# -gt 0 ]]; do
    case $1 in
        --include) INCLUDE_PATTERN="$2"; shift 2 ;;
        --exclude) EXCLUDE_PATTERN="$2"; shift 2 ;;
        --hf_username) HF_USERNAME="$2"; shift 2 ;;
        --hf_token) HF_TOKEN="$2"; shift 2 ;;
        --tool) TOOL="$2"; shift 2 ;;
        -x)
### 如何在Hugging Face平台上下载大模型文件 #### 使用Hugging Face Hub界面下载 对于大多数用户来说,最简单的方法是通过浏览器访问[Hugging Face](https://huggingface.co/)网站并找到感兴趣的模型页面。一旦定位到特定的大规模预训练模型,在其详情页中会有一个明显的“Files and versions”选项卡[^3]。 #### 利用命令行工具克隆仓库 为了更高效地获取这些庞大的模型资源,推荐采用版本控制系统Git配合LFS(Large File Storage)。这不仅限于简单的HTTP(S)协议拉取,还允许利用SSH密钥认证来简化流程。具体操作如下: 1. **安装必要的软件包** 如果尚未安装`git-lfs`,可以通过Homebrew轻松完成这一过程: ```bash /bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)" source ~/.zprofile brew install git-lfs ``` 2. **初始化Git LFS环境** 执行下面这条指令以激活本地计算机上的Git Large File Support功能: ```bash git lfs install ``` 3. **执行克隆动作** 接下来就是实际的下载环节了。有两种方式可以实现这一点——HTTPS或SSH。前者适合初次使用者;后者则提供了无需频繁输入用户名密码的优势。 - HTTPS 方式 ```bash git clone https://huggingface.co/bert-base-uncased ``` - SSH 方式 ```bash git clone git@hf.co:bert-base-uncased ``` 上述方法能够确保顺利获得所需的大规模机器学习模型及其配套资料,无论是用于研究还是生产环境中进一步开发调优都非常适用[^5]。 #### 借助官方客户端库自动化处理 除了手动操作之外,还可以借助Python编程语言以及专门为此设计的应用程序接口(API),即Transformers库中的`from_pretrained()`函数自动加载远程服务器上的权重参数至内存之中,从而省去了繁琐的人工干预步骤[^2]。 ```python from transformers import AutoModelForSequenceClassification, AutoTokenizer model_name = 'distilbert-base-uncased-finetuned-sst-2-english' tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForSequenceClassification.from_pretrained(model_name) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落难Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值