Spaces:
Sleeping
Sleeping
File size: 9,522 Bytes
63d9bd0 0ce56db 7e4a2f2 3de52ef 63d9bd0 5a5e484 63d9bd0 c008cce 7e4a2f2 c008cce 7e4a2f2 c008cce 5a5e484 7e4a2f2 5a5e484 63d9bd0 399d0c1 63d9bd0 5a5e484 7e4a2f2 399d0c1 23c0e5d 7e4a2f2 c008cce 7e4a2f2 399d0c1 c008cce 399d0c1 c008cce 7e4a2f2 c008cce 7e4a2f2 23c0e5d 7e4a2f2 23c0e5d 72ef847 23c0e5d 5a5e484 23c0e5d 7e4a2f2 23c0e5d 72ef847 a19db28 72ef847 5a5e484 7e4a2f2 72ef847 a19db28 7e4a2f2 23c0e5d a19db28 7e4a2f2 5e87361 23c0e5d 5a5e484 211e40f 5720b20 7e4a2f2 23c0e5d 7e4a2f2 23c0e5d 7e4a2f2 23c0e5d 7e4a2f2 a19db28 7e4a2f2 63d9bd0 19741de 5a5e484 72ef847 7e4a2f2 23c0e5d 72ef847 23c0e5d 72ef847 63d9bd0 7e4a2f2 399d0c1 7e4a2f2 c008cce 399d0c1 9afb0c3 399d0c1 7e4a2f2 23c0e5d 5e87361 23c0e5d 7e4a2f2 399d0c1 f3a78e2 399d0c1 5ffb587 399d0c1 c008cce 399d0c1 f3a78e2 5720b20 7e4a2f2 399d0c1 5a5e484 7e4a2f2 19741de 7e4a2f2 5a5e484 7e4a2f2 72ef847 5a5e484 7e4a2f2 23c0e5d 5a5e484 7e4a2f2 72ef847 399d0c1 7e4a2f2 72ef847 7e4a2f2 23c0e5d 72ef847 9afb0c3 7e4a2f2 c008cce 63d9bd0 7e4a2f2 63d9bd0 5799f8d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
import asyncio
import gradio as gr
import os
from agent import AudioAgent
# Global agent instance
agent = None
# Global demo instance
demo = None
def get_share_url(path):
"""Get the share URL for a given path"""
agent_url = os.environ.get('AGENT_URL')
if agent_url:
return f"{agent_url}/gradio_api/file={path}"
if demo:
return f"{demo.share_url}/gradio_api/file={path}"
return path
def update_agent(model_name, temperature, api_key):
"""Update the agent with new configuration"""
global agent
try:
agent = AudioAgent(
model_name=model_name,
temperature=float(temperature),
api_key=api_key
)
return True, None
except Exception as e:
return False, str(e)
def user_input(user_message, audio_files, history, custom_history, model_name, temperature, api_key):
"""
Handle user input with text and audio files
"""
# Try to update agent configuration
success, error = update_agent(model_name, temperature, api_key)
if not success:
raise gr.Error(error)
if not user_message.strip() and not audio_files:
return "", audio_files, history, custom_history
# Process audio files into URLs
audio_file_urls = []
if audio_files:
for audio_file in audio_files:
if hasattr(audio_file, 'name'):
file_path = audio_file.name
else:
file_path = str(audio_file)
audio_file_urls.append(get_share_url(file_path))
# Add user message to history with input files
history.append({
"role": "user",
"content": user_message,
})
# Update custom history
custom_history.append({
"role": "user",
"content": user_message,
"input_files": audio_file_urls
})
return "", audio_files, history, custom_history
async def bot_response(history, audio_file_urls, custom_history):
"""
Generate bot response using the agent
"""
if not agent:
raise gr.Error("Please configure the agent first")
if not history or history[-1]["role"] != "user":
return history, []
# Get the user message and input files
user_message = custom_history[-1]["content"]
input_files = custom_history[-1].get("input_files", [])
# If message is empty but we have audio files, provide default message
if not user_message.strip() and audio_file_urls:
user_message = "Please process these audio files"
try:
# Use the agent's run_agent method with history
result = await agent.run_agent(user_message, input_files, custom_history[:-1])
# Extract the final response and audio files from the result
final_response = result["final_response"]
output_audio_files = result["output_audio_files"]
# Add assistant response to history with output files
history.append({
"role": "assistant",
"content": final_response,
})
# Update custom history
custom_history.append({
"role": "assistant",
"content": final_response,
"output_files": output_audio_files
})
return history, output_audio_files
except Exception as e:
history.pop()
custom_history.pop()
raise gr.Error(str(e))
def bot_response_sync(history, audio_file_urls, custom_history):
"""
Synchronous wrapper for the async bot response
"""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(bot_response(history, audio_file_urls, custom_history))
finally:
loop.close()
def create_interface():
with gr.Blocks(
title="Audio Agent - Professional Audio Processing",
theme=gr.themes.Default(),
) as interface:
gr.Markdown("""
# Audio Agent - Your AI Audio Assistant
Upload your audio files and tell me what you need. I'll handle the rest!
""")
# Hidden state to store audio file URLs and custom history
audio_urls_state = gr.State([])
custom_history_state = gr.State([])
with gr.Row():
with gr.Column(scale=4):
chatbot = gr.Chatbot(
type="messages",
height=500,
show_copy_button=True,
show_share_button=False
)
msg = gr.Textbox(
label="Describe what you want to do?",
placeholder="e.g., 'Remove filler words and improve audio quality''",
lines=3,
submit_btn=True
)
with gr.Column(scale=1):
# Model Configuration
with gr.Group():
model_name = gr.Dropdown(
choices=["gpt-4.1", "gpt-4.1-mini", "gpt-4o", "o3"],
value="gpt-4.1",
label="Model",
info="Select the model to use"
)
temperature = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.3,
step=0.1,
label="Temperature",
info="Higher values make output more random"
)
api_key = gr.Textbox(
label="OpenAI API Key",
placeholder="sk-...",
type="password",
info="Your OpenAI API key"
)
# Set temperature to 1.0 when o3 model is selected
def update_temperature(model):
if model == "o3":
return gr.update(value=1.0, interactive=False)
return gr.update(interactive=True)
model_name.change(
update_temperature,
inputs=[model_name],
outputs=[temperature]
)
with gr.Group():
audio_files = gr.File(
file_count="multiple",
file_types=["audio"],
label="Upload Audio Files to Process",
height=150
)
output_audio_files = gr.File(
file_count="multiple",
file_types=["audio"],
label="Download Generated Audio",
height=150,
interactive=False,
visible=False # Start hidden
)
# Handle user input and bot response
def handle_submit(message, files, history, custom_history, model, temp, key):
new_msg, new_files, updated_history, updated_custom_history = user_input(
message, files, history, custom_history, model, temp, key
)
return new_msg, new_files, updated_history, updated_custom_history
def handle_bot_response(history, audio_urls, custom_history):
updated_history, output_files = bot_response_sync(history, audio_urls, custom_history)
output_visible = bool(output_files) # True if there are files, else False
return updated_history, gr.update(value=output_files, visible=output_visible), custom_history
msg.submit(
handle_submit,
[msg, audio_files, chatbot, custom_history_state, model_name, temperature, api_key],
[msg, audio_files, chatbot, custom_history_state],
queue=False
).then(
handle_bot_response,
[chatbot, audio_urls_state, custom_history_state],
[chatbot, output_audio_files, custom_history_state]
)
gr.Markdown("""
---
""")
with gr.Row():
gr.Markdown("""
## ποΈ What I Can Do For You
**Audio Manipulation:**
- Merge multiple audio files into one continuous track
- Cut or trim specific sections from any file
- Adjust volume levels (increase or decrease)
- Normalize audio levels for consistency
- Apply fade-in or fade-out effects for smooth transitions (Mono channel only)
- Change playback speed (faster or slower, with pitch change)
- Reverse audio for creative effects
- Remove silence from beginning or end of files
**Analysis & Transcription:** (English only)
- Transcribe speech in audio to text
- Analyze audio properties (duration, sample rate, etc.)
""")
gr.Markdown("""
## π‘ Example Requests
- *"Merge these two audio files and add a fade-in effect"*
- *"Remove the silence at the beginning of this recording"*
- *"Transcribe the speech in this audio file"*
- *"Increase the volume of the first track and normalize both files"*
- *"Cut out the middle section from 1:30 to 2:45"*
- *"Make this audio play 1.5x faster"*
- *"Apply a fade-out effect to the end of this track"*
""")
return interface
if __name__ == "__main__":
demo = create_interface()
demo.launch()
|