elfsong commited on
Commit
fac1a49
·
1 Parent(s): 2b717f1

refactor: Enhance chatbot UI by updating comments, fixing clear logic, and ensuring message handling is consistent across models.

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -14,17 +14,15 @@ def bot_response(user_message, history, model_name, system_message, max_tokens,
14
  token = oauth_token.token if oauth_token else None
15
  client = InferenceClient(token=token, model=model_name)
16
 
17
- # 1. 构造发送给 API 的消息序列
18
- # 注意:history 在 Gradio 6 中已经是 [{"role": "...", "content": "..."}] 格式
19
  api_messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": user_message}]
20
 
21
- # 2. 更新 UI 的历史记录
22
- # 先添加用户的消息,再添加一个空的助手消息用于流式占位
23
  new_history = history + [
24
  {"role": "user", "content": user_message},
25
  {"role": "assistant", "content": ""}
26
  ]
27
- yield new_history, "" # 立即更新 UI 显示用户消息
28
 
29
  try:
30
  response_text = ""
@@ -39,7 +37,6 @@ def bot_response(user_message, history, model_name, system_message, max_tokens,
39
  for chunk in stream:
40
  token_content = chunk.choices[0].delta.content or ""
41
  response_text += token_content
42
- # 更新最后一条消息的内容
43
  new_history[-1]["content"] = response_text
44
  yield new_history, ""
45
 
@@ -59,33 +56,41 @@ with gr.Blocks() as demo:
59
  gr.Markdown("# ⚔️ 独立输入 Chatbot Arena")
60
 
61
  with gr.Row():
62
- # --- 模型 A ---
63
  with gr.Column():
64
  model_a_name = gr.Dropdown(MODELS, label="选择模型 A", value=MODELS[0])
65
- chatbot_a = gr.Chatbot(label="Model A 聊天窗口")
 
66
  msg_a = gr.Textbox(placeholder="给模型 A 发送消息...", label="模型 A 输入")
67
  btn_a = gr.Button("发送给 A")
68
 
69
- # --- 模型 B ---
70
  with gr.Column():
71
  model_b_name = gr.Dropdown(MODELS, label="选择模型 B", value=MODELS[1])
72
- chatbot_b = gr.Chatbot(label="Model B 聊天窗口")
 
73
  msg_b = gr.Textbox(placeholder="给模型 B 发送消息...", label="模型 B 输入")
74
  btn_b = gr.Button("发送给 B")
75
 
76
- # --- 绑定事件 ---
77
 
78
- # 模型 A 的发送逻辑
79
  a_inputs = [msg_a, chatbot_a, model_a_name, system_msg, max_t, temp, top_p_val]
80
  msg_a.submit(bot_response, a_inputs, [chatbot_a, msg_a])
81
  btn_a.click(bot_response, a_inputs, [chatbot_a, msg_a])
82
 
83
- # 模型 B 的发送逻辑
84
  b_inputs = [msg_b, chatbot_b, model_b_name, system_msg, max_t, temp, top_p_val]
85
  msg_b.submit(bot_response, b_inputs, [chatbot_b, msg_b])
86
  btn_b.click(bot_response, b_inputs, [chatbot_b, msg_b])
87
 
88
- gr.Button("🗑️ 清空所有对话").click(lambda: ([], []), None, [chatbot_a, chatbot_b])
 
 
 
 
 
 
 
 
89
 
90
  if __name__ == "__main__":
91
  demo.launch()
 
14
  token = oauth_token.token if oauth_token else None
15
  client = InferenceClient(token=token, model=model_name)
16
 
17
+ # 1. Construct messages for API
 
18
  api_messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": user_message}]
19
 
20
+ # 2. Update UI history (Gradio 5/6 format)
 
21
  new_history = history + [
22
  {"role": "user", "content": user_message},
23
  {"role": "assistant", "content": ""}
24
  ]
25
+ yield new_history, ""
26
 
27
  try:
28
  response_text = ""
 
37
  for chunk in stream:
38
  token_content = chunk.choices[0].delta.content or ""
39
  response_text += token_content
 
40
  new_history[-1]["content"] = response_text
41
  yield new_history, ""
42
 
 
56
  gr.Markdown("# ⚔️ 独立输入 Chatbot Arena")
57
 
58
  with gr.Row():
59
+ # --- Model A ---
60
  with gr.Column():
61
  model_a_name = gr.Dropdown(MODELS, label="选择模型 A", value=MODELS[0])
62
+ # Explicitly set type="messages"
63
+ chatbot_a = gr.Chatbot(label="Model A 聊天窗口", type="messages")
64
  msg_a = gr.Textbox(placeholder="给模型 A 发送消息...", label="模型 A 输入")
65
  btn_a = gr.Button("发送给 A")
66
 
67
+ # --- Model B ---
68
  with gr.Column():
69
  model_b_name = gr.Dropdown(MODELS, label="选择模型 B", value=MODELS[1])
70
+ # Explicitly set type="messages"
71
+ chatbot_b = gr.Chatbot(label="Model B 聊天窗口", type="messages")
72
  msg_b = gr.Textbox(placeholder="给模型 B 发送消息...", label="模型 B 输入")
73
  btn_b = gr.Button("发送给 B")
74
 
75
+ # --- Bind Events ---
76
 
 
77
  a_inputs = [msg_a, chatbot_a, model_a_name, system_msg, max_t, temp, top_p_val]
78
  msg_a.submit(bot_response, a_inputs, [chatbot_a, msg_a])
79
  btn_a.click(bot_response, a_inputs, [chatbot_a, msg_a])
80
 
 
81
  b_inputs = [msg_b, chatbot_b, model_b_name, system_msg, max_t, temp, top_p_val]
82
  msg_b.submit(bot_response, b_inputs, [chatbot_b, msg_b])
83
  btn_b.click(bot_response, b_inputs, [chatbot_b, msg_b])
84
 
85
+ # Fix: Clear logic returning empty lists for both chatbots
86
+ def clear_chats():
87
+ return [], []
88
+
89
+ gr.Button("🗑️ 清空所有对话").click(
90
+ fn=clear_chats,
91
+ inputs=None,
92
+ outputs=[chatbot_a, chatbot_b]
93
+ )
94
 
95
  if __name__ == "__main__":
96
  demo.launch()