Skip to content

Commit

Permalink
add export button
Browse files Browse the repository at this point in the history
  • Loading branch information
rayrayraykk committed Jan 18, 2024
1 parent 36254e1 commit 9454b44
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions examples/game/app.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# -*- coding: utf-8 -*-
import copy
import sys
from typing import List
import os
import yaml
import datetime

import agentscope

Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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],
Expand All @@ -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, "你")
Expand All @@ -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()

0 comments on commit 9454b44

Please sign in to comment.