Skip to main content

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

1

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)
2

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
3

Music Generation

ModelsLab API generates the music:
  • Synthesizes audio based on prompt
  • Creates instrumental composition
  • Renders in MP3 format
  • Returns high-quality audio file
4

Playback & Download

User receives the generated music:
  • Instant in-app playback
  • Download as MP3 file
  • Save to local library

Setup

1

Clone the Repository

2

Install Dependencies

Required packages:
  • agno>=2.2.10 - Agent framework
  • Requests==2.32.3 - HTTP library
  • streamlit==1.44.1 - Web interface
  • openai==2.8.1 - OpenAI API client
3

Get API Keys

OpenAI API Key:ModelsLab API Key:
  • Sign up at ModelsLab
  • Get your API key for music generation
4

Run the Application

Navigate to http://localhost:8501 in your browser

Usage

Generating Music

1

Enter API Keys

In the sidebar, enter:
  • OpenAI API Key
  • ModelsLab API Key
2

Write Prompt

Describe the music you want to create:
3

Generate

Click “Generate Music” and wait while the AI creates your track
4

Listen & Download

  • Play the generated music in your browser
  • Download the MP3 file to your computer

Example Prompts

Output: Elegant orchestral piece with string instruments
Output: High-energy EDM track with modern synths
Output: Peaceful, atmospheric soundscape
Output: Energetic rock instrumental with guitar focus
Output: Smooth jazz piece with piano improvisation
Output: Chill lo-fi beat with retro aesthetic

Code Example

Agent Configuration

Music Generation Flow

Complete Streamlit App

Advanced Features

ModelsLab Tools Integration

The Agno framework provides seamless ModelsLab integration:
Features:
  • Automatic API handling
  • Built-in error handling
  • Progress tracking
  • Multiple format support

Debug Mode

Enable debug mode for detailed agent output:
Provides:
  • Tool call information
  • API request details
  • Response parsing logs
  • Error traces

File Management

Writing Effective Prompts

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

Be Descriptive: The more detailed your prompt, the better the results. Include genre, instruments, mood, tempo, and structure.
API Costs: Each music generation uses credits from both OpenAI (for prompt processing) and ModelsLab (for audio generation). Monitor your usage.
Generation Time: Music generation typically takes 10-30 seconds depending on complexity. The app shows a spinner during generation.

Troubleshooting

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
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
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
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