Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -63,17 +63,66 @@ def format_messages(system, history, user_text, audio_data_list=None):
|
|
| 63 |
|
| 64 |
# 处理历史记录
|
| 65 |
for item in history:
|
| 66 |
-
#
|
| 67 |
-
if isinstance(item, dict)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
# 添加当前用户消息
|
| 79 |
if user_text and audio_data_list:
|
|
@@ -146,10 +195,28 @@ def chat(system_prompt, user_text, audio_file, history, max_tokens, temperature,
|
|
| 146 |
return
|
| 147 |
|
| 148 |
# Debug: Print message format
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
# Update history with user message immediately
|
| 155 |
if audio_file:
|
|
|
|
| 63 |
|
| 64 |
# 处理历史记录
|
| 65 |
for item in history:
|
| 66 |
+
# Filter out thinking process messages
|
| 67 |
+
metadata = item.get("metadata") if isinstance(item, dict) else getattr(item, "metadata", None)
|
| 68 |
+
if metadata and isinstance(metadata, dict) and metadata.get("title") == "⏳ Thinking Process":
|
| 69 |
+
continue
|
| 70 |
+
|
| 71 |
+
role = item.get("role") if isinstance(item, dict) else getattr(item, "role", None)
|
| 72 |
+
content = item.get("content") if isinstance(item, dict) else getattr(item, "content", None)
|
| 73 |
+
|
| 74 |
+
if not role or content is None:
|
| 75 |
+
continue
|
| 76 |
+
|
| 77 |
+
# Check for Audio
|
| 78 |
+
is_audio = not isinstance(content, list) and content["component"] == "audio"
|
| 79 |
+
|
| 80 |
+
if is_audio:
|
| 81 |
+
audio_path = content["value"]["path"]
|
| 82 |
+
if audio_path and os.path.exists(audio_path):
|
| 83 |
+
try:
|
| 84 |
+
item_audio_data_list = process_audio(audio_path)
|
| 85 |
+
new_content = []
|
| 86 |
+
for audio_data in item_audio_data_list:
|
| 87 |
+
new_content.append({
|
| 88 |
+
"type": "input_audio",
|
| 89 |
+
"input_audio": {
|
| 90 |
+
"data": audio_data,
|
| 91 |
+
"format": "wav"
|
| 92 |
+
}
|
| 93 |
+
})
|
| 94 |
+
messages.append({"role": role, "content": new_content})
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"[ERROR] Failed to process history audio: {e}")
|
| 97 |
+
elif isinstance(content, str):
|
| 98 |
+
messages.append({"role": role, "content": content})
|
| 99 |
+
elif isinstance(content, list):
|
| 100 |
+
# Assume it's already a list of parts or mixed
|
| 101 |
+
safe_content = []
|
| 102 |
+
for c in content:
|
| 103 |
+
# Check for Audio in list
|
| 104 |
+
is_c_audio = c.get('component', None) == "audio"
|
| 105 |
+
|
| 106 |
+
if is_c_audio:
|
| 107 |
+
audio_path = c["value"]["path"]
|
| 108 |
+
if audio_path and os.path.exists(audio_path):
|
| 109 |
+
try:
|
| 110 |
+
item_audio_data_list = process_audio(audio_path)
|
| 111 |
+
for audio_data in item_audio_data_list:
|
| 112 |
+
safe_content.append({
|
| 113 |
+
"type": "input_audio",
|
| 114 |
+
"input_audio": {
|
| 115 |
+
"data": audio_data,
|
| 116 |
+
"format": "wav"
|
| 117 |
+
}
|
| 118 |
+
})
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"[ERROR] Failed to process history audio in list: {e}")
|
| 121 |
+
elif isinstance(c, dict):
|
| 122 |
+
safe_content.append(c)
|
| 123 |
+
elif isinstance(c, str):
|
| 124 |
+
safe_content.append({"type": "text", "text": c})
|
| 125 |
+
messages.append({"role": role, "content": safe_content})
|
| 126 |
|
| 127 |
# 添加当前用户消息
|
| 128 |
if user_text and audio_data_list:
|
|
|
|
| 195 |
return
|
| 196 |
|
| 197 |
# Debug: Print message format
|
| 198 |
+
debug_messages = []
|
| 199 |
+
for msg in messages:
|
| 200 |
+
if isinstance(msg, dict) and isinstance(msg.get("content"), list):
|
| 201 |
+
new_content = []
|
| 202 |
+
for item in msg["content"]:
|
| 203 |
+
if isinstance(item, dict) and item.get("type") == "input_audio":
|
| 204 |
+
item_copy = item.copy()
|
| 205 |
+
if "input_audio" in item_copy:
|
| 206 |
+
audio_info = item_copy["input_audio"].copy()
|
| 207 |
+
if "data" in audio_info:
|
| 208 |
+
audio_info["data"] = f"[BASE64_AUDIO_DATA_LEN_{len(audio_info['data'])}]"
|
| 209 |
+
item_copy["input_audio"] = audio_info
|
| 210 |
+
new_content.append(item_copy)
|
| 211 |
+
else:
|
| 212 |
+
new_content.append(item)
|
| 213 |
+
msg_copy = msg.copy()
|
| 214 |
+
msg_copy["content"] = new_content
|
| 215 |
+
debug_messages.append(msg_copy)
|
| 216 |
+
else:
|
| 217 |
+
debug_messages.append(msg)
|
| 218 |
+
|
| 219 |
+
print(f"[DEBUG] Messages to API: {json.dumps(debug_messages, ensure_ascii=False, indent=2)}")
|
| 220 |
|
| 221 |
# Update history with user message immediately
|
| 222 |
if audio_file:
|