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

相关推荐
从小就看凹凸曼^o^11 分钟前
Python基础5 - 字典与集合:(1)字典
开发语言·python
小赵AI手记26 分钟前
技术拆解(十七)具身智能:机器人动作生成为何走向Diffusion Policy?
人工智能·笔记·python·机器人
本地化文档36 分钟前
poethepoet-docs-l10n
python·github·gitcode·sphinx
baopixiaoz1 小时前
AI量化策略师|Web3 量化交易研究员
大数据·人工智能·python·区块链
Uncommon.1 小时前
使用Pytorch自动计算梯度
人工智能·pytorch·python
阿标的博客2 小时前
Python实训案例(二):输出植物证书
python
鸿芯微控科技2 小时前
压电点胶阀出胶量不稳定怎么排查?驱动波形、背压、点胶重量与Python分析
开发语言·python·压电点胶阀·精密点胶·点胶重量·流体控制
一位正在转型AI全栈的前端工程师2 小时前
从 0 到 1 搭建生产级 RAG 系统:切片、召回与重排序调优实录
python·前端工程化
糖炒栗子03262 小时前
learn-claude-code 简要记录
笔记·python
Logintern092 小时前
asyncio里面的await理解
开发语言·python