Created
April 11, 2020 01:21
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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
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.