Slothwolf commited on
Commit
2373965
·
verified ·
1 Parent(s): 4131912

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -44,6 +44,53 @@ user_end = tokenizer.encode_special("<|user_end|>")
44
  assistant_start = tokenizer.encode_special("<|assistant_start|>")
45
  assistant_end = tokenizer.encode_special("<|assistant_end|>")
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # ---------------------------------------------------------------------------
48
  # Chat function (streaming)
49
  # ---------------------------------------------------------------------------
 
44
  assistant_start = tokenizer.encode_special("<|assistant_start|>")
45
  assistant_end = tokenizer.encode_special("<|assistant_end|>")
46
 
47
+
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
89
+ return normalized
90
+
91
+ # 在构建 tokens 之前调用
92
+ history = normalize_history(history)
93
+
94
  # ---------------------------------------------------------------------------
95
  # Chat function (streaming)
96
  # ---------------------------------------------------------------------------