如何优雅的从Hugging Face下载repo中的部分目录下的文件

从Hugging Face上下载模型是作为深度学习开发者每天都要面对的问题。但是作为非图形页面的开发者而言,Hugging Face对文件的转发策略为我们直接从链接下载所需的文件带来了困难。本文提供了利用官方API下载整个repo中部分目录下的文件的方法。

下载某一目录下的全部文件

首先,安装Hugging Face的python API:

pip install huggingface_hub

随后新建download.py,并输入:

from huggingface_hub import snapshot_download
snapshot_download(repo_id='your/target/repo', allow_patterns='index/path/you/want/to/download/*', cache_dir='local/path/you/want/to/save')

最后python download.py执行即可。由于网络原因,可能中间会出现Timeout,重新执行会自动断点续传。

部分文件类型的下载

只允许下载部分类型的文件(以JSON为例)

from huggingface_hub import snapshot_download
snapshot_download(repo_id='your/target/repo', allow_patterns='*.json', cache_dir='local/path/you/want/to/save')

不允许下载部分类型的文件(以JSON为例)

from huggingface_hub import snapshot_download
snapshot_download(repo_id='your/target/repo', ignore_patterns=['*.json'], cache_dir='local/path/you/want/to/save')

其他使用详见官方API

你可能感兴趣的:(深度学习)