Lyria で音楽を生成する

Lyria を使用すると、プロンプトから新しい音楽トラックを生成できます。サポートされているインターフェースには、 Google Cloud コンソールと Gemini Enterprise Agent Platform API の Gemini API があります。

Agent Platform で Lyria を試す(Agent Platform Studio)

始める前に

  1. Google Cloud アカウントにログインします。 Google Cloudを初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $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 をインストールします。

      外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

    詳細については、 Google Cloud 認証ドキュメントの REST を使用して認証するをご覧ください。

コンソール

  1. Google Cloud コンソールで、[Vertex AI Studio] > [メディアを生成] > [メディアを生成] ページに移動します。

    スタジオを生成

  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 メソッドと URL:

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

次のステップ