Slothwolf commited on
Commit
550b9a8
·
verified ·
1 Parent(s): 98ad17b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -15
app.py CHANGED
@@ -48,41 +48,47 @@ assistant_end = tokenizer.encode_special("<|assistant_end|>")
48
 
49
 
50
  def normalize_history(history):
51
- """将 Gradio 传入的 history 统一转换为 [(user, assistant), ...] 格式"""
52
  if not history:
53
  return []
54
-
55
- # 处理 messages 格式(列表 of dicts,每个 dict 有 "role" 和 "content")
56
  if isinstance(history[0], dict):
57
  pairs = []
58
  user_msg = None
59
  for msg in history:
60
  role = msg.get("role")
61
  content = msg.get("content", "")
 
 
 
 
62
  if role == "user":
63
- if user_msg is not None: # 上一条 user 没有 assistant,补 None
64
  pairs.append([user_msg, None])
65
  user_msg = content
66
  elif role == "assistant":
67
  if user_msg is not None:
68
  pairs.append([user_msg, content])
69
  user_msg = None
70
- else: # 孤立 assistant,补空 user
71
- pairs.append([None, content])
72
- if user_msg is not None: # 最后一条是 user,无 assistant
73
  pairs.append([user_msg, None])
74
  return pairs
75
-
76
- # 处理 tuples 格式(列表 of list/tuple),确保每个元素长度为2
77
  normalized = []
78
  for turn in history:
79
  if isinstance(turn, (list, tuple)):
80
- if len(turn) >= 2:
81
- normalized.append([turn[0], turn[1]])
82
- elif len(turn) == 1:
83
- normalized.append([turn[0], None])
84
- else:
85
- normalized.append([None, None]) # 空轮次,忽略或警告
 
 
86
  else:
87
  # 忽略非列表/元组类型(通常不会出现)
88
  continue
@@ -102,6 +108,7 @@ def chat(message, history, temperature, max_tokens, top_k):
102
 
103
 
104
  for user_msg, assistant_msg in history:
 
105
  tokens.append(user_start)
106
  tokens.extend(tokenizer.encode(user_msg))
107
  tokens.append(user_end)
 
48
 
49
 
50
  def normalize_history(history):
51
+ """将 history 统一转换为 [(user_msg, assistant_msg), ...] 格式,并确保消息为字符串"""
52
  if not history:
53
  return []
54
+
55
+ # ------------------ 处理 messages 格式 (列表 of dicts) ------------------
56
  if isinstance(history[0], dict):
57
  pairs = []
58
  user_msg = None
59
  for msg in history:
60
  role = msg.get("role")
61
  content = msg.get("content", "")
62
+ # 将 content 强制转为字符串
63
+ if not isinstance(content, str):
64
+ content = str(content) if content is not None else ""
65
+
66
  if role == "user":
67
+ if user_msg is not None:
68
  pairs.append([user_msg, None])
69
  user_msg = content
70
  elif role == "assistant":
71
  if user_msg is not None:
72
  pairs.append([user_msg, content])
73
  user_msg = None
74
+ else: # 孤立 assistant,补空 user
75
+ pairs.append(["", content])
76
+ if user_msg is not None:
77
  pairs.append([user_msg, None])
78
  return pairs
79
+
80
+ # ------------------ 处理 tuples 格式 (列表 of list/tuple) ------------------
81
  normalized = []
82
  for turn in history:
83
  if isinstance(turn, (list, tuple)):
84
+ # 取前两个元素,多余忽略;不足补 None
85
+ user = turn[0] if len(turn) > 0 else None
86
+ assistant = turn[1] if len(turn) > 1 else None
87
+
88
+ # 强制转为字符串
89
+ user = str(user) if user is not None else ""
90
+ assistant = str(assistant) if assistant is not None else ""
91
+ normalized.append([user, assistant])
92
  else:
93
  # 忽略非列表/元组类型(通常不会出现)
94
  continue
 
108
 
109
 
110
  for user_msg, assistant_msg in history:
111
+ user_text = user_msg if isinstance(user_msg, str) else str(user_msg) if user_msg is not None else ""
112
  tokens.append(user_start)
113
  tokens.extend(tokenizer.encode(user_msg))
114
  tokens.append(user_end)