You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Langchain调用HuggingFaceHub加载Google Flan-T5模型遇504超时错误

解决HuggingFaceHub初始化时的504网关超时问题

针对你遇到的HfHubHTTPError: 504 Server Error: Gateway Time-out错误,可尝试以下几种解决方案:

  • 换用更稳定的轻量模型
    google/flan-t5-xl属于大模型,访问量过高时容易触发Hugging Face网关超时。可以先换成同系列的轻量模型验证代码逻辑是否正常,比如:

    repo_id = "google/flan-t5-small"
    

    确认代码能正常运行后,再尝试换回原模型,或者选择其他下载量高、响应稳定的模型。

  • 添加重试机制
    504错误大概率是临时的网关波动,通过重试机制可以规避这类问题。使用tenacity库实现自动重试:

    !pip install huggingface_hub tenacity > /dev/null
    from getpass import getpass
    from tenacity import retry, stop_after_attempt, wait_exponential
    import os
    from langchain import HuggingFaceHub
    
    HUGGINGFACEHUB_API_TOKEN = getpass()
    os.environ["HUGGINGFACEHUB_API_TOKEN"] = HUGGINGFACEHUB_API_TOKEN
    
    @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10))
    def init_llm():
        return HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":0, "max_length":64})
    
    llm = init_llm()
    
  • 排查网络环境

    • 本地运行时:检查是否有代理、防火墙拦截了请求,尝试切换网络(如手机热点)测试。
    • Google Colab运行时:重启当前运行时,更换Colab的网络节点后重试。

内容的提问来源于stack exchange,提问作者r1sh4bh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:47:13