|
| 1 | +import pyttsx3 #Python Text To Speech module to read inputs from the users. |
| 2 | +import speech_recognition as sr |
| 3 | +import pyaudio |
| 4 | +import datetime |
| 5 | +import wikipedia |
| 6 | +import webbrowser |
| 7 | +import os |
| 8 | + |
| 9 | +engine = pyttsx3.init('sapi5') |
| 10 | +voices = engine.getProperty('voices') |
| 11 | +engine.setProperty('voice', voices[0].id) |
| 12 | + |
| 13 | + |
| 14 | +def speak(audio): |
| 15 | + engine.say(audio) |
| 16 | + engine.runAndWait() |
| 17 | + |
| 18 | + |
| 19 | +def wishMe(): |
| 20 | + hour = int(datetime.datetime.now().hour) |
| 21 | + if hour >= 0 and hour < 12: |
| 22 | + speak("Good Morning sir!") |
| 23 | + |
| 24 | + elif hour >= 12 and hour < 16: |
| 25 | + speak("Good Afternoon sir!") |
| 26 | + |
| 27 | + else: |
| 28 | + speak("Good Evening sir!") |
| 29 | + |
| 30 | + speak('I am Morris. Please tell me. How may I help you?') |
| 31 | + |
| 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 | + # listen for 5 seconds and calculate the ambient noise energy level |
| 40 | + r.adjust_for_ambient_noise(source, duration=5) |
| 41 | + r.energy_threshold = 4000 |
| 42 | + r.dynamic_energy_threshold = True |
| 43 | + r.pause_threshold = 1 |
| 44 | + audio = r.listen(source) |
| 45 | + |
| 46 | + try: |
| 47 | + print("Recognizing...") |
| 48 | + query = r.recognize_google(audio, language="en-in") |
| 49 | + print(f"User said: {query}\n") |
| 50 | + |
| 51 | + except Exception as e: |
| 52 | + print(e) |
| 53 | + print("Say that again please...") |
| 54 | + return "None" |
| 55 | + return query |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == "__main__": |
| 59 | + wishMe() |
| 60 | + |
| 61 | + while True: |
| 62 | + # if 1: |
| 63 | + query = takeCommand().lower() |
| 64 | + |
| 65 | + # Logic for executing tasks based on query |
| 66 | + if 'wikipedia' in query: |
| 67 | + speak('Searching Wikipedia...') |
| 68 | + query = query.replace('wikipedia', '') |
| 69 | + results = wikipedia.summary(query, sentences=2) |
| 70 | + speak('According to Wikipedia') |
| 71 | + # print(results) |
| 72 | + speak(results) |
| 73 | + |
| 74 | + elif 'open youtube' in query: |
| 75 | + webbrowser.open('youtube.com') |
| 76 | + |
| 77 | + elif 'open Google' in query: |
| 78 | + webbrowser.open('google.com') |
| 79 | + |
| 80 | + elif 'open stackoverflow' in query: |
| 81 | + webbrowser.open('stackoverflow.com') |
| 82 | + |
| 83 | + elif 'play music' in query: |
| 84 | + music_dir = 'C:\\Users\\pro tricks\\Music\\Trap' |
| 85 | + songs = os.listdir(music_dir) |
| 86 | + print(songs) |
| 87 | + os.startfile(os.path.join(music_dir, songs[0])) |
| 88 | + |
| 89 | + elif 'time' in query: |
| 90 | + strTime = datetime.datetime.now().strftime("%H:%M:%S") |
| 91 | + speak(f"Sir, the time is {strTime}") |
0 commit comments