BUG: gradio RuntimeError: async generator raised StopAsyncIteration

BUG: gradio RuntimeError: async generator raised StopAsyncIteration

环境

python 复制代码
gradio                        4.20.0

详情

在使用gradio编写大模型可视化demo的时候,大模型正常输出,但gradio弹出此错误。

经过排除,发现是返回方式的问题,gradio传输信息给web网页的时候,我使用了return,但实际上应该 使用yield

解决方法

return改为yield

例子

python 复制代码
# 旧
def generate(message: str) -> Iterator[str]:
    stream = False
    response = "你好"
    if stream:
        for _ in range(5):
		        response += response
            yield response
    else:
        return response  # 错误

# 新
def generate(message: str) -> Iterator[str]:
    stream = False
    response = "你好"
    if stream:
        for _ in range(5):
		        response += response
            yield response
    else:
        yield response  # 正确

相关推荐
smj2302_796826522 小时前
解决leetcode第3989题网格中保持一致的最大列数
python·算法·leetcode
吴梓穆3 小时前
Python 基础 正则表达式
python
巧克力男孩dd3 小时前
Python超典型练习题(第一次作业)
开发语言·python·算法
闲猫4 小时前
LangChain / Core components / Models
开发语言·python·langchain
cui_ruicheng6 小时前
Python从入门到实战(十三):模块、包与环境管理
开发语言·python
李宸净7 小时前
Web自动化测试selenium+python
前端·python·selenium
而后笑面对7 小时前
生信bwa算法
python
不知名的老吴7 小时前
思考:要做到Bug Free需培养怎样的思维?
bug
至乐活着8 小时前
深入Python内存管理与垃圾回收:从引用计数到分代回收的实战解析
python·性能优化·内存管理·垃圾回收·引用计数
m0_547486669 小时前
《Python数据分析与挖掘》全套PPT课件
python·数据挖掘·数据分析