Spaces:
Runtime error
Runtime error
Refactor chat history handling in process_input function to ensure it initializes correctly when not a list; improve user message formatting for multimodal inputs in app.py.
Browse files
app.py
CHANGED
|
@@ -46,9 +46,15 @@ def process_input(image, audio, video, text, chat_history, voice_type, enable_au
|
|
| 46 |
conversation = [SYSTEM_PROMPT]
|
| 47 |
|
| 48 |
# Add previous chat history
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# Add current user input
|
| 54 |
conversation.append({"role": "user", "content": user_input_to_content(user_input)})
|
|
@@ -104,7 +110,7 @@ def process_input(image, audio, video, text, chat_history, voice_type, enable_au
|
|
| 104 |
text_response = text_response.strip()
|
| 105 |
|
| 106 |
# Format user message for chat history display
|
| 107 |
-
user_message_for_display = text
|
| 108 |
if image is not None:
|
| 109 |
user_message_for_display = (user_message_for_display or "Image uploaded") + " [Image]"
|
| 110 |
if audio is not None:
|
|
@@ -112,7 +118,13 @@ def process_input(image, audio, video, text, chat_history, voice_type, enable_au
|
|
| 112 |
if video is not None:
|
| 113 |
user_message_for_display = (user_message_for_display or "Video uploaded") + " [Video]"
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
# Update chat history with properly formatted entries
|
|
|
|
|
|
|
| 116 |
chat_history.append((user_message_for_display, text_response))
|
| 117 |
|
| 118 |
# Prepare output
|
|
@@ -230,7 +242,7 @@ def create_demo():
|
|
| 230 |
|
| 231 |
# Text input handling
|
| 232 |
text_submit.click(
|
| 233 |
-
fn=lambda text: text,
|
| 234 |
inputs=text_input,
|
| 235 |
outputs=[chatbot],
|
| 236 |
queue=False
|
|
@@ -243,15 +255,15 @@ def create_demo():
|
|
| 243 |
# Multimodal input handling
|
| 244 |
def prepare_multimodal_input(image, audio, video, text):
|
| 245 |
# Create a display message that indicates what was uploaded
|
| 246 |
-
display_message = text
|
| 247 |
if image is not None:
|
| 248 |
-
display_message = (display_message + " " if display_message else "") + "[Image]"
|
| 249 |
if audio is not None:
|
| 250 |
-
display_message = (display_message + " " if display_message else "") + "[Audio]"
|
| 251 |
if video is not None:
|
| 252 |
-
display_message = (display_message + " " if display_message else "") + "[Video]"
|
| 253 |
|
| 254 |
-
if not display_message:
|
| 255 |
display_message = "Multimodal content"
|
| 256 |
|
| 257 |
return display_message
|
|
|
|
| 46 |
conversation = [SYSTEM_PROMPT]
|
| 47 |
|
| 48 |
# Add previous chat history
|
| 49 |
+
if isinstance(chat_history, list):
|
| 50 |
+
for item in chat_history:
|
| 51 |
+
if isinstance(item, tuple) and len(item) == 2:
|
| 52 |
+
user_msg, bot_msg = item
|
| 53 |
+
conversation.append({"role": "user", "content": user_input_to_content(user_msg)})
|
| 54 |
+
conversation.append({"role": "assistant", "content": bot_msg})
|
| 55 |
+
else:
|
| 56 |
+
# Initialize chat history if it's not a list
|
| 57 |
+
chat_history = []
|
| 58 |
|
| 59 |
# Add current user input
|
| 60 |
conversation.append({"role": "user", "content": user_input_to_content(user_input)})
|
|
|
|
| 110 |
text_response = text_response.strip()
|
| 111 |
|
| 112 |
# Format user message for chat history display
|
| 113 |
+
user_message_for_display = str(text) if text is not None else ""
|
| 114 |
if image is not None:
|
| 115 |
user_message_for_display = (user_message_for_display or "Image uploaded") + " [Image]"
|
| 116 |
if audio is not None:
|
|
|
|
| 118 |
if video is not None:
|
| 119 |
user_message_for_display = (user_message_for_display or "Video uploaded") + " [Video]"
|
| 120 |
|
| 121 |
+
# If empty, provide a default message
|
| 122 |
+
if not user_message_for_display.strip():
|
| 123 |
+
user_message_for_display = "Multimodal input"
|
| 124 |
+
|
| 125 |
# Update chat history with properly formatted entries
|
| 126 |
+
if not isinstance(chat_history, list):
|
| 127 |
+
chat_history = []
|
| 128 |
chat_history.append((user_message_for_display, text_response))
|
| 129 |
|
| 130 |
# Prepare output
|
|
|
|
| 242 |
|
| 243 |
# Text input handling
|
| 244 |
text_submit.click(
|
| 245 |
+
fn=lambda text: str(text) if text is not None else "",
|
| 246 |
inputs=text_input,
|
| 247 |
outputs=[chatbot],
|
| 248 |
queue=False
|
|
|
|
| 255 |
# Multimodal input handling
|
| 256 |
def prepare_multimodal_input(image, audio, video, text):
|
| 257 |
# Create a display message that indicates what was uploaded
|
| 258 |
+
display_message = str(text) if text is not None else ""
|
| 259 |
if image is not None:
|
| 260 |
+
display_message = (display_message + " " if display_message.strip() else "") + "[Image]"
|
| 261 |
if audio is not None:
|
| 262 |
+
display_message = (display_message + " " if display_message.strip() else "") + "[Audio]"
|
| 263 |
if video is not None:
|
| 264 |
+
display_message = (display_message + " " if display_message.strip() else "") + "[Video]"
|
| 265 |
|
| 266 |
+
if not display_message.strip():
|
| 267 |
display_message = "Multimodal content"
|
| 268 |
|
| 269 |
return display_message
|