stop criteria
#30
by
ismailhakki37
- opened
- handler.py +40 -3
handler.py
CHANGED
|
@@ -327,20 +327,41 @@ def generate_response(message_text, image_input, temperature=0.05, top_p=1.0, ma
|
|
| 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 |
-
#
|
| 335 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
except Exception as e:
|
| 337 |
print(f"[DEBUG] Failed to reset conversation: {e}")
|
| 338 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 343 |
prompt = our_chatbot.conversation.get_prompt()
|
|
|
|
|
|
|
| 344 |
|
| 345 |
# Tokenize input
|
| 346 |
input_ids = tokenizer_image_token(
|
|
@@ -394,6 +415,22 @@ def generate_response(message_text, image_input, temperature=0.05, top_p=1.0, ma
|
|
| 394 |
|
| 395 |
response = our_chatbot.tokenizer.decode(outputs[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 396 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
print(f"[DEBUG] Conversation messages length: {len(our_chatbot.conversation.messages)}")
|
| 398 |
if len(our_chatbot.conversation.messages) > 0:
|
| 399 |
last_message = our_chatbot.conversation.messages[-1]
|
|
|
|
| 327 |
return {"error": f"Image processing failed: {str(e)}"}
|
| 328 |
|
| 329 |
# Prepare conversation - reset for each request to avoid history issues
|
| 330 |
+
print(f"[DEBUG] Resetting conversation for new request...")
|
| 331 |
try:
|
| 332 |
if hasattr(our_chatbot, 'conv_mode') and our_chatbot.conv_mode and LLAVA_AVAILABLE:
|
| 333 |
our_chatbot.conversation = conv_templates[our_chatbot.conv_mode].copy()
|
| 334 |
+
print(f"[DEBUG] Conversation reset using conv_mode: {our_chatbot.conv_mode}")
|
| 335 |
else:
|
| 336 |
+
# Create a completely new conversation instance
|
| 337 |
+
from llava.conversation import conv_templates, SeparatorStyle
|
| 338 |
+
default_conv_mode = "llava_v1"
|
| 339 |
+
if default_conv_mode in conv_templates:
|
| 340 |
+
our_chatbot.conversation = conv_templates[default_conv_mode].copy()
|
| 341 |
+
print(f"[DEBUG] Conversation reset using default conv_mode: {default_conv_mode}")
|
| 342 |
+
else:
|
| 343 |
+
# Fallback: create minimal conversation
|
| 344 |
+
our_chatbot.conversation.messages = []
|
| 345 |
+
print(f"[DEBUG] Conversation reset using fallback method")
|
| 346 |
except Exception as e:
|
| 347 |
print(f"[DEBUG] Failed to reset conversation: {e}")
|
| 348 |
+
# Emergency fallback: clear messages
|
| 349 |
+
try:
|
| 350 |
+
our_chatbot.conversation.messages = []
|
| 351 |
+
print(f"[DEBUG] Emergency conversation reset completed")
|
| 352 |
+
except:
|
| 353 |
+
print(f"[DEBUG] Emergency conversation reset failed")
|
| 354 |
|
| 355 |
inp = DEFAULT_IMAGE_TOKEN + "\n" + message_text
|
| 356 |
+
print(f"[DEBUG] Conversation roles: {our_chatbot.conversation.roles}")
|
| 357 |
+
print(f"[DEBUG] Adding user message: {inp[:100]}...")
|
| 358 |
+
|
| 359 |
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[0], inp)
|
| 360 |
our_chatbot.conversation.append_message(our_chatbot.conversation.roles[1], None)
|
| 361 |
+
|
| 362 |
prompt = our_chatbot.conversation.get_prompt()
|
| 363 |
+
print(f"[DEBUG] Generated prompt length: {len(prompt)}")
|
| 364 |
+
print(f"[DEBUG] Prompt preview: {prompt[:200]}...")
|
| 365 |
|
| 366 |
# Tokenize input
|
| 367 |
input_ids = tokenizer_image_token(
|
|
|
|
| 415 |
|
| 416 |
response = our_chatbot.tokenizer.decode(outputs[0][input_ids.shape[1]:], skip_special_tokens=True)
|
| 417 |
|
| 418 |
+
# Validate response
|
| 419 |
+
if not response or not response.strip():
|
| 420 |
+
print(f"[DEBUG] Empty response detected, trying alternative decoding...")
|
| 421 |
+
# Try decoding without skip_special_tokens
|
| 422 |
+
response = our_chatbot.tokenizer.decode(outputs[0][input_ids.shape[1]:], skip_special_tokens=False)
|
| 423 |
+
if not response or not response.strip():
|
| 424 |
+
print(f"[DEBUG] Still empty response, checking raw outputs...")
|
| 425 |
+
# Check if outputs are valid
|
| 426 |
+
raw_response = our_chatbot.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 427 |
+
print(f"[DEBUG] Raw response: {raw_response[:200]}...")
|
| 428 |
+
# Extract only the new part
|
| 429 |
+
response = raw_response[len(our_chatbot.tokenizer.decode(input_ids[0], skip_special_tokens=True)):]
|
| 430 |
+
response = response.strip()
|
| 431 |
+
|
| 432 |
+
print(f"[DEBUG] Final response: {response[:100]}...")
|
| 433 |
+
|
| 434 |
print(f"[DEBUG] Conversation messages length: {len(our_chatbot.conversation.messages)}")
|
| 435 |
if len(our_chatbot.conversation.messages) > 0:
|
| 436 |
last_message = our_chatbot.conversation.messages[-1]
|