-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathjelvis
executable file
·175 lines (140 loc) · 5.18 KB
/
jelvis
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python3
import os
import sys
import time
import aiml
import pyttsx3
import urllib3
import warnings
import threading
import pocketsphinx
from os import system
import speech_recognition as sr
from PyQt5 import QtCore,QtGui
from PyQt5.QtGui import QMovie , QIcon
from PyQt5.QtWidgets import QApplication, QLabel, QSystemTrayIcon, QMenu , QMessageBox
mode = "voice"
if len(sys.argv) > 1:
if sys.argv[1] == "--voice" or sys.argv[1] == "voice":
try:
import speech_recognition as sr
mode = "voice"
except ImportError:
print("\nInstall SpeechRecognition to use this feature.\nStarting text mode\n")
terminate = ['bye','buy','shutdown','exit','quit','gotosleep','goodbye']
class QTextMovieLabel(QLabel):
def __init__(self, fileName):
QLabel.__init__(self)
thread = Thread(self)
m = QMovie(fileName)
m.start()
self.setMovie(m)
app.aboutToQuit.connect(thread.stop)
thread.start()
def setMovie(self, movie):
QLabel.setMovie(self, movie)
s=movie.currentImage().size()
self._movieWidth = s.width()
self._movieHeight = s.height()
class Thread(QtCore.QThread):
def __init__(self, parent):
QtCore.QThread.__init__(self, parent)
self.window = parent
self._lock = threading.Lock()
self.running = True
def run(self):
self.running = True
def internet_on():
try:
urllib2.urlopen('http://216.58.192.142', timeout=1)
return True
except urllib2.URLError as err:
return False
def 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 = pyttsx3.init()
engine.say(jarvis_speech)
engine.runAndWait()
def listen():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Talk to JELVIS: ")
audio = r.listen(source)
try:
print (r.recognize_google(audio))
return r.recognize_google(audio)
#if internet_on() == True:
# print r.recognize_google(audio)
# return r.recognize_google(audio)
#else:
# print r.recognize_sphinx(audio)
# return r.recognize_sphinx(audio)
except sr.UnknownValueError:
#offline_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 speech service; {0}".format(e))
#except sr.UnknownValueError:
# return(listen())
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 JELVIS : ")
if response.lower().replace(" ","") in terminate:
#break
response = listen()
jarvis_speech = kernel.respond(response)
print ("JELVIS: " + jarvis_speech)
offline_speak(jarvis_speech)
#if internet_on() == True:
# speak(jarvis_speech)
#else:
# offline_speak(jarvis_speech)
def stop(self):
engine = pyttsx3.init()
engine.say("goodbye sir")
engine.runAndWait()
self.running = False
if __name__ == '__main__':
app = QApplication(sys.argv)
# Body with show jelvis
l = QTextMovieLabel('icons/jelvis.gif')
l.setWindowTitle("JELVIS")
l.show()
# Tray icon and menu
trayIcon = QSystemTrayIcon(QIcon('icons/jelvis_try.png'), parent=app)
trayIcon.setToolTip('JELVIS 1.0.2')
trayIcon.show()
menu = QMenu()
# Show Jelvis window
showAction = menu.addAction('Show JELVIS')
showAction.triggered.connect(l.show)
# Hide Jelvis Window
showAction = menu.addAction('Hide JELVIS')
showAction.triggered.connect(l.hide)
# QMessageBox Info
msg = QMessageBox()
msg.setWindowTitle("Info")
msg.setText("\nJELVIS v1.0.2\n\nThis project can be an audio assistant on your operating system and perform the tasks that you are considering for it. You can use different scripts to use in the language interface\n\nhttps://github.com/kiahamedi/JELVIS\[email protected]\n")
moreAction = menu.addAction('More info')
moreAction.triggered.connect(msg.show)
# Exit Tray icon
exitAction = menu.addAction('Exit')
exitAction.triggered.connect(app.quit)
trayIcon.setContextMenu(menu)
sys.exit(app.exec_())