利用diffusers配置 Diffusion 模型出现OSError: xxxx does not appear to have a file named config.json.

利用diffusers配置 Diffusion 模型出现OSError: xxxx does not appear to have a file named config.json.

问题描述:

利用 diffusers 调用模型时会用到 huggingface_hub 的相关包,如在下载Tokenizer的时候:

tokenizer = AutoTokenizer.from_pretrained("csebuetnlp/mT5_multilingual_XLSum")

或者下载stable diffusion 模型时:

u*** = U***2DConditionModel.from_pretrained("stabilityai/stable-diffusion-2-1-base")

会出现以下报错:


requests.exceptions.ConnectionError: (MaxRetryError(“HTTPSConnectionPool(host=‘huggingface.co’, port=443): Max retries exceeded with url: /stabilityai/stable-diffusion-2-1-base/resolve/main/u***/config.json (Caused by NewConnectionError(‘<urllib3.connection.HTTPSConnection object at 0x…>: Failed to establish a new connection: [Errno 101] ***work is unreachable’))”), ‘(Request ID: …-…-447a-a301-…)’)

huggingface_hub.errors.LocalEntryNotFoundError: An error happened while trying to locate the file on the Hub and we cannot find the requested files in the local cache. Please check your connection and try again or make sure your Inter*** connection is on.

raise EnvironmentError(
OSError: stabilityai/stable-diffusion-2-1-base does not appear to have a file named config.json.


原因分析:

原因是 HuggingFace的官网在境内无法访问。


解决方案:

博客【出现了HTTPSConnectionPool(host=‘huggingface.co‘, port=443)错误的解决方法】给出了两种解决方案。

  1. 直接下载模型:tokenizer = AutoTokenizer.from_pretrained("模型的本地路径") ,其他模块的from_pretrained均可类比。
  2. 在代码中使用代理:
    import os
    os.environ["http_proxy"] = "http://127.0.0.1:1080"
    os.environ["https_proxy"] = "http://127.0.0.1:1080"
    

最好的方法是利用国内镜像,在代码的开头设置环境变量:

import os
os.environ["HF_ENDPOINT"] = "https://hf-mirror.***"

但是实测发现,os.environ这个语句必须要在import huggingface库相关语句之前执行。
受博客【这可能是全网最好解决中国hugggingface.co无法访问问题】下面的评论启发,为了彻底解决麻烦,直接在huggingface_hub 包里面设置,即在 xxx/.conda/env/xxx/lib/python3.10/site-packages/huggingface_hub/__init__.py 的开头加入这个语句:

import importlib
import os
import sys
from typing import TYPE_CHECKING

os.environ["HF_ENDPOINT"] = "https://hf-mirror.***"

__version__ = "0.27.0"

HuggingFace 缓存路径

此外,通过Hugging Face的相关库自动下载模型,会先缓存在默认路径1
Linux:~/.cache/huggingface/hub
Windows :C:\Users\username\.cache\huggingface\hub

为了修改默认的下载路径,可以采用2

  1. AutoModel.from_pretrained('bert-base-chinese', cache_dir='./放你的路径'),其他模块的from_pretrained均可类比。
  2. 在代码开头设置:os.environ["HF_HOME"] = "./放你的路径"

  1. 不会修改HuggingFace模型下载默认缓存路径?一篇教会你! ↩︎

  2. HuggingFace下载模型默认保存在~/.cache/huggingface下面怎么修改这个路径? - 知乎 ↩︎

转载请说明出处内容投诉
CSS教程网 » 利用diffusers配置 Diffusion 模型出现OSError: xxxx does not appear to have a file named config.json.

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买