由于 HuggingFace 网络访问比较慢,国内通常会使用魔搭下载模型,如果魔搭上还没有,需要从 HuggingFace 准存一下,本文将通过 Colab + AliyunPan 的方式下载模型并进行转存。
登录Colab 并运行一下命令
安装依赖包,Huggingface 和云盘
# 安装需要的包
!pip install huggingface_hub aligo
下载模型
import huggingface_hub as hhrepo_id = "TencentBAC/Conan-embedding-v1"
revision_list = hh.list_repo_refs(repo_id)
ref = revision_list.branches[0].ref
hh.snapshot_download(repo_id=repo_id, revision=ref, local_dir="./download")
登录阿里云盘
# 上传阿里云盘,填上token
from aligo import Aligo
ali = Aligo()# 获取用户信息和获取网盘根目录文件列表
user = ali.get_user()
print(user.user_name, user.nick_name, user.phone)
ll = ali.get_file_list()
上传目录
outpath="/content/download"
remote_folder = ali.get_folder_by_path("model100", create_folder=True)
ali.upload_folder(outpath, remote_folder.file_id)
文件成功上传
上传 ModelScope
在 ModelScope 上启动 Notebook
进入 Notebook 安装依赖
!pip install aligo
登录云盘,会出现二维码,扫码登录
from aligo import Aligo
ali = Aligo()
下载模型,设置云盘的目录位置,指定目标路径并下载
file = ali.get_folder_by_path('model100/download')ali.download_folder(folder_file_id=file.file_id, local_folder="/tmp")
上传到魔搭,替换为自己的 Token,设置模型 Id 为自己创建的模型的 Id,甚至本地路径。
from modelscope.hub.api import HubApi
local_dir = "/tmp/download"
'''
魔搭需要一个配置文件,否则上传失败,这里创建一个空文件
'''
!touch {local_dir+"/configuration.json"}
YOUR_ACCESS_TOKEN = '9ec19501-230a-4749-9909-b093e5466e74'
api = HubApi()
api.login(YOUR_ACCESS_TOKEN)
api.push_model(model_id="model1001/Conan",model_dir=local_dir
)
修改配置文件内容,model 的配置信息直接从 HuggingFace config.json 文件中拷贝即可。
{"framework": "pytorch","task": "sentence-embedding","model": {"architectures": ["BertModel"],"attention_probs_dropout_prob": 0.1,"classifier_dropout": null,"directionality": "bidi","gradient_checkpointing": false,"hidden_act": "gelu","hidden_dropout_prob": 0.1,"hidden_size": 1024,"initializer_range": 0.02,"intermediate_size": 4096,"layer_norm_eps": 1e-12,"max_position_embeddings": 512,"model_type": "bert","num_attention_heads": 16,"num_hidden_layers": 24,"pad_token_id": 0,"pooler_fc_size": 768,"pooler_num_attention_heads": 12,"pooler_num_fc_layers": 3,"pooler_size_per_head": 128,"pooler_type": "first_token_transform","position_embedding_type": "absolute","torch_dtype": "float32","transformers_version": "4.36.2","type_vocab_size": 2,"use_cache": true,"vocab_size": 21128},"pipeline": {"type": "sentence-embedding"}
}
上传成功后,会进入审核进度,审核很快可以完成。
测试上传的模型
from modelscope.models import Model
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasksmodel_id = "model1001/Conan"
pipeline_se = pipeline(Tasks.sentence_embedding,model=model_id,sequence_length=512) # 当输入包含“soure_sentence”与“sentences_to_compare”时,会输出source_sentence中首个句子与sentences_to_compare中每个句子的向量表示,以及source_sentence中首个句子与sentences_to_compare中每个句子的相似度。
inputs = {"source_sentence": ["吃完海鲜可以喝牛奶吗?"],"sentences_to_compare": ["不可以,早晨喝牛奶不科学","吃了海鲜后是不能再喝牛奶的,因为牛奶中含得有维生素C,如果海鲜喝牛奶一起服用会对人体造成一定的伤害","吃海鲜是不能同时喝牛奶吃水果,这个至少间隔6小时以上才可以。","吃海鲜是不可以吃柠檬的因为其中的维生素C会和海鲜中的矿物质形成砷"]}result = pipeline_se(input=inputs)
print (result)
总结
HuggingFace 模型转到 ModelScope 基本上就是直接的文件拷贝,上传前需要将模型配置稍微调整一下。