from transformers import pipeline
import os
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
classifier = pipeline("sentiment-analysis")
classifier(
[
"I've been waiting for a HuggingFace course my whole life.",
"I hate this so much!",
]
)
提示报错:明明设置了镜像源,为什么还是无法连接到 HuggingFace?
OSError: We couldn't connect to 'https://huggingface.co' to load the files, and couldn't find them in the cached files.
Check your internet connection or see how to run the library in offline mode at 'https://huggingface.co/docs/transformers/installation#offline-mode'.
深度解析
在 Python 中,模块导入机制是一个复杂的过程,涉及到多个层次的查找和加载。HuggingFace 的 Transformers 库在导入时会尝试连接到默认的 HuggingFace 服务器以获取模型和配置文件。如果设置了环境变量 HF_ENDPOINT
,理论上应该能够重定向到指定的镜像源。 然而,问题可能出在以下几个方面:
环境变量设置不正确:确保在运行代码之前,环境变量
HF_ENDPOINT
已经被正确设置。可以通过打印该变量来验证。print(os.environ.get('HF_ENDPOINT'))
如果返回的是 None,则需要设置该环境变量。
Transformers 版本问题:某些版本的 Transformers 库可能不支持通过
HF_ENDPOINT
环境变量来重定向请求。可以尝试升级到最新版本:pip install --upgrade transformers
网络连接问题:即使设置了镜像源,如果网络连接不稳定或被防火墙阻止,也可能导致无法连接到指定的镜像源。可以尝试在浏览器中访问
https://hf-mirror.com
来验证是否能够正常访问。缓存问题:Transformers 库会缓存下载的模型和配置文件。如果之前尝试连接 HuggingFace 服务器失败,可能会导致缓存中没有有效的文件。可以尝试清除缓存:
transformers-cli cache clear
代码执行顺序:确保在导入
pipeline
之前设置了环境变量。Python 的模块导入机制会在导入时解析环境变量,如果在导入之后才设置,可能不会生效。import os os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com' from transformers import pipeline