-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
238 lines (195 loc) · 8.18 KB
/
main.py
File metadata and controls
238 lines (195 loc) · 8.18 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# Import required libraries
import os
import webbrowser
import pyautogui
from time import sleep
import speech_recognition as sr
import re
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
# My packages
from browser import play_yt, google_ask, website # To use websites on browser
# from open_ai import openai_ask # To use open AI chatGPT
from gemini_ai import ask_gemini
from spotify_web import direct_play, login, search_play, lab_sch_play, like_plist, current_play, like_song, forward_song, backward_song, home # To use spotify web music
from tools import say, wish, save_result, take_ss # To take screenshot, save file, etc...
# Audio into text function...
def get_audio():
try:
r = sr.Recognizer()
with sr.Microphone() as source:
audio= r.listen(source)
print("Recognizing...")
query = r.recognize_google(audio, language="en-in").lower() # return result text in lowercase
print(f"User said🔊 {query}")
return query
except Exception:
return "something went wrong"
""" ALL VIRTUAL ASSISTANT COMMANDS """
def main():
# wish to time
say(wish())
say("i am your virtual assistant sir, please tell me how may i help you")
# Listening continuously...
while True:
print("Listening...")
query = get_audio() # All text outputs in lowercase
web_browser(query)
my_computer(query)
spotify_music(query)
gemini(query)
# todo: Website on webbrowser commands
def web_browser(query):
# Open direct logged in websites
sites = [
["youtube", "https://www.youtube.com"],
["python status", "https://cs50.me/cs50p"],
["facebook", "https://www.facebook.com"],
["instagram", "https://www.instagram.com"],
["linkedin", "https://www.linkedin.com"],
["github", "https://www.github.com"],
["telegram", "https://web.telegram.org/k/"],
["stack overflow", "https://stackoverflow.com/"],
["canva", "https://canva.com/"],
]
for site in sites:
if f"open {site[0]}" in query:
say(f"opening {site[0]}")
webbrowser.open(site[1])
# Open new or non logged in websites
elif "website" in query:
web = query.removeprefix("open ").replace(" website", "").removesuffix(" open")
website(web)
break
# search on google
if "on google" in query:
ask = query.replace(" on google", "")
google_ask(ask)
sleep(3)
say("these are google result")
# search on youtube
elif "on youtube" in query:
video = query.replace("on youtube", "")
play_yt(video)
sleep(3)
say("these are videos on searched topic")
# close current website in browser
elif "close webpage" in query or "close web page" in query or "close this webpage" in query or "close this web page" in query or "close current webpage" in query:
pyautogui.hotkey("ctrl", "w")
say("sure")
# todo: My computer commands
def my_computer(query):
# open application cmd
apps = [
["vlc", r' "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\VideoLAN\VLC media player.lnk" '],
["edge", r' "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk" '],
["vs code", r' "C:\Users\shres\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Visual Studio Code\Visual Studio Code.lnk" '],
["jetbrains", r' "C:\Users\shres\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\JetBrains Toolbox\JetBrains Toolbox.lnk" '],
# [" ", r' '],
]
for app in apps:
if f"open {app[0]}" in query or f"open {app[0]} app" in query or f"open {app[0]} application" in query:
say(f"opening {app[0]} application")
os.system(app[1])
# close applications cmd
apps = [
["vlc", r"taskkill /im vlc.exe"],
["edge", r"taskkill /im edge.exe"]
]
for app in apps:
if f"close {app[0]}" in query or f"exit {app[0]}" in query or f"close {app[0]} app" in query or f"close {app[0]} application" in query:
os.system(app[1])
say(f"{app[0]} is closed")
# Take screenshot cmd
if "take screenshot" in query or "take a screenshot" in query:
take_ss("ai_shot") # screenshot save in tools file !!
print("Your Screenshot is save in tools package file !!")
say("got it")
# close current app
elif "close app" in query or "close this app" in query or "close current app" in query : #or "close current application" in query or "close application" in query or "close this application" in query:
pyautogui.hotkey("alt", "f4")
say("Done")
# For exit program
elif "close program" in query or "exit program" in query:
say("program is closing, have a good day master")
exit()
# open history file command
elif "chat history file" in query or "history file" in query:
os.system("chat_history.txt")
say("here is chat history file")
# todo: sportify music cmds
def spotify_music(query):
# direct search song/playlist and play
if "on spotify" in query:
songs = re.search(r'\b\s*(.*?)\s*\bon\s*spotify\b', query).group(1).replace("play","").replace("search", "")
direct_play(songs)
sleep(2)
say("searched song is playing")
# spotify web commands...
elif "open spotify" in query or "music app" in query:
try:
say("opening spotify")
login()
say("spotify is opened, what would you like to listen")
except Exception:
say("i think somthing went wrong, please check your internet and chrome browser permission ")
elif "search song" in query or "search playlist" in query:
song = query.replace("search","").replace("song", "")
search_play(song)
sleep(2)
say("searched song is playing")
elif "play song" in query or "stop song" in query:
current_play()
say("okay")
elif "like song" in query or "like this song" in query or "unlike song" in query or "unlike this song" in query:
like_song()
say("done")
elif "like playlist" in query or "like this" in query or "unlike playlist" in query or "unlike this playlist" in query:
like_plist()
say("done")
elif "search in library" in query or "in library" in query:
plist = query.replace("search in library", "").replace("in library", "")
lab_sch_play(plist)
sleep(2)
say("searched playlist is playing")
elif "back to home" in query or "go to home" in query or "open home page" in query:
try:
home()
say("allright")
except Exception:
print("That's spotify_web command")
elif "change song" in query or "next song" in query:
try:
forward_song()
except Exception:
say("spotify is not open")
elif "play again" in query or "back song" in query or "play past song" in query:
try:
backward_song()
except Exception:
say("spotify is not open")
# todo: AI cmds
def gemini(query):
# result from open_ai 'chat gpt'
if "explain" in query or "in detail" in query or "gemini ai" in query or "gpt" in query or "essay" in query or "application" in query or "letter" in query:
result = ask_gemini(query)
try:
# say or read only 1 or 2 sentences
sentences = re.findall(r'[^.!?]*[.!?]', result)
[say(sentence.strip()) for sentence in sentences[:1]]
# save detail answer in file
print(result, "\n\nThis result is also save in chat_history.txt file")
save_result(result)
say("The detail answer is also save in chat history.txt file")
except Exception as e:
print(e)
say("sorry i can't understand")
# if wh or how in query so return that's answer
elif re.search(r'\bwh\w+\b|how\b', " ".join(query.split()[:5]), re.IGNORECASE):
result = ask_gemini(f"{query} give short answer")
print(result)
say(result)
# Call main function to run program
if __name__ == "__main__":
main()