huggingface-cli 是 Hugging Face 官方提供的命令行工具,自帶完善的下載功能。
安裝依賴
pip install -U huggingface_hub
設(shè)置環(huán)境變量
linux
# 建議將上面這一行寫入 ~/.bashrc。若沒有寫入,則每次下載時都需要先輸入該命令
export HF_ENDPOINT=https://
Windows Powershell
$env:HF_ENDPOINT = "https://" # 暫時不知如何使用
下載模型樣例
使用命令行下載
下載全部文件添加--resume-download 參數(shù),此時將保存至/root/.cache/.../ 文件夾中
huggingface-cli download --resume-download meta-llama/Llama-2-13b-chat-hf
下載全部文件并保存到指定位置時,添加--local-dir 參數(shù),此時將保存至./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 參數(shù)
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://" # 設(shè)置為hf的國內(nèi)鏡像網(wǎng)站
from huggingface_hub import snapshot_download
model_name = "meta-llama/Llama-2-13b-chat-hf"
# while True 是為了防止斷聯(lián)
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
下載數(shù)據(jù)集
將wikitext 數(shù)據(jù)集下載到本地wikitext 文件中,并取消軟連接。
huggingface-cli download --repo-type dataset --resume-download wikitext --local-dir wikitext --local-dir-use-symlinks False
|