-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjarvis3.py
More file actions
161 lines (127 loc) · 4.54 KB
/
jarvis3.py
File metadata and controls
161 lines (127 loc) · 4.54 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
# ==========================================================
# JARVIS 2.0 — Enterprise Edition
# With blocking speak() for perfect timing
# ==========================================================
from gtts import gTTS
from io import BytesIO
import pygame
import speech_recognition as sr
import datetime
import wikipedia
import webbrowser
import os
import smtplib
import time
import sys
pygame.mixer.init()
# ==========================================================
# FIXED BLOCKING SPEAK FUNCTION
# ==========================================================
def speak(text):
"""Speak using gTTS + pygame (BLOCKING so mic waits)."""
try:
mp3_fp = BytesIO()
tts = gTTS(text=text, lang='en')
tts.write_to_fp(mp3_fp)
mp3_fp.seek(0)
pygame.mixer.music.load(mp3_fp)
pygame.mixer.music.play()
# WAIT HERE UNTIL SPEAKING IS DONE
while pygame.mixer.music.get_busy():
time.sleep(0.1)
except Exception as e:
print("Speak Error:", e)
# ==========================================================
def wishMe():
hour = int(datetime.datetime.now().hour)
if hour < 12:
speak("Good Morning Sir!")
elif hour < 18:
speak("Good Afternoon Sir!")
else:
speak("Good Evening Sir!")
intro = (
"I am Jarvis 2.0 Sir, built by Mohammed Touheed Patel. "
"I can open applications, search Wikipedia, play music, "
"send emails, tell you the time, and much more. "
"Please tell me how may I assist you."
)
print(intro)
speak(intro)
# ==========================================================
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("\nListening...")
r.adjust_for_ambient_noise(source)
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"You said: {query}\n")
return query.lower()
except:
speak("Sorry, can you please repeat that?")
return "none"
# ==========================================================
def sendEmail(to, content):
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("your-email@gmail.com", "your-app-password")
server.sendmail("your-email@gmail.com", to, content)
server.close()
speak("Email has been sent.")
except Exception as e:
print(e)
speak("Sorry, I could not send the email.")
# ==========================================================
def performTask(query):
if "wikipedia" in query:
speak("Searching Wikipedia.")
query = query.replace("wikipedia", "").strip()
try:
result = wikipedia.summary(query, sentences=2)
print(result)
speak(result)
except:
speak("Sorry, I could not find that on Wikipedia.")
elif "open youtube" in query:
webbrowser.open("https://youtube.com")
elif "open google" in query:
webbrowser.open("https://google.com")
elif "open stackoverflow" in query:
webbrowser.open("https://stackoverflow.com")
elif "play music" in query:
music_dir = r"C:\Users\YourName\Music" # CHANGE THIS
try:
songs = os.listdir(music_dir)
os.startfile(os.path.join(music_dir, songs[0]))
speak("Playing music.")
except:
speak("Music folder not found.")
elif "the time" in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
speak(f"The time is {strTime}")
print(strTime)
elif "open code" in query:
codePath = r"C:\Users\YourName\AppData\Local\Programs\Microsoft VS Code\Code.exe"
os.startfile(codePath)
speak("Opening Visual Studio Code")
elif "email" in query:
speak("What should I say?")
content = takeCommand()
to = "example@gmail.com"
sendEmail(to, content)
elif "quit" in query or "exit" in query:
speak("Exiting Jarvis. Thank you for using me.")
print("Goodbye!")
sys.exit()
# ==========================================================
if __name__ == "__main__":
wishMe()
while True:
query = takeCommand()
if query != "none":
performTask(query)