Mon*_*oni 4 python ssl ssl-certificate huggingface-transformers
我正在尝试从 Huggingface 下载 BERT 的分词器。
我正在执行:
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
Run Code Online (Sandbox Code Playgroud)
错误:
<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.
Run Code Online (Sandbox Code Playgroud)
根据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')])")))
Run Code Online (Sandbox Code Playgroud)
在检查页面的证书 - https://huggingface.co/bert-base-uncased/resolve/main/config.json时,我发现它是由我的 IT 部门签名的,而不是我期望找到的标准 CA 根。根据此处的讨论,SSL 代理似乎可以做这样的事情。
我的 IT 部门的证书位于受信任的机构列表中。但请求似乎没有考虑信任证书的列表。
从关于如何让请求信任自签名证书的堆栈溢出讨论中得到启发,我还尝试附加 cacert.pem (curl-config --ca 指向的文件)以及为 Huggingface 出现的 ROOT 证书,将此 pem 的路径添加到 REQUESTS_CA_BUNDLE
export REQUESTS_CA_BUNDLE=/mnt/<path>/wsl-anaconda/ssl/cacert.pem
Run Code Online (Sandbox Code Playgroud)
但这根本没有帮助。
您知道如何让请求知道可以信任我的 IT 部门的证书吗?
PS:如果重要的话,我正在 Windows 上工作,并且在 WSL 中也面临着这个问题。
我最终可以让一切正常工作 - 在这里分享相同的内容,以防将来对其他人有用。
解决方案非常简单,我最初尝试过,但在尝试时犯了一个小错误。无论如何,解决方案如下:
从浏览器访问 URL(在我的例子中为huggingface.co URL)并访问该网站附带的证书。
A。在大多数浏览器(chrome/firefox/edge)中,您可以通过单击地址栏中的“锁定”图标来访问它。
保存所有证书 - 一直到根证书。
A。我认为,从技术上讲,您可以只保存根证书,它仍然可以工作,但我还没有尝试过。如果我有时间尝试一下,我可能会更新这个。如果您碰巧在我之前尝试过,请发表评论。
按照此堆栈溢出答案中提到的步骤获取 CA 捆绑包并在编辑器中将其打开,以将上一步中下载的证书附加到文件中。
A。原始 CA 捆绑文件在每个证书之前都有标题行,提及该证书属于哪个 CA 根。我们想要添加的证书不需要这样做。我已经这样做了,我猜想额外的空格、回车符等可能导致它之前对我不起作用。
在我的 python 程序中,我更新了环境变量以指向更新的 CA 根包
os.environ['REQUESTS_CA_BUNDLE'] = '路径/cacert.crt'
人们可能会认为,因为大多数 python 包使用“requests”来进行此类 GET 调用,而“requests”使用“certifi”包指向的证书。那么,为什么不找到 certifi 指向的证书的位置并更新它。它的问题是 - 每当您使用 conda 更新包时, certifi 也可能会更新,从而导致您的更改被冲走。因此,我发现动态更新环境变量是一个更好的选择。
干杯
| 归档时间: |
|
| 查看次数: |
11341 次 |
| 最近记录: |