Skip to content

Hugging Face Mirrors

https://hf-mirror.com/ HF-Mirror 是一个公益项目,旨在帮助国内 AI 开发者快速、稳定地下载 Hugging Face 上的模型和数据集。以下是使用 HF-Mirror 的几种方法:

方法一:网页下载

  1. 在 HF-Mirror 网站搜索所需的模型或数据集。
  2. 在模型主页的 "Files and Version" 部分下载文件。

方法二:使用 huggingface-cli

  1. 安装依赖
pip install -U huggingface_hub
  1. 设置环境变量
  2. Linux:

    export HF_ENDPOINT=https://hf-mirror.com
    
  3. Windows Powershell:

    $env:HF_ENDPOINT = "https://hf-mirror.com"
    

建议将此行写入 ~/.bashrc 文件以便永久生效。 3. 下载模型

huggingface-cli download --resume-download gpt2 --local-dir gpt2
  1. 下载数据集
huggingface-cli download --repo-type dataset --resume-download wikitext --local-dir wikitext

可以添加 --local-dir-use-symlinks False 参数禁用文件软链接。

方法三:使用 hfd

  1. 下载 hfd
wget https://hf-mirror.com/hfd/hfd.sh
chmod a+x hfd.sh
  1. 设置环境变量
  2. Linux:

    export HF_ENDPOINT=https://hf-mirror.com
    
  3. Windows Powershell:

    $env:HF_ENDPOINT = "https://hf-mirror.com"
    
  4. 下载模型

./hfd.sh gpt2
  1. 下载数据集
./hfd.sh wikitext --dataset

方法四:使用环境变量(非侵入式)

在运行脚本时设置环境变量:

HF_ENDPOINT=https://hf-mirror.com python your_script.py

对于某些数据集,可能需要手动修改脚本内的地址。

常见问题

Q: 有些项目需要登录,如何下载? A: 部分 Gated Repo 需登录申请许可。为保障账号安全,HF-Mirror 不支持登录。请先前往 Hugging Face 官网登录、申请许可,并获取 Access Token。然后在镜像站使用命令行下载:

  • huggingface-cli
huggingface-cli download --token hf_*** --resume-download meta-llama/Llama-2-7b-hf --local-dir Llama-2-7b-hf
  • hfd
hfd meta-llama/Llama-2-7b --hf_username YOUR_HF_USERNAME --hf_token hf_***

通过以上方法,可以有效地使用 HF-Mirror 来加速模型和数据集的下载。