ysharma HF Staff commited on
Commit
69fa6dd
·
verified ·
1 Parent(s): 8648d06

Update chat_handler.py

Browse files
Files changed (1) hide show
  1. chat_handler.py +68 -18
chat_handler.py CHANGED
@@ -335,25 +335,19 @@ class ChatHandler:
335
  }
336
  ))
337
 
338
- # Only add separate media display if:
339
- # 1. We extracted a valid URL
340
- # 2. The result text is NOT just a URL (to avoid double rendering)
341
- # 3. The result text doesn't contain the exact URL
342
- if media_url:
343
- # Check if the result is essentially just the URL (with possible whitespace)
344
- cleaned_result = result_text.strip()
345
- is_just_url = (cleaned_result == media_url or
346
- cleaned_result.startswith('http') and len(cleaned_result.split()) == 1)
347
-
348
- # Only add separate media if the result is more than just the URL
349
- if not is_just_url and media_url not in result_text:
350
- logger.info(f"🎯 Adding separate media display for: {media_url}")
351
- chat_messages.append(ChatMessage(
352
- role="assistant",
353
- content={"path": media_url}
354
- ))
355
  else:
356
- logger.info(f"🚫 Skipping separate media - result is just URL: {is_just_url}, contains URL: {media_url in result_text}")
357
 
358
  else:
359
  # Add error message for failed tool call
@@ -385,6 +379,62 @@ class ChatHandler:
385
 
386
  return chat_messages
387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
  def _get_native_system_prompt(self) -> str:
389
  """Get system prompt for Claude without MCP servers"""
390
  return f"""You are Claude Sonnet 4, a helpful AI assistant with native multimodal capabilities. You can have conversations, answer questions, help with various tasks, and provide information on a wide range of topics.
 
335
  }
336
  ))
337
 
338
+ # Only add separate media display if the tool result does NOT contain
339
+ # any Gradio file data structures that would be auto-rendered
340
+ if media_url and not self._contains_gradio_file_structure(result_text):
341
+ logger.info(f"🎯 Adding separate media display for: {media_url}")
342
+ chat_messages.append(ChatMessage(
343
+ role="assistant",
344
+ content={"path": media_url}
345
+ ))
346
+ else:
347
+ if media_url:
348
+ logger.info(f"🚫 Skipping separate media - tool result contains Gradio file structure")
 
 
 
 
 
 
349
  else:
350
+ logger.info(f"🚫 No media URL extracted")
351
 
352
  else:
353
  # Add error message for failed tool call
 
379
 
380
  return chat_messages
381
 
382
+ def _contains_gradio_file_structure(self, text: str) -> bool:
383
+ """Check if the text contains ANY Gradio file data structures that would be auto-rendered"""
384
+
385
+ # Check for key indicators of Gradio file structures
386
+ gradio_indicators = [
387
+ # Gradio FileData type indicators
388
+ "'_type': 'gradio.FileData'",
389
+ '"_type": "gradio.FileData"',
390
+ 'gradio.FileData',
391
+
392
+ # File structure patterns
393
+ "'path':",
394
+ '"path":',
395
+ "'url':",
396
+ '"url":',
397
+ "'orig_name':",
398
+ '"orig_name":',
399
+ "'mime_type':",
400
+ '"mime_type":',
401
+ 'is_stream',
402
+ 'meta_type',
403
+
404
+ # Common file result patterns
405
+ "{'image':",
406
+ '{"image":',
407
+ "{'audio':",
408
+ '{"audio":',
409
+ "{'video':",
410
+ '{"video":',
411
+ "{'file':",
412
+ '{"file":',
413
+
414
+ # List patterns that typically contain file objects
415
+ "[{'image'",
416
+ '[{"image"',
417
+ "[{'audio'",
418
+ '[{"audio"',
419
+ "[{'video'",
420
+ '[{"video"',
421
+ "[{'file'",
422
+ '[{"file"'
423
+ ]
424
+
425
+ # If we find multiple indicators, it's likely a Gradio file structure
426
+ indicator_count = sum(1 for indicator in gradio_indicators if indicator in text)
427
+
428
+ # Also check for simple URL patterns (for audio case)
429
+ is_simple_url = (text.strip().startswith('http') and
430
+ len(text.strip().split()) == 1 and
431
+ any(ext in text.lower() for ext in ['.wav', '.mp3', '.mp4', '.png', '.jpg', '.jpeg', '.gif', '.svg', '.webm', '.ogg']))
432
+
433
+ result = indicator_count >= 2 or is_simple_url
434
+ logger.debug(f"📋 File structure check: {indicator_count} indicators, simple_url: {is_simple_url}, result: {result}")
435
+
436
+ return result
437
+
438
  def _get_native_system_prompt(self) -> str:
439
  """Get system prompt for Claude without MCP servers"""
440
  return f"""You are Claude Sonnet 4, a helpful AI assistant with native multimodal capabilities. You can have conversations, answer questions, help with various tasks, and provide information on a wide range of topics.