拷贝 hugging face 仓库到 colab

发布时间:2024年01月16日
# 挂在谷歌云硬盘
from google.colab import drive
drive.mount('/content/drive')
# 转到文件夹
import os
os.chdir('/content/drive/MyDrive/')
!pwd
# 安装并引入包
!pip install gradio bypy huggingface_hub
import os
import shutil
from huggingface_hub import snapshot_download
from bypy import ByPy
def copy_data(source_directory,target_directory):
  for entry in os.scandir(source_directory):
        if entry.is_dir():
            # 如果是目录,则递归复制子目录
            subdirectory = os.path.join(target_directory, entry.name)
            os.makedirs(subdirectory, exist_ok=True)
            copy_data(entry.path, subdirectory)
        else:
            # 如果是文件,则直接复制到目标目录
            shutil.copy2(entry.path, target_directory)

def download(repo_id):
    source_directory = snapshot_download(repo_id=repo_id, cache_dir="/app/data/", force_download=True)
    print("文件路径:", source_directory)
    path_name = repo_id.replace("/", "---")
    target_directory = f"{os.getcwd()}/download/{path_name}"

    os.makedirs(target_directory, exist_ok=True)  # 创建目标目录,如果目录已存在则不会报错

    copy_data(source_directory, target_directory)
    print("复制路径:", target_directory)

    return target_directory
repo_id = "hfl/chinese-roberta-wwm-ext-large"
out_path = download(repo_id)

repo_id:
在这里插入图片描述

参考:
如何将 huggingface上的模型文件下载到本地
hugggingface_download.ipynb

文章来源:https://blog.csdn.net/weixin_40994552/article/details/135620167
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。