您可以使用 Lyria,根據提示生成新穎的音樂曲目。 支援的介面包括 Cloud de Confiance 控制台和 Gemini API。
在 Agent Platform (Agent Platform Studio) 試用 Lyria
事前準備
-
In the Cloud de Confiance console, on the project selector page, select or create a Cloud de Confiance 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, if it is not already enabled.
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.-
設定環境的驗證機制。
請根據您打算如何使用本頁範例,選取對應的分頁標籤:
控制台
使用 Cloud de Confiance 控制台存取 Cloud de Confiance by S3NS 服務和 API 時,您不需要設定驗證機制。
REST
如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。
安裝 Google Cloud CLI,然後 使用聯合身分登入 gcloud CLI。
詳情請參閱 Cloud de Confiance 驗證說明文件中的「使用 REST 進行驗證」一節。
控制台
在 Cloud de Confiance 控制台中,依序前往「Vertex AI Studio」 > 「Generate Media」 > 「Generate media」頁面。
按一下「音樂」。
在「工作」選單中選取「文字轉音樂」。
從「模型」選單中,選取列出的選項。
選用:在「Input assets」(輸入資產) 區段中,按一下「Add」(新增),為這項要求新增資產。
在「Prompt」(提示詞) 方塊中,以美式英文輸入文字提示詞,描述要生成的音樂。
按一下「執行」。
生成的音訊片段可供預覽,並可下載為 WAV 檔案。
Lyria 3 REST
使用任何要求資料之前,請先修改下列項目的值:
-
PROJECT_ID:代表 Cloud de Confiance by S3NS 專案 ID 的字串。 -
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 音樂生成提示詞指南。
探索 Lyria API 參考資料。