I'm trying to create an application like Alexa for the computer called "Emma" using Python. By using Speech Recognition module it'll use a microphone as a source to listen to the user. it works fine but after answering or doing some stuff like searching it'll freeze and doesn't work anymore.
I thought that maybe speech recognition has some limited time for using but after searching I've found nothing about it. Now I just don't know it's because of speech recognition or some other modules like GTTS (Google Text To Speech).
Here is the link to my repository if you need to see the whole code: https://github.com/sina1mhi/emma_virtual_assistant
Please let me know your ways to solve the problem.
Here is the part of speech recognition code:
def record_audio(ask=False, lang="en-US"):
with sr.Microphone() as source: # microphone as source
print("Emma: I'm listening")
if ask:
speak(ask)
time.sleep(1)
audio = r.listen(source) # listen for the audio via source
voice_data = ''
try:
voice_data = r.recognize_google(
audio, language=lang) # convert audio to text
except sr.UnknownValueError: # error: recognizer does not understand
speak("I did'nt get that")
exit()
except sr.RequestError:
# error: recognizer is not connected
speak('Sorry, the service is down')
exit()
print(f">> {voice_data.lower()}") # print what user said
return voice_data.lower()