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

相关推荐
ZTLJQ4 小时前
序列化的艺术:Python JSON处理完全解析
开发语言·python·json
H5css�海秀5 小时前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
阿贵---5 小时前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
无敌昊哥战神5 小时前
【LeetCode 257】二叉树的所有路径(回溯法/深度优先遍历)- Python/C/C++详细题解
c语言·c++·python·leetcode·深度优先
李昊哲小课7 小时前
第1章-PySide6 基础认知与环境配置
python·pyqt·pyside
2401_894241927 小时前
用Pygame开发你的第一个小游戏
jvm·数据库·python
Zzzz_my8 小时前
正则表达式(RE)
pytorch·python·正则表达式
天天鸭9 小时前
前端仔写了个 AI Agent,才发现大模型只干了 10% 的活
前端·python·ai编程
setmoon2149 小时前
使用Scikit-learn构建你的第一个机器学习模型
jvm·数据库·python
2401_8331977310 小时前
为你的Python脚本添加图形界面(GUI)
jvm·数据库·python