Speech Recognition Request Failed: Service Unavailable

Viewed 14

I'm creating a simple virtual assistant but I keep getting an error saying recognition request failed service unavailable.

Might the problem be that I cannot be able to access the google speech api? I'm not sure.

Here is my code

import pyttsx3
import os
from time import sleep
import playsound
import speech_recognition as sr


voiceEngine = pyttsx3.init()
# rate
voiceEngine.setProperty('rate', 150)
# voice
voices = voiceEngine.getProperty('voices')
voiceEngine.setProperty('voice', voices[1].id)

def speak(text):
    voiceEngine.say(text)
    voiceEngine.runAndWait()
    
def takeCommand():
    print('\nListening...')
    recog = sr.Recognizer()
    with sr.Microphone() as source:
        print("Listening to the user")
        recog.pause_threshold = 1
        userInput = recog.listen(source)

        try:
            print("Recognizing the command")
            command = recog.recognize_google(userInput, language ='en-in')
            print(f"Command is: {command}\n")
            
        except Exception as e:
            print(e)
            print("Unable to Recognize the voice.")
            return "None"

    return command

def wish():
    print("Wishing.")
    time = int(datetime.datetime.now().hour)
    if time>= 0 and time<12:
        speak("Good Morning!")
    elif time<18:
        speak("Good Afternoon!")
    else:
        speak("Good Evening!")
    speak("I am your Voice Assistant, DIVA")
    print("I am your Voice Assistant, DIVA")
os.system('cls')
wish()
while True:
    for j in range(5):   
        speak("What can I do for you")
        command = takeCommand().lower()
        print(command)
        if command:
            break
    if "DIVA" in command:
        wish()
    else:
        speak("Sorry, I am not able to understand you")

When I run it I get this error

Wishing.
I am your Voice Assistant, DIVA
Listening...
Listening to the user
Recognizing the command
recognition request failed: Service Unavailable
Unable to Recognize the voice.

Any help will be highly appreciated. Thanks.

0 Answers
Related