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  # 正确

相关推荐
ZhengEnCi4 小时前
M3-markconv库找不到wkhtmltopdf问题
python
2301_764441337 小时前
LISA时空跃迁分析,地理时空分析
数据结构·python·算法
chushiyunen8 小时前
python rest请求、requests
开发语言·python
cTz6FE7gA8 小时前
Python异步编程:从协程到Asyncio的底层揭秘
python
baidu_huihui8 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip
南 阳8 小时前
Python从入门到精通day63
开发语言·python
lbb 小魔仙8 小时前
Python_RAG知识库问答系统实战指南
开发语言·python
FreakStudio9 小时前
MicroPython LVGL基础知识和概念:底层渲染与性能优化
python·单片机·嵌入式·电子diy
素玥9 小时前
实训5 python连接mysql数据库
数据库·python·mysql