Add ModelsLab Music Generator Agent
This commit is contained in:
parent
668c27ae3f
commit
b8d7d44fba
3 changed files with 131 additions and 0 deletions
|
|
@ -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.
|
||||
|
|
@ -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.")
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
agno==1.2.8
|
||||
Requests==2.32.3
|
||||
streamlit==1.44.1
|
||||
Loading…
Reference in a new issue