-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lucy_Assistant.py
330 lines (289 loc) · 11.9 KB
/
Lucy_Assistant.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import google.generativeai as genai
from dotenv import load_dotenv
import os
import pyttsx3
import speech_recognition as sr
import webbrowser
import spacy
from datetime import datetime, timedelta
import threading
import getpass
import time
from AppOpener import open, close
from plyer import notification
import requests
import pywhatkit as kit
load_dotenv()
api_key = os.getenv('gemini_api_key')
weather_api = os.getenv('open_weather_api')
# Configuration and initialization of the gemini model
genai.configure(api_key=api_key)
model = genai.GenerativeModel(model_name="gemini-pro")
# English language model for spaCy
nlp = spacy.load("en_core_web_sm")
health_tips = [
"Drink at least 8 glasses of water to stay hydrated.",
"Eat a balanced diet rich in fruits, vegetables, and whole grains.",
"Get at least 7-8 hours of sleep each night.",
"Practice deep breathing exercises to reduce stress.",
"Take breaks and stretch regularly if you sit for long periods.",
"Incorporate more physical activity into your daily routine."
]
fitness_tips = [
"Aim for at least 30 minutes of moderate exercise, such as brisk walking, every day.",
"Incorporate strength training exercises into your workout routine.",
"Warm up before exercising and cool down afterward.",
"Stay consistent with your workouts to see long-term benefits.",
"Listen to your body and avoid overexertion.",
"Try different types of exercise to keep your routine interesting."
]
def speech_engine_settings():
lucy = pyttsx3.init()
lucy.setProperty('rate', 130)
voices = lucy.getProperty('voices')
lucy.setProperty('voice', voices[1].id)
return lucy
def set_reminder(interval, message):
def reminder():
time.sleep(interval)
notification_title = "Reminder"
notification_message = message
notification.notify(title=notification_title, message=notification_message, timeout=10)
reminder_thread = threading.Thread(target=reminder)
reminder_thread.start()
def get_current_date_time():
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
current_date = now.strftime("%Y-%m-%d")
current_day = now.strftime("%A")
return f"The current date is {current_date}, it is a {current_day} and the time is {current_time}"
def remind_drinking_water():
def send_notification():
while True:
notification.notify(
title="Reminder",
message="This is your reminder to drink water",
timeout=5
)
time.sleep(1800)
reminder_thread = threading.Thread(target=send_notification)
reminder_thread.daemon = True
reminder_thread.start()
def recognize_speech(prompt="Listening..."):
r = sr.Recognizer()
with sr.Microphone() as source:
print(prompt)
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
recognized_text = r.recognize_google(audio).lower()
return recognized_text
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print(f"Could not request results from Google Speech Recognition service; {e}")
except Exception as e:
print(f"An error occurred: {e}")
return None
def health_tips_function(command):
return f"Here are some fitness tips, {health_tips}"
def fitness_tips_function(command):
return f"Here are some fitness tips, {fitness_tips}"
def extract_city(command):
doc = nlp(command)
for ent in doc.ents:
if ent.label_ == "GPE":
return ent.text
return None
def execute_command(command):
keywords = {
"reminder": set_reminder_function,
"date": get_current_date_time_function,
"time": get_current_date_time_function,
"open": open_application_function,
"close": close_application_function,
"google": google_search_function,
"ask gemini to": gemini_function,
"weather": weather_function,
"open a file": open_file_function,
"close a file": close_file_function,
"delete a file": delete_file_function,
"youtube": play_yt_content_function,
"thank you": custom_responses_function,
"how are you": custom_responses_function,
"i am doing fine": custom_responses_function,
"i am talking to someone else": custom_responses_function,
"health tips:": health_tips_function,
"fitness tips": fitness_tips_function,
"play My Likes PlayList on youtube music": play_yt_music_function,
"exit": exit_function
}
for keyword, function in keywords.items():
if keyword in command:
return function(command)
return "I'm sorry, I didn't understand that command."
def set_reminder_function(command):
lucy = speech_engine_settings()
lucy.say("What is the reminder message?")
lucy.runAndWait()
reminder_message = recognize_speech("Listening for reminder message...")
if reminder_message is None:
return "I couldn't understand the reminder message. Please try again."
lucy.say("In how many minutes should I remind you?")
lucy.runAndWait()
interval_text = recognize_speech("Listening for time interval...")
if interval_text is None:
return "I couldn't understand the time interval. Please try again."
try:
# Attempt to extract minutes from the recognized text
interval = int(next(filter(str.isdigit, interval_text), 0))
set_reminder(interval*60, reminder_message)
return f"Reminder set for {interval} minutes."
except ValueError:
return "I couldn't understand the time interval. Please try again."
def play_yt_content_function(command):
yt_content = command.replace("play video", "").strip()
kit.playonyt(yt_content)
return f"Certainly"
def play_yt_music_function(command):
yt_music = "https://music.youtube.com/watch?v=Bu0LKfB3CR8&list=LM"
webbrowser.open(yt_music)
def custom_responses_function(command):
if "thank you" in command:
return f"Your welcome"
elif "how are you" or "how are you doing" in command:
return f"i am doing well, thanks for asking, what about you?"
elif "i am doing fine" in command or "i am fine" in command or "i'm good" in command:
return f"That's good to hear"
elif "i was talking to someone else" in command:
return "i'm sorry for interrupting your conversation, please continue"
def get_current_date_time_function(command):
return get_current_date_time()
def open_application_function(command):
application_name = command.replace("open", "").strip()
try:
open(application_name)
return f"Opening {application_name}."
except Exception as e:
return f"Could not open {application_name}. Error: {e}"
def close_application_function(command):
application_name = command.replace("close", "").strip()
try:
close(application_name)
return f"Closing {application_name}."
except Exception as e:
return f"Could not close {application_name}. Error: {e}"
def google_search_function(command):
search_query = command.replace("google", "").strip()
google_search_url = f"https://www.google.com/search?q={search_query.replace(' ', '+')}"
webbrowser.open(google_search_url)
return f"Opening Google search for: {search_query}"
def gemini_function(command):
user = command.replace("ask gemini to", "").strip()
response = model.generate_content(user)
formatted_response = response.strip("*", "")
print("Gemini Response:", formatted_response.text)
return formatted_response.text
def get_weather(city):
weather_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={weather_api}"
response = requests.get(weather_url)
data = response.json()
if data['cod'] == 200:
main = data['main']
weather = data['weather'][0]
temperature = main['temp']
temp_in_celcius = temperature-273.15
final_temp_in_celcius = round(temp_in_celcius, 2)
description = weather['description']
return f"The weather in {city} is {description} with a temperature of {final_temp_in_celcius}°C."
else:
return "City not found."
def weather_function(command):
city = extract_city(command)
print(city)
if city:
return get_weather(city)
else:
return "I couldn't determine the city for the weather request."
def open_file_function(command):
lucy = speech_engine_settings()
lucy.say("Please enter the file path you want to open:")
lucy.runAndWait()
file_path = input("Enter the file path: ").strip()
try:
if os.path.isfile(file_path):
os.startfile(file_path)
return f"Opening file: {file_path}"
else:
return f"File not found: {file_path}"
except Exception as e:
return f"Could not open file. Error: {e}"
def close_file_function(command):
lucy = speech_engine_settings()
lucy.say("Please enter the file path you want to close:")
lucy.runAndWait()
file_path = input("Enter the file path: ").strip()
try:
if os.path.isfile(file_path):
os.close(file_path)
return f"Closing file: {file_path}"
else:
return f"File not found: {file_path}"
except Exception as e:
return f"Could not close file. Error: {e}"
def delete_file_function(command):
lucy = speech_engine_settings()
lucy.say("Please enter the file path you want to delete:")
lucy.runAndWait()
file_path = input("Enter the file path: ").strip()
try:
if os.path.isfile(file_path):
os.remove(file_path)
return f"Deleting file: {file_path}"
else:
return f"File not found: {file_path}"
except Exception as e:
return f"Could not delete file. Error: {e}"
def exit_function(command):
return "exit"
def main():
remind_drinking_water()
while True:
user_command = recognize_speech("Listening...")
if user_command:
print("What you said:", user_command)
if "lucy" in user_command:
lucy = speech_engine_settings()
lucy.say("Hello, my name is Lucy, please enter your passkey for authorization")
lucy.runAndWait()
auth_passkey = getpass.getpass("Enter your passkey: ")
if auth_passkey == os.getenv('passkey'):
lucy.say("Authorization successful, Hello Niall, how may I assist you today?")
lucy.runAndWait()
while True:
lucy.say("Please tell me what you want me to do. To exit the program just say 'exit'.")
lucy.runAndWait()
command = recognize_speech("Listening for command...")
if command:
result = execute_command(command)
if result == "exit":
lucy.say("Exiting the program. Goodbye Niall.")
lucy.runAndWait()
return
elif result == "do not exit" or result == "don't exit":
continue
else:
lucy.say(result)
lucy.runAndWait()
else:
lucy.say("I didn't catch that. Could you please repeat?")
lucy.runAndWait()
else:
lucy.say("You have entered an invalid passkey, Authorization failed.")
lucy.runAndWait()
else:
print("Error: 'lucy' not found in the command")
else:
print("Error: Could not process the command")
if __name__ == "__main__":
main()