Skip to content

Commit

Permalink
Merge pull request #39 from Jothin-kumar/issue_16
Browse files Browse the repository at this point in the history
GUI support!
  • Loading branch information
Harsha200105 authored Oct 9, 2021
2 parents acc2d46 + ba34bd4 commit 29ec429
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 100 deletions.
156 changes: 82 additions & 74 deletions Jarvis2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import speech_recognition as sr
import wikipedia

import gui

print("Initializing Jarvis....")
master = getpass.getuser()

Expand Down Expand Up @@ -46,6 +48,7 @@ def search(search_query, search_engine):


def speak(text):
gui.speak(text)
engine.say(text)
engine.runAndWait()

Expand Down Expand Up @@ -95,81 +98,86 @@ def take_command():

speak("Initializing Jarvis....")
wish_me()
query = take_command().lower()

# logic for executing basic tasks
if "wikipedia" in query.lower():
speak("Searching wikipedia....")
query = query.replace("wikipedia", "")
print_and_speak(wikipedia.summary(query, sentences=2))

elif "what's up" in query or "how are you" in query:
st_msgs = (
"Just doing my thing!",
"I am fine!",
"Nice!",
"I am nice and full of energy",
)
speak(random.choice(st_msgs))

elif "date" in query:
print_and_speak(f"{datetime.datetime.now():%A, %B %d, %Y}")

elif "time" in query:
print_and_speak(f"{datetime.datetime.now():%I %M %p}")

elif "open" in query.lower():
website = query.replace("open", "").strip().lower()
try:
open_url(popular_websites[website])
except IndexError: # If the website is unknown
print(f"Unknown website: {website}")
speak(f"Sorry, I don't know the website {website}")

elif "search" in query.lower():
search_query = query.split("for")[-1]
search_engine = query.split("for")[0].replace("search", "").strip().lower()
search(search_query, search_engine)

elif "email" in query:
speak("Who is the recipient? ")
recipient = take_command()
def execute_the_command_said_by_user():
query = take_command().lower()

# logic for executing basic tasks
if "wikipedia" in query:
speak("Searching wikipedia....")
query = query.replace("wikipedia", "")
print_and_speak(wikipedia.summary(query, sentences=2))

elif "what's up" in query or "how are you" in query:
st_msgs = (
"Just doing my thing!",
"I am fine!",
"Nice!",
"I am nice and full of energy",
)
speak(random.choice(st_msgs))

elif "date" in query:
print_and_speak(f"{datetime.datetime.now():%A, %B %d, %Y}")

elif "time" in query:
print_and_speak(f"{datetime.datetime.now():%I %M %p}")

if "me" in recipient:
elif "open" in query.lower():
website = query.replace("open", "").strip().lower()
try:
speak("What should I say? ")
content = take_command()

server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login("Your_Username", "Your_Password")
server.sendmail("Your_Username", "Recipient_Username", content)
server.close()
speak("Email sent!")
except Exception:
speak(
"Sorry, Sir! I am unable to send your message at this moment!"
)

elif "nothing" in query or "abort" in query or "stop" in query:
speak("okay")
speak("Bye Sir, have a good day.")
sys.exit()

elif "hello" in query:
speak("Hello Sir")

elif "bye" in query:
speak("Bye Sir, have a good day.")
sys.exit()

elif "play music" in query:
music_folder = "Your_music_folder_path(absolute_path)"
music = ("music1", "music2", "music3", "music4", "music5")
random_music = music_folder + random.choice(music) + ".mp3"
os.system(random_music)

speak("Playing your request")

speak("Next Command! Sir!")
open_url(popular_websites[website])
except IndexError: # If the website is unknown
print(f"Unknown website: {website}")
speak(f"Sorry, I don't know the website {website}")

elif "search" in query.lower():
search_query = query.split("for")[-1]
search_engine = query.split("for")[0].replace("search", "").strip().lower()
search(search_query, search_engine)

