【报错解决】‘HTTPSConnectionPool(host=‘huggingface.co‘, port=443)/ConnectTimeoutError(<urllib3.connection. - checkpoint,
报错:'HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /tokenizer_config.json (Caused by ConnectTimeoutError(, 'Connection to huggingface.co timed out. (connect timeout=10)'))' thrown while requesting HEAD https://huggingface.co/tokenizer_config.json 'HTTPSConnectionPool(host='huggingface.co', port=443): Max retries exceeded with url: /mengzi-t5-base/resolve/main/config.json (Caused by ConnectTimeoutError(, 'Connection to huggingface.co timed out. (connect timeout=10)'))' thrown while requesting HEAD https://huggingface.co/main/config.json
错误原因:在尝试从 Hugging Face 服务器下载 mengzi-t5-base 模型的分词器(Tokenizer)和配置文件时,由于网络连接超时而失败了
解决方案:
方法一:挂好科学上网方法二:使用国内镜像源
修改代码,通过国内镜像站加载模型:
from transformers import AutoTokenizer, AutoModel
# 使用清华镜像源
checkpoint = "mengzi-t5-base"#改成你的模型名称
tokenizer = AutoTokenizer.from_pretrained(
checkpoint,
cache_dir="./models", # 指定缓存目录
mirror="https://mirror.tuna.tsinghua.edu.cn/hugging-face" # 国内镜像
)
方法三:手动下载模型文件
Hugging Face 官网下载:“https://huggingface.co/你的模型名称”
下载以下文件到本地目录(如 ./models/mengzi-t5-base):tokenizer.json、tokenizer_config.json、config.json、pytorch_model.bin(如果需要模型权重)
从本地加载 拼多多帮助力入口
tokenizer = AutoTokenizer.from_pretrained("./models/mengzi-t5-base") # 本地路径
测试:验证网络连接,在 Python 中测试是否能访问 Hugging Face:
import requests
try:
response = requests.get("https://huggingface.co", timeout=5)
print("网络正常" if response.status_code == 200 else "访问受限")
except Exception as e:
print(f"网络错误: {e}")