Skip to content

Instantly share code, notes, and snippets.

@adameubanks
Created April 11, 2020 01:21
Show Gist options
  • Save adameubanks/7886acf1a51d8635ec2ab09f81de6268 to your computer and use it in GitHub Desktop.
Save adameubanks/7886acf1a51d8635ec2ab09f81de6268 to your computer and use it in GitHub Desktop.
Code for the video where we build a Jarvis like virtual assistant in python 3
import wolframalpha
client = wolframalpha.Client("lilpumpsaysnopeeking")
import wikipedia
import PySimpleGUI as sg
sg.theme('DarkPurple')
layout =[[sg.Text('Enter a command'), sg.InputText()],[sg.Button('Ok'), sg.Button('Cancel')]]
window = sg.Window('PyDa', layout)
import pyttsx3
engine = pyttsx3.init()
while True:
event, values = window.read()
if event in (None, 'Cancel'):
break
try:
wiki_res = wikipedia.summary(values[0], sentences=2)
wolfram_res = next(client.query(values[0]).results).text
engine.say(wolfram_res)
sg.PopupNonBlocking("Wolfram Result: "+wolfram_res,"Wikipedia Result: "+wiki_res)
except wikipedia.exceptions.DisambiguationError:
wolfram_res = next(client.query(values[0]).results).text
engine.say(wolfram_res)
sg.PopupNonBlocking(wolfram_res)
except wikipedia.exceptions.PageError:
wolfram_res = next(client.query(values[0]).results).text
engine.say(wolfram_res)
sg.PopupNonBlocking(wolfram_res)
except:
wiki_res = wikipedia.summary(values[0], sentences=2)
engine.say(wiki_res)
sg.PopupNonBlocking(wiki_res)
engine.runAndWait()
print (values[0])
window.close()
@kromil24
Copy link

Sir please give me advance jarvis code🙏

@TonyYTCZ
Copy link

Sorry, but I didn't get that.... how I run it? I haven't that text below code. Can anyone help me? And yes I actually didn't anything with python before.

@azrael0learza
Copy link

Sorry, but I didn't get that.... how I run it? I haven't that text below code. Can anyone help me? And yes I actually didn't anything with python before.

Before you can try run the code you'll need to setup a couple of things.
First you'll need to download python for the operating system you're using, once that is done you'll need to select and IDE [code editor] that you would like to use, after that just copy over the code and run it from there, you should see a window pop up that allows you to enter something to search for.

If I misunderstood you just let me know.

Also check out KhanradCoders YouTube or Udemy, think he still has some content on how to set up everything.

@gibbrann
Copy link

How do I install wolframalpha

@azrael0learza
Copy link

How do I install wolframalpha

Hi there, all you have to do is go to their website and create an account, once your done you'll need to go through to the 'My Apps' option and from there you'll see a window that allows you to get your key that can be added into your code, below is a video by Khanrad that goes through the process, https://youtu.be/NZMTWBpLUa4?t=152

@Deadcrazy-Gamer
Copy link

Exception has occurred: ModuleNotFoundError
No module named 'wolframalpha'
File "/Users/ramprasadmuduli/Desktop/7886acf1a51d8635ec2ab09f81de6268-7f16c6f6111e9727b4e9e243842857cb1c199cd1/PyDa.py", line 1, in
import wolframalpha

@TheSoupMasta
Copy link

Exception has occurred: ModuleNotFoundError
No module named 'wolframalpha'
File "/Users/ramprasadmuduli/Desktop/7886acf1a51d8635ec2ab09f81de6268-7f16c6f6111e9727b4e9e243842857cb1c199cd1/PyDa.py", line 1, in
import wolframalpha

Okay, open your terminal and type in "pip install wolframalpha".
If the editor you are using doesn't have a terminal, open your command prompt and type in 'pip install wolframalpha".

@TheSoupMasta
Copy link

Alright, so since I was quarantined I decided to add speech recognition and turn the code into object oriented programming.

Here is the code:

