首页 > 解决方案 > BertTokenizer.from_pretrained 出现“连接错误”错误

问题描述

我正在尝试从 Huggingface 下载用于 BERT 的标记器。

我正在执行:

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

错误:

<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 的 repo 中关于 github的类似讨论,我收集到上述调用要下载的文件是: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时,我看到它是由我的 IT 部门签署的,而不是我希望找到的标准 CA 根. 根据这里的讨论,看起来 SSL 代理做这样的事情是合理的。

我的 IT 部门的证书在受信任的机构列表中。但是 requests 似乎没有考虑信任证书的列表。

关于如何让请求信任自签名证书的堆栈溢出讨论中得到启发,我还尝试将 cacert.pem(curl-config --ca 指向的文件)与出现在拥抱脸和将此 pem 的路径添加到 REQUESTS_CA_BUNDLE

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

但这根本没有帮助。

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

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

标签: pythonsslssl-certificatehuggingface-transformers

解决方案


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

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

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

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

  3. 按照此堆栈溢出答案中提到的步骤来获取 CA 捆绑包并在编辑器中打开它,以将文件附加到在上一步中下载的证书。
    一种。原始 CA 捆绑文件在每个证书前都有标题行,说明证书属于哪个 CA 根。对于我们要添加的证书,这不是必需的。我已经这样做了,我猜一个额外的空间,回车等可能导致它更早地对我不起作用。

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

    os.environ['REQUESTS_CA_BUNDLE'] = 'path/cacert.crt'

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

干杯


推荐阅读