ChatGPT API发布了语音转文本的Whisper API,我非常兴奋地尝试了一下。这是链接
我尝试了他们的示例代码
# Note: you need to be using OpenAI Python v0.27.0 for the code below to workimport openaiaudio_file= open("/path/to/file/audio.mp3", "rb")
然后得到了以下错误
AttributeError: module 'openai' has no attribute 'Audio'
我确定我使用的是0.27.0版本
pip list | grep openaiopenai 0.27.0
你认为openai还没有更新吗?
回答:
你遇到AttributeError: module 'openai' has no attribute 'Audio'
的原因可能有以下三种。
原因1:你没有升级Python
官方OpenAI GitHub仓库中指出,需要使用3.7.1
或更新的Python版本。
原因2:你没有升级OpenAI Python包
首先,在终端中运行以下命令检查你的OpenAI包版本:
pip show openai
如果你想使用OpenAI Whisper API,你需要使用0.27.0
或更新的版本。
如果你使用的是旧版本,在终端中运行以下命令来更新OpenAI包:
pip install --upgrade openai
原因3:你的代码不正确
尝试以下代码。
选项1(推荐):
test.py
import openaiimport osopenai.api_key = os.getenv('OPENAI_API_KEY')audio_file = open('audio.mp3', 'rb')transcript = openai.Audio.transcribe('whisper-1', audio_file)
选项2:
test.py
import openaiopenai.api_key = 'sk-xxxxxxxxxxxxxxxxxxxx'audio_file = open('audio.mp3', 'rb')transcript = openai.Audio.transcribe('whisper-1', audio_file)