Spaces:
Runtime error
Runtime error
fix: guard Chatbot messages format
Browse files
app.py
CHANGED
|
@@ -105,3 +105,28 @@ with gr.Blocks(title=TITLE, theme="soft") as demo:
|
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
demo.launch(share=False, server_name="0.0.0.0", server_port=7860, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
if __name__ == "__main__":
|
| 107 |
demo.launch(share=False, server_name="0.0.0.0", server_port=7860, show_error=True)
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def _coerce_messages(history):
|
| 111 |
+
"""確保是 [{'role','content'}] 形式。"""
|
| 112 |
+
fixed = []
|
| 113 |
+
for r, m in (history or []):
|
| 114 |
+
if isinstance(m, dict) and 'role' in m and 'content' in m:
|
| 115 |
+
fixed.append((r, m))
|
| 116 |
+
elif isinstance(m, str):
|
| 117 |
+
fixed.append((r, {"role": r, "content": m}))
|
| 118 |
+
else:
|
| 119 |
+
# 最保守兜底
|
| 120 |
+
fixed.append((r, {"role": r, "content": str(m)}))
|
| 121 |
+
return fixed
|
| 122 |
+
|
| 123 |
+
def respond(history, text, image, audio, mx, tp, top):
|
| 124 |
+
history = _coerce_messages(history)
|
| 125 |
+
history.append(("user", {"content": text}))
|
| 126 |
+
try:
|
| 127 |
+
ans = generate_reply(history, image, audio, mx, tp, top)
|
| 128 |
+
except Exception as e:
|
| 129 |
+
ans = f"(推理失敗:{e})"
|
| 130 |
+
history.append(("assistant", {"content": ans}))
|
| 131 |
+
return history, ""
|
| 132 |
+
|