您可以使用 Lyria 根据提示生成新颖的音乐作品。 支持的界面包括 Google Cloud 控制台和 Gemini Enterprise Agent Platform API 中的 Gemini API。
在 Agent Platform 上试用 Lyria (Agent Platform Studio)
准备工作
- 登录您的 Google Cloud 账号。如果您是新手 Google Cloud, 请创建一个账号来评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Enable the Gemini Enterprise Agent Platform API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
Enable the Gemini Enterprise Agent Platform API.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
为您的环境设置身份验证。
选择标签页以了解您打算如何使用本页面上的示例:
控制台
当您使用 Google Cloud 控制台访问 Google Cloud 服务和 API 时,无需设置身份验证。
REST
如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的 凭证。
安装 Google Cloud CLI。
如果您使用的是外部身份提供方 (IdP),则必须先 使用联合身份登录 gcloud CLI。
如需了解详情,请参阅 身份验证文档中的使用 REST 时进行身份验证 。 Google Cloud
控制台
在 Google Cloud 控制台中,依次前往 Vertex AI Studio > 生成媒体 > 生成媒体 页面。
点击音乐 。
在任务 菜单中,选择 Text-to-music (文本转音乐)。
在模型 菜单中,从列出的选项中选择一个模型。
可选:在输入资产 部分中,点击添加 以为此请求添加资产。
在提示 框中,输入描述要生成的音乐的英文文本提示。
点击运行 。
生成的音频片段可供预览,并可下载为 WAV 文件。
Lyria 3 REST
在使用任何请求数据之前, 请先进行以下替换:
-
PROJECT_ID:表示您的项目 ID 的字符串。 Google Cloud -
MODEL_ID:表示要使用的模型 ID 的字符串 。接受的值如下:lyria-3-clip-previewlyria-3-pro-preview
-
TEXT_PROMPT:用于指导音乐创作的文本提示。 -
IMAGE_URI:可选: 输入图片所在的 Cloud Storage URI。 -
IMAGE_DATABYTES:可选:采用 Base64 编码的输入图片。
HTTP 方法和网址:
POST https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/interactions
请求 JSON 正文:
{
"model": "MODEL_ID",
"input": [
{
"type": "text",
"text": "TEXT_PROMPT"
},
{
"type": "image",
"mime_type": "image/jpeg",
"uri": "IMAGE_URI"
},
{
"type": "image",
"mime_type": "image/png",
"data": "IMAGE_DATABYTES"
},
]
}
如需发送请求,请选择以下方式之一:
curl
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/interactions"
PowerShell
将请求正文保存在名为 request.json 的文件中,然后执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/global/interactions" | Select-Object -Expand Content
{
"status": "completed",
"outputs": [
{
"text": "LYRICS",
"type": "text"
},
{
"text": "DESCRIPTION",
"type": "text"
},
{
"mime_type": "audio/mpeg",
"data": "GENERATED_SONG_DATABYTES",
"type": "audio"
},
{}
],
"role": "model",
"created": "CREATED_TIME",
"updated": "UPDATE_TIME",
"object": "interaction",
"model": "lyria-3-pro-preview"
}
Lyria 2 REST
使用 predict 方法发送音乐创作请求。响应将直接包含音频数据,如果响应是 JSON,则通常采用 base64 编码。
如需详细了解 lyria-002 模型请求,请参阅
lyria-002 模型 API 参考文档。
如需生成音乐,请向模型的 predict 端点发送 POST 请求。
请求:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/lyria-002:predict \
-d '{
"instances": [
{
"prompt": "An uplifting and hopeful orchestral piece with a soaring string melody and triumphant brass.",
"negative_prompt": " dissonant, minor key",
"seed": 12345
}
],
"parameters": {
// "sample_count": 1 // Use either seed or sample_count
}
}'
响应:
如果请求成功,则会返回一个包含生成的音频数据的 JSON 对象。 该对象应如下所示:
{
"predictions": [
{
"audioContent": "BASE64_ENCODED_WAV_STRING_SAMPLE_1",
"mimeType": "audio/wav"
},
{
"audioContent": "BASE64_ENCODED_WAV_STRING_SAMPLE_2",
"mimeType": "audio/wav"
}
],
"deployedModelId": "xxxxxxxxxxxxxxx",
"model": "projects/PROJECT_ID/locations/LOCATION/publishers/google/models/lyria-002",
"modelDisplayName": "Lyria 2"
}
然后,您需要对 audioContent (base64) 进行解码,以获取 WAV 音频文件。
每个片段的时长为 32.8 秒。
后续步骤
了解如何在 Lyria 音乐创作提示 指南 中撰写有效的提示