使用colab快速下载huggingface的大模型文件

写在前面:需要访问colab,要会用colab,会用浏览器开发者工具找token

 

近期在学习大模型的微调与部署应用,需要再huggingface下载预训练好的模型参数文件,这时候问题来了!

huggingface被屏蔽了呀,好不容易解决了访问问题,但是下载也太费流量了,怎么办?这里给两个解决方案。

 

1.使用hf的国内镜像站 HF-Mirror

直接打开就行了

 

2.colab+阿里云盘yyds

首先我们打开https://colab.research.google.com/

第一步,安装必要的库 gradio,aligo,huggingface_hub

!pip install gradio aligo huggingface_hub

a837d303ded643f6be2c803703393fd5.png

第二步,导入包,定义函数,并从huggingface获取文件列表,代码参考了HuggingFace 国内下载 阿里云盘下载速度20MB/s-CSDN博客

import os
import shutil
import huggingface_hub as hh
import pandas as pd

def format_size(bytes, precision=2):
    """
    Convert a file size in bytes to a human-readable format like KB, MB, GB, etc.
    Huggingface use 1000 not 1024
    """
    units = ["B", "KB", "MB", "GB", "TB", "PB"]
    size = float(bytes)
    index = 0

    while size >= 1000 and index < len(units) - 1:
        index += 1
        size /= 1000

    return f"{size:.{precision}f} {units[index]}"


def list_repo_files_info(repo_id,token=None):
    data_ls = []
    for file in list(hh.list_repo_tree(repo_id)):
        data_ls.append([file.path,format_size(file.size)])
    files = [file[0] for file in data_ls]
    data = pd.DataFrame(data_ls,columns = ['文件名','大小'])
    return data, files


def download_file(repo_id,filenames):
    print(filenames)
    repo_name = repo_id.replace("/","---")

    for filename in filenames:
        print(filename)
        out = hh.hf_hub_download(repo_id=repo_id,filename=filename,local_dir=f"./download/{repo_name}",local_dir_use_symlinks=False,force_download =True)
    out_path = f"./download/{repo_name}"
    return out_path

## 设置 repo_id,我这里用的是llama3,根据需要修改
repo_id = "hfl/llama-3-chinese-8b-instruct-v3"
data, filenames = list_repo_files_info(repo_id)
data

repo_id就是模型库的名字,可以直接复制 

a5fe31e503a84a90a273af0149698b8f.png

执行完代码后会出现如下文件列表

25a92ae041e647af8029187ae56336d9.png

第三步,指定需要下载的文件list,执行下载操作,在这里我只下载了几个比较大的参数文件,默认下载路径为/content/download/repo_id,如/content/download/hfl---llama-3-chinese-8b-instruct-v3,在colab的文件夹列表中可以找到

filenames = ["model-00001-of-00004.safetensors",'model-00002-of-00004.safetensors','model-00003-of-00004.safetensors','model-00004-of-00004.safetensors']
out_path = download_file(repo_id,filenames)

执行完上述代码你会看到下载速度快得飞起 

6b0b843f0b3b47708ea4a57d5e7a3ac9.png

第四步,上传文件到阿里云盘!!!有3个参数需要注意,一是refresh_token,二是上传路径(提前创建),三是colab上的文件夹路径,我们直接上传整个文件夹

接下来给大家演示refresh_token怎么获取(google chrome为例)

首先打开阿里云盘并登录,进入开发者工具(F12)或者右键-检查,切换到Application,在storage中找到local storage,找到阿里云盘的缓存文件,并在token中找到refresh_token,复制下来填写到上述代码中即可

1bdc6c77c9f842ba80c3ac05697153d1.png

执行代码 

import aligo
from aligo import Aligo
refresh_token = "替换为你的token"
ali = Aligo(refresh_token=refresh_token)
print(ali.get_user())

remote_folder = ali.get_folder_by_path(path = '阿里云盘的存储路径,如hfl---llama-3-chinese-8b-instruct-v3')
print(remote_folder)

ali.upload_folder(
	'/content/download/hfl---llama-3-chinese-8b-instruct-v3', # colab上的文件夹路径
	parent_file_id=remote_folder.file_id
)

如果阿里云上有该模型参数的缓存,则会提示文件秒传成功

5f816a8fe9ba48d8a88a55084727e76f.png

到这里我们就把文件传到阿里云盘上了

8857936537a6418d99f0edc4b34fbede.png

第五步,接下来我们在本地或者服务器上从阿里云盘下载参数文件即可,在服务器上也可使用python脚本完成,下面是python脚本代码,注意refresh_token与上传时的token用同样的方法获取即可,如果token没有过期,可以直接复用。

import aligo
from aligo import Aligo
refresh_token = "替换为你的refresh_token"
ali = Aligo(refresh_token=refresh_token)
print(ali.get_user())
# 阿里云盘上的文件路径,仅上传单个文件使用
# file = ali.get_file_by_path(path = 'hfl---llama-3-chinese-8b-instruct-v3')

# 阿里云盘上的文件夹路径,仅上传文件夹使用
file = ali.get_folder_by_path(path = 'hfl---llama-3-chinese-8b-instruct-v3')

#下载到本地的文件夹路径
local_folder = './hfl---llama-3-chinese-8b-instruct-v3'
if file.type == 'file':
    ali.download_file(file=file, local_folder=local_folder)
else:
    ali.download_folder(folder_file_id=file.file_id, local_folder=local_folder)

 

 -------------tips-----------------

如果遇到file为none的问题,请检查阿里云盘的文件夹路径是否正确

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值