クイックスタート: Azure OpenAI の Whisper モデルを使った音声テキスト変換

このクイック スタートでは、音声からテキストへの変換に Azure OpenAI の Whisperモデル を使用する方法について説明します。 Whisper モデルは、さまざまな言語での人間の音声を文字起こしすることができ、他の言語を英語に翻訳することもできます。

Whisper モデルのファイル サイズ制限は 25 MB です。 25 MB を超えるファイルを文字起こしする必要がある場合は、Azure AI 音声のバッチ文字起こし API を使用できます。

前提条件

設定

キーとエンドポイントを取得する

Azure OpenAI に対して正常に呼び出しを行うには、エンドポイントキーが必要です。

変数名
AZURE_OPENAI_ENDPOINT この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 Azure OpenAI Studio>[プレイグラウンド]>[コード ビュー] で値を確認することもできます。 エンドポイントの例: https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 KEY1 または KEY2 を使用できます。

Azure portal でリソースに移動します。 [エンドポイントとキー][リソース管理] セクションにあります。 エンドポイントとアクセス キーをコピーします。これらは、API 呼び出しを認証するために両方とも必要です。 KEY1 または KEY2 を使用できます。 常に 2 つのキーを用意しておくと、サービスを中断させることなく、キーのローテーションと再生成を安全に行うことができます。

Azure portal の Azure OpenAI リソースの概要 UI のスクリーンショット。エンドポイントおよびアクセス キーの場所が赤色の丸で囲まれています。

環境変数

キーとエンドポイントの永続的な環境変数を作成して割り当てます。

重要

API キーを使用する場合は、それを Azure Key Vault などの別の場所に安全に保存します。 API キーは、コード内に直接含めないようにし、絶対に公開しないでください。

AI サービスのセキュリティの詳細については、「Azure AI サービスに対する要求の認証」を参照してください。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

REST API の要求と応答を作成する

Bash シェルで、次のコマンドを実行します。 YourDeploymentName は、Whisper モデルのデプロイ時に選んだデプロイ名に置き換える必要があります。 デプロイ名は必ずしもモデル名と同じではありません。 基になるモデル名と同じデプロイ名を選ばないと、モデル名の入力時にエラーが発生します。

curl $AZURE_OPENAI_ENDPOINT/openai/deployments/YourDeploymentName/audio/transcriptions?api-version=2024-02-01 \
 -H "api-key: $AZURE_OPENAI_API_KEY" \
 -H "Content-Type: multipart/form-data" \
 -F file="@./wikipediaOcelot.wav"

エンドポイントの例を含む、前のコマンドの最初の行は次のようになります。

curl https://aoai-docs.openai.azure.com/openai/deployments/{YourDeploymentName}/audio/transcriptions?api-version=2024-02-01 \

wikipediaOcelot.wav などのサンプル オーディオ ファイルを、GitHub の Azure AI Speech SDK リポジトリから取得できます。

重要

運用環境では、Azure Key Vault などの安全な方法で資格情報を保存してアクセスします。 資格情報のセキュリティについて詳しくは、「Azure AI サービスのセキュリティ」をご覧ください。

出力

{"text":"The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs."}

前提条件

設定

キーとエンドポイントを取得する

Azure OpenAI に対して正常に呼び出しを行うには、エンドポイントキーが必要です。

変数名
AZURE_OPENAI_ENDPOINT この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 Azure OpenAI Studio>[プレイグラウンド]>[コード ビュー] で値を確認することもできます。 エンドポイントの例: https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 KEY1 または KEY2 を使用できます。

Azure portal でリソースに移動します。 [エンドポイントとキー][リソース管理] セクションにあります。 エンドポイントとアクセス キーをコピーします。これらは、API 呼び出しを認証するために両方とも必要です。 KEY1 または KEY2 を使用できます。 常に 2 つのキーを用意しておくと、サービスを中断させることなく、キーのローテーションと再生成を安全に行うことができます。

Azure portal の Azure OpenAI リソースの概要 UI のスクリーンショット。エンドポイントおよびアクセス キーの場所が赤色の丸で囲まれています。

環境変数

キーとエンドポイントの永続的な環境変数を作成して割り当てます。

重要

API キーを使用する場合は、それを Azure Key Vault などの別の場所に安全に保存します。 API キーは、コード内に直接含めないようにし、絶対に公開しないでください。

AI サービスのセキュリティの詳細については、「Azure AI サービスに対する要求の認証」を参照してください。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

パスワードレス認証の場合は、次が必要になります:

  1. @azure/identity パッケージを使用します。
  2. ユーザー アカウントに Cognitive Services User ロールを割り当てます。 これは、Azure portal の [アクセスの制御 (IAM)]>[ロールの割り当ての追加] で実行できます。
  3. az login などの Azure CLI でサインインします。

