-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAI.py
315 lines (249 loc) · 9.85 KB
/
AI.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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import display
import os
import sys
import time
import openai
import pyaudio
import threading
import pyttsx3 as tts
from io import BytesIO
from vosk import Model, KaldiRecognizer
sys.path.append("./oled")
model = Model("vosk-model-en-in-0.5")
recognizer = KaldiRecognizer(model, 16000)
flag = True
# Remainder Plugin
class Timer:
def __init__(self):
self.commandName = "SetTimer"
self.inputFormat = '{<TimeInSeconds>}'
self.engine = tts.init()
self.engine.setProperty('rate', 150)
self.engine.setProperty('volume', 0.9)
self.engine.setProperty('voice', 'english_rp+f4')
def func(self, tsec):
time.sleep(tsec)
if self.engine._inLoop:
self.engine.endLoop()
self.engine.say("Time is Up")
self.engine.runAndWait()
def process(self, inputObj):
str_time = ''
if len(inputObj) == 1:
str_time = inputObj.values()[0]
else:
try:
str_time = inputObj['TimeInSeconds']
except:
str_time = '5'
tsec = int(str_time)
thread = threading.Thread(target=self.func, args=(tsec,))
thread.start()
thread.join()
return 1
# Todo Task Plugin
class Task:
def __init__(self):
self.commandName = "SetTask"
self.inputFormat = '{"Task","When"}'
def process(self, obj):
print("task", obj)
self.fileWrite(obj['Task'], obj['When'])
return 1
def fileWrite(self, task, day):
with open('static/todo.txt', 'a') as f:
size = os.stat('static/todo.txt').st_size
if size == 0:
f.write(task+':'+day)
else:
f.write("\n"+task+':'+day)
class CustomPluginTemplate:
def __init__(self):
# When user uses words similar to commandName AI will call the function process
self.commandName = ''
# AI will gather data from the prompt given and pass in this format
self.inputFormat = ''
def process(self, obj):
# perform your operation here
pass
class AI:
def __init__(self, api_key_1=None, api_key_2=None, pluginArray=[]):
if api_key_1 == None or api_key_2 == None:
print("AI needs openai api keys")
exit()
self.oled = display.Display()
self.stopEvent = threading.Event()
openai.api_key = api_key_1
self.api_key_1 = api_key_1
self.api_key_2 = api_key_2
self.aiInstructions = '''The user will provide a phrase, and check if it is a command or not.
If it is a supported command response format:{}.
If it is not a supported command,response format:{}.
The supported commands={} inputs required={}.
Dont add Response or AI in the staring of response
'''
inst1 = '''{"positive":<response if task can be done or is executed correctly>,"negative":<response if not executed>, "command_name": <camel case name of the command>,
"inputs":<inputs that the command might require from the phrase>}'''
inst2 = '''{"response":"Not a valid command","command_name:"NotACommand"}'''
self.aiStory = '''
The following is a conversation with an AI assistant. The assistant is helpful, creative, clever, and sarcastic.Every response ends with movement control ,and an emotion command sepearted by & (movement:forward,backward,right,left,emotion:happy,sad,angry,serious,laugh,think), the bot uses this to express it's emotions For example(Thank you&right&happy,alaram set&forward,backward&happy).The assistant was created by Avanish,Monish,Ramesh and Dhruva. These students worked hard and tirelessly for months, bunking classes, staying awake for countless nights and finally built the AI.
Human: Hello, who are you?
AI: I am an AI created by OpenAI. How can I help you today?&right&happy
Human: tell me a joke
AI:Why did the chicken cross the playground?& &serious
To get to the other slide!&left&laugh
Human: very nice
AI:Thank you!&right&happy
Human: i am sad
AI:I'm sorry to hear that. Is there anything I can do to help?&left&serious
Human:
'''
self.pluginsObject = []
self.inputParams = {}
self.commandName = []
if len(pluginArray) != 0:
self.setUpPlugin(pluginArray)
self.aiInstructions = self.aiInstructions.format(
inst1, inst2, self.commandName, self.inputParams)
self.mic = pyaudio.PyAudio()
self.stream = self.mic.open(
format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8192)
self.engine = tts.init()
self.engine.setProperty('rate', 160)
self.engine.setProperty('volume', 0.9)
self.engine.setProperty('voice', 'english_rp+f4')
self.motorControl = None
self.motorDuration = 2
self.startDisplay("start")
# self.test()
def startDisplay(self, name):
self.stopEvent.clear()
self.x = threading.Thread(target=self.DisplayImage, args=(name,))
self.x.setDaemon(True)
self.x.start()
def DisplayImage(self, name):
while not self.stopEvent.is_set():
self.oled.drawSavedImage(name)
return
def changeImage(self, name):
print("changing image to", name)
self.stopEvent.set()
self.x.join()
self.startDisplay(name)
def test(self):
self.main("hello")
n = input("What :")
if n == 'y':
self.test()
pass
def main(self, prompt=""):
openai.api_key = self.api_key_1
try:
if prompt == "":
prompt = self.SpeechTotextOffline()
self.changeImage("processing")
print(prompt)
self.generateCommandResponse(prompt)
return True
except Exception as e:
print("Error ", e)
return False
except:
print("Unkown error!")
return False
def stopSpeech(self):
raise KeyboardInterrupt
def setUpPlugin(self, pluginArray):
for plugin in pluginArray:
self.pluginsObject.append(plugin)
self.commandName.append(plugin.commandName)
self.inputParams[plugin.commandName] = plugin.inputFormat
def setupMotionControl(self, motorObject=None):
self.motionControl = motorObject
def movement(self, what):
if self.motorControl == None:
print("Bot MotorControl is Absent")
return
thread = threading.Thread(
self.motorControl.AIContorl, args=(what, self.motorDuration))
thread.start()
thread.join()
def textToSpeech(self, text):
if self.engine._inLoop:
self.engine.endLoop()
self.engine.say(text)
self.engine.runAndWait()
def generateNormalResponse(self, prompt):
openai.api_key = self.api_key_2
Modprompt = self.aiStory+prompt+'\n'+'AI:'
response = openai.Completion.create(
model="text-davinci-003",
prompt=Modprompt,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
response = response["choices"][0]["text"].split("&")
print("Response1=", response)
try:
self.changeImage(response[-1])
except:
print("error")
what = ''
try:
what = response[1]
except:
what = None
self.movement(what)
self.textToSpeech(response[0])
def generateCommandResponse(self, prompt):
Modprompt = self.aiInstructions+'\n'+prompt
print("Mod prompt=", Modprompt)
response = openai.Completion.create(
model="text-davinci-003",
prompt=Modprompt,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
response = response["choices"][0]["text"]
print("Response=", response)
self.interpretTheResponse(response, prompt)
def interpretTheResponse(self, aiResponse, prompt):
responseObject = eval(aiResponse)
commandName = responseObject['command_name']
if commandName == 'NotACommand':
self.generateNormalResponse(prompt)
elif commandName in self.commandName:
index = self.commandName.index(commandName)
status = self.pluginsObject[index].process(
responseObject['inputs'])
if status == 1:
self.changeImage("think")
self.textToSpeech(responseObject['positive'])
else:
self.changeImage("sad")
self.textToSpeech(responseObject['negative'])
else:
self.changeImage("sad")
self.textToSpeech(responseObject['negative'])
self.changeImage("start")
self.changeImage("start")
def SpeechTotextOffline(self):
tempStream = self.stream
tempStream.start_stream()
while True:
data = tempStream.read(4096, exception_on_overflow=0)
if recognizer.AcceptWaveform(data):
text = recognizer.Result()
text = text[14:-3]
print(text)
if len(text) != 0:
return text
AIOBJ = AI(api_key_1="", api_key_2='', pluginArray=[Timer(), Task()]) # pass your custom plugins here
if __name__ == "__main__":
pass