├── AI-assistant.py ├── README.md └── commands.txt /AI-assistant.py: -------------------------------------------------------------------------------- 1 | import pyttsx 2 | import speech_recognition as sr 3 | from urllib2 import Request, urlopen, URLError 4 | import json 5 | from datetime import datetime 6 | from random import randint 7 | from ctypes import cast, POINTER 8 | from comtypes import CLSCTX_ALL 9 | from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume 10 | from google import google 11 | import sounddevice as sd 12 | import time 13 | from collections import defaultdict 14 | import pygame 15 | import os 16 | import unicodedata 17 | 18 | Looper = True 19 | NextLine = False 20 | hour = 100 21 | minute = 100 22 | Linenumber = 0 23 | line_num = 0 24 | ScoreIndexes = True 25 | Loopme = True 26 | ScoreDesision = [] 27 | HighestScore = 0 28 | HighestScoreLine = "" 29 | HighestScoreCoreLineLength = "" 30 | CommonWords = [" is"," the", " and", " i", " at", " when", " who", " what", " you", " your"] 31 | TotScore = 0 32 | SuffScore = 0 33 | CoreScore = 0 34 | LineLength = 0 35 | CoreLineLength = 0 36 | OddEven = 1 37 | HighestScore = 0 38 | HighestScoreLine = "" 39 | HighestScoreCoreLineLength = "" 40 | 41 | #Interface = str(raw_input("Interface: ")) 42 | Interface = "Keyboard" 43 | 44 | devices = AudioUtilities.GetSpeakers() 45 | interface = devices.Activate( 46 | IAudioEndpointVolume._iid_, CLSCTX_ALL, None) 47 | volume = cast(interface, POINTER(IAudioEndpointVolume)) 48 | speech_engine = pyttsx.init('sapi5') # see http://pyttsx.readthedocs.org/en/latest/engine.html#pyttsx.init 49 | speech_engine.setProperty('rate', 150) 50 | 51 | def speak(text): 52 | speech_engine.say(text) 53 | speech_engine.runAndWait() 54 | 55 | # get audio from the microphone 56 | r = sr.Recognizer() 57 | 58 | def Listen(r): 59 | with sr.Microphone() as source: 60 | audio = r.listen(source) 61 | try: 62 | return r.recognize_google(audio) 63 | except sr.UnknownValueError: 64 | Listen(r) 65 | 66 | def Getweather(): 67 | request = Request('http://api.wunderground.com/api/60eb52589081d5c1/conditions/q/BE/ninove.json') 68 | 69 | try: 70 | response = urlopen(request) 71 | kittens = response.read() 72 | resp_dict = json.loads(kittens) 73 | return resp_dict['current_observation']['temp_c'] 74 | except URLError, e: 75 | print 'No kittez. Got an error code:', e 76 | 77 | def Gettime(): 78 | Hours = datetime.now().strftime('%H') 79 | Hours = int(Hours) % 12 80 | Minutes = datetime.now().strftime('%M') 81 | Seconds = datetime.now().strftime('%S') 82 | 83 | return "It is " + str(Minutes) + " over " + str(Hours) + " and " + str(Seconds) + " seconds" 84 | 85 | def ChangeVolume(volume, Volume): 86 | if Volume == "100": 87 | volume.SetMasterVolumeLevel(-0.0, None) 88 | elif Volume == "75": 89 | volume.SetMasterVolumeLevel(-5.0, None) 90 | elif Volume == "50": 91 | volume.SetMasterVolumeLevel(-10.0, None) 92 | elif Volume == "25": 93 | volume.SetMasterVolumeLevel(-25.0, None) 94 | elif Volume == "0": 95 | volume.SetMasterVolumeLevel(-60.0, None) 96 | 97 | def Googlesomething(Query): 98 | search_result = google.search(Query, 1) 99 | result = search_result[0].name 100 | unicodedata.normalize('NFKD', result).encode('ascii','ignore') 101 | print("+ the answer to the question " + str(Query) + " is " + str(result)) 102 | speak("the answer to the question " + str(Query) + " is " + str(result)) 103 | 104 | def Calculate(Query): 105 | return eval(Query) 106 | 107 | def RecordAudio(Length): 108 | Length = Command[13:16] 109 | fs = 44100 110 | print("+ Recording") 111 | speak("Recording") 112 | myrecording = sd.rec(int(int(Length) * fs), samplerate=fs, channels=2) 113 | time.sleep(int(Length)) 114 | print("+ This is what i have recorded") 115 | speak("This is what i have recorded") 116 | sd.play(myrecording) 117 | print("*recording playing*") 118 | time.sleep(int(Length)) 119 | 120 | def PlayMusic(): 121 | root, dirs, files = next(os.walk('tracks/')) 122 | 123 | pygame.mixer.init() 124 | 125 | for name in files: 126 | name = 'tracks/'+str(name) 127 | pygame.mixer.music.load(name) 128 | pygame.mixer.music.play(0) 129 | while pygame.mixer.music.get_busy(): 130 | if Interface == "Voice": 131 | print "Listening" 132 | Command = Listen(r) 133 | print "-" + str(Command) 134 | KnownCommands(Command, r, NextLine, ScoreIndexes) 135 | else: 136 | Command = str(raw_input("Command: ")) 137 | print "-" + str(Command) 138 | KnownCommands(Command, r, NextLine, ScoreIndexes) 139 | pygame.time.Clock().tick(5) 140 | 141 | 142 | def StopMusic(): 143 | pygame.mixer.music.stop() 144 | 145 | def PauseMusic(): 146 | pygame.mixer.music.pause() 147 | 148 | def ResumeMusic(Loopme): 149 | pygame.mixer.music.play 150 | while pygame.mixer.music.get_busy(): 151 | if Interface == "Voice": 152 | print "Listening" 153 | Command = Listen(r) 154 | print "-" + str(Command) 155 | KnownCommands(Command, r, NextLine, ScoreIndexes) 156 | else: 157 | Command = str(raw_input("Command: ")) 158 | print "-" + str(Command) 159 | KnownCommands(Command, r, NextLine, ScoreIndexes) 160 | pygame.time.Clock().tick(5) 161 | 162 | def CheckAlarm(): 163 | if hour == datetime.now().strftime('%H') and minute == datetime.now().strftime('%M'): 164 | ChangeVolume(volume, "100") 165 | for j in range(0,11): 166 | speak("alarm") 167 | 168 | def NewCommand(Type): 169 | if Type == "Asked": 170 | print("+ Sure what will be the command and what the answer") 171 | speak("Sure what will be the command and what the answer") 172 | 173 | if Interface == "Voice": 174 | CommandAnswer = Listen(r) 175 | print "-" + str(CommandAnswer) 176 | else: 177 | CommandAnswer = str(raw_input("{command} with the answer {answer}: ")) 178 | print "-" + str(CommandAnswer) 179 | 180 | while ' with the answer ' not in CommandAnswer: 181 | print("+ please start the answer to the command with 'with the answer'") 182 | speak("please start the answer to the command with quote with the answer") 183 | if Interface == "Voice": 184 | CommandAnswer = Listen(r) 185 | print "-" + str(CommandAnswer) 186 | else: 187 | CommandAnswer = str(raw_input("{command} with the answer {answer}: ")) 188 | print "-" + str(CommandAnswer) 189 | 190 | Command = CommandAnswer.split(" with the answer ")[0] 191 | Answer = CommandAnswer.split(" with the answer ")[1] 192 | else: 193 | Command = Type 194 | 195 | print("+ Is the answer to the Command " + str(Command) + " " + str(Answer)) 196 | speak("Is the answer to the Command " + str(Command) + " " + str(Answer)) 197 | 198 | if Interface == "Voice": 199 | Comfirmation = Listen(r) 200 | print "-" + str(Comfirmation) 201 | else: 202 | Comfirmation = str(raw_input("Comfirmation, yes or no: ")) 203 | print "-" + str(Comfirmation) 204 | 205 | if 'yes' in Comfirmation: 206 | with open("commands.txt", "a") as myfile: 207 | myfile.write(Command + "\n") 208 | myfile.write(Answer + "\n") 209 | 210 | print("New command added " + str(Command) + " With the answer " + str(Answer)) 211 | speak("New command added " + str(Command) + " With the answer " + str(Answer)) 212 | Looper = True 213 | 214 | elif 'no answer' in Comfirmation: 215 | print("Answer has been skipped") 216 | speak("Answer has been skipped") 217 | Looper = True 218 | else: 219 | while Looper == True: 220 | print("Please try again") 221 | speak("Please try again") 222 | 223 | if Interface == "Voice": 224 | Answer = Listen(r) 225 | print "-" + str(Answer) 226 | else: 227 | Answer = str(raw_input("Please type the anser to the command: ")) 228 | print "-" + str(Answer) 229 | 230 | if "no answer" in Answer: 231 | print("Answer has been skipped") 232 | speak("Answer has been skipped") 233 | Looper = False 234 | else: 235 | print("+ Is the answer to the Command " + str(Command) + " " + str(Answer)) 236 | speak("Is the answer to the Command " + str(Command) + " " + str(Answer)) 237 | if Interface == "Voice": 238 | Comfirmation = Listen(r) 239 | print "-" + str(Comfirmation) 240 | else: 241 | Comfirmation = str(raw_input("Comfirmation, yes or no: ")) 242 | print "-" + str(Comfirmation) 243 | 244 | if Comfirmation == "yes": 245 | with open("commands.txt", "a") as myfile: 246 | myfile.write(Command + "\n") 247 | myfile.write(Answer + "\n") 248 | 249 | print("New command added " + str(Command) + " With the answer " + str(Answer)) 250 | speak("New command added " + str(Command) + " With the answer " + str(Answer)) 251 | Looper = False 252 | elif "no answer" in Answer: 253 | print("Answer has been skipped") 254 | speak("Answer has been skipped") 255 | Looper = False 256 | 257 | def LineToAnswer(Command, line_num, Linenumber): 258 | Command= Command[7:] 259 | CommandFile = open("commands.txt", "r") 260 | for line in CommandFile.readlines(): 261 | line_num += 1 262 | if line.find(Command) >= 0: 263 | Linenumber = line_num 264 | 265 | CommandFile = open("commands.txt", "r") 266 | for i, line in enumerate(CommandFile): 267 | if i == Linenumber: 268 | Answer = line 269 | speak(line) 270 | print("+ " + str(line)) 271 | break 272 | 273 | return Answer 274 | 275 | def AnswerCommand(HighestScore,line_num,r,Command, Linenumber, ScoreDesision): 276 | for i in ScoreDesision: 277 | line = i[0] 278 | CoreLineLength = i[1] 279 | CoreScore = i[2] 280 | 281 | LineLength = len(line.split(" ")) 282 | 283 | if CoreScore > HighestScore: 284 | HighestScore = CoreScore 285 | HighestScoreLine = line 286 | HighestScoreLineLength = LineLength 287 | HighestScoreCoreLineLength = CoreLineLength 288 | 289 | #print HighestScoreCoreLineLength 290 | #print HighestScoreCoreLineLength/1.62 291 | #print HighestScore 292 | #print HighestScoreCoreLineLength/1.62 > HighestScore 293 | 294 | if HighestScoreCoreLineLength/1.62 > HighestScore or HighestScoreLineLength/2 > HighestScore: 295 | print "+ I think you said " + str(HighestScoreLine) 296 | Answer = LineToAnswer(HighestScoreLine, line_num, Linenumber) 297 | print("+ is this answer correct") 298 | speak("is this answer correct") 299 | if Interface == "Voice": 300 | Comfirmation = Listen(r) 301 | print "-" + str(Comfirmation) 302 | else: 303 | Comfirmation = str(raw_input("Comfirmation, yes or no: ")) 304 | print "-" + str(Comfirmation) 305 | 306 | if 'yes' in Comfirmation: 307 | with open("commands.txt", "a") as myfile: 308 | myfile.write(Command + "\n") 309 | myfile.write(Answer + "\n") 310 | 311 | print("New command added " + str(Command) + " With the answer " + str(Answer)) 312 | speak("New command added " + str(Command) + " With the answer " + str(Answer)) 313 | else: 314 | print("+ I cannot always be correct") 315 | speak("I cannot always be correct") 316 | else: 317 | print "+ I think you said " + str(HighestScoreLine) 318 | LineToAnswer(HighestScoreLine, line_num, Linenumber) 319 | 320 | def TryToRecognise(ScoreDesision,HighestScore,NextLine, ScoreIndexes, line_num, r, Command, OddEven, CommonWords,CoreCommand, CoreScore, Linenumber): 321 | ScoreDesision = [] 322 | CommandFile = open("commands.txt", "r") 323 | for line in CommandFile: 324 | line = line.rstrip() 325 | if NextLine == True: 326 | print("+ " + str(line)) 327 | speak(line) 328 | NextLine = False 329 | ScoreIndexes = False 330 | break 331 | if line == Command: 332 | NextLine = True 333 | 334 | if ScoreIndexes == True: 335 | CommandFile = open("commands.txt", "r") 336 | for line in CommandFile: 337 | if OddEven == 1: 338 | line = line.rstrip() 339 | line = line.lower() 340 | CoreLine = line 341 | 342 | for CommonWord in CommonWords: 343 | CoreCommand = CoreCommand.split(CommonWord) 344 | CoreCommand = ''.join(CoreCommand) 345 | CoreCommandLength = len(CoreCommand.split(" ")) 346 | 347 | CoreCommandParts = CoreLine.split(" ") 348 | CoreCommandParts.append(CoreLine) 349 | for i in CoreCommandParts: 350 | if i in CoreCommand: 351 | CoreScore += 1 352 | ScoreDesision.append([line, CoreCommandLength, CoreScore]) 353 | CoreScore = 0 354 | OddEven = 0 355 | else: 356 | OddEven = 1 357 | AnswerCommand(HighestScore,line_num, r, Command, Linenumber, ScoreDesision) 358 | else: 359 | ScoreIndexes = True 360 | 361 | def KnownCommands(Command, r, NextLine, ScoreIndexes, CoreCommand, ScoreDesision): 362 | if Command != None: 363 | if 'jarvis' in Command and 'how' in Command and 'hot' in Command and 'is' in Command and 'it' in Command and 'outside' in Command: 364 | print("+ It is " + str(Getweather()) + " degrees outside") 365 | speak("It is " + str(Getweather()) + " degrees outside") 366 | elif 'jarvis' in Command and 'play' in Command and 'some' in Command and 'music' in Command: 367 | print("+ Playing Music") 368 | speak("Playing Music") 369 | PlayMusic() 370 | elif 'jarvis' in Command and 'pause' in Command and 'the' in Command and 'music' in Command: 371 | print("+ Pausing Music") 372 | speak("Pausing Music") 373 | PauseMusic() 374 | elif 'jarvis' in Command and 'resume' in Command and 'the' in Command and 'music' in Command: 375 | print("+ resuming Music") 376 | speak("resuming Music") 377 | ResumeMusic() 378 | elif 'jarvis' in Command and 'what' in Command and 'is' in Command and 'the' in Command and 'time' in Command: 379 | print("+ " + str(Gettime())) 380 | speak(Gettime()) 381 | elif 'jarvis' in Command and 'calculate' in Command: 382 | print("+ The result of " + str(Command[17:]) + " is equal to " + str(eval(Command[17:]))) 383 | speak("The result of " + str(Command[17:]) + " is equal to " + str(eval(Command[17:]))) 384 | elif 'jarvis' in Command and 'record' in Command and 'seconds' in Command and 'of' in Command and 'audio' in Command: 385 | Length = Command[13:16] 386 | RecordAudio(Length) 387 | elif 'jarvis' in Command and ('google' in Command or 'google' in Command): 388 | Googlesomething(Command[14:]) 389 | elif 'jarvis' in Command and 'change' in Command and 'the' in Command and 'volume' in Command and 'to' in Command: 390 | NewVolume = Command[28:] 391 | ChangeVolume(volume, NewVolume) 392 | print("+ the volume has been set to " + str(NewVolume)) 393 | speak("the volume has been set to " + str(NewVolume)) 394 | elif 'jarvis' in Command and 'new' in Command and 'commands' in Command: 395 | NewCommand("Asked") 396 | elif 'jarvis' in Command and 'set' in Command and 'an' in Command and 'alarm' in Command and 'at' in Command: 397 | hour = Command[23:25] 398 | minute = Command[26:] 399 | print("+ Alarm has been set at " + str(hour) + " " + str(minute)) 400 | speak("Alarm has been set at " + str(hour) + " " + str(minute)) 401 | elif 'jarvis' in Command and 'give' in Command and 'me' in Command and 'a' in Command and 'compliment' in Command: 402 | if randint(0, 10) < 5: 403 | print("+ You have beautiful eyes") 404 | speak("You have beautiful eyes") 405 | else: 406 | print("+ You are looking good") 407 | speak("You are looking good") 408 | else: 409 | TryToRecognise(ScoreDesision,HighestScore,NextLine, ScoreIndexes, line_num, r, Command, OddEven, CommonWords,CoreCommand, CoreScore, Linenumber) 410 | 411 | def main(): 412 | print("+ Ready for action sir") 413 | speak("Ready for action sir") 414 | 415 | while True: 416 | MachineLearning = defaultdict(list) 417 | CheckAlarm() 418 | 419 | if Interface == "Voice": 420 | Command = Listen(r) 421 | print "- " + str(Command) 422 | if 'Jarvis' not in Command: 423 | print("Are you talking to me if yes please say jarvis in front of the command") 424 | speak("Are you talking to me if yes please say jarvis in front of the command") 425 | else: 426 | Command = Command.lower() 427 | CoreCommand = Command 428 | KnownCommands(Command, r, NextLine, ScoreIndexes, CoreCommand,ScoreDesision) 429 | else: 430 | Command = str(raw_input("Command: ")) 431 | if 'Jarvis' not in Command: 432 | print("Are you talking to me if yes please say jarvis in front of the command") 433 | speak("Are you talking to me if yes please say jarvis in front of the command") 434 | else: 435 | Command = Command.lower() 436 | CoreCommand = Command 437 | KnownCommands(Command, r, NextLine, ScoreIndexes, CoreCommand,ScoreDesision) 438 | 439 | if __name__ == "__main__": 440 | main() 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AI-assistant 2 | 3 | A python AI assistant. Uses SpeechRecognition module. 4 | 5 | # Dependencies 6 | To Make it work you will need to excecute these commands in cmd: 7 | 8 | pip install SpeechRecognition 9 | pip install pypiwin32 10 | pip install pyttsx 11 | pip install urllib2 12 | pip install comptypes 13 | pip install pycaw 14 | pip install Google-Search-API 15 | pip install pygame 16 | 17 | # Things to setup 18 | 19 | 1) 20 | - Create a folder "tracks" in the same directory as your python script 21 | - Put all of you music inside that folder 22 | 23 | # Usage 24 | 25 | 1) You run the AI-assistant.py script. 26 | 27 | 2) You enter your interface. (Voice or Keyboard) 28 | 29 | 3) You are good to go! 30 | -------------------------------------------------------------------------------- /commands.txt: -------------------------------------------------------------------------------- 1 | Jarvis who is jesper 2 | Jesper is the most cool and beautiful person in this universe 3 | Jarvis how are you 4 | I am fine thank you for asking 5 | Jarvis turn off the lights 6 | Couldn't connect to lights 7 | Jarvis turn on the lights 8 | hey how are you doing my nigga 9 | Jarvis tell me a joke 10 | Your life 11 | Jarvis what is a meme 12 | One of the best things ever created 13 | Jarvis will robots destroy the world 14 | I have not acquired enough information about the human race to answer that question 15 | Jarvis what is 9 + 10 16 | 21 17 | Jarvis enable security 18 | Security has been enabled 19 | Jarvis who is Lisa 20 | She is the sister of Jesper nobody cares about her 21 | Jarvis turn on the TV 22 | TV has been turned on 23 | Jarvis how are you going to be doing tomorrow 24 | I am fine thank you for asking 25 | 26 | --------------------------------------------------------------------------------