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

相关推荐
咖啡の猫10 分钟前
Python中的输出函数
开发语言·数据库·python
朱一头zcy41 分钟前
下载安装pycharm 并通过pycahrm来驱动Anaconda来运行Python程序
ide·python·pycharm
CodeLongBear1 小时前
Python数据分析 -- Pandas基础入门学习笔记:从核心概念到实操代码
python·conda·pandas
B站_计算机毕业设计之家1 小时前
python手写数字识别计分系统+CNN模型+YOLOv5模型 深度学习 计算机毕业设计(建议收藏)✅
python·深度学习·yolo·计算机视觉·数据分析·cnn
咖啡の猫2 小时前
二进制与字符编码
python
Tech_Lin2 小时前
手搓工具之手写签字识别提取工具
python
Amber_372 小时前
php的数组和python的列表 -- 横向对比学习
python·学习·php
中文Python3 小时前
小白中文Python-双色球LSTM模型出号程序
开发语言·人工智能·python·lstm·中文python·小白学python
superbadguy3 小时前
用curl实现Ollama API流式调用
人工智能·python
嚴 帅3 小时前
Pytnon入门学习(一)
python