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