Trying to convert text-to-speech using OpenAI whisper model and I keep getting this error message
I have tried everything, switching to different versions of openai via pip
The code transcribes my voice into text, sends that to chatGPT and gets a response, but the TTS API is not working.
import os
from pathlib import Path
import openai
import speech_recognition as sr
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
OPENAI_API_KEY = os.getenv('OPENAI_API_KEY')
# Set the OpenAI API key
openai.api_key = OPENAI_API_KEY
# Initialize the speech recognition engine
recognizer = sr.Recognizer()
def transcribe_audio_from_mic():
"""Transcribes audio from the microphone using speech recognition"""
with sr.Microphone() as source:
print("Speak something:")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio)
print("You said:", text)
return text
except sr.UnknownValueError:
print("Could not understand audio")
return ""
except sr.RequestError as e:
print(f"Error from recognition service: {e}")
return ""
def get_chatgpt_response(prompt):
"""Gets a response from ChatGPT"""
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
],
)
return response.choices[0].message["content"]
def speak_chatgpt_response(chatgpt_response):
"""Speaks the ChatGPT response using OpenAI's text-to-speech"""
speech_file_path = Path(__file__).parent / "speech.mp3"
# Create spoken audio from the ChatGPT response
response = openai.Audio.create(
model="tts-1",
voice="alloy",
input=chatgpt_response,
response_format="mp3"
)
# Write the audio stream to a file
with open(speech_file_path, "wb") as f:
f.write(response.content)
# Play the audio file (using the default media player on Windows)
os.system(f'start {speech_file_path}')
# Main program loop
if __name__ == "__main__":
while True:
prompt = transcribe_audio_from_mic()
if prompt:
chatgpt_response = get_chatgpt_response(prompt)
print("ChatGPT:", chatgpt_response)
speak_chatgpt_response(chatgpt_response)
The weird thing is, this cURL command works:
curl -X POST “h**ps://api.openai.com/v1/audio/speech” -H “Content-Type: application/json” -H “Authorization: Bearer TOKEN-HERE” -d “{"model":"tts-1","input":"The quick brown fox jumped over the lazy dog.","voice":"alloy"}” --output speech.mp3
So obviously the endpoint is able to be reached, but something is not right with my code. Can someone please help?