import pyaudio
import wolframalpha
import PySimpleGUI as sg
import pyttsx3
import speech_recognition as sr
client = wolframalpha.Client(<Enter your code here>)
engine = pyttsx3.init()


class Jarvis():
    def __init__(self, mic_obj, rec_obj):
        self.mic = mic_obj
        self.r = rec_obj
        self.search_engine()


    def search_engine(self):
        sg.theme('DarkPurple')
        layout = [[sg.Text('Enter a command'), sg.InputText()], [sg.Button('Ok'), sg.Button('Cancel'), sg.Button('Use Voice Recognition')]]
        self.window = sg.Window('PyDa', layout)

        while True:
            event, values = self.window.read()
            if event in (None, 'Cancel'):
                break
            if event in (None, 'Use Voice Recognition'):
                self.voicerecognition()
                break
            try:
                wolfram_res = next(client.query(values[0]).results).text
                engine.say(wolfram_res)
                sg.PopupNonBlocking("Wolfram Result: " + wolfram_res)
            except:
                quit('Error')


            engine.runAndWait()

        self.window.close()

    def voicerecognition(self):
        sg.theme('DarkPurple')
        layout = [[sg.Button("Press and Speak"), sg.Button('Cancel')]]
        self.speechrecogwindow = sg.Window("Speech Recognition Assistant", layout)

        while True:
            event, values = self.speechrecogwindow.read()
            if event in (None, "Press and Speak"):
                self.speechrecogwindow.close()
                self.recog(self.r)
                break
            if event in (sg.WIN_CLOSED, 'Cancel'):
                self.speechrecogwindow.close()
                quit()


    def recog(self, rec_obj):
        while True:
            wolfram_res = None
            with self.mic as source:
                print("Speak")
                rec_obj.adjust_for_ambient_noise(source)
                audio = rec_obj.listen(source)
                try:
                    response = rec_obj.recognize_google(audio)
                except(sr.UnknownValueError):
                    quit("Could not recognize")
            try:
                wolfram_res = next(client.query(response).results).text
                engine.say(wolfram_res)
                engine.runAndWait()
                layout = [[sg.Text(wolfram_res)], [sg.Button('Ok')]]
                self.popup = sg.Window('Popup', layout)
                while True:
                    event, values = self.popup.read()
                    if event in (sg.WIN_CLOSED, 'Ok'):
                        self.popup.close()
                        break
                self.voicerecognition()
            except(StopIteration):
                print("Could not find answer")
                self.voicerecognition()

if __name__ == "__main__":
    jarvis = Jarvis(sr.Microphone(), sr.Recognizer())

When you run the code, make sure all libraries have been installed and click "Use Voice Recognition". Then click 'Press to Speak' and talk. I took out the wikipedia entries because it bugged out, and speech recognition works exactly like you think it does. When you talk, it'll respond. After responding, it will open a window with the response in text format. Press "Ok" or the close window button to exit out. I will create a repo with a .exe of the engine when I have time, so if you're reading this then you should probably check if I've done so.

Thanks for reading - TheSoupMasta

@SliverKitsune
Copy link

SliverKitsune commented Jul 22, 2021

OK after messing around I was able to get the right window to appear so you can press the Run Voice Recognition button instead of the other window. Sorry, there wasn't much of a change.

 

import pyaudio
import wolframalpha
import PySimpleGUI as sg
import pyttsx3
import speech_recognition as sr

client = wolframalpha.Client("NoLooking")
engine = pyttsx3.init()



