Spaces:
Sleeping
Sleeping
Commit
·
71426d8
1
Parent(s):
3601f9d
bug
Browse files
app.py
CHANGED
|
@@ -98,19 +98,30 @@ def generate_reply(chat_history, user_message, max_new_tokens=512, temperature=0
|
|
| 98 |
return reply
|
| 99 |
|
| 100 |
def gradio_chat(user_message, history, max_new_tokens, temperature, top_p):
|
| 101 |
-
# Chatbot
|
|
|
|
| 102 |
history_messages = history or []
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
history_pairs = []
|
| 106 |
pending_user = None
|
| 107 |
for msg in history_messages:
|
| 108 |
if isinstance(msg, (list, tuple)) and len(msg) == 2:
|
| 109 |
-
history_pairs.append((msg[0], msg[1]))
|
| 110 |
pending_user = None
|
| 111 |
continue
|
| 112 |
role = msg.get("role")
|
| 113 |
-
content = msg.get("content", "")
|
| 114 |
if role == "user":
|
| 115 |
pending_user = content
|
| 116 |
elif role == "assistant":
|
|
@@ -125,12 +136,9 @@ def gradio_chat(user_message, history, max_new_tokens, temperature, top_p):
|
|
| 125 |
top_p=float(top_p),
|
| 126 |
)
|
| 127 |
|
| 128 |
-
|
| 129 |
-
{"role": "user", "content": user_message},
|
| 130 |
-
{"role": "assistant", "content": reply},
|
| 131 |
-
]
|
| 132 |
|
| 133 |
-
return "",
|
| 134 |
|
| 135 |
# Custom CSS for messenger-style UI with peach/pink theme
|
| 136 |
custom_css = """
|
|
|
|
| 98 |
return reply
|
| 99 |
|
| 100 |
def gradio_chat(user_message, history, max_new_tokens, temperature, top_p):
|
| 101 |
+
# Gradio Chatbot typically returns list of (user, assistant) tuples.
|
| 102 |
+
# Handle both tuple and dict formats defensively.
|
| 103 |
history_messages = history or []
|
| 104 |
|
| 105 |
+
def _as_text(content):
|
| 106 |
+
# Gradio may wrap content as list of {"type": "text", "text": "..."} dicts
|
| 107 |
+
if isinstance(content, list):
|
| 108 |
+
return " ".join(
|
| 109 |
+
c.get("text", "") if isinstance(c, dict) else str(c)
|
| 110 |
+
for c in content
|
| 111 |
+
)
|
| 112 |
+
if isinstance(content, dict):
|
| 113 |
+
return content.get("text", "") or str(content)
|
| 114 |
+
return content
|
| 115 |
+
|
| 116 |
history_pairs = []
|
| 117 |
pending_user = None
|
| 118 |
for msg in history_messages:
|
| 119 |
if isinstance(msg, (list, tuple)) and len(msg) == 2:
|
| 120 |
+
history_pairs.append((_as_text(msg[0]), _as_text(msg[1])))
|
| 121 |
pending_user = None
|
| 122 |
continue
|
| 123 |
role = msg.get("role")
|
| 124 |
+
content = _as_text(msg.get("content", ""))
|
| 125 |
if role == "user":
|
| 126 |
pending_user = content
|
| 127 |
elif role == "assistant":
|
|
|
|
| 136 |
top_p=float(top_p),
|
| 137 |
)
|
| 138 |
|
| 139 |
+
new_history_pairs = history_pairs + [(user_message, reply)]
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
+
return "", new_history_pairs
|
| 142 |
|
| 143 |
# Custom CSS for messenger-style UI with peach/pink theme
|
| 144 |
custom_css = """
|