Search code examples
pythonartificial-intelligence

Error when creating jarvis ai, object is not callable, etc


Error Code 👈This is the picture This is the code for my ai👇

            import datetime
            import webbrowser
            import pyttsx3
            import pywhatkit
            import speech_recognition
            import wikipedia

            engine = pyttsx3.init("sapi5")
            voices = engine.getProperty("voices")
            engine.setProperty("voice", voices[0].id)
            engine.setProperty("rate", 150)


            def speak(audio):
                engine.say(audio)
                engine.runAndWait()


            def takeCommand():
                r = speech_recognition.Recognizer()
                with speech_recognition.Microphone() as source:
                    print("Listening...")
                    r.pause_threshold = 4
                    r.energy_threshold = 300
                    audio = r.listen(source, 0, 4)

                try:
                    print("Understanding...")
                    query = r.recognize_google(audio, language='en-us')
                    print(f"You said: {query}\n")
                except Exception as e:
                    print("Say that again")
                    return "None"
                return query


            # Starting...............................................................................................................
            if __name__ == "__main__":
                while True:
                    query = takeCommand().lower()
                    if "wake up David" in query:
                        hour = int(datetime.datetime.now().hour)
                        if 0 <= hour <= 12:
                            speak("Good Morning sir, how are you?")
                        elif 12 < hour <= 18:
                            speak("Good Afternoon sir, how are you?")

                        else:
                            speak("Good Evening sir, how are you?")

                    while True:
                        query = takeCommand().lower()
                        if "go to sleep david" in query:
                            speak("Ok, See you, Remember if you need anything just say, wake up david")
                            break

                        elif "I'm good how are you" in query:
                            speak("I am also good, how may I help you today?")
                        elif "thank you" in query:
                            speak("You are welcome sir")


            # Web browser searching..................................................................................................

                        def searchGoogle(query):
                            if "google" in query:
                                import wikipedia as googleScrap
                                query = query.replace("david", "")
                                query = query.replace("google search", "")
                                query = query.replace("google", "")
                                speak("This is what I found")
                                try:
                                    pywhatkit.search(query)
                                    result = googleScrap.summary(query, 1)
                                    speak(result)

                                except:
                                    speak("Did not find anything about that, sorry")

                query = query.replace("jarvis", "")
                query = query.replace("google search", "")
                query = query.replace("google", "")
                speak("This is what I found on google")

                try:
                    pywhatkit.search(query)
                    result = googleScrap.summary(query, 1)
                    speak(result)

                finally:
                    speak("No speakable output available")


            def searchYoutube(query):
                if "youtube" in query:
                    speak("This is what I found for your search!")
                    query = query.replace("youtube search", "")
                    query = query.replace("youtube", "")
                    query = query.replace("jarvis", "")
                    web = "https://www.youtube.com/results?search_query=" + query
                    webbrowser.open(web)
                    pywhatkit.playonyt(query)
                    speak("Done, Sir")


            def searchWikipedia(query):
                if "wikipedia" in query:
                    speak("Searching from wikipedia....")
                    query = query.replace("wikipedia", "")
                    query = query.replace("search wikipedia", "")
                    query = query.replace("jarvis", "")
                    results = wikipedia.summary(query,sentences=2)
                    speak("According to wikipedia..")
                    print(results)
                    speak(results)

These are the error codes I get

line 51, in query = takeCommand().lower()

line 25, in takeCommand audio = r.listen(source, 0, 4)

line 652, in listen buffer = source.stream.read(source.CHUNK)

line 161, in read return self.pyaudio_stream.read(size, exception_on_overflow=False)

line 608, in read return pa.read_stream(self._stream, num_frames, exception_on_overflow)

KeyboardInterrupt

Exception ignored on calling ctypes callback function: <function catch_errors..call_with_this at 0x000001E3FA726560> Traceback (most recent call last):

line 97, in call_with_this

line 1734, in isEnabledFor TypeError: 'NoneType' object is not callable

These are the modules I have installed:

import datetime import webbrowser import pyttsx3 import pywhatkit import speech_recognition import wikipedia


Solution

  • replace your takeCommand() function with this:

    def takeCommand():
    r = speech_recognition.Recognizer()
    with speech_recognition.Microphone() as source:
        speech_recognition.Recognizer().adjust_for_ambient_noise(source, duration=0.2)
        print("Listening...")
        audio = r.listen(source)
    
        try:
            query = r.recognize_google(audio, language='en-in')
            print(f"user said:{query}\n")
    
        except Exception as e:
            return "None"
        return query
    

    Other than this there are no other errors in your code and for the web scraping functions. You are not actually calling them

    Update

    engine = pyttsx3.init("sapi5")
    voices = engine.getProperty("voices")
    engine.setProperty("voice", voices[0].id)
    engine.setProperty("rate", 150)
    
    
    def speak(audio):
        engine.say(audio)
        engine.runAndWait()
    
    
    def takeCommand():
        r = speech_recognition.Recognizer()
        with speech_recognition.Microphone() as source:
            print("Listening...")
            r.pause_threshold = 4
            r.energy_threshold = 300
            audio = r.listen(source, 0, 4)
    
        try:
            print("Understanding...")
            query = r.recognize_google(audio, language='en-us')
            print(f"You said: {query}\n")
        except Exception as e:
            print("Say that again")
            return "None"
        return query
    
    def searchGoogle(query):
        import wikipedia as googleScrap
        speak("This is what I found")
        try:
            pywhatkit.search(query)
            result = googleScrap.summary(query, 1)
            speak(result)
    
        except:
            speak("Did not find anything about that, sorry")
    
    
    def searchYoutube(query):
        web = "https://www.youtube.com/results?search_query=" + query
        webbrowser.open(web)
        pywhatkit.playonyt(query)
        speak("Done, Sir")
    
    
    def searchWikipedia(query):
        results = wikipedia.summary(query,sentences=2)
        speak("According to wikipedia..")
        print(results)
        speak(results)
    
    
    # Starting...............................................................................................................
    if __name__ == "__main__":
        while True:
            query = takeCommand().lower()
            if "wake up David" in query:
                hour = int(datetime.datetime.now().hour)
                if 0 <= hour <= 12:
                    speak("Good Morning sir, how are you?")
                elif 12 < hour <= 18:
                    speak("Good Afternoon sir, how are you?")
    
                else:
                    speak("Good Evening sir, how are you?")
    
            while True:
                query = takeCommand().lower()
                if "go to sleep david" in query:
                    speak("Ok, See you, Remember if you need anything just say, wake up david")
                    break
    
                elif "I'm good how are you" in query:
                    speak("I am also good, how may I help you today?")
    
                elif "thank you" in query:
                    speak("You are welcome sir")
    
                elif "wikipedia" in query:
                    speak("Searching from wikipedia....")
                    query = query.replace("wikipedia", "")
                    query = query.replace("search wikipedia", "")
                    query = query.replace("jarvis", "")
                    searchWikipedia(query)
    
                elif "youtube" in query:
                    speak("This is what I found for your search!")
                    query = query.replace("youtube search", "")
                    query = query.replace("youtube", "")
                    query = query.replace("jarvis", "")
                    searchYoutube(query)
                
                elif "google" in query:
                    query = query.replace("david", "")
                    query = query.replace("google search", "")
                    query = query.replace("google", "")
                    searchGoogle(query)