-
Notifications
You must be signed in to change notification settings - Fork 3
/
smart_buddy.py
137 lines (119 loc) · 5.39 KB
/
smart_buddy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pyttsx3
import speech_recognition as sr
import datetime
from time import sleep
import wikipedia
import os
import webbrowser
import pyjokes
import random
import psutil
import subprocess
import os
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice',voices[0].id)
hr=int(datetime.datetime.now().hour)
m = int(datetime.datetime.now().minute)
#random lists---------------------------------------------------------------------
think = ["Thinking...","Recognizing..."]
sorry = ["Apologies...I could not get that","Sorry..Will you please repeat that","Please try one more time..","Sorry..","Didn't get that","Please make sure you are connected to the internet!"]
greetings = ["Hi there! ", "Hello?","What\'s up","Hey Sir","Pleased to meet you!","Namaste"]
farewell = ["See you soon","Great talking with you...See you soon","Bye Bye","Nice talking with you","Take Care!"]
#---------------------------------------------------------------------------------
def speak(audio):
engine.say(audio)
engine.runAndWait() #without this the audio is not audible
def wishme():
hr=int(datetime.datetime.now().hour)
if hr>=0 and hr<12:
speak("Good Morning Sir, How can I help you?")
elif hr>=12 and hr<=15:
speak("Good Afternoon Sir, How can I help you?")
else:
speak("Good Evening Sir, How can I help you?")
def takeCommand():
r=sr.Recognizer()
with sr.Microphone() as source:
print('Listening.....')
r.pause_threshold=1 #time to pause after listening
r.energy_threshold = 600 #so that the assistant don't pay attention to disturbing voices
audio=r.listen(source)
try:
print(random.choice(think)
query = r.recognize_google(audio, language='en-in')
print(f"{query}\n")
except Exception as e:
sorry_temp = random.choice(sorry)
print(sorry_temp)
speak(sorry_temp)
return "None"
return query
if __name__ == "__main__":
wishme()
while True:
query = takeCommand().lower() #converting query to lower case
if 'wikipedia' in query:
query = query.replace("wikipedia","")
if query==" ":
speak("I didn't get that! ")
else:
speak("hmmmmm.....OK..Let's see..")
results = wikipedia.summary(query, sentences=2)
speak("According to wikipedia")
print(results)
speak(results)
elif 'on youtube' in query or 'open youtube' in query or 'youtube' in query:
query = query.replace("on youtube","")
speak("Opening youtube...")
webbrowser.open("youtube.com")
elif 'open google' in query:
speak("Opening google...")
webbrowser.Chrome()
webbrowser.open("google.in")
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"Currently, its {strTime}")
elif 'stop listening' in query or 'bye' in query:
speak(random.choice(farewell))
exit(0)
elif 'joke' in query or 'jokes' in query or 'laugh' in query:
speak(pyjokes.get_joke())
speak("Ha Ha Ha Ha")
elif 'hello' in query or 'hi' in query or 'hey' in query:
speak(random.choice(greetings))
elif 'good morning buddy' in query:
if hr>=0 and hr<12:
speak("Good Morning to you too...")
print("Good Morning to you too...")
else:
speak(f"Really...? Its {hr} {m}.Anyways..Good Morning!")
print(f"Really...? Its {hr} {m}.Anyways..Good Morning!")
elif 'battery status' in query:
battery = psutil.sensors_battery()
percent= str(battery.percent)
speak(f"Your device is at {percent} %")
print(percent+"%")
if int(percent)<15:
speak("I suggest you put your device on charging!")
elif 'open calculator' in query or 'start calulator' in query or 'calculate' in query or 'calculator' in query:
subprocess.Popen('C:\\Windows\\System32\\calc.exe')
elif 'open wordpad' in query or 'start wordpad' in query or 'wordpad' in query:
subprocess.Popen('C:\\Windows\\System32\\write.exe')
elif 'make notes' in query or 'open notes' in query or 'notes' in query or 'note' in query:
speak("Which one would you like to open ? Notepad or OneNote")
ask = takeCommand().lower()
if 'onenote' in query:
os.system('start onenote:')
elif 'notepad' in query:
speak("Opening Notepad")
subprocess.Popen('C:\\Windows\\System32\\notepad.exe')
else:
speak("Sorry.....Please try again")
print("Sorry.....Please try again")
elif 'open start' in query or 'click on start' in query:
pyautogui.click(39, 1048) # these coordinates are according to my screen
elif 'screenshot' in query:
speak('screenshot captured') # for capturing screenshot
im = pyautogui.screenshot()
im.show()