Skip to content

Commit

Permalink
Merge pull request #31 from ZiTao-Li/dev_weirui
Browse files Browse the repository at this point in the history
System window
  • Loading branch information
ZiTao-Li authored Jan 22, 2024
2 parents eeeb343 + c257121 commit e087377
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
68 changes: 58 additions & 10 deletions examples/game/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def check_uuid(uid):
glb_history_dict = defaultdict(init_uid_list)
glb_signed_user = []
is_init = False
MAX_NUM_DISPLAY_MSG = 20
MAX_NUM_DISPLAY_MSG = 30


# 图片本地路径转换为 base64 格式
Expand Down Expand Up @@ -88,7 +88,7 @@ def export_chat_history(uid):
return gr.update(value=export_filename, visible=True)


def get_chat(uid) -> List[List]:
def get_dial_chat(uid) -> List[List]:
"""Load the chat info from the queue, and put it into the history
Returns:
Expand All @@ -100,7 +100,32 @@ def get_chat(uid) -> List[List]:
line = get_chat_msg(uid=uid)
if line is not None:
glb_history_dict[uid] += [line]
return glb_history_dict[uid][-MAX_NUM_DISPLAY_MSG:]

dial_msg = []
for line in glb_history_dict[uid]:
_, msg = line
if isinstance(msg, dict):
if "【系统】" not in msg.get("text", ""):
dial_msg.append(line)
else:
# User chat, format: (msg, None)
dial_msg.append(line)

return dial_msg[-MAX_NUM_DISPLAY_MSG:]


def get_sys_chat(uid) -> List[List]:
uid = check_uuid(uid)
global glb_history_dict

sys_msg = []
for line in glb_history_dict[uid]:
_, msg = line
if isinstance(msg, dict):
if "【系统】" in msg.get("text", ""):
sys_msg.append(line)

return sys_msg[-MAX_NUM_DISPLAY_MSG:]


def fn_choice(data: gr.EventData, uid):
Expand Down Expand Up @@ -156,12 +181,23 @@ def start_game(uid):
}

user_chat_bot_cover = gr.HTML(format_cover_html(welcome))
chatbot = mgr.Chatbot(
label="Dialog",
show_label=False,
height=600,
visible=False,
)
with gr.Row():
chatbot = mgr.Chatbot(
label="Dialog",
show_label=False,
height=500,
visible=False,
bubble_full_width=False,
)

chatsys = mgr.Chatbot(
label="系统栏",
show_label=True,
height=500,
visible=False,
bubble_full_width=False,
layout="panel",
)

with gr.Row():
with gr.Column():
Expand Down Expand Up @@ -221,6 +257,7 @@ def game_ui():
invisible = False
return {
chatbot: mgr.Chatbot(visible=visible),
chatsys: mgr.Chatbot(visible=visible),
user_chat_input: gr.Text(visible=visible),
send_button: gr.Button(visible=visible),
new_button: gr.Button(visible=invisible),
Expand All @@ -235,6 +272,7 @@ def welcome_ui():
invisible = False
return {
chatbot: mgr.Chatbot(visible=invisible),
chatsys: mgr.Chatbot(visible=invisible),
user_chat_input: gr.Text(visible=invisible),
send_button: gr.Button(visible=invisible),
new_button: gr.Button(visible=visible),
Expand All @@ -246,6 +284,7 @@ def welcome_ui():

outputs = [
chatbot,
chatsys,
user_chat_input,
send_button,
new_button,
Expand All @@ -268,6 +307,7 @@ def welcome_ui():
)

chatbot.custom(fn=fn_choice, inputs=[uuid])
chatsys.custom(fn=fn_choice, inputs=[uuid])

# change ui
new_button.click(game_ui, outputs=outputs)
Expand All @@ -284,7 +324,15 @@ def welcome_ui():
# update chat history
demo.load(init_game)
demo.load(check_for_new_session, inputs=[uuid], every=0.1)
demo.load(get_chat, inputs=[uuid], outputs=chatbot, every=0.5)

demo.load(get_dial_chat,
inputs=[uuid],
outputs=chatbot,
every=0.5)
demo.load(get_sys_chat,
inputs=[uuid],
outputs=chatsys,
every=0.5,)

demo.queue()
demo.launch()
2 changes: 1 addition & 1 deletion examples/game/ruled_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def cook(self):
),
]

choose_ingredient = f"""请选择需要的食材: <select-box shape="card"
choose_ingredient = f"""【系统】请选择需要的食材: <select-box shape="card"
type="checkbox" item-width="auto"
options='{json.dumps(ingredients_list)}' select-once
submit-text="确定"></select-box>"""
Expand Down

0 comments on commit e087377

Please sign in to comment.