WaveSpeedAI Python SDKの使い方

WaveSpeedAI Python SDKの使い方

WaveSpeedAI Python SDKは、AI画像およびビデオ生成をPythonアプリケーションに統合する簡単な方法を提供します。このガイドでは、開始するために必要なすべてのことをカバーしています。

前提条件

始める前に、以下を確認してください:

インストール

pipを使用してSDKをインストールします:

pip install wavespeed

認証の設定

SDKがリクエストを認証するにはAPIキーが必要です。2つのオプションがあります:

オプション1:環境変数(推奨)

WAVESPEED_API_KEY環境変数を設定します:

export WAVESPEED_API_KEY="your-api-key-here"

その後、SDKを直接使用します:

import wavespeed

output = wavespeed.run(
    "wavespeed-ai/z-image/turbo",
    {"prompt": "Cat"}
)

オプション2:APIキーを直接渡す

ClientクラスをインポートしてコンストラクタにAPIキーを渡します:

from wavespeed import Client

client = Client(api_key="your-api-key-here")

最初の画像を生成する

Z-Image Turboを使用して画像を生成する完全な例です:

import wavespeed

output = wavespeed.run(
    "wavespeed-ai/z-image/turbo",
    {"prompt": "A serene mountain landscape at sunset with golden light"}
)

print(output["outputs"][0])  # URL to the generated image

run()関数は、リクエストの送信、完了のポーリング、結果の返却など、ワークフロー全体を処理します。

ファイルのアップロード

入力画像が必要なワークフロー(画像からビデオへなど)の場合、upload()関数を使用してWaveSpeedAIがアクセスできるURLを取得します:

import wavespeed

# ローカル画像ファイルをアップロード
image_url = wavespeed.upload("./my-image.png")

# アップロードされた画像をビデオ生成に使用
video = wavespeed.run(
    "wavespeed-ai/wan-2.1/image-to-video",
    {
        "image": image_url,
        "prompt": "Camera slowly zooms in while clouds move in the background"
    }
)

print(video["outputs"][0])  # URL to the generated video

設定オプション

クライアントオプション

クライアントを初期化するときにリトライ動作を構成します:

from wavespeed import Client

client = Client(
    api_key="your-api-key",
    max_retries=3,            # Max retries for failed requests
    max_connection_retries=5, # Max retries for connection errors
    retry_interval=1.0        # Seconds between retries
)

実行オプション

個々のrun()呼び出しを構成します:

import wavespeed

output = wavespeed.run(
    "wavespeed-ai/z-image/turbo",
    {"prompt": "A cute orange cat wearing a tiny hat"},
    timeout=60.0,           # Max seconds to wait for completion
    poll_interval=0.5,      # Seconds between status checks
    enable_sync_mode=True   # Use synchronous mode if available
)

異なるモデルの使用

テキストから画像へ

テキスト説明から画像を生成します:

import wavespeed

output = wavespeed.run(
    "wavespeed-ai/z-image/turbo",
    {
        "prompt": "A futuristic cityscape with flying cars and neon lights",
        "size": "1024x1024"
    }
)

画像からビデオへ

静止画をビデオに変換します:

import wavespeed

image_url = wavespeed.upload("./landscape.jpg")

video = wavespeed.run(
    "wavespeed-ai/wan-2.1/image-to-video",
    {
        "image": image_url,
        "prompt": "Gentle wind blowing through the trees"
    }
)

テキストからビデオへ

テキストから直接ビデオを生成します:

import wavespeed

video = wavespeed.run(
    "wavespeed-ai/wan-2.1/t2v-480p",
    {"prompt": "A golden retriever running through a field of flowers"}
)

非同期サポート

SDKは非ブロッキング操作のためのasync/awaitをサポートしています:

import asyncio
import wavespeed

async def generate_image():
    output = await wavespeed.async_run(
        "wavespeed-ai/z-image/turbo",
        {"prompt": "An astronaut riding a horse on Mars"}
    )
    return output["outputs"][0]

# 非同期関数を実行
image_url = asyncio.run(generate_image())
print(image_url)

エラーハンドリング

本番環境のアプリケーションでは、リトライを設定してエラーを適切に処理します:

from wavespeed import Client

client = Client(
    max_retries=3,
    max_connection_retries=5,
    retry_interval=1.0
)

try:
    output = client.run(
        "wavespeed-ai/z-image/turbo",
        {"prompt": "A beautiful sunset over the ocean"},
        timeout=120.0
    )
    print("Generated:", output["outputs"][0])
except Exception as e:
    print(f"Generation failed: {e}")

リソース

今日からWaveSpeedAIで構築を開始し、AIを利用した画像およびビデオ生成をPythonアプリケーションにもたらしましょう。