Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

AI Gateway 経由で Pruna P-video を使う

最終更新 Markdown で表示Agent セットアップ

このチュートリアルでは、AI Gateway 経由で Replicate 上の Pruna の P-video モデルを呼び出す方法を説明します。

前提条件

1. Replicate の API トークンを取得する

  1. replicate.com を開き、アカウントを作成します。
  2. ログイン後、replicate.com/settings/api-tokens を開きます。
  3. Create token を選択し、名前を付けます。
  4. トークンをコピーし、安全な場所に保管します。

2. AI Gateway を作成する

AI Gateway を開く ↗
  1. Cloudflare ダッシュボード にログインし、アカウントを選択します。
  2. AI > AI Gateway を開きます。
  3. Create Gateway を選択します。
  4. Gateway name を入力します。ゲートウェイ名は 64 文字までです。
  5. Workers AI Billing で、このゲートウェイ経由の Workers AI リクエストの課金方法を選びます。
    • Standard billing は、各請求サイクルの終わりに Cloudflare アカウントへ課金します。
    • Unified billing は、前払いの AI Gateway クレジット残高からリアルタイムで差し引きます。
  6. Create を選択します。

API で AI Gateway を設定するには、次の手順を行います。

  1. 次の権限を持つ API トークンを作成 します。

    • AI Gateway - Read
    • AI Gateway - Edit
  2. Account ID を取得します。

  3. その API トークンと Account ID を使い、Cloudflare API へ POST リクエスト を送ります。

後の手順で使う Account IDGateway name を控えます。

ゲートウェイに認証を追加する場合は、Authenticated Gateway を参照してください。

3. ゲートウェイ URL を組み立てる

標準の Replicate API ベース URL を、AI Gateway の URL に置き換えます。

# Instead of:
https://api.replicate.com/v1

# Use:
https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate

たとえば、アカウント ID が abc123、ゲートウェイが my-gateway の場合は次のとおりです。

https://gateway.ai.cloudflare.com/v1/abc123/my-gateway/replicate

4. 動画を生成する

P-video の予測は、通常 30 秒以内に完了します。Replicate の同期上限 60 秒未満なので、Prefer: wait ヘッダーを使い、1 回の呼び出しでリクエスト送信と結果取得ができます。

curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions \
  --header "Authorization: Bearer {replicate_api_token}" \
  --header "cf-aig-authorization: Bearer {cloudflare_api_token}" \
  --header "Content-Type: application/json" \
  --header "Prefer: wait" \
  --data '{
    "version": "prunaai/p-video",
    "input": {
      "prompt": "A cat walking through a field of flowers in slow motion",
      "duration": 5,
      "aspect_ratio": "16:9",
      "resolution": "720p",
      "fps": 24
    }
  }'
  • Authorization — Replicate の API トークンです(Replicate への認証に使います)。
  • cf-aig-authorization — Cloudflare の API トークンです(認証済みゲートウェイ向けです)。
  • Prefer: wait — すぐに返すのではなく、予測が完了するまで待機します。

利用できる入力パラメーターの一覧は、Replicate の prunaai/p-video モデルページ を確認してください。

予測が完了すると、レスポンスの output フィールドに、生成された動画ファイルの URL が含まれます。

5.(任意)長いリクエストでは非同期ポーリングを使う

リクエストが 60 秒を超える可能性がある場合(長い duration や高い解像度など)は、非同期モードを使います。Prefer: wait ヘッダーなしでリクエストを送ります。

curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions \
  --header "Authorization: Bearer {replicate_api_token}" \
  --header "cf-aig-authorization: Bearer {cloudflare_api_token}" \
  --header "Content-Type: application/json" \
  --data '{
    "version": "prunaai/p-video",
    "input": {
      "prompt": "A cat walking through a field of flowers in slow motion",
      "duration": 5,
      "aspect_ratio": "16:9",
      "resolution": "720p",
      "fps": 24
    }
  }'

レスポンスには予測の id が含まれます。

{
  "id": "xyz789...",
  "status": "starting",
  "urls": {
    "get": "https://api.replicate.com/v1/predictions/xyz789...",
    "cancel": "https://api.replicate.com/v1/predictions/xyz789.../cancel"
  }
}

予測のステータスを、完了するまでポーリングします。

curl https://gateway.ai.cloudflare.com/v1/{account_id}/{gateway_id}/replicate/predictions/{prediction_id} \
  --header "Authorization: Bearer {replicate_api_token}" \
  --header "cf-aig-authorization: Bearer {cloudflare_api_token}"

statussucceeded(または failed)になるまでポーリングを続けます。完了すると、output フィールドに生成された動画ファイルの URL が含まれます。

次のステップ

ここから次のことができます。

役に立ちましたか?