-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
214 lines (194 loc) · 9.04 KB
/
main.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import datetime
import os
import sys
import time
import webbrowser
import pyautogui
import pyttsx3
import speech_recognition as sr
import json
import pickle
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.sequence import pad_sequences
import random
import numpy as np
import psutil
import subprocess
with open("intents.json") as file:
data = json.load(file)
model = load_model("chat_model.h5")
with open("tokenizer.pkl", "rb") as f:
tokenizer=pickle.load(f)
with open("label_encoder.pkl", "rb") as encoder_file:
label_encoder=pickle.load(encoder_file)
def initialize_engine():
engine = pyttsx3.init("sapi5")
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-50)
volume = engine.getProperty('volume')
engine.setProperty('volume', volume+0.25)
return engine
def speak(text):
engine = initialize_engine()
engine.say(text)
engine.runAndWait()
def command():
r = sr.Recognizer()
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=0.5)
print("Listening.......", end="", flush=True)
r.pause_threshold=1.0
r.phrase_threshold=0.3
r.sample_rate = 48000
r.dynamic_energy_threshold=True
r.operation_timeout=5
r.non_speaking_duration=0.5
r.dynamic_energy_adjustment=2
r.energy_threshold=4000
r.phrase_time_limit = 10
# print(sr.Microphone.list_microphone_names())
audio = r.listen(source)
try:
print("\r" ,end="", flush=True)
print("Recognizing......", end="", flush=True)
query = r.recognize_google(audio, language='en-in')
print("\r" ,end="", flush=True)
print(f"User said : {query}\n")
except Exception as e:
print("Say that again please")
return "None"
return query
def cal_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.keys():
day_of_week = day_dict[day]
print(day_of_week)
return day_of_week
def wishMe():
hour = int(datetime.datetime.now().hour)
t = time.strftime("%I:%M:%p")
day = cal_day()
if(hour>=0) and (hour<=12) and ('AM' in t):
speak(f"Good morning Prabh, it's {day} and the time is {t}")
elif(hour>=12) and (hour<=16) and ('PM' in t):
speak(f"Good afternoon Prabh, it's {day} and the time is {t}")
else:
speak(f"Good evening Prabh, it's {day} and the time is {t}")
def social_media(command):
if 'facebook' in command:
speak("opening your facebook")
webbrowser.open("https://www.facebook.com/")
elif 'whatsapp' in command:
speak("opening your whatsapp")
webbrowser.open("https://web.whatsapp.com/")
elif 'discord' in command:
speak("opening your discord server")
webbrowser.open("https://discord.com/")
elif 'instagram' in command:
speak("opening your instagram")
webbrowser.open("https://www.instagram.com/")
else:
speak("No result found")
def schedule():
day = cal_day().lower()
speak("Boss today's schedule is ")
week={
"monday": "Boss, from 5:30 to 7:00 you have gym, from 10:00 to 11:50 please do projects, from 12:00 to 2:00 you have a break, and today you have Programming Lab from 2:00 onwards.",
"tuesday": "Boss, from 9:00 to 9:50 you have Web Development work, from 10:00 to 10:50 you have a break, from 11:00 to 12:50 you have Database Systems work, from 1:00 to 2:00 you have a break, and today you have Open Source Projects lab from 2:00 onwards.",
"wednesday": "Boss, today you have a full day of workes. From 9:00 to 10:50 you have Machine Learning work, from 11:00 to 11:50 you have Operating Systems work, from 12:00 to 12:50 you have Ethics in Technology work, from 1:00 to 2:00 you have a break, and today you have Software Engineering workshop from 2:00 onwards.",
"thursday": "Boss, today you have a full day of workes. From 9:00 to 10:50 you have Computer Networks work, from 11:00 to 12:50 you have Cloud Computing work, from 1:00 to 2:00 you have a break, and today you have Cybersecurity lab from 2:00 onwards.",
"friday": "Boss, today you have a full day of workes. From 9:00 to 9:50 you have Artificial Intelligence work, from 10:00 to 10:50 you have Advanced Programming work, from 11:00 to 12:50 you have UI/UX Design work, from 1:00 to 2:00 you have a break, and today you have Capstone Project work from 2:00 onwards.",
"saturday": "Boss, today you have a more relaxed day. From 9:00 to 11:50 you have team meetings for your Capstone Project, from 12:00 to 12:50 you have Innovation and Entrepreneurship work, from 1:00 to 2:00 you have a break, and today you have extra time to work on personal development and coding practice from 2:00 onwards.",
"sunday": "Boss, today is a holiday, but keep an eye on upcoming deadlines and use this time to catch up on any reading or project work."
}
if day in week.keys():
speak(week[day])
def openApp(command):
if "calculator" in command:
speak("opening calculator")
os.startfile('C:\\Windows\\System32\\calc.exe')
elif "notepad" in command:
speak("opening notepad")
os.startfile('C:\\Windows\\System32\\notepad.exe')
elif "paint" in command:
speak("opening paint")
os.startfile('C:\\Windows\\System32\\mspaint.exe')
def closeApp(command):
if "calculator" in command:
speak("closing calculator")
os.system("taskkill /f /im calc.exe")
elif "notepad" in command:
speak("closing notepad")
os.system('taskkill /f /im notepad.exe')
elif "paint" in command:
speak("closing paint")
os.system('taskkill /f /im mspaint.exe')
def browsing(query):
if 'google' in query:
speak("Boss, what should i search on google..")
s = command().lower()
webbrowser.open(f"{s}")
# elif 'edge' in query:
# speak("opening your microsoft edge")
# os.startfile()
def condition():
usage = str(psutil.cpu_percent())
speak(f"CPU is at {usage} percentage")
battery = psutil.sensors_battery()
percentage = battery.percent
speak(f"Boss our system have {percentage} percentage battery")
if percentage>=80:
speak("Boss we could have enough charging to continue our recording")
elif percentage>=40 and percentage<=75:
speak("Boss we should connect our system to charging point to charge our battery")
else:
speak("Boss we have very low power, please connect to charging otherwise recording should be off...")
if __name__ == "__main__":
wishMe()
# engine_talk("Allow me to introduce myself I am Jarvis, the virtual artificial intelligence and I'm here to assist you with a variety of tasks as best I can, 24 hours a day seven days a week.")
while True:
query = command().lower()
# query = input("Enter your command-> ")
if ('facebook' in query) or ('discord' in query) or ('whatsapp' in query) or ('instagram' in query):
social_media(query)
elif ("Project time table" in query) or ("schedule" in query):
schedule()
elif ("volume up" in query) or ("increase volume" in query):
pyautogui.press("volumeup")
speak("Volume increased")
elif ("volume down" in query) or ("decrease volume" in query):
pyautogui.press("volumedown")
speak("Volume decrease")
elif ("volume mute" in query) or ("mute the sound" in query):
pyautogui.press("volumemute")
speak("Volume muted")
elif ("open calculator" in query) or ("open notepad" in query) or ("open paint" in query):
openApp(query)
elif ("close calculator" in query) or ("close notepad" in query) or ("close paint" in query):
closeApp(query)
elif ("what" in query) or ("who" in query) or ("how" in query) or ("hi" in query) or ("thanks" in query) or ("hello" in query):
padded_sequences = pad_sequences(tokenizer.texts_to_sequences([query]), maxlen=20, truncating='post')
result = model.predict(padded_sequences)
tag = label_encoder.inverse_transform([np.argmax(result)])
for i in data['intents']:
if i['tag'] == tag:
speak(np.random.choice(i['responses']))
elif ("open google" in query) or ("open edge" in query):
browsing(query)
elif ("system condition" in query) or ("condition of the system" in query):
speak("checking the system condition")
condition()
elif "exit" in query:
sys.exit()
# speak("Hello, I'm JARVIS")