Chapter 28
Lyria 2 Music Generation
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.Lyria 2 Music Generation
Share to:
| Author |
|---|
| Katie Nguyen |
Overview
Lyria 2
Lyria 2 on Agent Platform is Google's latest music generation model. It is capable of generating high-fidelity audio tracks across a range of genres, developed with input from musicians and producers.
In this tutorial, you will learn how to interact with Lyria 2 to generate music from text prompts that showcase:
- Various styles and genres
- How to play with moods and emotions in audio
- Different tempos and instrumentation
Get started
Authenticate your notebook environment (Colab only)
If you are running this notebook on Google Colab, run the following cell to authenticate your environment.
import sys
if "google.colab" in sys.modules:
from google.colab import auth
auth.authenticate_user()Set Google Cloud project information
To get started using Agent Platform, you must have an existing Google Cloud project and enable the Agent Platform API.
Learn more about setting up a project and a development environment.
PROJECT_ID = "[your-project-id]" # @param {type:"string"}Import libraries
import base64
from IPython.display import Audio
import google.auth
import google.auth.transport.requests
import requestsDefine helper functions
def send_request_to_google_api(api_endpoint, data=None):
"""
Sends an HTTP request to a Google API endpoint.
Args:
api_endpoint: The URL of the Google API endpoint.
data: (Optional) Dictionary of data to send in the request body (for POST, PUT, etc.).
Returns:
The response from the Google API.
"""
# Get access token calling API
creds, project = google.auth.default()
auth_req = google.auth.transport.requests.Request()
creds.refresh(auth_req)
access_token = creds.token
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
response = requests.post(api_endpoint, headers=headers, json=data)
response.raise_for_status()
return response.json()
def generate_music(request: dict):
req = {"instances": [request], "parameters": {}}
print(req)
resp = send_request_to_google_api(music_model, req)
return resp["predictions"]
def play_audio(preds):
for pred in preds:
bytes_b64 = dict(pred)["bytesBase64Encoded"]
decoded_audio_data = base64.b64decode(bytes_b64)
audio = Audio(decoded_audio_data, rate=48000, autoplay=False)
display(audio)Load the audio model
music_model = f"https://us-central1-aiplatform.googleapis.com/v1/projects/{PROJECT_ID}/locations/us-central1/publishers/google/models/lyria-002:predict"Generate music from text prompts
Explore various genres
When prompting Lyria 2 it's helpful to consider the overall style of music you want to generate. Consider options such as: classical, electronic, rock, jazz, hip hop, or pop. You can even describe more general styles that include cinematic, ambient, or lo-fi.
With Lyria 2, you can generate a 30 second WAV audio at a 48kHz sample rate from a text prompt. In order to generate an audio clip in the following sample, specify the following info:
- Prompt: A detailed description of the music you would like to generate.
- Negative prompt: Optionally, you can specify a description of what to exclude from the generated audio.
- Sample count: The number of audio samples to generate.
prompt = "Smooth, atmospheric jazz. Moderate tempo, rich harmonies. Featuring mellow brass" # @param {type:"string"}
negative_prompt = "fast" # @param {type:"string"}
sample_count = 2 # @param {type:"number"}
music = generate_music(
{"prompt": prompt, "negative_prompt": negative_prompt, "sample_count": sample_count}
)
play_audio(music)Describe mood and emotion
Another attribute to consider is the mood or emotion you want in your generated music. Describe the desired feeling or atmosphere with words like happy, melancholy, energetic, calm, tense, or dreamy.
When generating music with Lyria 2 you can also set a seed value for deterministic generation. If provided, the model will attempt to produce the same audio with the same prompt and other parameters.
Note: seed and sample_count cannot be set in the same request.
prompt = "Dramatic dance symphony" # @param {type:"string"}
negative_prompt = "" # @param {type:"string"}
seed = 111 # @param {type:"number"}
music = generate_music(
{"prompt": prompt, "negative_prompt": negative_prompt, "seed": seed}
)
play_audio(music)Play with tempo and instrumentation
It can also help to include descriptions of the tempo or rhythm, such as fast, slow, or syncopated. You might also include specific instruments such as a piano, synthesizer, acoustic guitar, drums, strings, or flute.
By default, all music generated with Lyria utilizes SynthID, a technology that embeds an inaudible watermark directly into its waveform.
prompt = "Acoustic guitar melody with a fast tempo" # @param {type:"string"}
music = generate_music({"prompt": prompt})
play_audio(music)