从镜像网站下载huggingface的模型详细教程
问题说明
在服务器上使用huggingface模型时,如果直接指定模型名称,用
AutoTokenizer.from_pretrained(“model_name”)
可能会由于网络原因会报错
Failed to connect to huggingface.co port 443 after 75018 ms: Operation
time out
因此我们需要下载模型到服务器上,得到模型本地的路径model_dir,再通过
AutoTokenizer.from_pretrained(model_dir)
方式来使用。
解决方法
1.访问镜像网站
2.在运行环境安装huggingface_hub库
pip install huggingface_hub
3.把下载器源码复制到项目文件目录
4.在huggingface上找到要下载的模型并复制下载代码
5.在环境中下载
6.配置本地模型
模型装载方法
(1)将模型下载到本地文件夹
(2)AutoModel.from_pretrained的pretrained_model_name_or_path参数指向到本地文件夹
#以THUDM/chatglm-6b模型为例
#model_path = "THUDM/chatglm-6b" #从huggingface.co装载模型的方法
model_path = "./model/" #从本地文件夹装载模型的方法
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
model = AutoModel.from_pretrained(model_path,trust_remote_code=True).half().cuda()