From b8d7d44fba331aab2f9783a89a1c3fc79db4c0c8 Mon Sep 17 00:00:00 2001 From: priyanshm07 Date: Thu, 17 Apr 2025 16:00:32 +0530 Subject: [PATCH] Add ModelsLab Music Generator Agent --- .../README.md | 43 ++++++++++ .../models_lab_music_generator_agent.py | 85 +++++++++++++++++++ .../requirements.txt | 3 + 3 files changed, 131 insertions(+) create mode 100644 ai_agent_tutorials/ai_models_lab_music_generator_agent/README.md create mode 100644 ai_agent_tutorials/ai_models_lab_music_generator_agent/models_lab_music_generator_agent.py create mode 100644 ai_agent_tutorials/ai_models_lab_music_generator_agent/requirements.txt diff --git a/ai_agent_tutorials/ai_models_lab_music_generator_agent/README.md b/ai_agent_tutorials/ai_models_lab_music_generator_agent/README.md new file mode 100644 index 0000000..e6ff7ff --- /dev/null +++ b/ai_agent_tutorials/ai_models_lab_music_generator_agent/README.md @@ -0,0 +1,43 @@ +## ModelsLab Music Generator + +This is a Streamlit-based application that allows users to generate music using the ModelsLab API and OpenAI's GPT-4 model. Users can input a prompt describing the type of music they want to generate, and the application will generate a music track in MP3 format based on the given prompt. + +## Features + +- **Generate Music**: Enter a detailed prompt for music generation (genre, instruments, mood, etc.), and the app will generate a music track. +- **MP3 Output**: The generated music will be in MP3 format, available for listening or download. +- **User-Friendly Interface**: Simple and clean Streamlit UI for ease of use. +- **API Key Integration**: Requires both OpenAI and ModelsLab API keys to function. API keys are entered in the sidebar for authentication. + +## Setup + +### Requirements + +1. **API Keys**: + - **OpenAI API Key**: Sign up at [OpenAI](https://platform.openai.com/api-keys) to obtain your API key. + - **ModelsLab API Key**: Sign up at [ModelsLab](https://modelslab.com/dashboard/api-keys) to get your API key. + +2. **Python 3.8+**: Ensure you have Python 3.8 or higher installed. + +### Installation +1. Clone this repository: + ```bash + git clone https://github.com/Shubhamsaboo/awesome-llm-apps + cd ai_agent_tutorials/ai_models_lab_music_generator_agent + ``` + +2. Install the required Python packages: + ```bash + pip install -r requirements.txt + ``` +### Running the App + +1. Start the Streamlit app: + ```bash + streamlit run models_lab_music_generator_agent.py + ``` + +2. In the app interface: + - Enter a music generation prompt + - Click "Generate Music" + - Play the music & Download it. \ No newline at end of file diff --git a/ai_agent_tutorials/ai_models_lab_music_generator_agent/models_lab_music_generator_agent.py b/ai_agent_tutorials/ai_models_lab_music_generator_agent/models_lab_music_generator_agent.py new file mode 100644 index 0000000..9687958 --- /dev/null +++ b/ai_agent_tutorials/ai_models_lab_music_generator_agent/models_lab_music_generator_agent.py @@ -0,0 +1,85 @@ +import os +from uuid import uuid4 + +import requests +from agno.agent import Agent, RunResponse +from agno.models.openai import OpenAIChat +from agno.tools.models_labs import FileType, ModelsLabTools +from agno.utils.log import logger +import streamlit as st + +# Sidebar: User enters the API keys +st.sidebar.title("API Key Configuration") + +openai_api_key = st.sidebar.text_input("Enter your OpenAI API Key", type="password") +models_lab_api_key = st.sidebar.text_input("Enter your ModelsLab API Key", type="password") + +# Streamlit App UI +st.title("🎶 ModelsLab Music Generator") +prompt = st.text_area("Enter a music generation prompt:", "Generate a 30 second classical music piece", height=100) + +# Initialize agent only if both API keys are provided +if openai_api_key and models_lab_api_key: + agent = Agent( + name="ModelsLab Music Agent", + agent_id="ml_music_agent", + model=OpenAIChat(id="gpt-4o", api_key=openai_api_key), # Pass OpenAI API key here + 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 + description="You are an AI agent that can generate music using the ModelsLabs API.", + instructions=[ + "When generating music, use the `generate_media` tool with detailed prompts that specify:", + "- The genre and style of music (e.g., classical, jazz, electronic)", + "- The instruments and sounds to include", + "- The tempo, mood and emotional qualities", + "- The structure (intro, verses, chorus, bridge, etc.)", + "Create rich, descriptive prompts that capture the desired musical elements.", + "Focus on generating high-quality, complete instrumental pieces.", + ], + markdown=True, + debug_mode=True, + ) + + # Disable the button if either API key is missing + disable_button = not openai_api_key or not models_lab_api_key + + if st.button("Generate Music", disabled=disable_button): + if prompt.strip() == "": + st.warning("Please enter a prompt first.") + else: + with st.spinner("Generating music... 🎵"): + try: + music: RunResponse = agent.run(prompt) + + if music.audio and len(music.audio) > 0: + save_dir = "audio_generations" + os.makedirs(save_dir, exist_ok=True) + + # Download the generated audio + url = music.audio[0].url + response = requests.get(url) + filename = f"{save_dir}/music_{uuid4()}.mp3" + with open(filename, "wb") as f: + f.write(response.content) + + # Streamlit audio player + st.success("Music generated successfully! 🎶") + audio_bytes = open(filename, "rb").read() + st.audio(audio_bytes, format="audio/mp3") + + # Optional download button + st.download_button( + label="Download Music", + data=audio_bytes, + file_name="generated_music.mp3", + mime="audio/mp3" + ) + else: + st.error("No audio generated. Please try again.") + + except Exception as e: + st.error(f"An error occurred: {e}") + logger.error(f"Streamlit app error: {e}") +else: + # Show warning if keys are not entered + st.sidebar.warning("Please enter both the OpenAI and ModelsLab API keys to use the app.") diff --git a/ai_agent_tutorials/ai_models_lab_music_generator_agent/requirements.txt b/ai_agent_tutorials/ai_models_lab_music_generator_agent/requirements.txt new file mode 100644 index 0000000..5390220 --- /dev/null +++ b/ai_agent_tutorials/ai_models_lab_music_generator_agent/requirements.txt @@ -0,0 +1,3 @@ +agno==1.2.8 +Requests==2.32.3 +streamlit==1.44.1