From 9454b44303aaae26757427f2cd9e02049ad773db Mon Sep 17 00:00:00 2001 From: "weirui.kwr@alibaba-inc.com" Date: Thu, 18 Jan 2024 11:49:26 +0800 Subject: [PATCH] add export button --- examples/game/app.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/examples/game/app.py b/examples/game/app.py index 412b504c2..2b62f65d8 100644 --- a/examples/game/app.py +++ b/examples/game/app.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- -import copy -import sys from typing import List import os import yaml +import datetime import agentscope @@ -25,6 +24,17 @@ MAX_NUM_DISPLAY_MSG = 20 +def export_chat_history(): + timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + export_filename = f"chat_history_{timestamp}.txt" + + with open(export_filename, "w", encoding="utf-8") as file: + for role, message in glb_history_chat: + file.write(f"{role}: {message}\n") + + return gr.update(value=export_filename, visible=True) + + def get_chat() -> List[List]: """Load the chat info from the queue, and put it into the history @@ -64,6 +74,7 @@ def start_game(): with gr.Row(): chatbot = GroupChat(label="Dialog", show_label=False, height=600) + with gr.Row(): with gr.Column(): user_chat_input = gr.Textbox( @@ -72,10 +83,7 @@ def start_game(): show_label=False, interactive=True, ) - with gr.Column(): - send_button = gr.Button( - value="发送", - ) + user_chat_bot_suggest = gr.Dataset( label="选择一个", components=[user_chat_input], @@ -89,6 +97,16 @@ def start_game(): outputs=[user_chat_input], ) + with gr.Column(): + send_button = gr.Button( + value="发送", + ) + + with gr.Accordion("导出选项", open=False): + with gr.Column(): + export_button = gr.Button("导出完整游戏记录") + export_output = gr.File(label="下载完整游戏记录", visible=False) + def send_message(msg): send_player_input(msg) send_chat_msg(msg, "你") @@ -113,9 +131,11 @@ def update_suggest(): outputs = [chatbot, user_chat_bot_suggest] send_button.click(send_message, user_chat_input, user_chat_input) + export_button.click(export_chat_history, [], export_output) user_chat_input.submit(send_message, user_chat_input, user_chat_input) demo.load(get_chat, inputs=None, outputs=chatbot, every=0.5) demo.load(update_suggest, outputs=user_chat_bot_suggest, every=0.5) demo.load(start_game) + demo.queue() demo.launch()