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

refactor: Simplify bot_response function by improving message handling and UI updates for user interactions.

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -8,31 +8,28 @@ MODELS = [
8
 
9
  def bot_response(user_message, history, model_name, system_message, max_tokens, temperature, top_p, oauth_token: gr.OAuthToken | None):
10
  if not user_message:
11
- yield history
12
  return
13
 
14
- # 获取 Token (如果是本地运行,请确保已登录 HF)
15
  token = oauth_token.token if oauth_token else None
16
- if not token:
17
- history.append({"role": "assistant", "content": "⚠️ 请先在侧边栏登录 Hugging Face。"})
18
- yield history
19
- return
20
-
21
- # 初始化客户端
22
  client = InferenceClient(token=token, model=model_name)
23
 
24
- # 构建消息列表:系统提示词 + 历史记录 + 当前用户输入
25
- messages = [{"role": "system", "content": system_message}] + history + [{"role": "user", "content": user_message}]
 
26
 
27
- # UI 更新:先在界面显示用户发送的消息
28
- history.append({"role": "user", "content": user_message})
29
- history.append({"role": "assistant", "content": ""})
30
- yield history, "" # 返回 history 更新 UI,并清空对应的输入框
 
 
 
31
 
32
  try:
33
  response_text = ""
34
  stream = client.chat_completion(
35
- messages,
36
  max_tokens=max_tokens,
37
  stream=True,
38
  temperature=temperature,
@@ -42,12 +39,13 @@ def bot_response(user_message, history, model_name, system_message, max_tokens,
42
  for chunk in stream:
43
  token_content = chunk.choices[0].delta.content or ""
44
  response_text += token_content
45
- history[-1]["content"] = response_text
46
- yield history, "" # 持续流式输出
 
47
 
48
  except Exception as e:
49
- history[-1]["content"] = f"**Error:** {str(e)}"
50
- yield history, ""
51
 
52
  with gr.Blocks() as demo:
53
  with gr.Sidebar():
 
8
 
9
  def bot_response(user_message, history, model_name, system_message, max_tokens, temperature, top_p, oauth_token: gr.OAuthToken | None):
10
  if not user_message:
11
+ yield history, ""
12
  return
13
 
 
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 = ""
31
  stream = client.chat_completion(
32
+ api_messages,
33
  max_tokens=max_tokens,
34
  stream=True,
35
  temperature=temperature,
 
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
 
46
  except Exception as e:
47
+ new_history[-1]["content"] = f"**Error:** {str(e)}"
48
+ yield new_history, ""
49
 
50
  with gr.Blocks() as demo:
51
  with gr.Sidebar():