如何保存gradio.Chatbot的聊天记录

问题描述 投票:0回答:1

我有这个数据:

data = [
    {"human1": "i am human", "human2": None},
    {"human1": None, "human2": "i can help"},
    {"human1": None, "human2": "i can do many things"},
    {"human1": "give me an example", "human2": None}
]
with open('data.yaml', 'w') as file:
    yaml.dump(data, file)

使用 gradio.Chatbot 存储聊天历史记录。这是代码:

def send_message(msg, history):
    time.sleep(1)

    data.append({"human1": msg, "human2": None})
    with open('data.yaml', 'w') as file:
        yaml.dump(data, file)

    return "", history + [[msg, None]]

with gr.Blocks() as demo:
    with open('data.yaml', 'r') as file:
        data = yaml.load(file, Loader=yaml.FullLoader)
    history = gr.Chatbot([(item.get("human1"), item.get("human2")) for item in data])
    msg = gr.Textbox()

    msg.submit(send_message, [msg, history], [msg, history])

demo.launch()

问题是,每次我打开新的浏览器选项卡时,历史记录都会消失,但文件 data.yaml 正确存储对话。

有什么想法吗?

python chatbot gradio
1个回答
0
投票

当你刷新页面时,历史记录也会消失,对吗? 我也在寻找一种方法如何在刷新后再次显示历史记录。

© www.soinside.com 2019 - 2024. All rights reserved.