-
-
Save adameubanks/7886acf1a51d8635ec2ab09f81de6268 to your computer and use it in GitHub Desktop.
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() |
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".
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
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())
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.
Exception has occurred: ModuleNotFoundError
No module named 'wolframalpha'
File "/Users/ramprasadmuduli/Desktop/7886acf1a51d8635ec2ab09f81de6268-7f16c6f6111e9727b4e9e243842857cb1c199cd1/PyDa.py", line 1, in
import wolframalpha