Working with OpenAI API and below is my simple code, but getting deprecation warning for 'steam_to_file' method.
Code -
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 code
Warning as below -
** DeprecationWarning: Due to a bug, this method doesn't actually stream the response content, .with_streaming_response.method() should be used instead
response.stream_to_file("song.mp3")**
Can anyone please help?
I tried to check through different forums but could not find error related to stream_to_file.
I am using Python 3.12
This seems to be the recommended approach, at least it uses the methods that the error message suggests:
from openai import OpenAI
client = OpenAI()
with client.audio.speech.with_streaming_response.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""",
) as response:
response.stream_to_file("speech.mp3")
However, it seems like this still doesn't actually stream the response. If I repeatedly check the "last changed" time on the speech.mp3 file, I can see that it is created immediately, but it isn't changed again until the entire generation completes.
I'm still trying to figure out how to do proper streaming of TTS, but at least this is a way to use the API without a DeprecationError.
Using openai 1.12.0
The easy way is just to ignore this warning. Import the warnings library, then warnings.filterwarnings() It worked for me.
from openai import OpenAI
from pathlib import Path
import warnings
# Ignore DeprecationWarning
warnings.filterwarnings("ignore", category=DeprecationWarning)
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With