export huggingface国内源,提升下载速度

export HF_ENDPOINT=https://hf-mirror.com 用于配置 Hugging Face 相关工具网络访问的环境变量设置命令。

命令详细解释

  1. 核心构成拆解

    • export:这是 Linux/macOS 系统中设置环境变量的命令(Windows 系统对应 setsetx),作用是让后续执行的程序能读取到这个变量。
    • HF_ENDPOINT:是 Hugging Face 官方定义的环境变量名,专门用于指定 Hugging Face 相关工具(如 transformersdatasets 库,或 huggingface-hub 工具)访问模型/数据集的服务器地址。
    • https://hf-mirror.com:是 Hugging Face 的国内镜像站地址,由社区维护,目的是解决直接访问 Hugging Face 官方服务器(https://huggingface.co)速度慢、连接不稳定的问题。
  2. 实际作用
    平时你用 transformers 下载模型(比如 AutoModel.from_pretrained("bert-base-chinese"))时,工具默认会从 Hugging Face 官方服务器拉取文件。设置这个环境变量后,所有 Hugging Face 相关工具会自动切换到国内镜像站下载,大幅提升下载速度,避免超时、断连等问题。

  3. 使用注意事项

    • 这条命令仅对当前终端会话有效,关闭终端后失效。如果想永久生效,需要把它添加到系统配置文件(如 ~/.bashrc~/.zshrc)中。
    • 示例(永久生效配置):
      # 编辑配置文件
      echo 'export HF_ENDPOINT=https://hf-mirror.com' >> ~/.bashrc
      # 生效配置
      source ~/.bashrc
      
    • Windows 系统下的等效设置(命令提示符):
      set HF_ENDPOINT=https://hf-mirror.com  # 临时生效
      setx HF_ENDPOINT https://hf-mirror.com  # 永久生效(需重启终端)
      

总结

  1. 这条命令的核心是修改 Hugging Face 工具的下载源,从官方服务器切换到国内镜像站;
  2. 主要解决国内访问 Hugging Face 速度慢、连接不稳定的问题;
  3. 临时生效仅对当前终端有效,永久生效需写入系统配置文件。
    比如下载scare dataset:
import os
from huggingface_hub import snapshot_download, login

def download_scared_dataset():
    # Dataset repository ID
    repo_id = "maxhallan7/scared"
    
    # Local directory to save the dataset
    # Using the shared data directory as requested previously
    local_dir = "/home/dd/share_data/scared_dataset"
    
    print(f"Starting download of {repo_id} to {local_dir}...")
    
    # Ensure the directory exists
    os.makedirs(local_dir, exist_ok=True)
    
    try:
        # Download the entire dataset
        # resume_download=True is default in newer versions but good to be explicit
        local_path = snapshot_download(
            repo_id=repo_id,
            repo_type="dataset",
            local_dir=local_dir,
            resume_download=True,
            max_workers=8  # Adjust based on network/CPU
        )
        print(f"Download completed successfully! Files are located at: {local_path}")
        
    except Exception as e:
        print(f"An error occurred during download: {e}")
        print("If this is a private dataset, you may need to run 'huggingface-cli login' first.")

# 运行命令:export HF_ENDPOINT=https://hf-mirror.com && python /home/redpine/codedepth/C3VD/utils/download_scared_dataset.py 下载scared dataset.
if __name__ == "__main__":
    download_scared_dataset()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值