OSError: We couldn‘t connect to ‘https://huggingface.co‘ to load this file, couldn‘t find it in the

OSError: We couldn't connect to 'https://huggingface.co' to load this file, couldn't find it in the cached files and it looks like roberta-base is not the path to a directory containing a file named config.json.
Checkout your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.

这个错误是由于 无法连接 Hugging Face 服务器来下载 roberta-base 预训练模型的 config.json 文件,导致 AutoConfig.from_pretrained(“roberta-base”) 失败。

检查代理设置

网络可能无法直接访问 Hugging Face。可以手动设置代理:

$env:http_proxy="http://127.0.0.1:7890"
$env:https_proxy="http://127.0.0.1:7890"

或者在 pip 和 requests 级别设置代理:

import os
os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"

然后 重新运行 Python 代码,看看是否可以成功下载 roberta-base。

手动下载模型

如果无法访问 Hugging Face,可以手动下载模型,然后从本地加载:

1.访问 Hugging Face 模型页面:

https://huggingface.co/roberta-base

2.下载 roberta-base 目录下的所有文件:

config.json

pytorch_model.bin

tokenizer.json

tokenizer_config.json

vocab.json

merges.txt

3.放到本地某个目录(例如 C:/models/roberta-base)

4.修改代码,加载本地模型:

from transformers import AutoConfig

config = AutoConfig.from_pretrained("C:/models/roberta-base")

这样就不需要联网下载 Hugging Face 服务器的模型。

使用 Hugging Face 国内镜像

如果仍然希望联网下载,可以尝试使用 清华镜像:

from transformers.utils import cached_path

cached_path(
    "https://huggingface.co/roberta-base/resolve/main/config.json",
    cache_dir="C:/models/huggingface"
)

然后修改 AutoConfig.from_pretrained("C:/models/huggingface") 来加载缓存的文件。

离线模式(Hugging Face)

如果已经下载了 roberta-base,但仍然遇到 Hugging Face 服务器访问问题,可以开启 离线模式:

from transformers.utils.hub import offline_mode

offline_mode()

然后在本地加载模型。

你可能感兴趣的:(NLP,AI,DeepLearning,deep,learning,NLP)