-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjarvis.py
1315 lines (1122 loc) · 52.7 KB
/
jarvis.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import psutil
import pyttsx3
import speech_recognition as sr
from datetime import datetime, timedelta
import os
import random
import wikipedia
import webbrowser
import sys
import time
import pyautogui
import requests
import instaloader
import geocoder
import google.generativeai as genai
from bs4 import BeautifulSoup
import speedtest
import pywhatkit
import re
from deep_translator import GoogleTranslator
from pygments import highlight
from pygments.lexers import PythonLexer, HtmlLexer, get_lexer_by_name
from pygments.formatters import TerminalFormatter
from dotenv import load_dotenv
load_dotenv('config.env')
#API KEY Environment
AI_API_KEY = os.getenv("AI_API_KEY")
NEWS_API_KEY = os.getenv("NEWS_API_KEY")
COUNTRY = os.getenv("COUNTRY")
#Ai API CALLING
genai.configure(api_key = AI_API_KEY)
#Credentials calling
NAME = os.getenv("NAME")
PLACE = os.getenv("PLACE")
PASSWORD = os.getenv("PASSWORD")
# speech function
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voices', voices[0].id)
# engine.setProperty('rate', 180)
#REMINDER
def get_greeting():
current_hour = datetime.now().hour
if current_hour == 7:
return "Good Morning sir,"
elif current_hour == 12:
return "Good Afternoon sir,"
elif current_hour == 16:
return "Good Evening sir,"
elif current_hour == 21:
return "Good Night sir,"
else:
return None
def get_battery_status():
battery = psutil.sensors_battery()
if battery:
return battery.percent, battery.power_plugged
else:
return None, None
def speak_greeting_and_time():
greeting = get_greeting()
if greeting is not None:
speak(greeting)
speak_time()
def speak_time():
now = datetime.now().strftime("%I:%M %p")
speak(f"Time is {now} sir")
# Speak function
def speak(audio):
engine.say(audio)
print(audio)
engine.runAndWait()
# weather information
def weather():
search = f"weather in {PLACE}"
url = f"https://www.google.com/search?q={search}"
r = requests.get(url)
data = BeautifulSoup(r.text, "html.parser")
weather = data.find("div", class_="BNeawe tAd8D AP7Wnd").text
speak(f"current weather is {weather} sir ")
# Take command function
def takecommand():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source, timeout=None, phrase_time_limit=None)
try:
print("Recognizing...")
query = r.recognize_google(audio, language='en-in')
print(f"user said:{query}")
except Exception as e:
# speak("Sorry sir, I couldn't understand anything please say again...")
return "none"
query = query.lower()
return query
def wish():
hour = int(datetime.now().hour)
# tt = time.strftime("%I:%M %p")
if hour >= 0 and hour < 12:
speak(f"Good Morning sir") # , now {tt}
elif hour >= 12 and hour < 17:
speak(f"Good Afternoon sir")
elif hour >= 17 and hour < 21:
speak(f"Good Evening sir")
else:
speak(f"Good Night sir")
weather()
# Alarm set
def set_alarm():
speak("Please say the alarm time sir. For example, 7 45 or 10 45")
alarm_time = takecommand()
if ':' in alarm_time:
# Split the time into hours and minutes
parts = alarm_time.split(':')
hours = parts[0]
minutes = parts[1]
elif "o'clock" in alarm_time:
# Extract the hour from the input
hour_match = re.search(r'(\d+)', alarm_time)
if hour_match:
hour = hour_match.group(1)
if 1 <= int(hour) <= 12: # Ensure the hour is within range
hours = hour
minutes = "00"
else:
speak("Sorry sir, I couldn't understand the time. Please try again.")
set_alarm()
return
else:
# Split the time into hours and minutes
parts = alarm_time.split()
if len(parts) == 1:
# If only one part is provided, assume it's in the format "745"
time_str = parts[0]
if len(time_str) == 3:
hours = time_str[0]
minutes = time_str[1:]
elif len(time_str) == 4:
hours = time_str[:2]
minutes = time_str[2:]
else:
speak("Sorry sir, I couldn't understand the time format. Please try again.")
set_alarm()
return
elif len(parts) == 2:
# If two parts are provided, assume it's in the format "10 45"
hours = parts[0]
minutes = parts[1]
else:
speak("Sorry sir, I couldn't understand the time format. Please try again.")
set_alarm()
return
speak("Please say AM or PM for the alarm sir.")
am_pm = takecommand().lower()
if hours.isdigit() and minutes.isdigit() and am_pm in ['am', 'pm']:
# Type the hours
type_text(hours)
pyautogui.press('tab') # Move to the next field
# Type the minutes
type_text(minutes)
pyautogui.press('tab') # Move to the next field
# Type the AM/PM
pyautogui.write(am_pm) # Type AM or PM
else:
speak("Sorry sir, I couldn't understand the time. Please try again.")
set_alarm() # Prompt the user to repeat the input
def clock():
pyautogui.press('win')
time.sleep(0.5)
pyautogui.write("clock")
pyautogui.press('enter')
time.sleep(1)
pyautogui.press("tab")
time.sleep(0.5)
pyautogui.press('enter')
set_alarm()
pyautogui.press("tab")
pyautogui.press("tab")
pyautogui.press("tab")
pyautogui.press("tab")
pyautogui.press("tab")
pyautogui.press("tab")
pyautogui.press('enter')
speak("successfully set alarm sir.")
pyautogui.hotkey('alt', 'f4')
# calculation
def calculate(expression):
try:
result = eval(expression)
speak(f"The result is {result}")
except Exception as e:
speak("Sorry sir, I couldn't understand the expression. Please try again.")
def calculation():
while True:
speak("Would you like to say the question or input the question?")
choice = takecommand().lower()
if "say the question" in choice or "say" in choice:
speak("Please say the question sir")
query = takecommand()
calculate(query)
elif "input the question" in choice or "input" in choice:
speak("Please input the question sir")
expression = input("Enter the mathematical expression: ")
calculate(expression)
elif "no thanks" in choice or "exit" in choice or "no" in choice:
speak("ok sir, Exiting Calculation")
TaskExecution()
else:
speak("Sorry, I didn't understand. Please say again.")
continue
speak("Do you want to ask another mathematical question sir?")
response = takecommand().lower()
if "no thanks" in response or "exit" in response or "no" in response:
speak("ok sir, Exiting Calculation")
TaskExecution()
# Backspace function
def parse_erase_command(query):
num_mapping = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5,
'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10}
words = query.split()
num_to_erase = None
if ("erase" in words or "backspace" in words) and any(word in words for word in ["letter", "letters"]):
erase_index = words.index("erase") if "erase" in words else words.index("backspace")
for i in range(erase_index + 1, len(words)):
if words[i].isdigit():
num_to_erase = int(words[i])
break
elif words[i].lower() in num_mapping:
num_to_erase = num_mapping[words[i].lower()]
break
else:
speak("Please specify the number of letters to erase.")
return None
else:
speak("Sorry, I couldn't understand the command. Please try again.")
return None
return num_to_erase
def erase_letters(num_letters):
for _ in range(num_letters):
pyautogui.press('backspace')
# IP Address
def get_ip_address():
try:
response = requests.get('https://api.ipify.org')
if response.status_code == 200:
return response.text
else:
return "Unable to retrieve IP address. Status code: {}".format(response.status_code)
except Exception as e:
return "An error occurred: {}".format(str(e))
# type
def type_text(text):
pyautogui.typewrite(text)
# Geo location
def get_location():
try:
# Using geocoder to get the location
location = geocoder.ip('me')
if location.ok:
return location.address
else:
return "Unable to retrieve location."
except Exception as e:
return "An error occurred: {}".format(str(e))
#Choose game
def game():
speak("Which game do you want to play sir like Stone, Paper, Scissors or Number Guessing Game?")
choice = takecommand()
if "stone" in choice or "paper" in choice or "scissors" in choice:
SPS()
elif "number guessing" in choice or "number" in choice:
NGG()
# Stone, Paper, Scissor Game
def SPS():
speak("LETS PLAY STONE PAPER SCISSOR !!!")
print("LETS PLAYYYYYYYYY")
user_wins = 0
computer_wins = 0
tie_games = 0
for _ in range(5):
computer_choice = get_computer_choice()
user_choice = get_user_choice()
if user_choice == "break":
speak("Ok sir, Exiting the game.")
TaskExecution()
speak("You say " + user_choice)
speak("I choose " + computer_choice)
result = determine_winner(user_choice, computer_choice)
speak(result)
if "You won" in result:
user_wins += 1
elif "I won" in result:
computer_wins += 1
elif "It's a tie" in result:
tie_games += 1
speak("Game over! You won {} games and I won {} games and {} Tie games.".format(user_wins, computer_wins, tie_games))
speak("Do you want to play again the game sir? Say yes or No.")
play_again = takecommand()
while True:
if play_again == "yes":
SPS()
elif play_again == "no":
speak("Ok sir, Exiting the game.")
TaskExecution()
else:
speak("Sir, please say Yes or No")
play_again = takecommand()
def determine_winner(user_choice, computer_choice):
if user_choice == computer_choice:
return "It's a tie!"
elif (user_choice == 'stone' and computer_choice == 'scissors') or \
(user_choice == 'paper' and computer_choice == 'stone') or \
(user_choice == 'scissors' and computer_choice == 'paper'):
return "You won!"
else:
return "I won!"
def get_user_choice():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening for your choice...")
audio = r.listen(source)
try:
choice = r.recognize_google(audio).lower()
if choice in ['stone', 'paper', 'scissors']:
return choice
elif choice == 'scissor':
return 'scissors'
elif any(keyword in choice for keyword in ['break', 'exit']):
return "break"
else:
speak("I didn't understand. Please repeat.")
return get_user_choice()
except sr.UnknownValueError:
speak("I couldn't understand what you said.")
return get_user_choice()
except sr.RequestError:
speak("There was an issue with the speech recognition service. Please try again later.")
return get_user_choice()
def get_computer_choice():
return random.choice(['stone', 'paper', 'scissors'])
#Number Guessing Game
def NGG():
# Set the range for the random number
lower_bound = 1
upper_bound = 100
# Generate a random number within the specified range
number_to_guess = random.randint(lower_bound, upper_bound)
# Initialize the number of attempts
attempts = 0
speak("Let's Play Number Guessing Game sir!")
speak(f"I'm thinking of a number between {lower_bound} and {upper_bound}.")
speak("Can you guess what it is?")
while True:
# Get the user's guess
guess = input("Enter your guess: ")
try:
guess = int(guess)
except ValueError:
speak("Please enter a valid number sir.")
continue
# Increment the attempt count
attempts += 1
# Check if the guess is correct
if guess < number_to_guess:
speak("Too low sir! Try again.")
elif guess > number_to_guess:
speak("Too high sir! Try again.")
else:
speak(f"Congratulations! You've guessed the number {number_to_guess} in {attempts} attempts.")
break
speak("Do you want to play again the game sir? Say yes or No.")
play_again = takecommand()
while True:
if play_again == "yes":
NGG()
elif play_again == "no":
speak("Ok sir, Exiting the game.")
TaskExecution()
else:
speak("Sir, please say Yes or No")
play_again = takecommand()
# NEWS function
def fetch_news_from_mediastack(country=COUNTRY):
# Force category to "general"
category = "general"
main_url = f"http://api.mediastack.com/v1/news?access_key={NEWS_API_KEY}&categories={category}&countries={country}&limit=10"
response = requests.get(main_url)
if response.status_code != 200:
speak("There was an issue fetching news.")
print(f"Error: {response.status_code}")
print(response.json()) # Debugging: Print error details
return []
main_page = response.json()
if not main_page.get("data"):
speak("No news articles were found for the general category.")
return []
return main_page["data"]
def get_ordinal_word(num):
"""Convert a number to its word form (e.g., 1 -> first, 2 -> second, ...)."""
ordinals = {
1: "First", 2: "Second", 3: "Third", 4: "Fourth", 5: "Fifth",
6: "Sixth", 7: "Seventh", 8: "Eighth", 9: "Ninth", 10: "Tenth"
}
return ordinals.get(num, f"{num}th") # Default to 'nth' for values over 10, but we won't need it here.
def news():
articles = fetch_news_from_mediastack(country=COUNTRY)
if not articles:
speak("Sorry sir, I couldn't fetch any news at the moment.")
return
# Speak the top news with word ordinals
for i, article in enumerate(articles[:10], start=1): # Limiting to top 10 news
headline = article.get("title", "No headline available")
ordinal_word = get_ordinal_word(i) # Get the ordinal word representation (first, second, etc.)
speak(f"Today's {ordinal_word} news is: {headline}")
#Translator function
def translate_text(query):
# Languages supported by Google Translator (subset for example purposes)
supported_languages = {
'af': 'afrikaans', 'sq': 'albanian', 'am': 'amharic', 'ar': 'arabic', 'hy': 'armenian',
'az': 'azerbaijani', 'eu': 'basque', 'be': 'belarusian', 'bn': 'bengali', 'bs': 'bosnian',
'bg': 'bulgarian', 'ca': 'catalan', 'ceb': 'cebuano', 'ny': 'chichewa', 'zh-cn': 'chinese (simplified)',
'zh-tw': 'chinese (traditional)', 'co': 'corsican', 'hr': 'croatian', 'cs': 'czech', 'da': 'danish',
'nl': 'dutch', 'en': 'english', 'eo': 'esperanto', 'et': 'estonian', 'tl': 'filipino',
'fi': 'finnish', 'fr': 'french', 'fy': 'frisian', 'gl': 'galician', 'ka': 'georgian',
'de': 'german', 'el': 'greek', 'gu': 'gujarati', 'ht': 'haitian creole', 'ha': 'hausa',
'haw': 'hawaiian', 'iw': 'hebrew', 'he': 'hebrew', 'hi': 'hindi', 'hmn': 'hmong',
'hu': 'hungarian', 'is': 'icelandic', 'ig': 'igbo', 'id': 'indonesian', 'ga': 'irish',
'it': 'italian', 'ja': 'japanese', 'jw': 'javanese', 'kn': 'kannada', 'kk': 'kazakh',
'km': 'khmer', 'ko': 'korean', 'ku': 'kurdish (kurmanji)', 'ky': 'kyrgyz', 'lo': 'lao',
'la': 'latin', 'lv': 'latvian', 'lt': 'lithuanian', 'lb': 'luxembourgish', 'mk': 'macedonian',
'mg': 'malagasy', 'ms': 'malay', 'ml': 'malayalam', 'mt': 'maltese', 'mi': 'maori',
'mr': 'marathi', 'mn': 'mongolian', 'my': 'myanmar (burmese)', 'ne': 'nepali', 'no': 'norwegian',
'or': 'odia', 'ps': 'pashto', 'fa': 'persian', 'pl': 'polish', 'pt': 'portuguese',
'pa': 'punjabi', 'ro': 'romanian', 'ru': 'russian', 'sm': 'samoan', 'gd': 'scots gaelic',
'sr': 'serbian', 'st': 'sesotho', 'sn': 'shona', 'sd': 'sindhi', 'si': 'sinhala',
'sk': 'slovak', 'sl': 'slovenian', 'so': 'somali', 'es': 'spanish', 'su': 'sundanese',
'sw': 'swahili', 'sv': 'swedish', 'tg': 'tajik', 'ta': 'tamil', 'te': 'telugu',
'th': 'thai', 'tr': 'turkish', 'uk': 'ukrainian', 'ur': 'urdu', 'ug': 'uyghur',
'uz': 'uzbek', 'vi': 'vietnamese', 'cy': 'welsh', 'xh': 'xhosa', 'yi': 'yiddish',
'yo': 'yoruba', 'zu': 'zulu'
}
# Display supported languages
print("Supported languages:")
for code, language in supported_languages.items():
print(f"{code}: {language}")
speak("Please enter which language you want to translate sir?")
# Capture user input for the target language
destination_language = input("Enter language code: ").lower()
# Check if the entered language is valid
if destination_language not in supported_languages:
speak("Invalid language code entered. Please try again.")
return translate_text(query) # Retry if invalid language code
# Ask the user to speak the sentence to translate
speak("Please say the sentence you want to translate.")
sentence_to_translate = takecommand()
if sentence_to_translate:
# Translate the sentence
translated_text = GoogleTranslator(source='auto', target=destination_language).translate(sentence_to_translate)
# Display the translated text
speak("Your Translated text displayed sir")
print(f"Translated text: {translated_text}")
else:
speak("No sentence was recognized. Please try again.")
def translate():
query = "Translate sentence"
translate_text(query)
#Usage file calling colorful
def print_colorful_usage(content):
# ANSI escape codes for colors
BLUE = '\033[94m' # Color for text before the hyphen
GREEN = '\033[92m' # Color for text after the hyphen
RESET = '\033[0m' # Reset color to default
# Split the content into lines
lines = content.splitlines()
for line in lines:
# Check if there's a hyphen in the line
if '-' in line:
# Split the line at the hyphen
before_hyphen, after_hyphen = line.split('-', 1) # Only split at the first hyphen
# Print in specified colors
print(f"{BLUE}{before_hyphen.strip()}{RESET} - {GREEN}{after_hyphen.strip()}{RESET}")
else:
# Print lines without hyphen in default color (optional)
print(line)
# AI response
class SessionManager:
def __init__(self):
self.sessions = []
def save_session(self, user_input, ai_response):
self.sessions.append({"user_input": user_input, "ai_response": ai_response})
def get_session(self):
return self.sessions
def ai_response(input_text):
model = genai.GenerativeModel('gemini-pro')
response = model.generate_content(input_text)
return response.text
def is_code(text):
"""Check if the response contains code-like content."""
# Code detection patterns
code_indicators = ['<html>', 'def ', 'class ', 'import ', '</', 'function', 'let ', 'const ', 'public ', 'private ',
'```', '{', '}']
return any(indicator in text for indicator in code_indicators)
def split_response(response):
"""Split AI response into explanation and code parts."""
lines = response.splitlines()
explanation = []
code = []
in_code_block = False
for line in lines:
# Detect code block markers or code-like content
if '```' in line or is_code(line):
in_code_block = not in_code_block # Toggle code block
if in_code_block or is_code(line):
code.append(line)
else:
explanation.append(line)
return "\n".join(explanation), "\n".join(code)
def highlight_code(code, language="python"):
"""Highlight the code using Pygments for colorful display in the terminal."""
try:
lexer = get_lexer_by_name(language) # Get lexer by name like 'python', 'html', etc.
except:
lexer = PythonLexer() # Default to Python syntax highlighting if no lexer is found
formatter = TerminalFormatter()
return highlight(code, lexer, formatter)
def ai():
speak("Ok sir, Activated AI mode")
speak(f"Welcome to {NAME} AI. How can I help you?")
session_manager = SessionManager()
while True:
user_input = takecommand()
if user_input == "none":
continue # If no valid input, continue listening
if "deactivate" in user_input or "exit" in user_input:
speak("Deactivating AI mode.")
break
# Generate AI response
ai_reply = ai_response(user_input)
session_manager.save_session(user_input, ai_response)
# Split the response into explanation and code parts
explanation, code = split_response(ai_reply)
# Speak the explanation
if explanation:
speak(explanation)
# Print the code examples with colorful highlighting
if code:
print("Code/Examples")
print("-------------")
print(highlight_code(code, language="python")) # You can change language based on user input
# MAIN PROGRAM
def TaskExecution():
previous_plugged = None
previous_battery_percent = None
last_announcement_time = None
last_full_charge_announcement = None
last_15_percent_announcement = None
last_10_percent_announcement = None
last_5_percent_announcement = None
last_2_percent_announcement = None
while True:
current_minute = datetime.now().minute
current_hour = datetime.now().hour
# Check if it's a new minute
current_time = datetime.now().replace(second=0, microsecond=0)
if last_announcement_time is None or current_time != last_announcement_time:
last_announcement_time = current_time
if current_hour in [7, 12, 16, 21] and current_minute == 0:
speak_greeting_and_time()
elif current_minute == 0:
speak_time()
battery_percent, plugged = get_battery_status()
if battery_percent is not None and plugged is not None:
now = datetime.now()
if previous_plugged is not None and plugged != previous_plugged:
if plugged:
speak("Charger plugged in sir.")
else:
speak(f"Charger is removed, and you have {battery_percent} percent backup left.")
if battery_percent == 100 and plugged:
if last_full_charge_announcement is None or (now - last_full_charge_announcement) > timedelta(
minutes=1):
speak("Battery is full. Please remove the plug sir.")
last_full_charge_announcement = now
if battery_percent == 15 and not plugged:
if last_15_percent_announcement is None or (now - last_15_percent_announcement) > timedelta(minutes=1):
speak("Charge is 15, please charge the system sir.")
last_15_percent_announcement = now
if battery_percent == 10 and not plugged:
if last_10_percent_announcement is None or (now - last_10_percent_announcement) > timedelta(minutes=1):
speak("Charge is 10, please charge the system sir.")
last_10_percent_announcement = now
if battery_percent == 5 and not plugged:
if last_5_percent_announcement is None or (now - last_5_percent_announcement) > timedelta(minutes=1):
speak("Charge is 5, please charge the system sir.")
last_5_percent_announcement = now
if battery_percent <= 2 and not plugged:
if last_2_percent_announcement is None or (now - last_2_percent_announcement) > timedelta(seconds=30):
speak("Charge is very low sir, Quickly charge the system .")
last_2_percent_announcement = now
previous_plugged = plugged
previous_battery_percent = battery_percent
query = takecommand().lower()
# introduce ourself
if "tell me about yourself" in query or "introduce yourself" in query or "who are you" in query:
speak("Sure sir, I am Jarvis, an Advanced Voice Assistant. I am equipped with a variety of features to enhance your productivity and convenience. I can open and close any apps, search anything on Google and Wikipedia, check the temperature, facilitate message passing, transcribe spoken words into text, play games, utilize AI features for various tasks, perform keyboard shortcuts, control volume, provide the latest news updates, print documents, manage system functions such as shutdown, restart, and sleep, check internet speed, and much more. I can also translate languages to help you communicate effectively. Simply tell me what you need, and I'll do my best to assist you efficiently.")
# open any apps
elif ("open" in query) and ("settings" not in query and "task" not in query and "accessibility" not in query and "it" not in query and "run" not in query and "emoji" not in query and "clipboard" not in query and "mail" not in query and "notification" not in query and "tab" not in query and "facebook" not in query and "youtube" not in query and "window" not in query and "downloads" not in query):
pyautogui.press('win')
time.sleep(0.5)
text_to_type = query.split("open", 1)[-1].strip()
speak(f"ok sir, opening {text_to_type}")
type_text(text_to_type)
pyautogui.press('enter')
# closing any apps
elif ("close" in query) and ("settings" not in query and "task" not in query and "program" not in query and "it" not in query and "accessibility" not in query and "run" not in query and "emoji" not in query and "clipboard" not in query and "mail" not in query and "page" not in query and "notification" not in query and "tab" not in query and "facebook" not in query and "youtube" not in query and "window" not in query and "downloads" not in query):
text_to_type = query.split("close", 1)[-1].strip()
speak(f"ok sir, closing {text_to_type}")
pyautogui.hotkey('alt', 'f4')
speak("Do you have any other work sir....")
#show functions text file
elif "show usage file" in query or "how to use" in query or "show your functionalities" in query:
speak("Sure sir, showing my functionalities. Please read this...")
with open('Usage.txt', 'r') as file:
content = file.read()
print_colorful_usage(content)
speak("Do you have any other work sir....")
#time
elif "time" in query:
tt = time.strftime("%I:%M %p")
speak(f"now {tt} sir")
# camera functions
elif "take photo" in query or "take video" in query:
pyautogui.press('enter')
elif "change photo" in query or "switch to photo" in query:
pyautogui.press('down')
elif "change video" in query or "switch to video" in query:
pyautogui.press('up')
# Alarm set
elif "set alarm" in query:
clock()
speak("Do you have any other work sir....")
# settings open
elif "open settings" in query:
speak("Ok sir, Opening Settings")
pyautogui.hotkey('win', 'i')
# Task manager open
elif "open task manager" in query:
speak("ok sir, Opening task manager")
pyautogui.hotkey('ctrl', 'shift', 'esc')
# open run prompt
elif "open run prompt" in query or "open run bar" in query or "open run dialog box" in query:
pyautogui.hotkey('win', 'r')
# open accessibility
elif "open accessibility" in query:
speak("Ok sir, Opening Accessibility")
pyautogui.hotkey('win', 'u')
# lock the system
elif "lock the system" in query:
speak("Ok sir, Lock your system")
pyautogui.hotkey('win', 'l')
# open clipboard bar
elif "open clipboard" in query:
speak("Ok sir, Opening Clipboard")
pyautogui.hotkey('win', 'v')
# open emoji panel
elif "open emoji panel" in query:
speak("Ok sir, Opening emoji panel")
pyautogui.hotkey('win', '.')
# open mail
elif "open mail" in query or "check mail" in query or "open email" in query or "check email" in query:
speak("Please wait sir. I will display your inbox")
webbrowser.open("https://mail.google.com/mail/u/0/#inbox")
# Minimize and maximize control
elif "minimise all windows" in query or "minimise all window" in query:
speak("Ok sir, Minimizing all windows")
pyautogui.hotkey('win', 'm')
elif "maximize all windows" in query or "maximise all window" in query:
speak("Ok sir, Maximizing all windows")
pyautogui.hotkey('win', 'shift', 'm')
elif "minimise the window" in query or "minimise the page" in query:
speak("Ok sir, Minimizing the window")
pyautogui.hotkey('win', 'down')
elif "maximize the window" in query or "maximise the page" in query:
speak("Ok sir, Maximizing the window")
pyautogui.hotkey('win', 'up')
# Taskbar open and close function
elif "open first task" in query or "open 1st task" in query:
pyautogui.hotkey('win', '1')
elif "close first task" in query or "close 1st task" in query:
pyautogui.hotkey('win', '1')
pyautogui.hotkey('alt', 'f4')
elif "open second task" in query or "open 2nd task" in query:
pyautogui.hotkey('win', '2')
elif "close second task" in query or "close 2nd task" in query:
pyautogui.hotkey('win', '2')
pyautogui.hotkey('alt', 'f4')
elif "open third task" in query or "open 3rd task" in query:
pyautogui.hotkey('win', '3')
elif "close third task" in query or "close 3rd task" in query:
pyautogui.hotkey('win', '3')
pyautogui.hotkey('alt', 'f4')
elif "open fourth task" in query or "open 4th task" in query:
pyautogui.hotkey('win', '4')
elif "close fourth task" in query or "close 4th task" in query:
pyautogui.hotkey('win', '4')
pyautogui.hotkey('alt', 'f4')
elif "open fifth task" in query or "open 5th task" in query:
pyautogui.hotkey('win', '5')
elif "close fifth task" in query or "close 5th task" in query:
pyautogui.hotkey('win', '5')
pyautogui.hotkey('alt', 'f4')
elif "open sixth task" in query or "open 6th task" in query:
pyautogui.hotkey('win', '6')
elif "close sixth task" in query or "close 6th task" in query:
pyautogui.hotkey('win', '6')
pyautogui.hotkey('alt', 'f4')
elif "open seventh task" in query or "open 7th task" in query:
pyautogui.hotkey('win', '7')
elif "close seventh task" in query or "close 7th task" in query:
pyautogui.hotkey('win', '7')
pyautogui.hotkey('alt', 'f4')
elif "open eighth task" in query or "open 8th task" in query:
pyautogui.hotkey('win', '8')
elif "close eighth task" in query or "close 8th task" in query:
pyautogui.hotkey('win', '8')
pyautogui.hotkey('alt', 'f4')
elif "open ninth task" in query or "open 9th task" in query:
pyautogui.hotkey('win', '9')
elif "close ninth task" in query or "close 9th task" in query:
pyautogui.hotkey('win', '9')
pyautogui.hotkey('alt', 'f4')
elif "open tenth task" in query or "open 10th task" in query:
pyautogui.hotkey('win', '0')
elif "close tenth task" in query or "close 10th task" in query:
pyautogui.hotkey('win', '0')
pyautogui.hotkey('alt', 'f4')
# print screen
elif "print the page" in query or "print" in query:
speak("ok sir, printed the screen")
pyautogui.hotkey('ctrl', 'p')
pyautogui.press('enter')
speak("Do you have any other work sir....")
# notification open
elif "open notification bar" in query or "display notification" in query or "notification" in query or "show notification" in query:
speak("Ok sir, showing notifications")
pyautogui.hotkey('win', 'n')
speak("Do you have any other work sir....")
# Don't disturb mode
elif "don't disturb" in query:
pyautogui.hotkey('win', 'n')
time.sleep(0.5)
pyautogui.press('enter')
pyautogui.press('esc')
speak("Do you have any other work sir....")
# open new tab and close
elif "open new tab" in query or "create new tab" in query:
speak("Ok sir, Opening New Tab")
pyautogui.hotkey('ctrl', 't')
elif "no no close them" in query or "no no close it" in query or "close it" in query:
speak("ok sir, closing it")
pyautogui.hotkey('alt', 'f4')
speak("Do you have any other work sir....")
elif "close this tab" in query or "delete this tab" in query:
speak("Ok sir, closing this tab")
pyautogui.hotkey("ctrl", "w")
# Closing the page
elif "close the page" in query or "closing the page" in query or "close this page" in query or "closing this page" in query:
speak("Okay sir, Closing this page")
pyautogui.hotkey("ctrl", "w")
speak("Do you have any other work sir....")
#Delete
elif "delete it" in query:
speak("ok sir, Deleting it")
pyautogui.press('del')
#show downloads
elif "downloads" in query:
speak("ok sir, showing downloads page")
pyautogui.hotkey('ctrl', 'j')
#find something
elif "find" in query:
pyautogui.hotkey('ctrl', 'f')
time.sleep(0.5)
text_to_type = query.split("find", 1)[-1].strip()
speak("showing our result sir")
type_text(text_to_type)
# open new window
elif "open new window" in query or "create new window" in query:
speak("Ok sir, Opening New window")
pyautogui.hotkey('ctrl', 'n')
# Move tab next and previous
elif "move next tab" in query or "move the next tab" in query or "move to next tab" in query:
speak("Ok sir, Moving Next Tab")
pyautogui.hotkey('ctrl', 'tab')
elif "move previous tab" in query or "move the previous tab" in query or "move to previous tab" in query:
speak("Ok sir, Moving Previous Tab")
pyautogui.hotkey('ctrl', 'shift', 'tab')
# Select all,cut,copy,paste,save as
elif "select all text" in query or "select all" in query:
speak("Ok sir, selecting all text")
pyautogui.hotkey('ctrl', 'a')
elif "cut the text" in query or "cut" in query or "cut their" in query or "cut them" in query or "cut it" in query:
speak("Ok sir, cut the text")
pyautogui.hotkey('ctrl', 'x')
elif "copy the text" in query or "copy" in query or "copy their" in query or "copy them" in query or "copy it" in query:
speak("Ok sir, copied the text")
pyautogui.hotkey('ctrl', 'c')
elif "paste the text" in query or "paste" in query or "paste their" in query or "paste them" in query or "paste it" in query:
speak("Ok sir, pasting the text")
pyautogui.hotkey('ctrl', 'v')
elif "save as the file" in query or "save as" in query or "save as their" in query or "save as them" in query or "save as it" in query:
speak("Ok sir, saving the file")
pyautogui.hotkey('ctrl', 's')
speak("Do you have any other work sir....")
# youtube opening and closing
elif "open youtube" in query:
speak("Ok sir, Opening YouTube")
webbrowser.open("www.youtube.com")
speak("Do you have any other work sir....")
elif "close youtube" in query:
speak("Ok sir, Closing You Tube")
pyautogui.hotkey('alt', 'f4')
speak("Do you have any other work sir....")
elif "minimise the video" in query:
pyautogui.press('i')
elif "maximize the video" in query:
pyautogui.press('i')
elif "full screen" in query:
pyautogui.press('f')
elif "exit full screen" in query:
pyautogui.press('esc')
elif "caption" in query or "subtitle" in query:
pyautogui.press('c')
# facebook open and closing
elif "open facebook" in query:
speak("Okay sir, Opening Facebook")
webbrowser.open("www.facebook.com")
elif "close facebook" in query:
speak("Okay sir, Closing Facebook")
pyautogui.hotkey('alt', 'f4')
speak("Do you have any other work sir....")
# song on youtube
elif "song on youtube" in query:
speak("Please say the song name you want to play sir!")
song_name = takecommand()
# song_name = input("Please enter the name of the song you want to play: ")