|
| 1 | +import speech_recognition as sr # recognise speech |
| 2 | +import webbrowser # open browser |
| 3 | +import time |
| 4 | +import playsound # to play an audio file |
| 5 | +import random |
| 6 | +import os |
| 7 | +from gtts import gTTS # google text to speech |
| 8 | +from time import ctime |
| 9 | + |
| 10 | + |
| 11 | +def there_exists(terms): |
| 12 | + for term in terms: |
| 13 | + if term in voice_data: |
| 14 | + return True |
| 15 | + |
| 16 | +r = sr.Recognizer() # initialise a recogniser |
| 17 | +# listen for audio and convert it to text: |
| 18 | +def record_audio(ask=False): |
| 19 | + |
| 20 | + with sr.Microphone() as source: # microphone as source |
| 21 | + r.energy_threshold=500 #voice level number increse more sensitive |
| 22 | + r.adjust_for_ambient_noise(source,1.2)# noise cancel rate |
| 23 | + r.pause_threshold= 1 |
| 24 | + if ask: |
| 25 | + speak(ask) |
| 26 | + audio = r.listen(source) # listen for the audio via source |
| 27 | + voice_data = '' |
| 28 | + |
| 29 | + try : |
| 30 | + voice_data = r.recognize_google(audio) # convert audio to text |
| 31 | + |
| 32 | + except sr.RequestError: |
| 33 | + speak('Sorry, the service is down') # error: recognizer is not connected |
| 34 | + except sr.UnknownValueError: # error: recognizer does not understand |
| 35 | + print('Recognizing..') |
| 36 | + print(f">> {voice_data.lower()}") # print what user said |
| 37 | + return voice_data.lower() |
| 38 | + |
| 39 | +# get string and make a audio file to be played |
| 40 | +def speak(audio_string): |
| 41 | + tts = gTTS(text=audio_string, lang='en-in') # text to speech(voice) |
| 42 | + r = random.randint(1,20000000) |
| 43 | + audio_file = 'audio-' + str(r) + '.mp3' |
| 44 | + tts.save(audio_file) # save as mp3 |
| 45 | + playsound.playsound(audio_file) # play the audio file |
| 46 | + print(audio_string) # print what app said |
| 47 | + os.remove(audio_file) # remove audio file |
| 48 | + |
| 49 | +def respond(voice_data): |
| 50 | + |
| 51 | + # 1: greeting |
| 52 | + if there_exists(["hey","hi","hello","wake up","hai"]): |
| 53 | + greetings = ["hey", "hey, what's up? ", " how can I help you","I'm listening","hello"] |
| 54 | + greet = greetings[random.randint(0,len(greetings)-1)] |
| 55 | + speak(greet) |
| 56 | + |
| 57 | + # 2: name |
| 58 | + if there_exists(["your name","what i call you","what is your good name"]): |
| 59 | + name= record_audio("my name is Vavo stand for virtual assistance version One. what's your name?") |
| 60 | + speak('Nice to meet you '+ name ) |
| 61 | + speak('how can i help you ' + name) |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + # 3: Origin |
| 66 | + |
| 67 | + if there_exists(["who are you","your inventor","invented you","created you","who is your developer"]): |
| 68 | + greetings = ["I am Virtual Voice Assistant","I am developed by mr.abhijeet as a voice assistance"] # You can Add your name |
| 69 | + greet = greetings[random.randint(0,len(greetings)-1)] |
| 70 | + speak(greet) |
| 71 | + |
| 72 | + if there_exists(["what is your age","how old are you","when is your birthday"]): |
| 73 | + greetings = ["I came into this world in march 2021"] |
| 74 | + greet = greetings[random.randint(0,len(greetings)-1)] |
| 75 | + speak(greet) |
| 76 | + |
| 77 | + |
| 78 | + # 3: Take care's |
| 79 | + if there_exists(["how's everything" ,"how ia everything","how are you","how are you doing","what's up","whatsup"]): |
| 80 | + greetings = ["I am well ...thanks for asking ","i am well" ,"Doing Great" ] |
| 81 | + greet = greetings[random.randint(0,len(greetings)-1)] |
| 82 | + speak(greet) |
| 83 | + |
| 84 | + |
| 85 | + # 3: greeting |
| 86 | + if there_exists(["What are you doing" ,"what you doing","doing"]): |
| 87 | + greetings = ["nothing", "nothing...,just working for you","Nothing much"] |
| 88 | + greet = greetings[random.randint(0,len(greetings)-1)] |
| 89 | + speak(greet) |
| 90 | + |
| 91 | + |
| 92 | + # 4.1: time |
| 93 | + if there_exists(["what's the time","tell me the time","what time is it","what is the time","time is going on"]): |
| 94 | + time = ctime().split(" ")[3].split(":")[0:2] |
| 95 | + if time[0] == "00": |
| 96 | + hours = '12' |
| 97 | + else: |
| 98 | + hours = time[0] |
| 99 | + minutes = time[1] |
| 100 | + time = f'{hours} {minutes}' |
| 101 | + speak(time) |
| 102 | + |
| 103 | + # 5: search wekiapedia |
| 104 | + if there_exists(["wikipedia"]): |
| 105 | + search = record_audio('What do you want to search for?') |
| 106 | + url = 'https://en.wikipedia.org/wiki/'+ search |
| 107 | + webbrowser.get().open(url) |
| 108 | + speak('Here is what I found for' + search) |
| 109 | + |
| 110 | + # 5: search |
| 111 | + if there_exists(["do google","search google","on google","search for","in google"]): |
| 112 | + search = record_audio('What do you want to search for?') |
| 113 | + url = 'https://google.com/search?q='+ search |
| 114 | + webbrowser.get().open(url) |
| 115 | + speak('Here is what I found for' + search) |
| 116 | + |
| 117 | + # 5.6: opening youtube |
| 118 | + if there_exists(["open the youtube","open youtube"]): |
| 119 | + url = 'https://www.youtube.com/' |
| 120 | + webbrowser.get().open(url) |
| 121 | + speak('Opening') |
| 122 | + |
| 123 | + # 5.7: opening google |
| 124 | + if there_exists(["open the google","open google"]): |
| 125 | + url = 'https://www.google.com/' |
| 126 | + webbrowser.get().open(url) |
| 127 | + speak('Opening') |
| 128 | + # 5.7: opening gemail |
| 129 | + if there_exists(["open gmail","open email","open my email","check email"]): |
| 130 | + url = 'https://mail.google.com/' |
| 131 | + webbrowser.get().open(url) |
| 132 | + speak('Opening') |
| 133 | + |
| 134 | + # 5.5: find location |
| 135 | + if there_exists(["location"]): |
| 136 | + location = record_audio('What is the locatio n?') |
| 137 | + url = 'https://google.nl/maps/place/' + location + '/&' |
| 138 | + webbrowser.get().open(url) |
| 139 | + speak('Opening map of' + location ) |
| 140 | + |
| 141 | + |
| 142 | + # 6: search youtube |
| 143 | + if there_exists(["search youtube","search the youtube","search in youtube","in youtube","on youtube"]): |
| 144 | + search = record_audio('What do you want to search for?') |
| 145 | + r.pause_threshold=2 |
| 146 | + url = 'https://www.youtube.com/results?search_query='+search |
| 147 | + webbrowser.get().open(url) |
| 148 | + speak('Here is what I found') |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + #OS shutdown |
| 153 | + if there_exists(["shutdown system","system off","shutdown the system","system shutdown"]): |
| 154 | + speak('Okay system will off in 30 seconds') |
| 155 | + os.system("shutdown /s /t 30") |
| 156 | + |
| 157 | + |
| 158 | + if there_exists(["good","thank you","thanks","well done"]): |
| 159 | + greetings = ["my pleasure","Don't mention","Thanks for your compliment","No problem.","Thank you, it makes my day to hear that."] |
| 160 | + greet = greetings[random.randint(0,len(greetings)-1)] |
| 161 | + speak(greet) |
| 162 | + |
| 163 | + |
| 164 | + if there_exists(["exit", "quit","sleep","shut up","close"]): |
| 165 | + greetings = ["Going offline ! you can call me Anytime","Okay ,you can call me Anytime","See you later","See you soon","Have a good day."] |
| 166 | + greet = greetings[random.randint(0,len(greetings)-1)] |
| 167 | + speak(greet) |
| 168 | + exit() |
| 169 | + |
| 170 | +time.sleep(1) |
| 171 | +while(1): |
| 172 | + voice_data = record_audio() # get the voice input |
| 173 | + respond(voice_data) # respond |
| 174 | + |
| 175 | + |
| 176 | + |
0 commit comments