Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -313,18 +313,44 @@ def generate_reply_stream(ids: list[int], max_new_tokens=150, temperature=0.8, t
|
|
| 313 |
states = out.past_key_values
|
| 314 |
logits = out.logits[0, -1]
|
| 315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 316 |
def chat_function(message, history):
|
| 317 |
"""Gradio ChatInterface streaming handler formatted with speed stats (Live, Avg, Min, Max)."""
|
| 318 |
messages = []
|
| 319 |
for turn in history:
|
| 320 |
if isinstance(turn, (list, tuple)):
|
| 321 |
user_msg, asst_msg = turn
|
| 322 |
-
messages.append({"role": "user", "content": user_msg})
|
| 323 |
if asst_msg:
|
| 324 |
-
messages.append({"role": "assistant", "content": asst_msg})
|
| 325 |
elif isinstance(turn, dict):
|
| 326 |
-
messages.append(
|
| 327 |
-
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
# Encode token sequence according to model chat template
|
| 330 |
user_id = tokenizer.convert_tokens_to_ids(USER_TOKEN)
|
|
@@ -334,6 +360,8 @@ def chat_function(message, history):
|
|
| 334 |
for turn in messages:
|
| 335 |
role = turn["role"]
|
| 336 |
content = turn["content"]
|
|
|
|
|
|
|
| 337 |
content_ids = tokenizer.encode(" " + content)
|
| 338 |
if role == "user":
|
| 339 |
ids += [user_id] + content_ids
|
|
|
|
| 313 |
states = out.past_key_values
|
| 314 |
logits = out.logits[0, -1]
|
| 315 |
|
| 316 |
+
def extract_text_content(content) -> str:
|
| 317 |
+
"""Safely extract plain text from string, list, or dict content structures returned by Gradio."""
|
| 318 |
+
if isinstance(content, str):
|
| 319 |
+
return content
|
| 320 |
+
if isinstance(content, list):
|
| 321 |
+
parts = []
|
| 322 |
+
for item in content:
|
| 323 |
+
if isinstance(item, str):
|
| 324 |
+
parts.append(item)
|
| 325 |
+
elif isinstance(item, dict):
|
| 326 |
+
if "text" in item:
|
| 327 |
+
parts.append(str(item["text"]))
|
| 328 |
+
elif "content" in item:
|
| 329 |
+
parts.append(extract_text_content(item["content"]))
|
| 330 |
+
else:
|
| 331 |
+
parts.append(str(item))
|
| 332 |
+
return " ".join(parts)
|
| 333 |
+
if isinstance(content, dict):
|
| 334 |
+
if "text" in content:
|
| 335 |
+
return str(content["text"])
|
| 336 |
+
return str(content)
|
| 337 |
+
return str(content) if content is not None else ""
|
| 338 |
+
|
| 339 |
def chat_function(message, history):
|
| 340 |
"""Gradio ChatInterface streaming handler formatted with speed stats (Live, Avg, Min, Max)."""
|
| 341 |
messages = []
|
| 342 |
for turn in history:
|
| 343 |
if isinstance(turn, (list, tuple)):
|
| 344 |
user_msg, asst_msg = turn
|
| 345 |
+
messages.append({"role": "user", "content": extract_text_content(user_msg)})
|
| 346 |
if asst_msg:
|
| 347 |
+
messages.append({"role": "assistant", "content": extract_text_content(asst_msg)})
|
| 348 |
elif isinstance(turn, dict):
|
| 349 |
+
messages.append({
|
| 350 |
+
"role": turn.get("role", "user"),
|
| 351 |
+
"content": extract_text_content(turn.get("content", ""))
|
| 352 |
+
})
|
| 353 |
+
messages.append({"role": "user", "content": extract_text_content(message)})
|
| 354 |
|
| 355 |
# Encode token sequence according to model chat template
|
| 356 |
user_id = tokenizer.convert_tokens_to_ids(USER_TOKEN)
|
|
|
|
| 360 |
for turn in messages:
|
| 361 |
role = turn["role"]
|
| 362 |
content = turn["content"]
|
| 363 |
+
if not content.strip():
|
| 364 |
+
continue
|
| 365 |
content_ids = tokenizer.encode(" " + content)
|
| 366 |
if role == "user":
|
| 367 |
ids += [user_id] + content_ids
|