Python 環境の作成

次を使用して、OpenAI Python クライアント ライブラリをインストールします。

pip install openai

Python アプリを作成する

  1. quickstart.py という名前の新しい Python ファイルを作成します。 次に、任意のエディターまたは IDE で開きます。

  2. quickstart.py の内容を次のコードで置き換えます。 コードを変更してデプロイ名を追加します。

    import os
    from openai import AzureOpenAI
        
    client = AzureOpenAI(
        api_key=os.getenv("AZURE_OPENAI_API_KEY"),  
        api_version="2024-02-01",
        azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT")
    )
    
    deployment_id = "YOUR-DEPLOYMENT-NAME-HERE" #This will correspond to the custom name you chose for your deployment when you deployed a model."
    audio_test_file = "./wikipediaOcelot.wav"
    
    result = client.audio.transcriptions.create(
        file=open(audio_test_file, "rb"),            
        model=deployment_id
    )
    
    print(result)

クイック スタート ファイルに python コマンドを使用して、アプリケーションを実行します。

python quickstart.py

wikipediaOcelot.wav などのサンプル オーディオ ファイルを、GitHub の Azure AI Speech SDK リポジトリから取得できます。

重要

運用環境では、Azure Key Vault などの安全な方法で資格情報を保存してアクセスします。 資格情報のセキュリティについて詳しくは、「Azure AI サービスのセキュリティ」をご覧ください。

出力

{"text":"The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs."}

ソース コード | パッケージ (npm) | サンプル

前提条件

設定

キーとエンドポイントを取得する

Azure OpenAI に対して正常に呼び出しを行うには、エンドポイントキーが必要です。

変数名
AZURE_OPENAI_ENDPOINT この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 Azure OpenAI Studio>[プレイグラウンド]>[コード ビュー] で値を確認することもできます。 エンドポイントの例: https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 KEY1 または KEY2 を使用できます。

Azure portal でリソースに移動します。 [エンドポイントとキー][リソース管理] セクションにあります。 エンドポイントとアクセス キーをコピーします。これらは、API 呼び出しを認証するために両方とも必要です。 KEY1 または KEY2 を使用できます。 常に 2 つのキーを用意しておくと、サービスを中断させることなく、キーのローテーションと再生成を安全に行うことができます。

Azure portal の Azure OpenAI リソースの概要 UI のスクリーンショット。エンドポイントおよびアクセス キーの場所が赤色の丸で囲まれています。

環境変数

キーとエンドポイントの永続的な環境変数を作成して割り当てます。

重要

API キーを使用する場合は、それを Azure Key Vault などの別の場所に安全に保存します。 API キーは、コード内に直接含めないようにし、絶対に公開しないでください。

AI サービスのセキュリティの詳細については、「Azure AI サービスに対する要求の認証」を参照してください。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

パスワードレス認証の場合は、次が必要になります:

  1. @azure/identity パッケージを使用します。
  2. ユーザー アカウントに Cognitive Services User ロールを割り当てます。 これは、Azure portal の [アクセスの制御 (IAM)]>[ロールの割り当ての追加] で実行できます。
  3. az login などの Azure CLI でサインインします。

Node アプリケーションを作成する

コンソール ウィンドウ (cmd、PowerShell、Bash など) で、ご利用のアプリ用に新しいディレクトリを作成し、そこに移動します。 次に、npm init コマンドを実行し、package.json ファイルを使用してノード アプリケーションを作成します。

npm init

クライアント ライブラリをインストールする

次のコマンドを使用してクライアント ライブラリをインストールします。

npm install openai @azure/identity

アプリの package.json ファイルは依存関係を含めて更新されます。

サンプル アプリケーションを作成する

  1. Whisper.js という名前の新しいファイルを作成し、お好みのコード エディターで開きます。 次のコードを Whisper.js ファイルにコピーします。

    require("dotenv/config");
    const { createReadStream } = require("fs");
    const { AzureOpenAI } = require("openai");
    
    // You will need to set these environment variables or edit the following values
    const audioFilePath = process.env["AUDIO_FILE_PATH"] || "<audio file path>";
    const endpoint = process.env["AZURE_OPENAI_ENDPOINT"] || "<endpoint>";
    const apiKey = process.env["AZURE_OPENAI_API_KEY"] || "<api key>";
    
    // Required Azure OpenAI deployment name and API version
    const apiVersion = "2024-08-01-preview";
    const deploymentName = "whisper";
    
    function getClient() {
      return new AzureOpenAI({
        endpoint,
        apiKey,
        apiVersion,
        deployment: deploymentName,
      });
    }
    
    export async function main() {
      console.log("== Transcribe Audio Sample ==");
    
      const client = getClient();
      const result = await client.audio.transcriptions.create({
        model: "",
        file: createReadStream(audioFilePath),
      });
    
      console.log(`Transcription: ${result.text}`);
    }
    
    main().catch((err) => {
      console.error("The sample encountered an error:", err);
    });
    
  2. 次のコマンドを使用してこのスクリプトを実行します。

    node Whisper.js
    

