使用 Lyria 生成音樂

你可以使用 Lyria,根據提示詞生成新穎的音樂曲目。 支援的介面包括 Gemini Enterprise Agent Platform API 中的 Google Cloud 控制台和 Gemini API。

在 Agent Platform (Agent Platform Studio) 上試用 Lyria

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Enable the Gemini Enterprise Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  4. 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 the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  5. Enable the Gemini Enterprise Agent Platform API.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. 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.

    Enable the API

  6. 為環境設定驗證機制。

    選取這個頁面上的分頁,瞭解您打算如何使用範例:

    控制台

    使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。

    REST

    如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

      安裝 Google Cloud CLI。

      若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

    詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。

控制台

  1. 在 Google Cloud 控制台中,依序前往「Vertex AI Studio」>「Generate Media」>「Generate media」頁面。

    生成錄音室

  2. 按一下「音樂」

  3. 選取「工作」選單中的「文字轉音樂」

  4. 從「模型」選單中,選取列出的模型選項。

  5. 選用:在「輸入資產」部分中,按一下「新增」,即可為這項要求新增資產。

  6. 在「提示詞」方塊中,以美式英文輸入文字提示,描述要生成的音樂。

  7. 按一下「執行」

    你可以預覽生成的音訊片段,並下載為 WAV 檔案。

Lyria 3 REST

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID:代表 Google Cloud 專案 ID 的字串。
  • MODEL_ID:代表要使用模型 ID 的字串。可接受的值如下:

    • lyria-3-clip-preview
    • lyria-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 參考資料

如要生成音樂,請將 POST 要求傳送至模型的 predict 端點。

要求:

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

後續步驟