下载huggingface中数据集/模型(保存到本地指定路径)
1.若需要将数据集/模型放在指定路径,需要指定"cache-dir"参数,而不是“local-dir”2.直接从huggingface中下载对应的模型和数据集,需要外网。因此可以使用国内镜像“HF-Mirror - Huggingface 镜像站。
·
一. snapshot_download
# 1.安装huggingface_hub
# pip install huggingface_hub
import os
from huggingface_hub import snapshot_download
print('downloading entire files...')
# 注意,这种方式仍然保存在cache_dir中
snapshot_download(repo_id="ibrahimhamamci/CT-RATE", repo_type="dataset", # {'dataset', 'model'}
local_dir="本地路径",
local_dir_use_symlinks=False, resume_download=True,
token='hf_***',
# endpoint='https://hf-mirror.com' # 如果不能翻墙,可以添加这个参数,从而在hf-mirror上下载(不需要翻墙,默认huggingface需要外网)
)
# 使用cache_dir参数,将模型/数据集保存到指定“本地路径”
snapshot_download(repo_id="ibrahimhamamci/CT-RATE", repo_type="dataset",
cache_dir="本地路径",
local_dir_use_symlinks=False, resume_download=True,
token='hf_***')
# download single file...,下载单个文件
# from huggingface_hub import hf_hub_download
# hf_hub_download(repo_id="ibrahimhamamci/CT-RATE", filename='config.json',
# repo_type="dataset",
# local_dir="/home/miao/data/dataset/CT-RATE/dataset/train",
# local_dir_use_symlinks=False, resume_download=True,
# force_download=False, subfolder='dataset/train/train_10006')
# repo_type choices:{dataset, model}
# token获取地址:https://huggingface.co/settings/tokens
注意事项:
- 获取token地址:网址
https://huggingface.co/settings/tokens - 下图的repo_id为“google/gemma-7b”
- 如果不能翻墙,可以在snapshot_download函数中添加参数endpoint='https://hf-mirror.com' ,从而不需要外网。

二. huggingface-cli
# 1.安装huggingface-cli
# pip install -U "huggingface[cli]"
# 命令行输入:huggingface-cli -h,可以查看对应的帮助文档
# 2.下载模型
# 注意,模型仍然保存在cache_dir文档中
huggingface-cli download --token hf_*** --resume-download meta-llama/Llama-2-7b-hf --local-dir Llama-2-7b-hf
# 3.下载数据集
# 这种方式,数据集会保存到"/home/local-dir/"中
huggingface-cli download --repo-type dataset --token hf_** --resume-download ibrahimhamamci/CT-RATE --cache-dir /home/local-dir --local-dir-use-symlinks False
注意:当下载大规模数据集时,中途可能由于一些特殊原因,导致下载中断。但重新输入下面这个命令,仍然能继续下载(而不是重头开始下载)
# 3.下载数据集
# 这种方式,数据集会保存到"/home/local-dir/"中
huggingface-cli download --repo-type dataset --token hf_** --resume-download ibrahimhamamci/CT-RATE --cache-dir /home/local-dir --local-dir-use-symlinks False
三.总结
1.若需要将数据集/模型放在指定路径,需要指定"cache-dir"参数,而不是“local-dir”
2.直接从huggingface中下载对应的模型和数据集,需要外网。因此可以使用国内镜像“HF-Mirror - Huggingface 镜像站”
3.个人推荐使用huggingface-cli下载大规模数据集或者模型
四.参考
更多推荐

所有评论(0)