I recently followed a tutorial to create a talk back AI bot with open AI API and gTTS plugins in python. I followed the code exactly and it is all running fine. The code recognizes what I’m saying and tells it back to me in the terminal, yet when it gets to th response part, it just gives me an “Exception” then ends.
import os
import time
import pyaudio
import speech_recognition as sr
import playsound
from gtts import gTTS
import openai
api_key = "sk-proj-Ba6Q3Z36RSumUez9RwxnT3BlbkFJN6ZXJ3XTTgbfFT1pU2o3"
lang = 'en'
openai.api_key = api_key
guy = ""
while True:
def get_adio():
r = sr.Recognizer()
with sr.Microphone(device_index=1) as source:
audio = r.listen(source)
said = ""
try:
said = r.recognize_google(audio)
print(said)
global guy
guy = said
if "Friday" in said:
words = said.split()
new_string = ' '.join(words[1:])
print(new_string)
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo",
messages=[{"role": "user", "content": said}])
text = completion.choices[0].message.content
tts = gTTS(text=text, lang=lang, slow=False, tld="com.au")
tts.save("welcome1.mp3")
playsound=playsound("welcome1.mp3")
except Exception:
print("Exception")
return said
if "stop" in guy:
break
get_adio()
I am fairly new to Python so anything helps 🙂
I tried importing different codes I found online but nothing has worked. As I said, it understands what I’m saying but I hear no response at all.
CRE8ION Zero is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.