如何在huggingface下载模型/数据集到本地

Install

安装huggingface_hub库

pip install huggingface_hub

Export Mirror

如果本地没有部署魔法的话,请用huggingface的镜像网址

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

Login in

打开huggingface官网,左上角进入“Access Tokens”,获取User Access Tokens

huggingface-cli login --token "TOKEN"

Download Model

下载模型。这里以LLAMA3举例。如果获取模型需要审核的话,记得国籍请选择美国,不要选择中国!

huggingface-cli download --resume-download meta-llama/Meta-Llama-3-8B-Instruct --local-dir ~/model/Meta-Llama-3-8B-Instruct

Download Dataset

下载数据集。这里以COCO举例

huggingface-cli download --repo-type dataset --resume-download sayakpaul/coco-30-val-2014  --local-dir /home/dataset/val2014 --local-dir-use-symlinks False

Git LFS

除了用huggingface-cli下载,还有一种新的方法下载huggingface上的内容——Git LFS。本人有时候用huggingface-cli会发生如下报错:

RuntimeError: Data processing error: CAS service error : Error : single flight error: Real call failed: CasObjectError(InternalIOError(Custom { kind: Other, error: reqwest::Error 
{ kind: Decode, source: hyper::Error(Body, Os { code: 104, kind: ConnectionReset, message: "Connection reset by peer" }) } })) 

用Git LFS可以轻松解决,过程如下:

# 切换conda环境
conda activate xxx

# 使用 Conda 安装 git-lfs 
conda install -c conda-forge git-lfs

# 初始化 git-lfs
git lfs install

# 下载huggingface目标库的所有内容到本地
git lfs clone https://huggingface.co/distilbert-base-uncased

# 增量更新(万一后续目标库作者更新了仓库,运行此命令可以更新本地之前下载的内容)
git lfs pull
posted @ 2025-02-17 00:50  ZeroZeroSeven  阅读(7350)  评论(0)    收藏  举报