To generate an AI voice reading your text, use this python script below. You just have to insert your API key and a custom text and are ready to go. This script will save the audio file in the path defined in “audio_file_path”, you can also change that if you like.

<aside> 💡 To install the openai library run “pip install openai”

</aside>

from openai import OpenAI

# Initialize the client with your API key and base URL
client = OpenAI(api_key="your-api-key", base_url="<https://litellm.sph-prod.ethz.ch/v1>")

# Define the text you want to convert to speech
text_to_speak = "Hello, this is a test of text-to-speech using the OpenAI library and LiteLLM."

# Generate the speech
response = client.audio.speech.create(
    model="tts-1",  # You can choose a different model if available
    voice="alloy",  # You can choose a different voice (e.g., 'alloy', 'echo', 'fable', 'onyx', 'nova', 'shimmer')
    input=text_to_speak,
)

# Save the audio to a file
audio_file_path = "output_audio.mp3"
with open(audio_file_path, "wb") as f:
    f.write(response.content)

print(f"Audio saved to {audio_file_path}")