Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code not executing completely #279

Open
anshtyagi0 opened this issue Jun 13, 2023 · 11 comments
Open

Code not executing completely #279

anshtyagi0 opened this issue Jun 13, 2023 · 11 comments

Comments

@anshtyagi0
Copy link

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.

@anshtyagi0
Copy link
Author

Screen.Recording.2023-06-13.at.10.41.57.PM.mov

@UmerTariq1
Copy link

UmerTariq1 commented Jun 14, 2023

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.

@anshtyagi0
Copy link
Author

@UmerTariq1 Is there any fix for this issue?
Also will pyttsx4 solve it?

@JAlbert2
Copy link

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.

@florence26
Copy link

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.

@Jiangshan00001
Copy link

Jiangshan00001 commented Jun 24, 2023

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.")

@UmerTariq1
Copy link

UmerTariq1 commented Jun 24, 2023

@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)

@Jiangshan00001
Copy link

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: #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?

@UmerTariq1
Copy link

UmerTariq1 commented Jun 24, 2023

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

@Jiangshan00001
Copy link

Jiangshan00001 commented Jun 24, 2023

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

@m1guelperez
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants