huggingface-cli下载数据(含国内镜像源方法)

huggingface-cli 是 Hugging Face 官方提供的命令行工具,自带完善的下载功能。

安装依赖

pip install -U huggingface_hub

设置环境变量

linux

# 建议将上面这一行写入 ~/.bashrc。若没有写入,则每次下载时都需要先输入该命令
export HF_ENDPOINT=https://hf-mirror.com  

Windows Powershell

$env:HF_ENDPOINT = "https://hf-mirror.com"  # 暂时不知如何使用

下载模型样例

使用命令行下载

下载全部文件添加--resume-download参数,此时将保存至/root/.cache/.../文件夹中

huggingface-cli download --resume-download meta-llama/Llama-2-13b-chat-hf

下载全部文件并保存到指定位置时,添加--local-dir参数,此时将保存至./Llama-2-13b-chat-hf/

huggingface-cli download --resume-download meta-llama/Llama-2-13b-chat-hf --local-dir Llama-2-13b-chat-hf

下载多个文件时,再添加具体文件名即可

huggingface-cli download meta-llama/Llama-2-13b-chat-hf config.json model-00001-of-00003.safetensors --local-dir Llama-2-13b-chat-hf

下载多个文件并排除一些文件可使用--include--exclude命令

huggingface-cli download meta-llama/Llama-2-13b-chat-hf --include "*.safetensors" --exclude "*.bin"

需要 huggingface token 时 (Gated Repo),添加--token参数

huggingface-cli download meta-llama/Llama-2-13b-chat-hf --include "*.safetensors" --exclude "*.bin" --token hf_****

使用python脚本下载

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"  # 设置为hf的国内镜像网站

from huggingface_hub import snapshot_download

model_name = "meta-llama/Llama-2-13b-chat-hf"
# while True 是为了防止断联
while True:
    try:
        snapshot_download(
            repo_id=model_name,
            local_dir_use_symlinks=True,  # 在local-dir指定的目录中都是一些“链接文件”
            ignore_patterns=["*.bin"],  # 忽略下载哪些文件
            local_dir=model_name,
            token="*************",   # huggingface的token
            resume_download=True
        )
        break
    except:
        pass

下载数据集

wikitext数据集下载到本地wikitext文件中,并取消软连接。

huggingface-cli download --repo-type dataset --resume-download wikitext --local-dir wikitext --local-dir-use-symlinks False
  • 25
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值