class Jarvis:
    def __init__(self, mic_obj, rec_obj):
        self.mic = mic_obj
        self.r = rec_obj
        self.search_engine()

    def search_engine(self):
        sg.theme('DarkPurple')
        layout = [[sg.Text('Enter a command'), sg.InputText()],
                  [sg.Button('Ok'), sg.Button('Cancel'), sg.Button('Use Voice Recognition')]]
        window = sg.Window('PyDa', layout)

        while True:
            event, values = window.read()
            if event in (None, 'Cancel'):
                break
            if event in (None, 'Use Voice Recognition'):
                self.voicerecognition()
                break
            try:
                wolfram_res = next(client.query(values[0]).results).text
                engine.say(wolfram_res)
                sg.PopupNonBlocking("Wolfram Result: " + wolfram_res)
            except:
                quit('Error')

            engine.runAndWait()

        window.close()

    def voicerecognition(self):
        sg.theme('DarkPurple')
        layout = [[sg.Button("Press and Speak"), sg.Button('Cancel')]]
        self.speechrecogwindow = sg.Window("Speech Recognition Assistant", layout)

        while True:
            event, values = self.speechrecogwindow.read()
            if event in (None, "Press and Speak"):
                self.speechrecogwindow.close()
                self.recog(self.r)
                break
            if event in (sg.WIN_CLOSED, 'Cancel'):
                self.speechrecogwindow.close()
                quit()

    def recog(self, rec_obj):
        while True:
            wolfram_res = None
            with self.mic as source:
                print("Speak")
                rec_obj.adjust_for_ambient_noise(source)
                audio = rec_obj.listen(source)
                try:
                    response = rec_obj.recognize_google(audio)
                except(sr.UnknownValueError):
                    quit("Could not recognize")
            try:
                wolfram_res = next(client.query(response).results).text
                engine.say(wolfram_res)
                engine.runAndWait()
                layout = [[sg.Text(wolfram_res)], [sg.Button('Ok')]]
                self.popup = sg.Window('Popup', layout)
                while True:
                    event, values = self.popup.read()
                    if event in (sg.WIN_CLOSED, 'Ok'):
                        self.popup.close()
                        break
                self.voicerecognition()
            except(StopIteration):
                print("Could not find answer, Try Again")
                self.voicerecognition()


if __name__ == "__main__":
    jarvis = Jarvis(sr.Microphone(), sr.Recognizer())

@TheSoupMasta
Copy link

TheSoupMasta commented Jul 26, 2021

OK after messing around I was able to get the right window to appear so you can press the Run Voice Recognition button instead of the other window. Sorry, there wasn't much of a change.

 

import pyaudio
import wolframalpha
import PySimpleGUI as sg
import pyttsx3
import speech_recognition as sr

client = wolframalpha.Client("NoLooking")
engine = pyttsx3.init()



class Jarvis:
    def __init__(self, mic_obj, rec_obj):
        self.mic = mic_obj
        self.r = rec_obj
        self.search_engine()

    def search_engine(self):
        sg.theme('DarkPurple')
        layout = [[sg.Text('Enter a command'), sg.InputText()],
                  [sg.Button('Ok'), sg.Button('Cancel'), sg.Button('Use Voice Recognition')]]
        window = sg.Window('PyDa', layout)

        while True:
            event, values = window.read()
            if event in (None, 'Cancel'):
                break
            if event in (None, 'Use Voice Recognition'):
                self.voicerecognition()
                break
            try:
                wolfram_res = next(client.query(values[0]).results).text
                engine.say(wolfram_res)
                sg.PopupNonBlocking("Wolfram Result: " + wolfram_res)
            except:
                quit('Error')

            engine.runAndWait()

        window.close()

    def voicerecognition(self):
        sg.theme('DarkPurple')
        layout = [[sg.Button("Press and Speak"), sg.Button('Cancel')]]
        self.speechrecogwindow = sg.Window("Speech Recognition Assistant", layout)

        while True:
            event, values = self.speechrecogwindow.read()
            if event in (None, "Press and Speak"):
                self.speechrecogwindow.close()
                self.recog(self.r)
                break
            if event in (sg.WIN_CLOSED, 'Cancel'):
                self.speechrecogwindow.close()
                quit()

    def recog(self, rec_obj):
        while True:
            wolfram_res = None
            with self.mic as source:
                print("Speak")
                rec_obj.adjust_for_ambient_noise(source)
                audio = rec_obj.listen(source)
                try:
                    response = rec_obj.recognize_google(audio)
                except(sr.UnknownValueError):
                    quit("Could not recognize")
            try:
                wolfram_res = next(client.query(response).results).text
                engine.say(wolfram_res)
                engine.runAndWait()
                layout = [[sg.Text(wolfram_res)], [sg.Button('Ok')]]
                self.popup = sg.Window('Popup', layout)
                while True:
                    event, values = self.popup.read()
                    if event in (sg.WIN_CLOSED, 'Ok'):
                        self.popup.close()
                        break
                self.voicerecognition()
            except(StopIteration):
                print("Could not find answer, Try Again")
                self.voicerecognition()


