-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
script.py
103 lines (85 loc) · 2.85 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import aiml
import os
import time
import argparse
mode = "text"
voice = "pyttsx"
terminate = ['bye', 'buy', 'shutdown', 'exit', 'quit', 'gotosleep', 'goodbye']
def get_arguments():
parser = argparse.ArgumentParser()
optional = parser.add_argument_group('params')
optional.add_argument('-v', '--voice', action='store_true', required=False,
help='Enable voice mode')
optional.add_argument('-g', '--gtts', action='store_true', required=False,
help='Enable Google Text To Speech engine')
arguments = parser.parse_args()
return arguments
def gtts_speak(jarvis_speech):
tts = gTTS(text=jarvis_speech, lang='en')
tts.save('jarvis_speech.mp3')
mixer.init()
mixer.music.load('jarvis_speech.mp3')
mixer.music.play()
while mixer.music.get_busy():
time.sleep(1)
def offline_speak(jarvis_speech):
engine = pyttsx.init()
engine.say(jarvis_speech)
engine.runAndWait()
def speak(jarvis_speech):
if voice == "gTTS":
gtts_speak(jarvis_speech)
else:
offline_speak(jarvis_speech)
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Talk to J.A.R.V.I.S: ")
audio = r.listen(source)
try:
print r.recognize_google(audio)
return r.recognize_google(audio)
except sr.UnknownValueError:
speak(
"I couldn't understand what you said! Would you like to repeat?")
return(listen())
except sr.RequestError as e:
print("Could not request results from " +
"Google Speech Recognition service; {0}".format(e))
if __name__ == '__main__':
args = get_arguments()
if (args.voice):
try:
import speech_recognition as sr
mode = "voice"
except ImportError:
print("\nInstall SpeechRecognition to use this feature." +
"\nStarting text mode\n")
if (args.gtts):
try:
from gtts import gTTS
from pygame import mixer
voice = "gTTS"
except ImportError:
import pyttsx
print("\nInstall gTTS and pygame to use this feature." +
"\nUsing pyttsx\n")
else:
import pyttsx
kernel = aiml.Kernel()
if os.path.isfile("bot_brain.brn"):
kernel.bootstrap(brainFile="bot_brain.brn")
else:
kernel.bootstrap(learnFiles="std-startup.xml", commands="load aiml b")
# kernel.saveBrain("bot_brain.brn")
# kernel now ready for use
while True:
if mode == "voice":
response = listen()
else:
response = raw_input("Talk to J.A.R.V.I.S : ")
if response.lower().replace(" ", "") in terminate:
break
jarvis_speech = kernel.respond(response)
print "J.A.R.V.I.S: " + jarvis_speech
speak(jarvis_speech)