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

为何首次执行Transformers库Summarization Pipeline代码无正常输出?

Transformers摘要Pipeline首次执行报错、二次执行正常的原因

问题现象

使用Transformers库的summarization pipeline编写文本摘要代码时,出现异常表现:

  • 首次执行代码时,多次输出Error displaying widget错误,无法获取摘要结果
  • 二次执行同一代码时,无报错且能正常输出正确的摘要内容

用户代码

from transformers import pipeline

summarizer = pipeline("summarization")
result = summarizer(
    """
    America has changed dramatically during recent years. Not only has the number of 
    graduates in traditional engineering disciplines such as mechanical, civil, 
    electrical, chemical, and aeronautical engineering declined, but in most of 
    the premier American universities engineering curricula now concentrate on 
    and encourage largely the study of engineering science. As a result, there 
    are declining offerings in engineering subjects dealing with infrastructure, 
    the environment, and related issues, and greater concentration on high 
    technology subjects, largely supporting increasingly complex scientific 
    developments. While the latter is important, it should not be at the expense 
    of more traditional engineering.
"""
)
print(result)

首次执行错误输出

Error displaying widget
Error displaying widget
Error displaying widget
Error displaying widget
Error displaying widget

二次执行正确输出

[{'summary_text': ' America has changed dramatically during recent years . The number of engineering graduates in America has declined in traditional engineering disciplines such as mechanical, civil, electrical, chemical, and aeronautical engineering . Engineering curricula now concentrate on  the study of engineering science and technology . While the latter is important, it should not be at the expense of more traditional engineering .'}]

原因分析

说白了就是Transformers库的交互式可视化组件和你的运行环境不兼容:

  1. 第一次调用pipeline("summarization")时,库会自动加载配套的交互式Widget(专为Jupyter Notebook这类交互式环境设计的交互界面),但如果你的运行环境是普通Python脚本、或者Notebook的前端渲染有问题,这个Widget加载失败就会反复抛出错误。
  2. 但首次报错的同时,模型的下载、初始化过程其实已经在后台完成并缓存到本地了;二次执行时,直接调用本地缓存好的模型,而且不会再触发无效的Widget加载请求,所以代码就能正常运行并输出结果。

解决小技巧

如果想避免这类错误,可以:

  • 直接指定具体模型名初始化pipeline,比如summarizer = pipeline("summarization", model="facebook/bart-large-cnn"),跳过默认的Widget自动加载逻辑
  • 用transformers.utils.logging.set_verbosity_error()屏蔽非关键的日志错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 06:51:16