wikipediaOcelot.wav などのサンプル オーディオ ファイルを、GitHub の Azure AI Speech SDK リポジトリから取得できます。

重要

運用環境では、Azure Key Vault などの安全な方法で資格情報を保存してアクセスします。 資格情報のセキュリティについて詳しくは、「Azure AI サービスのセキュリティ」をご覧ください。

出力

{"text":"The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs."}

前提条件

設定

キーとエンドポイントを取得する

Azure OpenAI に対して正常に呼び出しを行うには、エンドポイントキーが必要です。

変数名
AZURE_OPENAI_ENDPOINT この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 Azure OpenAI Studio>[プレイグラウンド]>[コード ビュー] で値を確認することもできます。 エンドポイントの例: https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY この値は、Azure portal からリソースを確認する際に、 [Keys & Endpoint](キーとエンドポイント) セクションで確認することができます。 KEY1 または KEY2 を使用できます。

Azure portal でリソースに移動します。 [エンドポイントとキー][リソース管理] セクションにあります。 エンドポイントとアクセス キーをコピーします。これらは、API 呼び出しを認証するために両方とも必要です。 KEY1 または KEY2 を使用できます。 常に 2 つのキーを用意しておくと、サービスを中断させることなく、キーのローテーションと再生成を安全に行うことができます。

Azure portal の Azure OpenAI リソースの概要 UI のスクリーンショット。エンドポイントおよびアクセス キーの場所が赤色の丸で囲まれています。

環境変数

キーとエンドポイントの永続的な環境変数を作成して割り当てます。

重要

API キーを使用する場合は、それを Azure Key Vault などの別の場所に安全に保存します。 API キーは、コード内に直接含めないようにし、絶対に公開しないでください。

AI サービスのセキュリティの詳細については、「Azure AI サービスに対する要求の認証」を参照してください。

setx AZURE_OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" 
setx AZURE_OPENAI_ENDPOINT "REPLACE_WITH_YOUR_ENDPOINT_HERE" 

PowerShell アプリを作成する

次のコマンドを実行します。 YourDeploymentName は、Whisper モデルのデプロイ時に選んだデプロイ名に置き換える必要があります。 デプロイ名は必ずしもモデル名と同じではありません。 基になるモデル名と同じデプロイ名を選ばないと、モデル名の入力時にエラーが発生します。

# Azure OpenAI metadata variables
$openai = @{
    api_key     = $Env:AZURE_OPENAI_API_KEY
    api_base    = $Env:AZURE_OPENAI_ENDPOINT # your endpoint should look like the following https://YOUR_RESOURCE_NAME.openai.azure.com/
    api_version = '2024-02-01' # this may change in the future
    name        = 'YourDeploymentName' #This will correspond to the custom name you chose for your deployment when you deployed a model.
}

# Header for authentication
$headers = [ordered]@{
    'api-key' = $openai.api_key
}

$form = @{ file = get-item -path './wikipediaOcelot.wav' }

# Send a completion call to generate an answer
$url = "$($openai.api_base)/openai/deployments/$($openai.name)/audio/transcriptions?api-version=$($openai.api_version)"

$response = Invoke-RestMethod -Uri $url -Headers $headers -Form $form -Method Post -ContentType 'multipart/form-data'
return $response.text

wikipediaOcelot.wav などのサンプル オーディオ ファイルを、GitHub の Azure AI Speech SDK リポジトリから取得できます。

重要

運用環境では、Azure Key Vault を使用した PowerShell シークレット管理などの、安全な方法で資格情報を保存し、アクセスしてください。 資格情報のセキュリティについて詳しくは、「Azure AI サービスのセキュリティ」をご覧ください。

出力

The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States, Mexico, and Central and South America. This medium-sized cat is characterized by solid black spots and streaks on its coat, round ears, and white neck and undersides. It weighs between 8 and 15.5 kilograms, 18 and 34 pounds, and reaches 40 to 50 centimeters 16 to 20 inches at the shoulders. It was first described by Carl Linnaeus in 1758. Two subspecies are recognized, L. p. paradalis and L. p. mitis. Typically active during twilight and at night, the ocelot tends to be solitary and territorial. It is efficient at climbing, leaping, and swimming. It preys on small terrestrial mammals such as armadillo, opossum, and lagomorphs.

リソースをクリーンアップする

Azure OpenAI リソースをクリーンアップして削除したい場合は、リソースを削除できます。 リソースを削除する前に、まずデプロイされたモデルを削除する必要があります。

次のステップ