-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJarvis.py
More file actions
114 lines (97 loc) · 3.75 KB
/
Copy pathJarvis.py
File metadata and controls
114 lines (97 loc) · 3.75 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
import pyttsx3
import speech_recognition as sr
import webbrowser
import datetime
import wikipedia
# Initialize the recognizer and text-to-speech engine once
r = sr.Recognizer()
engine = pyttsx3.init()
def speak(audio):
"""Converts text to speech."""
engine.say(audio)
engine.runAndWait()
def takeCommand():
"""Listens for commands and returns the recognized text."""
try:
# Attempt to access the microphone
with sr.Microphone() as source:
print('Listening for command...')
r.adjust_for_ambient_noise(source, duration=1) # Adjust for ambient noise
r.pause_threshold = 1 # Pause threshold for speech
audio = r.listen(source)
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"Command: {query}")
return query
except sr.UnknownValueError:
speak("Sorry, I didn't catch that. Please say that again.")
return None
except sr.RequestError as e:
speak("Sorry, there seems to be an issue with the service.")
print(f"RequestError: {e}")
return None
except AssertionError as e:
speak("Microphone issue detected. Please check your audio input settings.")
print(f"AssertionError: {e}")
return None
except AttributeError as e:
speak("An audio input issue occurred. Please ensure your microphone is connected properly.")
print(f"AttributeError: {e}")
return None
except Exception as e:
speak("An unexpected error occurred. Please try again.")
print(f"Unexpected error: {e}")
return None
def tellDay():
"""Tells the current day."""
day = datetime.datetime.today().weekday() + 1
Day_dict = {1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday', 7: 'Sunday'}
if day in Day_dict:
speak(f"Today is {Day_dict[day]}")
def tellTime():
"""Tells the current time."""
time = datetime.datetime.now().strftime("%H:%M")
speak(f"The time is {time}")
def Hello():
"""Greets the user."""
speak("Hello, tell me how I can assist you.")
def Take_query():
"""Main function to take user queries and respond accordingly."""
Hello()
while True:
query = takeCommand()
if query is None:
continue # Skip if no valid command was recognized
query = query.lower()
if "open geeksforgeeks" in query:
speak("Opening Geeks for Geeks")
webbrowser.open("https://www.geeksforgeeks.com")
elif "open google" in query:
speak("What do you want to search for?")
search_query = takeCommand()
if search_query:
webbrowser.open("https://www.google.com/search?q=" + search_query)
elif "which day is it" in query:
tellDay()
elif "tell me the time" in query:
tellTime()
elif "exit" in query:
speak("Bye. See you! Have a nice day!")
break
elif "from wikipedia" in query:
speak("Checking Wikipedia")
query = query.replace("from wikipedia", "")
try:
result = wikipedia.summary(query, sentences=4)
speak("According to Wikipedia")
speak(result)
except wikipedia.exceptions.DisambiguationError:
speak("Multiple entries found. Please be more specific.")
except wikipedia.exceptions.PageError:
speak("No summary available for this topic.")
elif "tell me your name" in query:
speak("I am Jarvis, your desktop assistant.")
else:
speak("Sorry, I didn't understand that command.")
if __name__ == '__main__':
Take_query()