forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJarvis.py
More file actions
196 lines (155 loc) · 5.49 KB
/
Jarvis.py
File metadata and controls
196 lines (155 loc) · 5.49 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
import speech_recognition as sr
import pyttsx3
import os
import webbrowser
import random
import subprocess
import datetime
import requests
# Initialize the recognizer
recognizer = sr.Recognizer()
engine = pyttsx3.init()
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source, duration=2)
print("Making microphone Ready!..Done")
random_messages = ["How can I assist you today?",
"What can I do for you?",
"How's your day going?",
"What brings you here today?",
"Ready to help with whatever you need.",
"What can I help you with right now?",
"What’s on your mind?",
"How can I make your day better?",
"Need any assistance today?",
"Let’s make things happen. What do you need?"]
random_messages_2 = ["Hi there!",
"Hello!",
"Greetings!",
"What's up?"]
# Function to make Jarvis speak
def speak(text):
engine.say(text)
engine.runAndWait()
# Function to get voice input
def take_command():
with sr.Microphone() as source:
print("Listening...")
recognizer.pause_threshold = 1
audio = recognizer.listen(source)
try:
print("Recognizing...")
command = recognizer.recognize_google(audio, language='en-in')
print(f"User said: {command}\n")
except sr.RequestError:
speak("Sorry, the speech recognition service is down")
return None
except sr.UnknownValueError:
speak("Sorry, I did not catch that. Please try again.")
return None
except Exception as e:
print(f"An error occurred: {e}")
return None
return command.lower()
def tell_joke():
try:
response = requests.get("https://official-joke-api.appspot.com/random_joke")
if response.status_code == 200:
joke = response.json()
speak(f"Here's a joke: {joke['setup']} .. {joke['punchline']}")
else:
speak("Sorry, couldn't fetch the joke!")
except Exception as e:
speak(f"Error: {e}")
def fetch_fun_fact():
try:
response = requests.get("https://uselessfacts.jsph.pl/random.json?language=en")
if response.status_code == 200:
fact = response.json()['text']
return fact
else:
return "Sorry, I couldn't find a fun fact right now."
except Exception as e:
return f"Error fetching fun fact: {e}"
assistant_name = "Jarvis"
def change_assistant_name():
global assistant_name
speak("What would you like to name me?")
new_name = take_command()
if new_name:
assistant_name = new_name
speak(f"My new name is {assistant_name}!")
def easter_egg():
responses = [
"You found the secret code! Congratulations!",
"Did you know? I can dance... in my own way, at least!",
"I see you're curious, good job finding this hidden gem!"
]
speak(random.choice(responses))
def get_greeting():
current_hour = datetime.datetime.now().hour
if 0 <= current_hour < 12:
greeting = "Good morning!"
elif 12 <= current_hour < 17:
greeting = "Good afternoon!"
elif 17 <= current_hour < 21:
greeting = "Good evening!"
else:
greeting = "Good night!"
fun_fact = fetch_fun_fact()
return f"{greeting} here's a fun fact for you: {fun_fact}"
print(get_greeting())
speak(get_greeting())
# Function to handle commands
def handle_command(command):
if command is None:
return
commands = command.split('and')
for cmd in commands:
if 'hello' in cmd:
speak(random.choice(random_messages_2))
if 'time' in cmd:
current_time = datetime.datetime.now().strftime('%I:%M %P')
speak(f"The time is {current_time}")
elif 'open site' in cmd:
websites = {
'youtube': 'https://youtube.com',
'google': 'https://google.com',
'github': 'https://github.com',
'Linkedin': 'https://Linkedin.com'
}
speak("Which site would you like to open, sir?")
command = take_command()
site = command.split()[-1].lower() # Assume last word is the site name
if site in websites:
speak(f"Opening {site}.")
webbrowser.open(websites[site])
else:
speak(f"Sorry, I don't have {site} in my database.")
elif 'open notepad' in cmd:
speak("Opening Notepad")
os.system('notepad.exe')
elif 'open music' in cmd:
speak("opening music sir...")
music_path = "C:/Users/adity/Music/PairDrop_files_20240813_0043/Tears after the cloudy weather.mp3"
subprocess.Popen(["start", music_path], shell = True)
elif 'joke' in command:
tell_joke()
elif 'change name' in command:
change_assistant_name()
elif 'secret code' in command:
easter_egg()
elif 'exit' in command or 'quit' in cmd:
speak("Goodbye!")
return False
else:
speak("Sorry, I didn't understand that command.")
return True
# Main function to execute commands
def main():
get_greeting()
running = True
while running:
command = take_command()
running = handle_command(command)
if __name__ == "__main__":
main()