-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voice Assistent.py
103 lines (90 loc) · 3.27 KB
/
Voice Assistent.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
import os
import datetime
import webbrowser
import speech_recognition as sr
import pyttsx3
def initialize_engine():
engine = pyttsx3.init()
return engine
def speak(engine, text):
engine.say(text)
engine.runAndWait()
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
command = r.recognize_google(audio)
print(f"User said: {command}\n")
except Exception as e:
print("Sorry, I didn't catch that. Could you please repeat?")
return "None"
return command.lower()
def set_reminder(engine, command):
speak(engine, "What should I remind you about?")
reminder = listen()
speak(engine, "When do you want to be reminded? Please say the time in hours and minutes.")
reminder_time = listen()
try:
hour, minute = map(int, reminder_time.split())
now = datetime.datetime.now()
reminder_datetime = now.replace(hour=hour, minute=minute)
if now > reminder_datetime:
reminder_datetime += datetime.timedelta(days=1)
speak(engine, f"Alright, I will remind you about '{reminder}' at {hour:02d}:{minute:02d}.")
while True:
if datetime.datetime.now() >= reminder_datetime:
speak(engine, f"Reminder: {reminder}")
break
except ValueError:
speak(engine, "Sorry, I couldn't understand the time you provided. Please try again.")
def create_todo_list(engine, command):
todo_list = []
speak(engine, "Let's create a to-do list. Please say the tasks one by one. Say 'done' when you're finished.")
while True:
task = listen()
if task == "done":
break
todo_list.append(task)
speak(engine, f"Added: {task}")
speak(engine, "Here's your to-do list:")
for task in todo_list:
speak(engine, task)
def search_web(engine, command):
search_terms = command.replace("search", "").strip()
if search_terms:
url = f"https://www.google.com/search?q={search_terms}"
speak(engine, f"Searching for '{search_terms}'")
webbrowser.open(url)
else:
speak(engine, "Please provide a search term.")
def show_help(engine):
help_text = """
I can help you with the following tasks:
1. Set reminders: Say 'set reminder' followed by the reminder and time.
2. Create to-do lists: Say 'create to-do list' and then list your tasks one by one.
3. Search the web: Say 'search' followed by the search terms.
4. Show available commands: Say 'help'.
5. To exit, say 'exit' or 'quit'.
"""
print(help_text)
speak(engine, help_text)
def main():
engine = initialize_engine()
speak(engine, "Hello, I am your voice assistant. How can I help you today?")
while True:
command = listen()
if "reminder" in command:
set_reminder(engine, command)
elif "to-do" in command or "todo" in command:
create_todo_list(engine, command)
elif "search" in command:
search_web(engine, command)
elif "help" in command:
show_help(engine)
elif "exit" in command or "quit" in command:
speak(engine, "Goodbye!")
break
if __name__ == "__main__":
main()