-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (63 loc) · 2.62 KB
/
Copy pathmain.py
File metadata and controls
80 lines (63 loc) · 2.62 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
import speech_recognition as sr
import webbrowser
import pyttsx3
recognizer = sr.Recognizer()
engine = pyttsx3.init()
def speak(text):
"""Convert text to speech."""
engine.say(text)
engine.runAndWait()
def process_command(command):
"""Process user voice command and execute corresponding action."""
command = command.lower()
if "open google" in command:
speak("Opening Google")
webbrowser.open("https://google.com")
elif "open facebook" in command:
speak("Opening Facebook")
webbrowser.open("https://facebook.com")
elif "open youtube" in command:
speak("Opening YouTube")
webbrowser.open("https://youtube.com")
elif "open instagram" in command:
speak("Opening Instagram")
webbrowser.open("https://instagram.com")
elif "open chat" in command:
speak("Opening ChatGPT")
webbrowser.open("https://chatgpt.com")
elif "search" in command:
search_query = command.replace("search", "").strip()
if search_query:
url = f"https://www.google.com/search?q={search_query.replace(' ', '+')}"
speak(f"Searching for {search_query} on Google")
webbrowser.open(url)
else:
speak("What would you like to search for?")
elif "exit" in command or "stop" in command:
speak("Goodbye!")
exit()
else:
speak("Sorry, I didn't understand that command.")
if __name__ == "__main__":
speak("Initializing Jarvis...")
while True:
print("Listening for activation...")
try:
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source) # Reduce noise issues
audio = recognizer.listen(source, timeout=5, phrase_time_limit=2)
word = recognizer.recognize_google(audio).lower()
if "jarvis" in word:
speak("Yes? How can I help?")
print("Jarvis Active... Listening for command")
with sr.Microphone() as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
command = recognizer.recognize_google(audio)
process_command(command)
except sr.UnknownValueError:
print("Could not understand the command. Try again.")
except sr.RequestError:
print("Speech Recognition service unavailable. Check your internet connection.")
except Exception as e:
print(f"Error: {e}")