当前位置:   article > 正文

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

huggingface-cli download

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

安装依赖

pip install -U huggingface_hub
  • 1

设置环境变量

linux

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

Windows Powershell

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

下载模型样例

使用命令行下载

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

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

下载全部文件并保存到指定位置时,添加--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
  • 1

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

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

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

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

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

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

下载数据集

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

huggingface-cli download --repo-type dataset --resume-download wikitext --local-dir wikitext --local-dir-use-symlinks False
  • 1

使用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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/324601
推荐阅读
相关标签
  

闽ICP备14008679号