Python“Stream_to_file”中的方法不起作用

问题描述 投票:0回答:1

使用 OpenAI API 及以下是我的简单代码,但收到“steam_to_file”方法的弃用警告。

代码-

from openai import OpenAI
from pathlib import Path

client = OpenAI(
  api_key=os.getenv("OPENAI_API_KEY"),
 )

speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
  model="tts-1",
  voice="alloy",
  input= '''I see skies of blue and clouds of white
            The bright blessed days, the dark sacred nights
            And I think to myself
            What a wonderful world
         '''
)
response.stream_to_file(speech_file_path)

IDE - Visual Studio 代码

警告如下 -

** DeprecationWarning:由于错误,此方法实际上并不传输响应内容,应使用

.with_streaming_response.method()
response.stream_to_file("歌曲.mp3")**

有人可以帮忙吗?

我尝试检查不同的论坛,但找不到与stream_to_file相关的错误。

我正在使用Python 3.12

python-3.x text-to-speech openai-api
1个回答
0
投票

我遇到了同样的问题,但这解决了它。删除行

speech_file_path = Path(__file__).parent / "speech.mp3"
并写下
file_name = "speech.mp3"

response = openai.audio.speech.create(
  model="tts-1",
  voice=shimmer,
  input="The quick brown fox jumped over the lazy dog."
)

file_name = "speech.mp3"
response.stream_to_file(file_name)
© www.soinside.com 2019 - 2024. All rights reserved.