I'm lost when it comes to generating my Text to speech mp3 file

Hello, I’m a UI designer and new to ChatGPT/OpenAI. I’m being tasked to create some text to speech for videos I’m making, but I’m completely lost in how to get the mp3 file. I have my secret sk key, but I’m not a developer and working in VS code is foreign to me. I have python and VS code installed. I’ve tried running this but nothing happens and I don’t know where the mp3 is. Please help.

1 Like

Perhaps specify the full path to save to, so you don’t have to guess where it went.

Then improve with real streaming to a file.

Have the file extension set the api audio format…

from pathlib import Path
from openai import OpenAI, OpenAIError

def save_audio_stream(model: str, voice: str, input_text: str, file_path: str):
    """ Saves streamed audio data to a file, handling different OS path conventions. """
    # Construct the path object and validate the file extension
    path = Path(file_path)
    valid_formats = ['mp3', 'opus', 'aac', 'flac', 'wav', 'pcm']
    file_extension = path.suffix.lstrip('.').lower()
    
    if file_extension not in valid_formats:
        raise ValueError(f"Unsupported file format: {file_extension}. Please use one of {valid_formats}.")

    client = OpenAI()

    try:
        with client.audio.speech.with_streaming_response.create(
            model=model,
            voice=voice,
            input=input_text,
            response_format=file_extension
        ) as response:
            with open(path, 'wb') as f:
                for chunk in response.iter_bytes():
                    f.write(chunk)
    except OpenAIError as e:
        print(f"An error occurred while trying to fetch the audio stream: {e}")

# Usage example, demonstrating a Windows file path
save_audio_stream(
    model="tts-1",
    voice="alloy",
    input_text="hello there, I'm saving this as an audio file today",
    file_path="C:\\chat\\myaudio.mp3"
)

Thank you for your help. Can I just copy and paste this whole chunk of code? Updating the file_path of course to be local

Yes, there is a copy button on the code block, and I give you an irrevocable license to use the snippet however you wish.

The client uses the API key from an environment variable OPENAI_API_KEY by default. If you set this in your operating system, you won’t have to specify it in code again.

Amazing, thank you SO much. Would I then hit Run and Debug in VS code? Or how would I ‘input’?

If you install your own Python 3.11 by downloading the official build on your OS (which you may have already done instead of letting VSCode install Python “whatever” to wherever), then a simple editor IDE called IDLE is installed. You can open IDLE from the start menu, pick “new file” and paste the code there and run. The use of individual files is more transparent that way.

With VSCode, you pick new file → python file, then paste. The little play arrow with “run file” will force you to save the py file somewhere with a name.

Running this function code gives no feedback except for your new audio file being created, so you can add some print statements if you like, improve it by having a permanent directory save path, etc.

1 Like

I downloaded python independently, but I have IDLE too. I’ve opened it, pasted the code and determined the file_path. I don’t know how or where to run it, but when I hit enter, I got this error. Do I paste my path up there again?

You are in the IDLE shell, not the file editor. That’s where you can type individual lines of code and run them. You didn’t pick “new file” like I said to.


A windows path is created in code as I showed in my example, two backslashes for every directory, plus include the drive letter. The extra backslash is used to “escape” the first one, because backslash itself is the escape character.

Windows uses backslashes; Linux/UNIX uses forward slash.

If you had path=input() to accept user input, for example, the user wouldn’t have to type the extra backslash.


The pathlib library is so you can use either forward or backslashes in your file path without error - don’t change that import line.

Pathlib is smart enough to have saved you from the path you typed.

Okay, apologies for that. I’ve selected ‘New File’, pasted the code.
I’m on a Mac, so does that affect the naming structure?

MacOSX+ is based on BSD, so the UNIX forward slashes apply to you.

As long as the directory you supplied exists, you should be ready to watch mp3 files appear there after you pick run–>“run module” to hear an AI talk.

I feel like i’m so close. Gave me this error when i hit run module

The error you received has the instruction you need to follow: setting the API key environment variable in the OS, or hard-coding it as `client(api_key=“sk-mykey”)


If you’re feeling adventurous after getting your first mp3 and seeing how path works, you can follow this link to a version with a ttsFile class that maintains settings and would automatically produce timestamped files using a prefix. While it works as is, with a prompt to edit at the bottom, the entire if __name__ == "__main__": is simply example of how to use the class.

It saves to the present code directory with a prefix that is just part of the file name, but a full path and filename prefix could be used.

Thank you for sending that link. I just want to get one outputted and take it from there… code is completely foreign to me so i have no idea what i’m doing. I entered my sk key on line 14 and now there’s this error message. Something with the quotation marks

Did you terminate your string literal?

(translation: are there double quote characters at the start and the end of the API key value, still enclosed within the original parenthesis?)

I don’t think so. This is what that line looks like…

Somehow you lost the equals sign after client.

Since we like screenshots,

I can see how my previous instruction would be unclear. The openai library has both a OpenAI() and a Client() class that do the same thing and are interchangeable, so you can forget which was in what code.

1 Like

oh my god it worked!!! THANK YOU!!!

2 Likes

Hello, Can you copy the code for us. Thanks Sara

1 Like

Check the second post. OP just had a problem with continuing to edit it in a damaging manner and not placing an environment variable.

1 Like

Thank you, now I’ll read it. Thanks to everyone. I copied it again for others with your permission.

from pathlib import Path
from openai import OpenAI, OpenAIError

def save_audio_stream(model: str, voice: str, input_text: str, file_path: str):
    """ Saves streamed audio data to a file, handling different OS path conventions. """
    # Construct the path object and validate the file extension
    path = Path(file_path)
    valid_formats = ['mp3', 'opus', 'aac', 'flac', 'wav', 'pcm']
    file_extension = path.suffix.lstrip('.').lower()
    
    if file_extension not in valid_formats:
        raise ValueError(f"Unsupported file format: {file_extension}. Please use one of {valid_formats}.")

    client = OpenAI()

    try:
        with client.audio.speech.with_streaming_response.create(
            model=model,
            voice=voice,
            input=input_text,
            response_format=file_extension
        ) as response:
            with open(path, 'wb') as f:
                for chunk in response.iter_bytes():
                    f.write(chunk)
    except OpenAIError as e:
        print(f"An error occurred while trying to fetch the audio stream: {e}")

# Usage example, demonstrating a Windows file path
save_audio_stream(
    model="tts-1",
    voice="alloy",
    input_text="hello there, I'm saving this as an audio file today",
    file_path="C:\\chat\\myaudio.mp3"
)
print("If it didn't crash, you have an audio file!!!!")