tiny edit
#28
by
ismailhakki37
- opened
- handler.py +30 -4
handler.py
CHANGED
|
@@ -251,7 +251,14 @@ def clear_history():
|
|
| 251 |
|
| 252 |
try:
|
| 253 |
chatbot_instance = chat_manager.get_chatbot(args, args.model_path if args else "PULSE-ECG/PULSE-7B", tokenizer, model, image_processor, context_len)
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
return {"status": "success", "message": "Conversation history cleared"}
|
| 256 |
except Exception as e:
|
| 257 |
return {"error": f"Failed to clear history: {str(e)}"}
|
|
@@ -319,7 +326,17 @@ def generate_response(message_text, image_input, temperature=0.05, top_p=1.0, ma
|
|
| 319 |
print(f"[DEBUG] Image processing error: {str(e)}")
|
| 320 |
return {"error": f"Image processing failed: {str(e)}"}
|
| 321 |
|
| 322 |
-
# Prepare conversation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 323 |
inp = DEFAULT_IMAGE_TOKEN + "\n" + message_text
|
| 324 |
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[0], inp)
|
| 325 |
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[1], None)
|
|
@@ -368,10 +385,19 @@ def generate_response(message_text, image_input, temperature=0.05, top_p=1.0, ma
|
|
| 368 |
|
| 369 |
print(f"[DEBUG] Conversation messages length: {len(our_chatbot.conversation.messages)}")
|
| 370 |
if len(our_chatbot.conversation.messages) > 0:
|
| 371 |
-
|
| 372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
else:
|
| 374 |
print("[DEBUG] No conversation messages found")
|
|
|
|
|
|
|
| 375 |
|
| 376 |
print(f"[DEBUG] Generated response length: {len(response)}")
|
| 377 |
except Exception as e:
|
|
|
|
| 251 |
|
| 252 |
try:
|
| 253 |
chatbot_instance = chat_manager.get_chatbot(args, args.model_path if args else "PULSE-ECG/PULSE-7B", tokenizer, model, image_processor, context_len)
|
| 254 |
+
try:
|
| 255 |
+
if hasattr(chatbot_instance, 'conv_mode') and chatbot_instance.conv_mode and LLAVA_AVAILABLE:
|
| 256 |
+
chatbot_instance.conversation = conv_templates[chatbot_instance.conv_mode].copy()
|
| 257 |
+
else:
|
| 258 |
+
# Use default conversation template
|
| 259 |
+
chatbot_instance.conversation = chatbot_instance.conversation.__class__()
|
| 260 |
+
except Exception as e:
|
| 261 |
+
print(f"[DEBUG] Failed to reset conversation in clear_history: {e}")
|
| 262 |
return {"status": "success", "message": "Conversation history cleared"}
|
| 263 |
except Exception as e:
|
| 264 |
return {"error": f"Failed to clear history: {str(e)}"}
|
|
|
|
| 326 |
print(f"[DEBUG] Image processing error: {str(e)}")
|
| 327 |
return {"error": f"Image processing failed: {str(e)}"}
|
| 328 |
|
| 329 |
+
# Prepare conversation - reset for each request to avoid history issues
|
| 330 |
+
try:
|
| 331 |
+
if hasattr(our_chatbot, 'conv_mode') and our_chatbot.conv_mode and LLAVA_AVAILABLE:
|
| 332 |
+
our_chatbot.conversation = conv_templates[our_chatbot.conv_mode].copy()
|
| 333 |
+
else:
|
| 334 |
+
# Use default conversation template
|
| 335 |
+
our_chatbot.conversation = our_chatbot.conversation.__class__()
|
| 336 |
+
except Exception as e:
|
| 337 |
+
print(f"[DEBUG] Failed to reset conversation: {e}")
|
| 338 |
+
# Continue with existing conversation
|
| 339 |
+
|
| 340 |
inp = DEFAULT_IMAGE_TOKEN + "\n" + message_text
|
| 341 |
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[0], inp)
|
| 342 |
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[1], None)
|
|
|
|
| 385 |
|
| 386 |
print(f"[DEBUG] Conversation messages length: {len(our_chatbot.conversation.messages)}")
|
| 387 |
if len(our_chatbot.conversation.messages) > 0:
|
| 388 |
+
last_message = our_chatbot.conversation.messages[-1]
|
| 389 |
+
print(f"[DEBUG] Last message: {last_message}")
|
| 390 |
+
if isinstance(last_message, list) and len(last_message) > 1:
|
| 391 |
+
our_chatbot.conversation.messages[-1][-1] = response
|
| 392 |
+
print(f"[DEBUG] Response added to conversation")
|
| 393 |
+
else:
|
| 394 |
+
print(f"[DEBUG] Last message format unexpected: {last_message}")
|
| 395 |
+
# Add response as new message if format is wrong
|
| 396 |
+
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[1], response)
|
| 397 |
else:
|
| 398 |
print("[DEBUG] No conversation messages found")
|
| 399 |
+
# Add response as new message
|
| 400 |
+
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[1], response)
|
| 401 |
|
| 402 |
print(f"[DEBUG] Generated response length: {len(response)}")
|
| 403 |
except Exception as e:
|