pyttsx3
pyttsx3 copied to clipboard
Code not executing completely
import speech_recognition as sr
import pyttsx3
r = sr.Recognizer()
engine = pyttsx3.init()
def listen():
with sr.Microphone() as source:
speak("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
speak("Recognizing...")
query = r.recognize_google(audio, language='en')
print(f"User: {query}\n")
return query
except sr.UnknownValueError:
speak("Sorry, I didn't catch that. Can you please repeat?")
return listen()
except sr.RequestError:
speak("Oops! Something went wrong. Please check your internet connection.")
return None
def speak(text):
engine.say(text)
engine.runAndWait()
# def get_location(query):
# if 'weather' in query and 'in' in query:
# words = query.split()
# index = words.index('in')
# location = ' '.join(words[index+1:])
# return location
# return None
def logic(query):
# location = get_location(query)
if 'hello' in query:
speak("Hello! How can I assist you today?")
elif 'play music' in query:
speak("Playing your favorite music.")
elif 'set reminder' in query:
speak("Reminder set successfully.")
elif 'goodbye' in query:
speak("Goodbye! Have a great day.")
return False
# elif location:
# speak(f"The weather in {location} today is sunny.")
return True
speak("Hello! How can I assist you today?")
query = listen()
if query:
running = logic(query)
The code is not executing after speak("Hello! How can I assist you today?")
it only says "Hello! How can I assist you today?" after that code stops without any error in console.
https://github.com/nateshmbhat/pyttsx3/assets/73632579/9c4cb248-ec75-41a5-9a31-a7ef578ab94b
The function engine.runAndWait() is the culprit here. And the issue is with pytssx's integration with M1. And this is around since pytssx 1. #22 and Stackoverflow. And I had the same problem and the code worked on linux.
@UmerTariq1 Is there any fix for this issue? Also will pyttsx4 solve it?
pyttsx4 has the same issue. I'm running a Mac with an intel chip and runAndWait() kills the Python program. I tried the steps outlined in the Stack Overflow question from UmerTariq1 with no success.
I'm getting the same problem too
Running the code below:
import openai
import speech_recognition as sr
import pyttsx3
import time
messages = [{"role": "system", "content": 'You are a therapist who raps all your responses in a comedic way. Please respond in 50 words or less.'}]
engine = pyttsx3.init()
#voices = engine.getProperty('voices')
#engine.setProperty('voice', voices[1].id) # 0 for male, 1 for female
r = sr.Recognizer()
r.dynamic_energy_threshold = False
r.energy_threshold = 400
def speak(text):
engine.say(text)
engine.runAndWait()
while True:
with sr.Microphone() as source:
print("Listening through Mic")
r.adjust_for_ambient_noise(source, duration = 0.5)
audio = r.listen(source)
try:
user_input = r.recognize_google(audio)
except:
continue
messages.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
messages.append(response['choices'][0]['message'])
print(response.choices[0]["message"]["content"])
speak(f'{response.choices[0].message.content}')
And the python script escapes right after.
i have updated the pyttsx4 for the nsss driver of macos. could you try to find if it works?
@UmerTariq1
pip install pyttsx4 --upgrade
import` pyttsx4
engine = pyttsx4.init()
def speak(text):
engine.say(text)
engine.runAndWait()
speak("Hello! How can I assist you today?")
speak("Hello! i want to know, could you speak twice?")
speak("tell me something, so that i can try the speak function.")
@Jiangshan00001 Thank you for the update. But I just tried pyttsx4 version 3.0.15 and it still has the same problem.
Python version : 3.9.16
OS : 13.0
The below code does not print "after". and also the problem of #280 is still there (which i understand you did not claim to have solved. but it would be awesome to have it solved. )
import pyttsx4
engine = pyttsx4.init()
def speak(text):
engine.say(text)
print("before")
engine.runAndWait()
print("after")
text = "Hi. How are you? what are you doing?"
speak(text)
for the problem #280, is because default engine in linux is espeak, while default engine in mac is nsss. if you want better output in linux, you can try another engine:
pip install TTS
engine = pyttsx4.init('coqui_ai_tts')
engine.say('this is an english text to voice test, listen it carefully and tell who i am.')
engine.runAndWait()
for the current problem, i guess it maybe an package objc problem. see: https://github.com/nateshmbhat/pyttsx3/issues/274 could you please run: pip freeze and paste the package u are using?
and try: pip install pyobjc==9.0.1
to see if any difference?
Updating the pyobjc worked. Amazing. Thanks a lot.
P.s excuse my ignorance but isnt coqui_ai_tts paid?
Edit: just for reference, earlier i was using pyobjc v9.2. changing to 9.0.1 worked
coqui_ai_tts is opensource and can use offline. but it have to download the model first time when using. the model is downloaded automaticly first time you use it. it do has paid online service if you need. the TTS is mpl license
Can confirm the same behavior with ´pyobjc v9.2´ does terminate without any error or information. However, ´pyobjc v.9.0.1´ works without any problems so far.
Thanks for the fix @Jiangshan00001