BertTokenizer.from_pretrained 错误并显示“连接错误”

2024-03-25

我正在尝试从 Huggingface 下载 BERT 的分词器。

我正在执行:

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

Error:

<Path>\tokenization_utils_base.py in from_pretrained(cls, pretrained_model_name_or_path, *init_inputs, **kwargs)
   1663                         resume_download=resume_download,
   1664                         local_files_only=local_files_only,
-> 1665                         use_auth_token=use_auth_token,
   1666                     )
   1667 

<Path>\file_utils.py in cached_path(url_or_filename, cache_dir, force_download, proxies, resume_download, user_agent, extract_compressed_file, force_extract, use_auth_token, local_files_only)
   1140             user_agent=user_agent,
   1141             use_auth_token=use_auth_token,
-> 1142             local_files_only=local_files_only,
   1143         )
   1144     elif os.path.exists(url_or_filename):

<Path>\file_utils.py in get_from_cache(url, cache_dir, force_download, proxies, etag_timeout, resume_download, user_agent, use_auth_token, local_files_only)
   1347                 else:
   1348                     raise ValueError(
-> 1349                         "Connection error, and we cannot find the requested files in the cached path."
   1350                         " Please try again or make sure your Internet connection is on."
   1351                     )

ValueError: Connection error, and we cannot find the requested files in the cached path. Please try again or make sure your Internet connection is on.

基于类似的讨论Huggingface 仓库中的 github https://github.com/huggingface/transformers/issues/8690,我推测上述调用想要下载的文件是:https://huggingface.co/bert-base-uncased/resolve/main/config.json https://huggingface.co/bert-base-uncased/resolve/main/config.json

虽然我可以在浏览器上很好地访问该 json 文件,但无法通过请求下载它。 我得到的错误是:

>> import requests as r
>> r.get('https://huggingface.co/bert-base-uncased/resolve/main/config.json')
...
requests.exceptions.SSLError: HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /bert-base-uncased/resolve/main/config.json (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')])")))

在检查页面的证书时 -https://huggingface.co/bert-base-uncased/resolve/main/config.json https://huggingface.co/bert-base-uncased/resolve/main/config.json,我看到它是由我的 IT 部门签署的,而不是我期望找到的标准 CA 根。 基于讨论here https://stackoverflow.com/questions/5846652/can-proxy-change-ssl-certificate,看起来 SSL 代理做这样的事情是合理的。

我的 IT 部门的证书位于受信任的机构列表中。但请求似乎没有考虑信任证书的列表。

从中得到暗示关于如何让请求信任自签名证书的堆栈溢出讨论 https://stackoverflow.com/questions/30405867/how-to-get-python-requests-to-trust-a-self-signed-ssl-certificate我还尝试附加 cacert.pem (curl-config --ca 指向的文件)以及为 Huggingface 显示的根证书,并将该 pem 的路径添加到 REQUESTS_CA_BUNDLE

export REQUESTS_CA_BUNDLE=/mnt/<path>/wsl-anaconda/ssl/cacert.pem

但这根本没有帮助。

您知道如何让请求知道可以信任我的 IT 部门的证书吗?

P.S:如果重要的话,我正在 Windows 上工作,并且在 WSL 中也面临着这个问题。


我最终可以让一切正常工作 - 在这里分享相同的内容,以防将来对其他人有用。

解决方案非常简单,我最初尝试过,但在尝试时犯了一个小错误。无论如何,解决方案如下:

  1. 从浏览器访问 URL(在我的例子中为huggingface.co URL)并访问该网站附带的证书。
    A。在大多数浏览器(chrome/firefox/edge)中,您可以通过单击地址栏中的“锁定”图标来访问它。

  2. 保存所有证书 - 一直到根证书。
    A。我认为,从技术上讲,您可以只保存根证书,它仍然可以工作,但我还没有尝试过。如果我有时间尝试一下,我可能会更新这个。如果您碰巧在我之前尝试过,请发表评论。

  3. 按照中提到的步骤进行操作这个堆栈溢出答案 https://stackoverflow.com/questions/39356413/how-to-add-a-custom-ca-root-certificate-to-the-ca-store-used-by-pip-in-windows/52961564#52961564获取 CA 捆绑包并在编辑器中将其打开,以将上一步中下载的证书附加到文件中。
    A。原始 CA 捆绑文件在每个证书之前都有标题行,提及该证书属于哪个 CA 根。我们想要添加的证书不需要这样做。我已经这样做了,我猜想额外的空格、回车符等可能导致它之前对我不起作用。

  4. 在我的 python 程序中,我更新了环境变量以指向更新的 CA 根包

    os.environ['REQUESTS_CA_BUNDLE'] = '路径/cacert.crt'

人们可能会认为,因为大多数 python 包使用“requests”来进行此类 GET 调用,而“requests”使用“certifi”包指向的证书。那么,为什么不找到 certifi 指向的证书的位置并更新它。 它的问题是 - 每当您使用 conda 更新软件包时, certifi 也可能会更新,从而导致您的更改被冲走。因此,我发现动态更新环境变量是一个更好的选择。

Cheers

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BertTokenizer.from_pretrained 错误并显示“连接错误” 的相关文章

随机推荐