export HF_ENDPOINT=https://hf-mirror.com 用于配置 Hugging Face 相关工具网络访问的环境变量设置命令。
命令详细解释
-
核心构成拆解
export:这是 Linux/macOS 系统中设置环境变量的命令(Windows 系统对应set或setx),作用是让后续执行的程序能读取到这个变量。HF_ENDPOINT:是 Hugging Face 官方定义的环境变量名,专门用于指定 Hugging Face 相关工具(如transformers、datasets库,或huggingface-hub工具)访问模型/数据集的服务器地址。https://hf-mirror.com:是 Hugging Face 的国内镜像站地址,由社区维护,目的是解决直接访问 Hugging Face 官方服务器(https://huggingface.co)速度慢、连接不稳定的问题。
-
实际作用
平时你用transformers下载模型(比如AutoModel.from_pretrained("bert-base-chinese"))时,工具默认会从 Hugging Face 官方服务器拉取文件。设置这个环境变量后,所有 Hugging Face 相关工具会自动切换到国内镜像站下载,大幅提升下载速度,避免超时、断连等问题。 -
使用注意事项
- 这条命令仅对当前终端会话有效,关闭终端后失效。如果想永久生效,需要把它添加到系统配置文件(如
~/.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 # 永久生效(需重启终端)
- 这条命令仅对当前终端会话有效,关闭终端后失效。如果想永久生效,需要把它添加到系统配置文件(如
总结
- 这条命令的核心是修改 Hugging Face 工具的下载源,从官方服务器切换到国内镜像站;
- 主要解决国内访问 Hugging Face 速度慢、连接不稳定的问题;
- 临时生效仅对当前终端有效,永久生效需写入系统配置文件。
比如下载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()



308

被折叠的 条评论
为什么被折叠?



