Azure OpenAI Whisper语音转文本报404资源未找到错误求助
解决Azure OpenAI Whisper音频转录404错误的方案
排查方向及修复步骤
修正URL拼接问题
检查你的AZURE_OPENAI_ENDPOINT值是否带有结尾斜杠。如果端点是https://xxx.openai.azure.com/这类格式,拼接后会出现//openai/的无效路径,导致404。可以通过去除结尾斜杠解决:# 处理端点的结尾斜杠,避免拼接错误 api_base_clean = openai['api_base'].rstrip('/') url = f"{api_base_clean}/openai/deployments/{openai['name']}/audio/transcriptions?api-version={openai['api_version']}"更新API版本
你使用的2023-06-01-preview版本虽支持Whisper,但部分区域可能需要更新的版本。建议替换为2024-02-15-preview或官方最新的音频转录兼容版本,修改代码中的api_version字段即可。核对部署名称
确认代码中openai['name']的部署名称和Azure Playground里的完全一致,包括大小写、空格和特殊字符,任何细微差异都会导致资源找不到。验证请求头(可选)
虽然上传文件时requests会自动生成正确的Content-Type,但手动添加可避免潜在问题:headers = { 'api-key': openai['api_key'], 'Content-Type': 'multipart/form-data' }
修正后的完整代码示例
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': '2024-02-15-preview', 'name': 'your-deployment-name' # 确保此处与实际部署名称完全匹配 } # Header for authentication headers = { 'api-key': openai['api_key'] } # audio file file_path = '/content/drive/MyDrive/speech2text/sampleaudiO.wav' # 处理端点结尾斜杠,避免URL拼接错误 api_base_clean = openai['api_base'].rstrip('/') url = f"{api_base_clean}/openai/deployments/{openai['name']}/audio/transcriptions?api-version={openai['api_version']}" try: 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}")
内容的提问来源于stack exchange,提问作者Amanjeet Kumar
相关产品推荐
相关产品推荐

