Overview
The AI Music Generator Agent is a Streamlit application that creates custom music tracks based on detailed text prompts. Powered by ModelsLab’s music generation API and OpenAI’s GPT-4o, it transforms your musical vision into high-quality MP3 files that you can listen to and download.Features
Text-to-Music Generation
Generate music from detailed text descriptions:
- Genre and style specification
- Instrument selection
- Mood and tempo control
- Structure definition (intro, verse, chorus, etc.)
High-Quality Output
- Professional MP3 format
- 30-second to full-length tracks
- Studio-quality audio
- Immediate playback
Intelligent Agent
- GPT-4o enhances your prompts
- Automatic musical element detection
- Optimized prompt engineering
- Rich, descriptive generation
Easy to Use
- Simple web interface
- Instant audio playback
- One-click download
- No music theory required
How It Works
Prompt Input
User enters a music generation prompt describing:
- Genre (classical, jazz, electronic, rock, etc.)
- Instruments (piano, guitar, synthesizer, drums)
- Mood (upbeat, melancholic, energetic, calm)
- Structure (intro, verses, chorus, bridge, outro)
AI Enhancement
GPT-4o agent processes the prompt:
- Expands basic descriptions into rich prompts
- Adds musical context and details
- Ensures all necessary elements are specified
- Optimizes for ModelsLab API
Music Generation
ModelsLab API generates the music:
- Synthesizes audio based on prompt
- Creates instrumental composition
- Renders in MP3 format
- Returns high-quality audio file
Setup
Install Dependencies
agno>=2.2.10- Agent frameworkRequests==2.32.3- HTTP librarystreamlit==1.44.1- Web interfaceopenai==2.8.1- OpenAI API client
Get API Keys
OpenAI API Key:
- Sign up at OpenAI Platform
- Generate an API key from your dashboard
- Sign up at ModelsLab
- Get your API key for music generation
Usage
Generating Music
Example Prompts
Classical Music
Classical Music
Electronic/EDM
Electronic/EDM
Ambient/Chill
Ambient/Chill
Rock/Guitar
Rock/Guitar
Jazz
Jazz
Lo-fi Hip Hop
Lo-fi Hip Hop
Code Example
Agent Configuration
Music Generation Flow
Complete Streamlit App
Advanced Features
ModelsLab Tools Integration
The Agno framework provides seamless ModelsLab integration:- Automatic API handling
- Built-in error handling
- Progress tracking
- Multiple format support
Debug Mode
Enable debug mode for detailed agent output:- Tool call information
- API request details
- Response parsing logs
- Error traces
File Management
Writing Effective Prompts
- Genre & Style
- Instruments
- Mood & Tempo
- Structure
Be specific about the musical genre:✅ Good: “Generate smooth jazz with piano and saxophone”❌ Too vague: “Make some jazz”Genres to try:
- Classical (baroque, romantic, contemporary)
- Jazz (bebop, smooth, fusion)
- Electronic (house, techno, ambient)
- Rock (classic, indie, alternative)
- Hip hop (lo-fi, trap, boom bap)
- World music (flamenco, bossa nova, etc.)
Use Cases
Content Creation
Generate background music for videos, podcasts, presentations, and social media content
Game Development
Create custom soundtracks and ambient music for indie games and interactive experiences
Prototyping
Quickly prototype musical ideas and explore different genres and styles
Meditation & Wellness
Generate calming soundscapes for meditation apps, yoga classes, and relaxation
Education
Create example tracks for music theory lessons and composition classes
Personal Projects
Make custom ringtones, alarm sounds, or personal mood playlists
Best Practices
Generation Time: Music generation typically takes 10-30 seconds depending on complexity. The app shows a spinner during generation.
Troubleshooting
No Audio Generated
No Audio Generated
Issue: Agent completes but no audio file is createdSolutions:
- Check ModelsLab API key is valid
- Ensure you have credits in ModelsLab account
- Try a simpler, shorter prompt
- Check debug output for error messages
Invalid File Type Error
Invalid File Type Error
Issue: Downloaded file is not audioSolutions:
- Verify ModelsLab API returned audio URL
- Check content-type header in response
- Ensure FileType.MP3 is set in tools config
- Review debug logs for API response
Generation Timeout
Generation Timeout
Issue: Generation takes too long or times outSolutions:
- Request shorter tracks (30 seconds)
- Simplify prompt complexity
- Check ModelsLab service status
- Ensure
wait_for_completion=True
Poor Quality Results
Poor Quality Results
Issue: Generated music doesn’t match expectationsSolutions:
- Make prompt more specific and detailed
- Reference specific genres and artists
- Describe instruments explicitly
- Specify tempo and mood clearly
API Cost Considerations
OpenAI API
Usage: ~200-500 tokens per generationCost: $0.01-0.03 per track with GPT-4oUsed for prompt enhancement and agent reasoning
ModelsLab API
Usage: Credit-based systemCost: Varies by track length and qualityCheck ModelsLab pricing for current rates
Next Steps
Experiment
Try different genres, instruments, and moods to discover what works best
Combine Agents
Use with other agents to create multimedia content (videos with custom soundtracks)
More Examples
Explore other AI agent examples
GitHub
View source code and contribute
