Spaces:
Sleeping
Sleeping
Commit
·
a19db28
1
Parent(s):
5e87361
improve agent
Browse files- src/agent.py +5 -3
- src/ui.py +36 -43
src/agent.py
CHANGED
|
@@ -61,7 +61,7 @@ Remember: Stay focused on audio-related assistance and use your specialized tool
|
|
| 61 |
class AudioAgent:
|
| 62 |
def __init__(
|
| 63 |
self,
|
| 64 |
-
model_name: str = "gpt-
|
| 65 |
server_url: str = "https://agents-mcp-hackathon-audioeditor.hf.space/gradio_api/mcp/sse",
|
| 66 |
):
|
| 67 |
load_dotenv()
|
|
@@ -96,6 +96,8 @@ class AudioAgent:
|
|
| 96 |
Input Audio Files: {', '.join(input_audio_files) if input_audio_files else 'None'}
|
| 97 |
"""
|
| 98 |
|
| 99 |
-
|
| 100 |
{"messages": [{"role": "user", "content": input_context}]}
|
| 101 |
-
)
|
|
|
|
|
|
|
|
|
| 61 |
class AudioAgent:
|
| 62 |
def __init__(
|
| 63 |
self,
|
| 64 |
+
model_name: str = "gpt-4.1",
|
| 65 |
server_url: str = "https://agents-mcp-hackathon-audioeditor.hf.space/gradio_api/mcp/sse",
|
| 66 |
):
|
| 67 |
load_dotenv()
|
|
|
|
| 96 |
Input Audio Files: {', '.join(input_audio_files) if input_audio_files else 'None'}
|
| 97 |
"""
|
| 98 |
|
| 99 |
+
res = await self.agent.ainvoke(
|
| 100 |
{"messages": [{"role": "user", "content": input_context}]}
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
return res["structured_response"]
|
src/ui.py
CHANGED
|
@@ -33,63 +33,40 @@ def user_input(user_message, audio_files, history):
|
|
| 33 |
|
| 34 |
audio_file_urls.append(get_share_url(file_path))
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
audio_list = "\n".join([f"🎵 Uploaded: {url.split('/')[-1]}" for url in audio_file_urls])
|
| 39 |
-
combined_message = f"{user_message}\n\n{audio_list}" if user_message.strip() else f"Process uploaded audio files:\n{audio_list}"
|
| 40 |
-
else:
|
| 41 |
-
combined_message = user_message
|
| 42 |
-
|
| 43 |
-
history.append({"role": "user", "content": combined_message})
|
| 44 |
return "", [], history, audio_file_urls
|
| 45 |
|
| 46 |
async def bot_response(history, audio_file_urls):
|
| 47 |
"""
|
| 48 |
-
Generate bot response using the
|
| 49 |
"""
|
| 50 |
if not history or history[-1]["role"] != "user":
|
| 51 |
-
return history
|
| 52 |
|
| 53 |
-
# Get the
|
| 54 |
user_message = history[-1]["content"]
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
if
|
| 58 |
-
|
| 59 |
-
clean_lines = []
|
| 60 |
-
for line in lines:
|
| 61 |
-
if not line.strip().startswith('🎵 Uploaded:'):
|
| 62 |
-
clean_lines.append(line)
|
| 63 |
-
user_message = '\n'.join(clean_lines).strip()
|
| 64 |
-
|
| 65 |
-
# If message is empty after cleaning, provide default message
|
| 66 |
-
if not user_message:
|
| 67 |
-
user_message = "Please process these audio files"
|
| 68 |
|
| 69 |
try:
|
| 70 |
-
# Use the
|
| 71 |
result = await agent.run_agent(user_message, audio_file_urls or [])
|
| 72 |
|
| 73 |
# Extract the final response and audio files from the result
|
| 74 |
final_response = result.get("final_response", "")
|
| 75 |
output_audio_files = result.get("output_audio_files", [])
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# Add processed audio files section if any
|
| 81 |
-
if output_audio_files:
|
| 82 |
-
formatted_content += "\n\n## 🎵 Generated Audio Files\n\n"
|
| 83 |
-
for audio_file in output_audio_files:
|
| 84 |
-
formatted_content += f"Audio Ready: {audio_file}\n"
|
| 85 |
|
| 86 |
-
|
| 87 |
-
history.append({"role": "assistant", "content": formatted_content.rstrip()})
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
history.append({"role": "assistant", "content": f"❌ **Error**: {e}"})
|
| 91 |
-
|
| 92 |
-
return history
|
| 93 |
|
| 94 |
def bot_response_sync(history, audio_file_urls):
|
| 95 |
"""
|
|
@@ -107,7 +84,7 @@ def create_interface():
|
|
| 107 |
title="Audio Agent - Professional Audio Processing",
|
| 108 |
theme=gr.themes.Soft(),
|
| 109 |
css="""
|
| 110 |
-
.
|
| 111 |
background: #f0f9ff;
|
| 112 |
border: 1px solid #0891b2;
|
| 113 |
border-radius: 8px;
|
|
@@ -153,6 +130,18 @@ def create_interface():
|
|
| 153 |
- "How does audio normalization work?"
|
| 154 |
""")
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
with gr.Row():
|
| 157 |
msg = gr.Textbox(
|
| 158 |
label="Describe what you want to do",
|
|
@@ -201,15 +190,19 @@ def create_interface():
|
|
| 201 |
new_msg, new_files, updated_history, audio_urls = user_input(message, files, history)
|
| 202 |
return new_msg, new_files, updated_history, audio_urls
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
msg.submit(
|
| 205 |
handle_submit,
|
| 206 |
[msg, audio_files, chatbot],
|
| 207 |
[msg, audio_files, chatbot, audio_urls_state],
|
| 208 |
queue=False
|
| 209 |
).then(
|
| 210 |
-
|
| 211 |
[chatbot, audio_urls_state],
|
| 212 |
-
chatbot
|
| 213 |
)
|
| 214 |
|
| 215 |
send_btn.click(
|
|
@@ -218,19 +211,19 @@ def create_interface():
|
|
| 218 |
[msg, audio_files, chatbot, audio_urls_state],
|
| 219 |
queue=False
|
| 220 |
).then(
|
| 221 |
-
|
| 222 |
[chatbot, audio_urls_state],
|
| 223 |
-
chatbot
|
| 224 |
)
|
| 225 |
|
| 226 |
# Clear chat
|
| 227 |
def clear_chat():
|
| 228 |
-
return [], [], []
|
| 229 |
|
| 230 |
clear_btn.click(
|
| 231 |
clear_chat,
|
| 232 |
None,
|
| 233 |
-
[chatbot, audio_files, audio_urls_state],
|
| 234 |
queue=False
|
| 235 |
)
|
| 236 |
|
|
|
|
| 33 |
|
| 34 |
audio_file_urls.append(get_share_url(file_path))
|
| 35 |
|
| 36 |
+
# Add user message to history (no uploaded file display)
|
| 37 |
+
history.append({"role": "user", "content": user_message})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
return "", [], history, audio_file_urls
|
| 39 |
|
| 40 |
async def bot_response(history, audio_file_urls):
|
| 41 |
"""
|
| 42 |
+
Generate bot response using the agent
|
| 43 |
"""
|
| 44 |
if not history or history[-1]["role"] != "user":
|
| 45 |
+
return history, []
|
| 46 |
|
| 47 |
+
# Get the user message
|
| 48 |
user_message = history[-1]["content"]
|
| 49 |
|
| 50 |
+
# If message is empty but we have audio files, provide default message
|
| 51 |
+
if not user_message.strip() and audio_file_urls:
|
| 52 |
+
user_message = "Please process these audio files"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
try:
|
| 55 |
+
# Use the agent's run_agent method
|
| 56 |
result = await agent.run_agent(user_message, audio_file_urls or [])
|
| 57 |
|
| 58 |
# Extract the final response and audio files from the result
|
| 59 |
final_response = result.get("final_response", "")
|
| 60 |
output_audio_files = result.get("output_audio_files", [])
|
| 61 |
|
| 62 |
+
# Add assistant response to history (only final_response)
|
| 63 |
+
history.append({"role": "assistant", "content": final_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
return history, output_audio_files
|
|
|
|
| 66 |
|
| 67 |
except Exception as e:
|
| 68 |
history.append({"role": "assistant", "content": f"❌ **Error**: {e}"})
|
| 69 |
+
return history, []
|
|
|
|
| 70 |
|
| 71 |
def bot_response_sync(history, audio_file_urls):
|
| 72 |
"""
|
|
|
|
| 84 |
title="Audio Agent - Professional Audio Processing",
|
| 85 |
theme=gr.themes.Soft(),
|
| 86 |
css="""
|
| 87 |
+
.output-audio {
|
| 88 |
background: #f0f9ff;
|
| 89 |
border: 1px solid #0891b2;
|
| 90 |
border-radius: 8px;
|
|
|
|
| 130 |
- "How does audio normalization work?"
|
| 131 |
""")
|
| 132 |
|
| 133 |
+
# Output audio files section
|
| 134 |
+
with gr.Row():
|
| 135 |
+
with gr.Column():
|
| 136 |
+
gr.Markdown("### 🎵 Generated Audio Files")
|
| 137 |
+
output_audio_files = gr.File(
|
| 138 |
+
file_count="multiple",
|
| 139 |
+
label="Download Generated Audio",
|
| 140 |
+
interactive=False,
|
| 141 |
+
visible=True,
|
| 142 |
+
elem_classes=["output-audio"]
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
with gr.Row():
|
| 146 |
msg = gr.Textbox(
|
| 147 |
label="Describe what you want to do",
|
|
|
|
| 190 |
new_msg, new_files, updated_history, audio_urls = user_input(message, files, history)
|
| 191 |
return new_msg, new_files, updated_history, audio_urls
|
| 192 |
|
| 193 |
+
def handle_bot_response(history, audio_urls):
|
| 194 |
+
updated_history, output_files = bot_response_sync(history, audio_urls)
|
| 195 |
+
return updated_history, output_files
|
| 196 |
+
|
| 197 |
msg.submit(
|
| 198 |
handle_submit,
|
| 199 |
[msg, audio_files, chatbot],
|
| 200 |
[msg, audio_files, chatbot, audio_urls_state],
|
| 201 |
queue=False
|
| 202 |
).then(
|
| 203 |
+
handle_bot_response,
|
| 204 |
[chatbot, audio_urls_state],
|
| 205 |
+
[chatbot, output_audio_files]
|
| 206 |
)
|
| 207 |
|
| 208 |
send_btn.click(
|
|
|
|
| 211 |
[msg, audio_files, chatbot, audio_urls_state],
|
| 212 |
queue=False
|
| 213 |
).then(
|
| 214 |
+
handle_bot_response,
|
| 215 |
[chatbot, audio_urls_state],
|
| 216 |
+
[chatbot, output_audio_files]
|
| 217 |
)
|
| 218 |
|
| 219 |
# Clear chat
|
| 220 |
def clear_chat():
|
| 221 |
+
return [], [], [], []
|
| 222 |
|
| 223 |
clear_btn.click(
|
| 224 |
clear_chat,
|
| 225 |
None,
|
| 226 |
+
[chatbot, audio_files, audio_urls_state, output_audio_files],
|
| 227 |
queue=False
|
| 228 |
)
|
| 229 |
|