Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -48,18 +48,16 @@ assistant_end = tokenizer.encode_special("<|assistant_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 |
-
|
| 63 |
if not isinstance(content, str):
|
| 64 |
content = str(content) if content is not None else ""
|
| 65 |
|
|
@@ -71,26 +69,22 @@ def normalize_history(history):
|
|
| 71 |
if user_msg is not None:
|
| 72 |
pairs.append([user_msg, content])
|
| 73 |
user_msg = None
|
| 74 |
-
else:
|
| 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
|
| 95 |
return normalized
|
| 96 |
|
|
@@ -100,7 +94,7 @@ def normalize_history(history):
|
|
| 100 |
# ---------------------------------------------------------------------------
|
| 101 |
def chat(message, history, temperature, max_tokens, top_k):
|
| 102 |
"""Generate a streaming response for the chat interface."""
|
| 103 |
-
|
| 104 |
history = normalize_history(history)
|
| 105 |
# Build the full token sequence from conversation history
|
| 106 |
# history is a list of [user_msg, assistant_msg] tuples
|
|
|
|
| 48 |
|
| 49 |
|
| 50 |
def normalize_history(history):
|
|
|
|
| 51 |
if not history:
|
| 52 |
return []
|
| 53 |
|
|
|
|
| 54 |
if isinstance(history[0], dict):
|
| 55 |
pairs = []
|
| 56 |
user_msg = None
|
| 57 |
for msg in history:
|
| 58 |
role = msg.get("role")
|
| 59 |
content = msg.get("content", "")
|
| 60 |
+
|
| 61 |
if not isinstance(content, str):
|
| 62 |
content = str(content) if content is not None else ""
|
| 63 |
|
|
|
|
| 69 |
if user_msg is not None:
|
| 70 |
pairs.append([user_msg, content])
|
| 71 |
user_msg = None
|
| 72 |
+
else:
|
| 73 |
pairs.append(["", content])
|
| 74 |
if user_msg is not None:
|
| 75 |
pairs.append([user_msg, None])
|
| 76 |
return pairs
|
| 77 |
|
|
|
|
| 78 |
normalized = []
|
| 79 |
for turn in history:
|
| 80 |
if isinstance(turn, (list, tuple)):
|
|
|
|
| 81 |
user = turn[0] if len(turn) > 0 else None
|
| 82 |
assistant = turn[1] if len(turn) > 1 else None
|
| 83 |
|
|
|
|
| 84 |
user = str(user) if user is not None else ""
|
| 85 |
assistant = str(assistant) if assistant is not None else ""
|
| 86 |
normalized.append([user, assistant])
|
| 87 |
else:
|
|
|
|
| 88 |
continue
|
| 89 |
return normalized
|
| 90 |
|
|
|
|
| 94 |
# ---------------------------------------------------------------------------
|
| 95 |
def chat(message, history, temperature, max_tokens, top_k):
|
| 96 |
"""Generate a streaming response for the chat interface."""
|
| 97 |
+
|
| 98 |
history = normalize_history(history)
|
| 99 |
# Build the full token sequence from conversation history
|
| 100 |
# history is a list of [user_msg, assistant_msg] tuples
|