|
| 1 | +import pyttsx3 |
| 2 | +import speech_recognition as sr |
| 3 | +import datetime |
| 4 | +import wikipedia |
| 5 | +import webbrowser |
| 6 | +import os |
| 7 | +import smtplib |
| 8 | + |
| 9 | +engine = pyttsx3.init('sapi5') |
| 10 | +voices = engine.getProperty('voices') |
| 11 | +# print(voices[1].id) |
| 12 | +engine.setProperty('voice', voices[0].id) |
| 13 | + |
| 14 | + |
| 15 | +def speak(audio): |
| 16 | + engine.say(audio) |
| 17 | + engine.runAndWait() |
| 18 | + |
| 19 | + |
| 20 | +def wishMe(): |
| 21 | + hour = int(datetime.datetime.now().hour) |
| 22 | + if hour>=0 and hour<12: |
| 23 | + speak("Good Morning!") |
| 24 | + |
| 25 | + elif hour>=12 and hour<18: |
| 26 | + speak("Good Afternoon!") |
| 27 | + |
| 28 | + else: |
| 29 | + speak("Good Evening!") |
| 30 | + |
| 31 | + speak("I am Jarvis Sir. Please tell me how may I help you") |
| 32 | + |
| 33 | +def takeCommand(): |
| 34 | + #It takes microphone input from the user and returns string output |
| 35 | + |
| 36 | + r = sr.Recognizer() |
| 37 | + with sr.Microphone() as source: |
| 38 | + print("Listening...") |
| 39 | + r.pause_threshold = 1 |
| 40 | + audio = r.listen(source) |
| 41 | + |
| 42 | + try: |
| 43 | + print("Recognizing...") |
| 44 | + query = r.recognize_google(audio, language='en-in') |
| 45 | + print(f"User said: {query}\n") |
| 46 | + |
| 47 | + except Exception as e: |
| 48 | + # print(e) |
| 49 | + print("Say that again please...") |
| 50 | + return "None" |
| 51 | + return query |
| 52 | + |
| 53 | +def sendEmail(to, content): |
| 54 | + server = smtplib.SMTP('smtp.gmail.com', 587) |
| 55 | + server.ehlo() |
| 56 | + server.starttls() |
| 57 | + server. login( '[email protected]', 'your-password') |
| 58 | + server. sendmail( '[email protected]', to, content) |
| 59 | + server.close() |
| 60 | + |
| 61 | +if _name_ == "_main_": |
| 62 | + wishMe() |
| 63 | + while True: |
| 64 | + # if 1: |
| 65 | + query = takeCommand().lower() |
| 66 | + |
| 67 | + # Logic for executing tasks based on query |
| 68 | + if 'wikipedia' in query: |
| 69 | + speak('Searching Wikipedia...') |
| 70 | + query = query.replace("wikipedia", "") |
| 71 | + results = wikipedia.summary(query, sentences=2) |
| 72 | + speak("According to Wikipedia") |
| 73 | + print(results) |
| 74 | + speak(results) |
| 75 | + |
| 76 | + elif 'open youtube' in query: |
| 77 | + webbrowser.open("youtube.com") |
| 78 | + |
| 79 | + elif 'open google' in query: |
| 80 | + webbrowser.open("google.com") |
| 81 | + |
| 82 | + elif 'open stackoverflow' in query: |
| 83 | + webbrowser.open("stackoverflow.com") |
| 84 | + |
| 85 | + |
| 86 | + elif 'play music' in query: |
| 87 | + music_dir = 'D:\\Non Critical\\songs\\Favorite Songs2' |
| 88 | + songs = os.listdir(music_dir) |
| 89 | + print(songs) |
| 90 | + os.startfile(os.path.join(music_dir, songs[0])) |
| 91 | + |
| 92 | + elif 'the time' in query: |
| 93 | + strTime = datetime.datetime.now().strftime("%H:%M:%S") |
| 94 | + speak(f"Sir, the time is {strTime}") |
| 95 | + |
| 96 | + elif 'open code' in query: |
| 97 | + codePath = "C:\\Users\\Haris\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe" |
| 98 | + os.startfile(codePath) |
| 99 | + |
| 100 | + elif 'email to harry' in query: |
| 101 | + try: |
| 102 | + speak("What should I say?") |
| 103 | + content = takeCommand() |
| 104 | + |
| 105 | + sendEmail(to, content) |
| 106 | + speak("Email has been sent!") |
| 107 | + except Exception as e: |
| 108 | + print(e) |
| 109 | + speak("Sorry my friend harry bhai. I am not able to send this email") |
0 commit comments