调用OpenAI Whisper-1转写音频报错:'Transcription'对象不可下标访问
问题解决:'Transcription' object is not subscriptable 报错
你遇到的问题是因为OpenAI Python SDK返回的Transcription是一个对象,不是字典,不能用方括号[]来访问属性。
错误原因
你写的transcript['transcription']['text']是字典的访问方式,但实际返回的transcript是一个包含转写结果的对象,直接通过点语法就能拿到文本内容。
修正后的代码
from openai import OpenAI client = OpenAI(api_key="my_key") audio_file_path ="C:/Users/name/OneDrive/VS Folder/speech.mp3" with open(audio_file_path, "rb") as audio_file: try: transcript = client.audio.transcriptions.create( model="whisper-1", file=audio_file ) # 直接用点语法访问text属性 transcribed_text = transcript.text # 打印转写结果 print(transcribed_text) except Exception as e: print(f"An error occurred: {e}")
额外说明
如果需要查看Transcription对象的所有属性,可以打印dir(transcript)或者直接打印transcript,会看到它包含text、language等属性,都可以通过点语法访问。
内容的提问来源于stack exchange,提问作者TheAnalyst
相关产品推荐
相关产品推荐

