背景
在内网环境运行 Python 代码时,涉及下载模型常会遇到报错,例如 urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='bj.bcebos.com', port=443): Max retries exceeded with url。
解决办法
可以通过设置环境变量或配置 requests 代理来解决。
1. 测试代理连接
import os
import requests
proxies = {
"http": "http://xxxx:[email protected]:8080",
"https": "http://xxxx:[email protected]:8080"
}
try:
# 测试能否连接模型服务器
response = requests.get("https://bj.bcebos.com", proxies=proxies, timeout=3000)
print("代理连接成功,状态码:", response.status_code)
except Exception as e:
print("代理连接失败:", e)
2. 设置环境变量并加载模型
import os
from paddlenlp import Taskflow
# 设置代理环境变量
os.environ["HTTP_PROXY"] = "http://xxxx:[email protected]:8080"
os.environ["HTTPS_PROXY"] = "http://xxxx:[email protected]:8080"
# 加载模型任务
corrector = Taskflow("text_correction", timeout=3000)
print(corrector("今天天气真很好"))

