Trying to make assistant but facing speed problem

Viewed 150

My program is working perfectly fine but I am having speed problems. It takes too long to respond and give results. After 2,3 runs it takes more time than before. Also additionally it takes lots of time to start. What are the ways to speed up of program? I am new in this field.

# Imported speech recognition as sr, pyttsx3, pywhatkit, datetime, pyjokes, os
listener = sr.Recognizer()
engine = pyttsx3.init()


def talk(text):
        engine.say(text)
        engine.runAndWait()


talk('Hello what can i do for you')


def take_command():
        try:
            with sr.Microphone() as source:
                print("Listening...")
                voice = listener.listen(source)
                command = listener.recognize_google(voice)
                command = command.lower()
                if "jarvis" in command:
                    command = command.replace('jarvis', '')
                    return command
                else:
                    talk("I cannot continue until you say jarvis")
                    take_command()
        except:
            pass


def run_jarvis():
        command = take_command()
        if not command:
            take_command()
        else:
            print(command)
            if "play" in command:
                song = command.replace("play", "")
                talk("playing" + song)
                pywhatkit.playonyt(song)
            elif 'time' in command:
                time = datetime.datetime.now().strftime('%I:%M %p')
                print(time)
                talk(time)
            elif 'joke' in command:
                joke = pyjokes.get_joke()
                print(joke)
                talk(joke)
            elif 'downloads' in command:
                path = "C:/Users\Mayur\Downloads"
                path = os.path.realpath(path)
                os.startfile(path)
            elif "whatsapp" in command:
                msg = command.replace("whatsapp", "")
                pywhatkit.sendwhatmsg_instantly("+91xxxxxxxxxx", msg)
                print("Successfully Sent!")
                talk("successfully sent")
            elif 'who is' or 'what is' or 'where is' or 'when is' or 'information' or 'which is' or "how is" in command:
                result = pywhatkit.info(command, 3, True)
                talk(result)
                print(result)
            else:
                talk('Sorry sir can you repeat again')


while True:
        run_jarvis()
1 Answers

It is perfectly working fine for me and I didn't see any code that will make your program slow, and listener.recognize_google(voice) should connect to the internet to return result. So there may be your internet connection problem. And slow to start is fine because you have imported many different library to your program. Thus, that is making slow to start. Additionally it has to installize recognizer and engine that can also slow down the speed. Overall your program speed depends upon your computer speed and internet connection.

Related