feat: fix gradio history object bug
Browse files
app.py
CHANGED
|
@@ -23,10 +23,16 @@ def respond(
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def sanitize_messages(history):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
messages = [{"role": "system", "content": system_message}]
|
| 32 |
messages.extend(sanitize_messages(history))
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def sanitize_messages(history):
|
| 26 |
+
sanitized = []
|
| 27 |
+
for m in history:
|
| 28 |
+
# Check if it's the new dictionary format
|
| 29 |
+
if isinstance(m, dict):
|
| 30 |
+
sanitized.append({"role": m["role"], "content": m["content"]})
|
| 31 |
+
# Check if it's the old list/tuple format [user, bot]
|
| 32 |
+
elif isinstance(m, (list, tuple)):
|
| 33 |
+
if m[0]: sanitized.append({"role": "user", "content": m[0]})
|
| 34 |
+
if m[1]: sanitized.append({"role": "assistant", "content": m[1]})
|
| 35 |
+
return sanitized
|
| 36 |
|
| 37 |
messages = [{"role": "system", "content": system_message}]
|
| 38 |
messages.extend(sanitize_messages(history))
|