DesktopAssistant/Jarvis2_4windows.py

150 lines
3.9 KiB
Python
Raw Normal View History

import configparser
import os
import speech_recognition as sr
2021-10-08 12:32:05 +05:30
import gui
2021-10-03 15:22:09 +05:30
from actions import (
change_rate,
change_voice,
2021-10-09 12:47:49 +02:00
change_volume,
search_engine_selector,
speak,
wish_me,
2021-10-09 18:12:19 +05:30
set_gui_speak
)
2021-10-07 17:38:48 +02:00
from commands import (
command_bye,
command_hello,
command_mail,
command_nothing,
command_open,
command_pause_music,
command_play_music,
2021-10-07 17:38:48 +02:00
command_search,
command_stop_music,
command_unpause_music,
2021-10-07 17:38:48 +02:00
command_whatsup,
command_wikipedia,
)
2021-10-03 15:22:09 +05:30
popular_websites = {
"google": "https://www.google.com",
"youtube": "https://www.youtube.com",
"wikipedia": "https://www.wikipedia.org",
"amazon": "https://www.amazon.com",
2021-10-08 18:53:12 +02:00
"github": "https://www.github.com",
}
2021-10-04 07:56:18 +05:30
2021-10-08 18:53:12 +02:00
def main(search_engine, take_command, debug):
2021-10-09 17:44:21 +05:30
def execute_the_command_said_by_user():
2021-10-09 12:46:14 +02:00
query = take_command()
# logic for executing commands without arguments
phrases = {
"what's up": command_whatsup,
"nothing": command_nothing,
"abort": command_nothing,
"stop": command_nothing,
"hello": command_hello,
"bye": command_bye,
"play music": command_play_music,
"unpause": command_unpause_music,
"pause music": command_pause_music,
"stop music": command_stop_music
}
for phrase, command in phrases.items():
if phrase in query:
command()
# logic for executing commands with arguments
if "wikipedia" in query:
command_wikipedia(speak, debug, query)
elif "open" in query:
command_open(
query,
popular_websites,
debug,
search_engine,
take_command
)
elif "search" in query:
command_search(query, search_engine)
elif "mail" in query:
command_mail(take_command)
elif "change rate" in query:
change_rate(query, take_command)
elif "change voice" in query.lower():
change_voice(query, take_command)
elif "change volume" in query.lower():
change_volume(query, take_command)
speak("Next Command! Sir!")
2021-10-09 17:44:21 +05:30
gui.set_speak_command(execute_the_command_said_by_user)
2021-10-09 18:12:19 +05:30
set_gui_speak(gui.speak)
2021-10-09 17:44:21 +05:30
gui.mainloop()
2021-10-03 15:22:09 +05:30
2021-10-07 16:16:42 +02:00
def run():
2021-10-08 18:46:20 +02:00
master = config['DEFAULT']['master']
2021-10-03 15:22:09 +05:30
search_engine = search_engine_selector(config)
2021-10-08 12:32:05 +05:30
2021-10-07 16:16:42 +02:00
debug = config['DEFAULT']['debug']
2021-10-08 12:32:05 +05:30
2021-10-07 16:16:42 +02:00
if debug == "True":
2021-10-08 18:53:12 +02:00
def take_command():
return input("Command |--> ")
2021-10-07 16:16:42 +02:00
else:
2021-10-08 18:53:12 +02:00
def take_command():
2021-10-07 16:16:42 +02:00
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening....")
r.pause_threshold = 0.5
audio = r.listen(source)
query = " "
2021-10-03 15:22:09 +05:30
try:
2021-10-07 16:16:42 +02:00
print("Recognizing....")
query = r.recognize_google(audio, language="en-in")
print("user said: " + query)
except sr.UnknownValueError:
if debug == "True":
print("Sorry Could You please try again")
else:
pass
speak("Sorry Could You please try again")
except Exception as e:
if debug == "True":
print(e)
print("Say That Again Please")
else:
pass
return query
2021-10-07 16:16:42 +02:00
speak(text="Initializing Jarvis....")
2021-10-08 18:53:12 +02:00
wish_me(master)
main(search_engine, take_command, debug)
2021-10-07 16:16:42 +02:00
2021-10-07 16:09:11 +02:00
if os.path.isfile('./config.ini'): # Checks if config.ini exists.
config = configparser.ConfigParser() # if exists loads library.
config.read('config.ini') # and also the file.
2021-10-07 16:16:42 +02:00
run() # Then it launches the main program
2021-10-07 16:09:11 +02:00
else:
# if it doesn't exist it drops an error message and exits.
print('You need a config.ini file.')
print('Check the documentation in the Github Repository.')