当前位置:AIGC资讯 > AIGC > 正文

Azure openai speech to text -Whisper “code“:“404“,“message“: “Resource not found“

题意:Azure OpenAI 语音转文本 - Whisper 报错 "code":"404","message": "Resource not found

问题背景:

i'm trying to transcribe a audio file by using whisper through Azure openai key,endpoints,deployment

我正在尝试通过使用 Azure OpenAI 的密钥、端点和部署来转录音频文件,使用的是 Whisper 模型。

eventhough i.m adding right credencials by deploying in valid region for whisper and with their permission granted(Followed each steps) Convert speech to text with Azure OpenAI Service - Azure OpenAI | Microsoft Learn

即使我在正确的地区部署了 Whisper 并添加了正确的凭据,并且获得了相关权限(按照每个步骤操作),但还是出现了问题。

**getting this error: Error 404: {"error":{"code":"404","message": "Resource not found"}} **

tried transcription on azure playground using same deployment its working there

在 Azure Playground 上使用相同的部署尝试转录,结果在那里可以正常工作。

import os
import requests

# Azure OpenAI credentials
os.environ['AZURE_OPENAI_KEY'] = 'KEY'
os.environ['AZURE_OPENAI_ENDPOINT'] = 'ENDPOINT'

# Azure OpenAI metadata variables
openai = {
    'api_key': os.environ.get('AZURE_OPENAI_KEY'),
    'api_base': os.environ.get('AZURE_OPENAI_ENDPOINT'),
    'api_version': '2023-06-01-preview',
    'name': 'deployment name'
}

# Header for authentication
headers = {
    'api-key': openai['api_key']
}

# audio file
file_path = '/content/drive/MyDrive/speech2text/sampleaudiO.wav'

# URL for the API endpoint
url = f"{openai['api_base']}/openai/deployments/{openai['name']}/audio/transcriptions?api-version={openai['api_version']}"

try:
    # Reading the audio file as binary data
    with open(file_path, 'rb') as audio_file:
        # Send the request to Azure OpenAI for transcription
        response = requests.post(url, headers=headers, files={"file": audio_file})

        # Check if the request was successful and print the transcription
        if response.status_code == 200:
            transcription = response.json().get('text', 'Transcription not available')
            print("Transcription:", transcription)
        else:
            print(f"Error {response.status_code}: {response.text}")

except Exception as e:
    print(f"An error occurred: {e}")
here

Please help with code        请帮忙看看代码。

问题解决:

Getting this error: Error 404: {"error":{"code":"404","message": "Resource not found"}}

The above error occurs when you pass the wrong parameters, such as (api_key, api_base, api_version) in your request.

上述错误发生在你在请求中传递了错误的参数,例如 (api_key、api_base、api_version)。

In your request, you have passed the wrong API version. After passing the correct api_version=2023-09-01-preview in the Python code, it executed successfully in my environment.

上述错误发生在你在请求中传递了错误的参数,例如 (api_key、api_base、api_version)。

Code:        代码

import os
import requests

os.environ['AZURE_OPENAI_KEY'] = 'xxxx'
os.environ['AZURE_OPENAI_ENDPOINT'] = 'https://resource-name.openai.azure.com'

# Azure OpenAI metadata variables
openai = {
    'api_key': os.environ.get('AZURE_OPENAI_KEY'),
    'api_base': os.environ.get('AZURE_OPENAI_ENDPOINT'),
    'api_version': '2023-09-01-preview',
    'name': 'whisper-deployment'
}

headers = {
    'api-key': openai['api_key']
}

file_path = './harvard.wav'

url = f"{openai['api_base']}/openai/deployments/{openai['name']}/audio/transcriptions?api-version={openai['api_version']}"

try:
    # Reading the audio file as binary data
    with open(file_path, 'rb') as audio_file:
        response = requests.post(url, headers=headers, files={"file": audio_file})
        if response.status_code == 200:
            transcription = response.json().get('text', 'Transcription not available')
            print("Transcription:", transcription)
        else:
            print(f"Error {response.status_code}: {response.text}")

except Exception as e:
    print(f"An error occurred: {e}")

Output:        输出

Transcription: The stale smell of old beer lingers. It takes heat to bring out the odor. A cold dip restores health and zest. A salt pickle tastes fine with ham. Tacos al pastor are my favorite. A zestful food is the hot cross bun.

Reference: Azure OpenAI Service REST API reference - Azure OpenAI | Microsoft Learn.

总结

### 文章总结
**问题概述:**
用户尝试使用Azure OpenAI的Whisper模型,通过指定的密钥、端点和部署,对音频文件进行语音转文本的转录。然而,尽管遵循了正确的步骤并获取了必要的权限,用户仍遇到了“Resource not found”(资源未找到)的404错误。
**错误分析:**
错误的根本原因在于请求中使用了错误的API版本号 `api_version=2023-06-01-preview`。由于API的版本不正确,Azure无法识别并定位到正确的资源,从而导致了404错误。
**解决方案:**
1. **修正API版本号:** 用户将API版本号从 `2023-06-01-preview` 更改为 `2023-09-01-preview`。
2. **确保环境变量正确:** 确保 `AZURE_OPENAI_KEY` 和 `AZURE_OPENAI_ENDPOINT` 环境变量中的密钥和端点URL正确无误。
3. **调整请求URL:** 构建请求URL时,确保路径中包含正确的部署名称,并通过URL传递正确的API版本号。
**代码调整及验证**:
经过上述修改后,用户的Python脚本成功运行,并且能够正确地转录音频文件,输出了正确的转录文本内容。
**参考链接**:
用户可以参考Azure官方文档中的Azure OpenAI Service REST API部分,了解更多关于API接口和可用版本的信息。
**总结**:
此问题的关键在于确认和更新API使用的版本号,以确保与Azure服务兼容。正确的环境配置和API版本是解决此类问题的关键步骤。

更新时间 2024-09-14