如何下载huggingface模型到本地

我以前都是在浏览器里一个个下载,最近看手册发现何以用程序下载,我用deepseek写了一个下载的程序

下载前准备:

pip install huggingface_hub tqdm

下载的代码如下:

from huggingface_hub import hf_hub_download, list_repo_files
import os
from tqdm import tqdm

# 设置 Hugging Face 仓库路径
repo_id = "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
# 设置本地保存路径
local_dir = "./DeepSeek-R1-Distill-Qwen-32B"

# 创建本地文件夹(如果不存在)
os.makedirs(local_dir, exist_ok=True)

# 获取仓库中的所有文件
file_list = list_repo_files(repo_id=repo_id, repo_type="model")

# 初始化 tqdm 进度条
pbar = tqdm(file_list, desc="下载进度", unit="file", colour="green")

# 逐个下载文件
for file in pbar:
    pbar.set_description(f"正在下载: {file}")
    hf_hub_download(
        repo_id=repo_id,
        filename=file,
        repo_type="model",
        local_dir=local_dir,
        local_dir_use_symlinks=False,
        resume_download=True,
    )

# 关闭进度条
pbar.close()

print(f"模型文件已下载到: {local_dir}")

上述程序会先获取模型的所有文件,然后按个下载,下载过程会显示进度条。

你可能感兴趣的:(LLM,自然语言处理,大语言模型,LLM,huggingface)