elif "email" in query:
speak("Who is the recipient? ")
recipient = take_command()

if "me" in recipient:
try:
speak("What should I say? ")
content = take_command()

server = smtplib.SMTP("smtp.gmail.com", 587)
server.ehlo()
server.starttls()
server.login("Your_Username", "Your_Password")
server.sendmail("Your_Username", "Recipient_Username", content)
server.close()
speak("Email sent!")
except Exception:
speak("Sorry Sir! I am unable to send your message at this moment!")

elif "nothing" in query or "abort" in query or "stop" in query:
speak("okay")
speak("Bye Sir, have a good day.")
sys.exit()

elif "hello" in query:
speak("Hello Sir")

elif "bye" in query:
speak("Bye Sir, have a good day.")
sys.exit()

elif "play music" in query:
music_folder = "Your_music_folder_path(absolute_path)"
music = ("music1", "music2", "music3", "music4", "music5")
random_music = music_folder + random.choice(music) + ".mp3"
os.system(random_music)

speak("Playing your request")

speak("Next Command! Sir!")


gui.set_speak_command(execute_the_command_said_by_user)
gui.mainloop()
56 changes: 30 additions & 26 deletions Jarvis2_4windows.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import configparser
import os

import speech_recognition as sr

from actions import (
change_rate,
change_voice,
change_volume,
search_engine_selector,
speak,
wish_me,
import configparser # isort: skip
import os # isort: skip

import gui # isort: skip
import speech_recognition as sr # isort: skip
from actions import ( # isort: skip
change_rate,
change_voice,
change_volume,
search_engine_selector,
set_gui_speak,
speak,
wish_me
)
from commands import (
command_bye,
command_hello,
command_mail,
command_nothing,
command_open,
command_pause_music,
command_play_music,
command_search,
command_stop_music,
command_unpause_music,
command_whatsup,
command_wikipedia,
from commands import ( # isort: skip
command_bye,
command_hello,
command_mail,
command_nothing,
command_open,
command_pause_music,
command_play_music,
command_search,
command_stop_music,
command_unpause_music,
command_whatsup,
command_wikipedia
)

popular_websites = {
Expand All @@ -36,7 +37,7 @@


def main(search_engine, take_command, debug):
while True:
def execute_the_command_said_by_user():
query = take_command()

# logic for executing commands without arguments
Expand Down Expand Up @@ -85,6 +86,9 @@ def main(search_engine, take_command, debug):
change_volume(query, take_command)

speak("Next Command! Sir!")
gui.set_speak_command(execute_the_command_said_by_user)
set_gui_speak(gui.speak)
gui.mainloop()


def run():
Expand Down
10 changes: 10 additions & 0 deletions actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@ def search(search_query, search_engine):
open_url(f"{search_engine}/search?q={search_query}")


def gui_speak(text):
pass


def set_gui_speak(command):
global gui_speak
gui_speak = command


def speak(text):
gui_speak(text)
engine.say(text)
engine.runAndWait()

Expand Down
29 changes: 29 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import tkinter as tk

root = tk.Tk()
main_frame = tk.Frame(master=root)
chat_listbox = tk.Listbox(master=main_frame, height=200, width=50)
scroll_bar = tk.Scrollbar(master=main_frame)
speak_button = tk.Button(master=root, text='Speak', command=lambda: None)


def set_speak_command(command):
speak_button.configure(command=command)


speak_button.pack(side=tk.LEFT, anchor=tk.SW)


def speak(text):
chat_listbox.insert('end', f'Assistant: {text}')
root.geometry('700x500')


chat_listbox.pack(fill=tk.Y, side=tk.LEFT)
scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
scroll_bar.configure(command=chat_listbox.yview)
chat_listbox.configure(yscrollcommand=scroll_bar.set)
main_frame.pack(fill=tk.BOTH)
root.wm_title('Desktop assistant')
root.resizable(False, False)
mainloop = root.mainloop

0 comments on commit 29ec429

Please sign in to comment.