-
Notifications
You must be signed in to change notification settings - Fork 22
/
DaVinciAltVoice.py
373 lines (307 loc) · 10.9 KB
/
DaVinciAltVoice.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# the following program is provided by DevMiser - https://github.com/DevMiser
import datetime
import io
import openai
import os
import pvcobra
import pvleopard
import pvporcupine
import pyaudio
import random
import struct
import sys
import textwrap
import threading
import time
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame
from colorama import Fore, Style
from openai import OpenAI
from PIL import Image,ImageDraw,ImageFont,ImageOps,ImageEnhance
from pvleopard import *
from pvrecorder import PvRecorder
from threading import Thread, Event
from time import sleep
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
led1_pin=18
led2_pin=24
GPIO.setup(led1_pin, GPIO.OUT)
GPIO.output(led1_pin, GPIO.LOW)
GPIO.setup(led2_pin, GPIO.OUT)
GPIO.output(led2_pin, GPIO.LOW)
audio_stream = None
cobra = None
pa = None
porcupine = None
recorder = None
wav_file = None
GPT_model = "gpt-4"
openai.api_key = "put your secret API key between these quotation marks"
pv_access_key= "put your secret access key between these quotation marks"
client = OpenAI(api_key=openai.api_key)
prompt = ["How may I assist you?",
"How may I help?",
"What can I do for you?",
"Ask me anything.",
"Yes?",
"I'm here.",
"I'm listening.",
"What would you like me to do?"]
chat_log=[
{"role": "system", "content": "Your name is DaVinci. You do not have a namesake. You are a helpful AI-based assistant."},
]
#DaVinci will 'remember' earlier queries so that it has greater continuity in its response
#the following will delete that 'memory' three minutes after the start of the conversation
def append_clear_countdown():
sleep(180)
global chat_log
chat_log.clear()
chat_log=[
{"role": "system", "content": "Your name is DaVinci. You do not have a namesake. You are a helpful assistant."},
]
global count
count = 0
t_count.join
def ChatGPT(query):
user_query = [
{"role": "user", "content": query},
]
send_query = (chat_log + user_query)
response = client.chat.completions.create(
model=GPT_model,
messages=send_query
)
answer = response.choices[0].message.content
chat_log.append({"role": "assistant", "content": answer})
return answer
def current_time():
time_now = datetime.datetime.now()
formatted_time = time_now.strftime("%m-%d-%Y %I:%M %p\n")
print("The current date and time is:", formatted_time)
def detect_silence():
cobra = pvcobra.create(access_key=pv_access_key)
silence_pa = pyaudio.PyAudio()
cobra_audio_stream = silence_pa.open(
rate=cobra.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=cobra.frame_length)
last_voice_time = time.time()
while True:
cobra_pcm = cobra_audio_stream.read(cobra.frame_length)
cobra_pcm = struct.unpack_from("h" * cobra.frame_length, cobra_pcm)
if cobra.process(cobra_pcm) > 0.2:
last_voice_time = time.time()
else:
silence_duration = time.time() - last_voice_time
if silence_duration > 1.3:
print("End of query detected\n")
GPIO.output(led1_pin, GPIO.LOW)
GPIO.output(led2_pin, GPIO.LOW)
cobra_audio_stream.stop_stream
cobra_audio_stream.close()
cobra.delete()
last_voice_time=None
break
def fade_leds(event):
pwm1 = GPIO.PWM(led1_pin, 200)
pwm2 = GPIO.PWM(led2_pin, 200)
event.clear()
while not event.is_set():
pwm1.start(0)
pwm2.start(0)
for dc in range(0, 101, 5):
pwm1.ChangeDutyCycle(dc)
pwm2.ChangeDutyCycle(dc)
time.sleep(0.05)
time.sleep(0.75)
for dc in range(100, -1, -5):
pwm1.ChangeDutyCycle(dc)
pwm2.ChangeDutyCycle(dc)
time.sleep(0.05)
time.sleep(0.75)
def listen():
cobra = pvcobra.create(access_key=pv_access_key)
listen_pa = pyaudio.PyAudio()
listen_audio_stream = listen_pa.open(
rate=cobra.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=cobra.frame_length)
print("Listening...")
while True:
listen_pcm = listen_audio_stream.read(cobra.frame_length)
listen_pcm = struct.unpack_from("h" * cobra.frame_length, listen_pcm)
if cobra.process(listen_pcm) > 0.3:
print("Voice detected")
listen_audio_stream.stop_stream
listen_audio_stream.close()
cobra.delete()
break
def responseprinter(chat):
wrapper = textwrap.TextWrapper(width=70) # Adjust the width to your preference
paragraphs = res.split('\n')
wrapped_chat = "\n".join([wrapper.fill(p) for p in paragraphs])
for word in wrapped_chat:
time.sleep(0.055)
print(word, end="", flush=True)
print()
def voice(chat):
response = client.audio.speech.create(
model="tts-1",
voice="echo",
input=chat
)
response.stream_to_file("speech.mp3")
pygame.mixer.init()
pygame.mixer.music.load("speech.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pass
sleep(0.2)
def wake_word():
porcupine = pvporcupine.create(keywords=["computer", "jarvis", "DaVinci",],
access_key=pv_access_key,
sensitivities=[0.1, 0.1, 0.1], #from 0 to 1.0 - a higher number reduces the miss rate at the cost of increased false alarms
)
devnull = os.open(os.devnull, os.O_WRONLY)
old_stderr = os.dup(2)
sys.stderr.flush()
os.dup2(devnull, 2)
os.close(devnull)
wake_pa = pyaudio.PyAudio()
porcupine_audio_stream = wake_pa.open(
rate=porcupine.sample_rate,
channels=1,
format=pyaudio.paInt16,
input=True,
frames_per_buffer=porcupine.frame_length)
Detect = True
while Detect:
porcupine_pcm = porcupine_audio_stream.read(porcupine.frame_length)
porcupine_pcm = struct.unpack_from("h" * porcupine.frame_length, porcupine_pcm)
porcupine_keyword_index = porcupine.process(porcupine_pcm)
if porcupine_keyword_index >= 0:
GPIO.output(led1_pin, GPIO.HIGH)
GPIO.output(led2_pin, GPIO.HIGH)
print(Fore.GREEN + "\nWake word detected\n")
current_time()
porcupine_audio_stream.stop_stream
porcupine_audio_stream.close()
porcupine.delete()
os.dup2(old_stderr, 2)
os.close(old_stderr)
Detect = False
class Recorder(Thread):
def __init__(self):
super().__init__()
self._pcm = list()
self._is_recording = False
self._stop = False
def is_recording(self):
return self._is_recording
def run(self):
self._is_recording = True
recorder = PvRecorder(device_index=-1, frame_length=512)
recorder.start()
while not self._stop:
self._pcm.extend(recorder.read())
recorder.stop()
self._is_recording = False
def stop(self):
self._stop = True
while self._is_recording:
pass
return self._pcm
try:
o = create(
access_key=pv_access_key,
)
event = threading.Event()
count = 0
while True:
try:
Chat = 1
if count == 0:
t_count = threading.Thread(target=append_clear_countdown)
t_count.start()
else:
pass
count += 1
wake_word()
# comment out the next line if you do not want DaVinci to verbally respond to his name
voice(random.choice(prompt))
recorder = Recorder()
recorder.start()
listen()
detect_silence()
transcript, words = o.process(recorder.stop())
t_fade = threading.Thread(target=fade_leds, args=(event,))
t_fade.start()
recorder.stop()
print("You said: " + transcript)
if Chat == 1:
(res) = ChatGPT(transcript)
print("\nDaVinci's response is:\n")
t1 = threading.Thread(target=voice, args=(res,))
t2 = threading.Thread(target=responseprinter, args=(res,))
t1.start()
t2.start()
t1.join()
t2.join()
event.set()
GPIO.output(led1_pin, GPIO.LOW)
GPIO.output(led2_pin, GPIO.LOW)
recorder.stop()
o.delete
recorder = None
except openai.APIError as e:
print("\nThere was an API error. Please try again in a few minutes.")
voice("\nThere was an A P I error. Please try again in a few minutes.")
event.set()
GPIO.output(led1_pin, GPIO.LOW)
GPIO.output(led2_pin, GPIO.LOW)
recorder.stop()
o.delete
recorder = None
sleep(1)
except openai.RateLimitError as e:
print("\nYou have hit your assigned rate limit.")
voice("\nYou have hit your assigned rate limit.")
event.set()
GPIO.output(led1_pin, GPIO.LOW)
GPIO.output(led2_pin, GPIO.LOW)
recorder.stop()
o.delete
recorder = None
break
except openai.APIConnectionError as e:
print("\nI am having trouble connecting to the API. Please check your network connection and then try again.")
voice("\nI am having trouble connecting to the A P I. Please check your network connection and try again.")
event.set()
GPIO.output(led1_pin, GPIO.LOW)
GPIO.output(led2_pin,
GPIO.LOW)
recorder.stop()
o.delete
recorder = None
sleep(1)
except openai.AuthenticationError as e:
print("\nYour OpenAI API key or token is invalid, expired, or revoked. Please fix this issue and then restart my program.")
voice("\nYour Open A I A P I key or token is invalid, expired, or revoked. Please fix this issue and then restart my program.")
event.set()
GPIO.output(led1_pin, GPIO.LOW)
GPIO.output(led2_pin, GPIO.LOW)
recorder.stop()
o.delete
recorder = None
break
except KeyboardInterrupt:
print("\nExiting ChatGPT Virtual Assistant")
o.delete
GPIO.cleanup()