if __name__ == "__main__":
    jarvis = Jarvis(sr.Microphone(), sr.Recognizer())
import pyaudio
import wolframalpha
import PySimpleGUI as sg
import pyttsx3
import speech_recognition as sr
import webbrowser as wb
client = wolframalpha.Client("U5RKU7-26R988VUA5")
engine = pyttsx3.init()


class Jarvis():
    def __init__(self, mic_obj, rec_obj):
        self.mic = mic_obj
        self.r = rec_obj
        self.r.energy_threshold = 300
        self.search_engine()


    def search_engine(self):
        sg.theme('DarkPurple')
        layout = [[sg.Text('Enter a command'), sg.InputText()], [sg.Button('WolfRamAlpha'), sg.Button('Google'), sg.Button('Use Voice Recognition'), sg.Button('Cancel')]]
        self.window = sg.Window('PyDa', layout)

        while True:
            event, values = self.window.read()
            if event in (None, 'Cancel'):
                break
            if event in (None, 'Use Voice Recognition'):
                self.voicerecognition()
                break
            if event in (None, 'Google'):
                try:
                    wb.get().open_new_tab('https://www.google.com/search?q=' + values[0][0::])
                except:
                    print('Error')
                    self.search_engine()
            if event in (None, 'Ok'):
                try:
                    wolfram_res = next(client.query(values[0]).results).text
                    engine.say(wolfram_res)
                    sg.PopupNonBlocking("Wolfram Result: " + wolfram_res)
                except:
                    quit('Error')




            engine.runAndWait()

        self.window.close()

    def voicerecognition(self):
        sg.theme('DarkPurple')
        layout = [[sg.Button("Press and Speak"), sg.Button('Cancel')]]
        self.speechrecogwindow = sg.Window("Speech Recognition Assistant", layout)

        while True:
            event, values = self.speechrecogwindow.read()
            if event in (None, "Press and Speak"):
                self.speechrecogwindow.close()
                self.recog(self.r)
                break
            if event in (sg.WIN_CLOSED, 'Cancel'):
                self.speechrecogwindow.close()
                quit()


    def recog(self, rec_obj):
        while True:
            wolfram_res = None
            with self.mic as source:
                print("Speak")
                rec_obj.adjust_for_ambient_noise(source)
                audio = rec_obj.listen(source)
                try:
                    response = rec_obj.recognize_google(audio)
                except(sr.UnknownValueError):
                    quit("Could not recognize")
            if response.split()[0] != 'Google':
                try:
                    wolfram_res = next(client.query(response).results).text
                    engine.say(wolfram_res)
                    engine.runAndWait()
                    layout = [[sg.Text(wolfram_res)], [sg.Button('Ok')]]
                    self.popup = sg.Window('Popup', layout)
                    while True:
                        event, values = self.popup.read()
                        if event in (sg.WIN_CLOSED, 'Ok'):
                            self.popup.close()
                            break
                    self.voicerecognition()
                except(StopIteration):
                    print("Could not find answer")
                    self.voicerecognition()
            elif response.split()[0] == 'Google':
                wb.get().open_new_tab('https://www.google.com/search?q=' + response[7::])
                self.voicerecognition()

if __name__ == "__main__":
    jarvis = Jarvis(sr.Microphone(), sr.Recognizer())

Thanks for the help. I also added a choice to google using the google button or saying "google" before your question/comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment