OSError: We couldn’t connect to ‘https://hf-mirror.com’ to load this model
https://hf-mirror.com可以连接且本地模型也已经下载但是更换环境后却无法加载模型问题
文章目录
问题描述
尝试使用国内huggingface镜像地址进行本地下载好了离线模型后,但是更换环境后依旧出现代码报错提示:
报错一
OSError: We couldn't connect to 'https://hf-mirror.com' to load this model, couldn't find it in the cached files and it looks like /data0/caochao/pre_model/CogVideoX-5b/ is not the path to a directory
containing a config.json file.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'.
报错二
OSError: We couldn't connect to 'https://huggingface.co' to load this model, couldn't find it in the cached files and it looks like /data0/caochao/pre_model/CogVideoX-5b/ is not the path to a directory
containing a config.json file.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/diffusers/installation#offline-mode'.
本文主要针对报错一进行解决,报错二可参考文章
原因分析
1、代码加载本地模型时,会优先从本地缓存文件中的路径来查找文件,但是当你更换环境以及路径后,会出现找不到的情况。
2、环境变量问题,这个主要是针对从huggingface下载来的模型文件,问题描述如上述二所示,根据huggingface官方地址https://huggingface.co/docs/diffusers/installation#offline-mode.没有设置好环境变量。
原因一 解决方案
方案一
直接找到模型文件中的缓存文件 .cache文件直接删除就好了
例如: 我以CogVideo-5B模型文件内容为例,以下是模型文件的结构
直接将其中的.cache文件删除即可
方案二
1、重新下载指定模型文件在新环境的新路径下,具体操作可以参考国内官方文档:
https://hf-mirror.com/
2、修改cache文件中的路径,具体操作情况可以参考这篇文章:
https://blog.youkuaiyun.com/yyh2508298730/article/details/137773125
原因二 解决方案
环境变量设置
Tips: 对于HF_HUB_OFFLINE变量设置后,如果要下载模型的话要记得改为False或0
linux
如果直接终端临时添加变量
export HF_ENDPOINT=https://hf-mirror.com
export HF_HUB_OFFLINE=True
如果永久添加变量
echo export HF_ENDPOINT=https://hf-mirror.com > ~/.bashrc
echo export HF_HUB_OFFLINE=True > ~/.bashrc
source ~/.bashrc
其中的~/.bashrc是linux的home路径下的隐藏文件,主要负责系统环境变量配置。