Share via


快速入門:使用 Azure OpenAI Whisper 模型語音轉換文字

在本快速入門中,您會使用 Azure OpenAI Whisper 模型進行語音轉換文字。

Azure OpenAI Whisper 模型的檔案大小限制為 25 MB。 如果您需要轉譯大於 25 MB 的檔案,您可以使用 Azure AI 語音 批次轉譯 API。

必要條件

注意

目前,您必須提交應用程式以存取 Azure OpenAI 服務。 若要申請存取權限,請填寫此表單

設定

擷取金鑰和端點

若要成功對 Azure OpenAI 發出呼叫,您將需要 端點金鑰

變數名稱
AZURE_OPENAI_ENDPOINT 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 或者,您可以在 Azure OpenAI Studio遊樂場>程式代碼檢視中找到值。> 範例端點為:https://aoai-docs.openai.azure.com/
AZURE_OPENAI_API_KEY 從 Azure 入口網站查看您的資源時,可以在 [金鑰與端點] 區段中找到此值。 您可以使用 KEY1KEY2

移至您在 Azure 入口網站中的資源。 您可以在 [資源管理] 區段中找到 [端點和金鑰]。 複製您的端點和存取金鑰,因為您需要這兩者才能驗證 API 呼叫。 您可以使用 KEY1KEY2。 隨時持有兩個金鑰可讓您安全地輪替和重新產生金鑰,而不會造成服務中斷。

Azure 入口網站 中 Azure OpenAI 資源概觀 UI 的螢幕快照,其中端點和存取密鑰位置會以紅色圓圈顯示。

為您的金鑰和端點建立及指派永續性環境變數。

環境變數

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 \

您可以從 GitHubAzure AI 語音 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."}

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

您可以從 GitHubAzure AI 語音 SDK 存放庫取得範例音訊檔案。

重要

針對生產環境,請使用安全的方式來儲存和存取您的認證,例如PowerShell秘密管理與 Azure 金鑰保存庫。 如需有關認證安全性的詳細資訊,請參閱 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.

Python

必要條件

設定

使用下列項目安裝 OpenAI Python 用戶端連結庫:

pip install openai
  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 命令執行應用程式:

您可以從 GitHubAzure AI 語音 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 資源,您可以刪除資源。 刪除資源之前,您必須先刪除任何已部署的模型。

下一步