-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
292 lines (273 loc) · 12.6 KB
/
Main.py
File metadata and controls
292 lines (273 loc) · 12.6 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from gtts import gTTS
import os
import pygame
import speech_recognition as sr
import datetime
import psutil
import webbrowser
import time
import subprocess
import wolframalpha
from ytmusicapi import YTMusic
import pyautogui
import keyFile
import openai
import weather
import requests
import pywhatkit
class Shabnam:
def __init__(self):
pygame.init()
self.engine = None
self.recognizer = sr.Recognizer()
self.wolfram_alpha_app_id = (
keyFile.WOLFRAM
) # Replace with your Wolfram Alpha app ID
self.ytmusic = YTMusic("oauth.json")
self.openai_api_key = keyFile.OPENAI # Replace with your OpenAI API key
openai.api_key = self.openai_api_key
self.weather_api_key = (
keyFile.WEATHER_API
) # Replace with your OpenWeatherMap API key
def take_command(self):
with sr.Microphone() as source:
print("Listening for wake word...")
try:
audio = self.recognizer.listen(source)
command = self.recognizer.recognize_google(audio).lower()
if "shabnam" in command:
self.detected_callback()
except sr.UnknownValueError:
pass
except sr.RequestError as e:
print(
f"Could not request results from Google Speech Recognition service; {e}"
)
def detected_callback(self):
while True:
with sr.Microphone() as source:
print("Listening for command...")
try:
audio = self.recognizer.listen(source, timeout=5)
command = self.recognizer.recognize_google(audio).lower()
print("You said:", command)
if "hello" in command:
self.talk("Hi there! How can I help you?")
elif (
"how are you" in command
or "how are you doing" in command
or "how r u" in command
):
self.talk("I'm doing great! How about you?")
audio_how = self.recognizer.listen(source, timeout=5)
command_how = self.recognizer.recognize_google(
audio_how
).lower()
print("You said:", command_how)
if "i am good" in command_how or "i am fine" in command_how:
self.talk("That's great to hear!")
elif (
"i am not good" in command_how
or "i am not fine" in command_how
):
self.talk("I'm sorry to hear that.")
else:
self.talk("I'm sorry, I didn't understand that.")
elif "what's your name" in command:
self.talk("My name is Shabnam.")
elif "what can you do" in command:
self.talk(
"I can do a lot of things. You can ask me to open apps, search the web, play music, and more."
)
elif "who made you" in command:
self.talk("I was made by Gagan Raj. He is my creators.")
elif "who are you" in command:
self.talk("I am Shabnam, a virtual assistant.")
elif "what is your purpose" in command:
self.talk(
"I was created to help you with your daily tasks and make your life easier."
)
elif "goodbye" in command or "bye" in command:
self.talk("Goodbye! Have a great day!")
exit()
elif "time" in command:
current_time = datetime.datetime.now().strftime("%H:%M")
self.talk(f"The current time is {current_time}")
elif "battery" in command:
battery_percentage = psutil.sensors_battery().percent
self.talk(f"The battery is at {battery_percentage}%")
elif "open google" in command:
self.talk("Sure, opening Google.")
webbrowser.open("https://www.google.com")
elif "open youtube" in command:
self.talk("Sure, opening YouTube.")
webbrowser.open("https://www.youtube.com")
elif "system information" in command:
self.system_information()
elif "search" in command:
search_query = command.replace("search", "").strip()
self.search_information(search_query)
elif "calculate" in command or "math" in command:
math_query = (
command.replace("calculate", "").replace("math", "").strip()
)
self.calculate_math(math_query)
elif "open whatsapp" in command:
self.talk("Sure, opening WhatsApp.")
subprocess.run(["open", "-a", "WhatsApp"])
elif "open telegram" in command:
self.talk("Sure, opening Telegram.")
subprocess.run(["open", "-a", "Telegram"])
elif "open vscode" in command:
self.talk("Sure, opening VS Code.")
subprocess.run(["open", "-a", "Visual Studio Code"])
elif "open spotify" in command:
self.talk("Sure, opening Spotify.")
subprocess.run(["open", "-a", "Spotify"])
elif "open terminal" in command:
self.talk("Sure, opening Terminal.")
subprocess.run(["open", "-a", "Terminal"])
elif "open finder" in command:
self.talk("Sure, opening Finder.")
subprocess.run(["open", "-a", "Finder"])
elif "open app store" in command:
self.talk("Sure, opening App Store.")
subprocess.run(["open", "-a", "App Store"])
elif "open chrome" in command:
self.talk("Sure, opening Chrome.")
subprocess.run(["open", "-a", "Google Chrome"])
elif "open notes" in command or "open notes" in command:
self.talk("Sure, opening Notes.")
subprocess.run(["open", "-a", "Notes"])
elif "open calendar" in command or "show calendar" in command:
self.talk("Sure, opening Calendar.")
subprocess.run(["open", "-a", "Calendar"])
elif "open website" in command:
website = (
command.replace("open website", "")
.strip()
.replace(" ", "")
.replace("dot", ".")
.strip()
)
self.talk(f"Sure, opening {website}.")
webbrowser.open(f"https://{website}")
elif "play music" in command:
self.play_music()
elif "play replay mix" in command or "play replay" in command:
self.talk("Sure, playing replay mix.")
webbrowser.open(keyFile.REPLAY_PLAYLIST)
time.sleep(3)
pyautogui.press("space")
elif "tell me a joke" in command:
joke = self.get_joke()
self.talk(joke)
elif "what's the weather at" in command:
location = command.replace("what's the weather at", "").strip()
print(location)
self.get_weather_at(location)
elif "weather" in command:
self.get_weather()
else:
self.talk(
"Sorry, I didn't understand that. Can you please repeat?"
)
except sr.WaitTimeoutError:
print("Timed out waiting for command")
break
except sr.UnknownValueError:
break
except sr.RequestError as e:
print(
f"Could not request results from Google Speech Recognition service; {e}"
)
def talk(self, text):
print(text)
tts = gTTS(text=text, lang="en")
tts.save("temp.mp3")
pygame.mixer.music.load("temp.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
os.remove("temp.mp3")
def system_information(self):
cpu_usage = psutil.cpu_percent(interval=1)
ram_usage = psutil.virtual_memory().percent
disk_usage = psutil.disk_usage("/").percent
self.talk(
f"CPU usage is {cpu_usage} percent. RAM usage is {ram_usage} percent. Disk usage is {disk_usage} percent."
)
def search_information(self, query):
self.talk(f"Searching for {query}")
webbrowser.open(f"https://www.google.com/search?q={query}")
def calculate_math(self, query):
try:
client = wolframalpha.Client(self.wolfram_alpha_app_id)
res = client.query(query)
answer = next(res.results).text
self.talk(f"The answer is {answer}")
except Exception as e:
print(f"Error calculating math: {e}")
self.talk("Sorry, I couldn't perform the calculation.")
def play_music(self):
try:
playlists = self.ytmusic.get_library_playlists()
if playlists:
playlist_id = playlists[0]["playlistId"]
playlist_url = f"https://music.youtube.com/playlist?list={playlist_id}"
self.talk("Sure, playing music.")
webbrowser.open(playlist_url)
time.sleep(3) # Wait for the page to load
pyautogui.press("space")
else:
self.talk(
"Sorry, I couldn't find any playlists in your YouTube Music library."
)
except Exception as e:
print(f"Error playing music: {e}")
self.talk("Sorry, I couldn't play music at the moment.")
def get_joke(self):
response = openai.Completion.create(
engine="davinci",
prompt="Tell me a joke:",
max_tokens=50,
)
joke = response.choices[0].text.strip()
return joke
def get_weather(self):
location = "chennai" # Replace with the desired city
try:
current_weather = weather.get_weather(location, self.weather_api_key)
current_weather = current_weather.split("-")
temperature = current_weather[1]
description = current_weather[2]
self.talk(
f"The current weather in {location} is {temperature} with {description}."
)
except Exception as e:
print(f"Error fetching weather information: {e}")
self.talk("Sorry, I couldn't fetch the weather information.")
def get_weather_at(self, location):
location = location # Replace with the desired city
try:
current_weather = weather.get_weather(location, self.weather_api_key)
current_weather = current_weather.split("-")
temperature = current_weather[1]
description = current_weather[2]
self.talk(
f"The current weather in {location} is {temperature} with {description}."
)
except Exception as e:
print(f"Error fetching weather information: {e}")
self.talk("Sorry, I couldn't fetch the weather information.")
shabnam = Shabnam()
with sr.Microphone() as source:
print("Adjusting for ambient noise...")
shabnam.recognizer.adjust_for_ambient_noise(source, duration=5)
print("Shabnam is ready. Say 'Shabnam' to start.")
try:
while True:
shabnam.take_command()
time.sleep(2)
except KeyboardInterrupt:
print("Shutting down...")