Merge pull request #255 from abhaysingh1122/fix/music-generator-audio-playback

Fix/music generator audio playback
This commit is contained in:
Shubham Saboo 2025-06-28 21:41:33 -05:00 committed by GitHub
commit 96f1ce349a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,5 @@
import os import os
from uuid import uuid4 from uuid import uuid4
import requests import requests
from agno.agent import Agent, RunResponse from agno.agent import Agent, RunResponse
from agno.models.openai import OpenAIChat from agno.models.openai import OpenAIChat
@ -23,9 +22,9 @@ if openai_api_key and models_lab_api_key:
agent = Agent( agent = Agent(
name="ModelsLab Music Agent", name="ModelsLab Music Agent",
agent_id="ml_music_agent", agent_id="ml_music_agent",
model=OpenAIChat(id="gpt-4o", api_key=openai_api_key), # Pass OpenAI API key here model=OpenAIChat(id="gpt-4o", api_key=openai_api_key),
show_tool_calls=True, show_tool_calls=True,
tools=[ModelsLabTools(api_key=models_lab_api_key, wait_for_completion=True, file_type=FileType.MP3)], # Pass ModelsLab API key here tools=[ModelsLabTools(api_key=models_lab_api_key, wait_for_completion=True, file_type=FileType.MP3)],
description="You are an AI agent that can generate music using the ModelsLabs API.", description="You are an AI agent that can generate music using the ModelsLabs API.",
instructions=[ instructions=[
"When generating music, use the `generate_media` tool with detailed prompts that specify:", "When generating music, use the `generate_media` tool with detailed prompts that specify:",
@ -40,10 +39,7 @@ if openai_api_key and models_lab_api_key:
debug_mode=True, debug_mode=True,
) )
# Disable the button if either API key is missing if st.button("Generate Music"):
disable_button = not openai_api_key or not models_lab_api_key
if st.button("Generate Music", disabled=disable_button):
if prompt.strip() == "": if prompt.strip() == "":
st.warning("Please enter a prompt first.") st.warning("Please enter a prompt first.")
else: else:
@ -55,19 +51,31 @@ if openai_api_key and models_lab_api_key:
save_dir = "audio_generations" save_dir = "audio_generations"
os.makedirs(save_dir, exist_ok=True) os.makedirs(save_dir, exist_ok=True)
# Download the generated audio
url = music.audio[0].url url = music.audio[0].url
response = requests.get(url) response = requests.get(url)
# 🛡️ Validate response
if not response.ok:
st.error(f"Failed to download audio. Status code: {response.status_code}")
st.stop()
content_type = response.headers.get("Content-Type", "")
if "audio" not in content_type:
st.error(f"Invalid file type returned: {content_type}")
st.write("🔍 Debug: Downloaded content was not an audio file.")
st.write("🔗 URL:", url)
st.stop()
# ✅ Save audio
filename = f"{save_dir}/music_{uuid4()}.mp3" filename = f"{save_dir}/music_{uuid4()}.mp3"
with open(filename, "wb") as f: with open(filename, "wb") as f:
f.write(response.content) f.write(response.content)
# Streamlit audio player # 🎧 Play audio
st.success("Music generated successfully! 🎶") st.success("Music generated successfully! 🎶")
audio_bytes = open(filename, "rb").read() audio_bytes = open(filename, "rb").read()
st.audio(audio_bytes, format="audio/mp3") st.audio(audio_bytes, format="audio/mp3")
# Optional download button
st.download_button( st.download_button(
label="Download Music", label="Download Music",
data=audio_bytes, data=audio_bytes,
@ -80,6 +88,6 @@ if openai_api_key and models_lab_api_key:
except Exception as e: except Exception as e:
st.error(f"An error occurred: {e}") st.error(f"An error occurred: {e}")
logger.error(f"Streamlit app error: {e}") logger.error(f"Streamlit app error: {e}")
else: else:
# Show warning if keys are not entered
st.sidebar.warning("Please enter both the OpenAI and ModelsLab API keys to use the app.") st.sidebar.warning("Please enter both the OpenAI and ModelsLab API keys to use the app.")