Spaces:
Runtime error
Runtime error
Enrique Cardoza commited on
Commit ·
58cb2c2
1
Parent(s): 7bcdea6
fix(context): prevent context window exhaustion by removing base64 audio data
Browse files- Add clean_base64_audio() helper function to detect and replace audio tags
- Replace base64-encoded audio data with small placeholder text
- Modify format_history() to clean messages before sending to Claude
- Preserve full audio data in UI while reducing context usage
- Add logging to track context size reduction from cleaning
- Maintain playback functionality in UI while optimizing context window
app.py
CHANGED
|
@@ -69,12 +69,18 @@ def format_history(history):
|
|
| 69 |
print(f"Item {i} type: {type(item)}")
|
| 70 |
|
| 71 |
if isinstance(item, dict) and 'role' in item and 'content' in item:
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
elif hasattr(item, 'role') and hasattr(item, 'content'):
|
| 74 |
# Handle ChatMessage objects
|
| 75 |
role = item.role
|
| 76 |
content = item.content
|
| 77 |
|
|
|
|
|
|
|
|
|
|
| 78 |
# Ensure valid role values for Claude API
|
| 79 |
if role not in ["user", "assistant"]:
|
| 80 |
role = "user" if role == "human" else "assistant"
|
|
@@ -85,6 +91,30 @@ def format_history(history):
|
|
| 85 |
print(f"Formatted {len(messages)} messages for Claude")
|
| 86 |
return messages
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
# Process the transcription using the MCP tool with URL
|
| 89 |
def process_transcription_from_url(audio_url, user_groq_api_key):
|
| 90 |
try:
|
|
@@ -260,6 +290,7 @@ def should_use_tts_tool(prompt, history=None):
|
|
| 260 |
Respond with false if:
|
| 261 |
- The user is just chatting or asking questions not related to speech generation
|
| 262 |
- The user is asking about transcription (speech-to-text) instead
|
|
|
|
| 263 |
"""
|
| 264 |
|
| 265 |
# Prepare the messages for Claude
|
|
@@ -382,6 +413,9 @@ def chat_with_tools(user_message, history, user_groq_api_key, user_anthropic_api
|
|
| 382 |
|
| 383 |
How does the audio sound? Let me know if you'd like to generate speech from any other text.
|
| 384 |
"""
|
|
|
|
|
|
|
|
|
|
| 385 |
history.append(ChatMessage(
|
| 386 |
role="assistant",
|
| 387 |
content=response_with_audio
|
|
|
|
| 69 |
print(f"Item {i} type: {type(item)}")
|
| 70 |
|
| 71 |
if isinstance(item, dict) and 'role' in item and 'content' in item:
|
| 72 |
+
content = item["content"]
|
| 73 |
+
# Clean base64 audio data from content before sending to Claude
|
| 74 |
+
content = clean_base64_audio(content)
|
| 75 |
+
messages.append({"role": item["role"], "content": content})
|
| 76 |
elif hasattr(item, 'role') and hasattr(item, 'content'):
|
| 77 |
# Handle ChatMessage objects
|
| 78 |
role = item.role
|
| 79 |
content = item.content
|
| 80 |
|
| 81 |
+
# Clean base64 audio data from content before sending to Claude
|
| 82 |
+
content = clean_base64_audio(content)
|
| 83 |
+
|
| 84 |
# Ensure valid role values for Claude API
|
| 85 |
if role not in ["user", "assistant"]:
|
| 86 |
role = "user" if role == "human" else "assistant"
|
|
|
|
| 91 |
print(f"Formatted {len(messages)} messages for Claude")
|
| 92 |
return messages
|
| 93 |
|
| 94 |
+
# Helper function to clean base64 audio data from message content
|
| 95 |
+
def clean_base64_audio(content):
|
| 96 |
+
if not isinstance(content, str):
|
| 97 |
+
return content
|
| 98 |
+
|
| 99 |
+
# Check if there's base64 audio data in the content
|
| 100 |
+
if "<audio controls src=\"data:audio/wav;base64," in content:
|
| 101 |
+
# Replace the entire audio tag and base64 data with a placeholder
|
| 102 |
+
import re
|
| 103 |
+
cleaned_content = re.sub(
|
| 104 |
+
r'<audio controls src="data:audio/wav;base64,[^"]*"[^>]*></audio>',
|
| 105 |
+
'<audio>[BASE64_AUDIO_REMOVED_TO_SAVE_CONTEXT]</audio>',
|
| 106 |
+
content
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
# Log the size reduction
|
| 110 |
+
size_before = len(content)
|
| 111 |
+
size_after = len(cleaned_content)
|
| 112 |
+
print(f"Cleaned base64 audio data. Size before: {size_before}, after: {size_after}, saved: {size_before - size_after} bytes")
|
| 113 |
+
|
| 114 |
+
return cleaned_content
|
| 115 |
+
|
| 116 |
+
return content
|
| 117 |
+
|
| 118 |
# Process the transcription using the MCP tool with URL
|
| 119 |
def process_transcription_from_url(audio_url, user_groq_api_key):
|
| 120 |
try:
|
|
|
|
| 290 |
Respond with false if:
|
| 291 |
- The user is just chatting or asking questions not related to speech generation
|
| 292 |
- The user is asking about transcription (speech-to-text) instead
|
| 293 |
+
- The user is asking about content generation
|
| 294 |
"""
|
| 295 |
|
| 296 |
# Prepare the messages for Claude
|
|
|
|
| 413 |
|
| 414 |
How does the audio sound? Let me know if you'd like to generate speech from any other text.
|
| 415 |
"""
|
| 416 |
+
# Store a custom attribute on the message to indicate it has audio
|
| 417 |
+
# This will be displayed in the UI but the base64 data will be stripped
|
| 418 |
+
# when sending to Claude
|
| 419 |
history.append(ChatMessage(
|
| 420 |
role="assistant",
|
| 421 |
content=response_with_audio
|