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