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

相关推荐
全栈练习生16 分钟前
大模型推理全链路
python·ai
泡海椒36 分钟前
JQuick-Excel VALIDATION 实战:用作用范围约束 Excel 导入校验
开发语言·python·excel
小静AI工程实验室2 小时前
Python 爬虫解析 JSON-LD:多块 script、@graph 与坏数据的 9 个边界
爬虫·python·json
jason.zeng@15022072 小时前
(八)现有架构上新增一个通用Excel导出工具
python·架构·langchain·excel·llama
袁袁袁袁满2 小时前
AI Agent 如何“看懂“互联网?
爬虫·python·自动化·爬虫实战·多线程爬虫
泡海椒2 小时前
JQuick-Excel 实战:用 VALIDATION 建立可维护的 Excel 导入校验
开发语言·python·excel
Duang007_3 小时前
生产可观测性:从“系统慢“到“根因“的完整链路(Go / TypeScript)
后端·python·golang·typescript·prometheus
jason.zeng@15022073 小时前
(九)多轮对话式新增维修记录实现方案
python·prompt·交互·llama
黑妹天下第一乖3 小时前
小智改造实战解读-首 token 延迟去哪了:云端与端侧大模型的分段对照
开发语言·人工智能·python·嵌入式硬件·自然语言处理·iot
坊钰3 小时前
【LangChain框架入门级】10. 文本向量与向量数据库(Embedding / Redis / Pinecone / MMR)
数据库·python·langchain·embedding