-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdison.py
More file actions
538 lines (447 loc) · 17.1 KB
/
Edison.py
File metadata and controls
538 lines (447 loc) · 17.1 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# the following program is provided by DevMiser - https://github.com/DevMiser
#!/usr/bin/env python3
import board
import boto3
import datetime
import io
import openai
import os
import pvcobra
import pvleopard
import pvporcupine
import pyaudio
import random
import re
import struct
import subprocess
import sys
import textwrap
import threading
import time
import vlc
#import pytz #uncomment to select timezone
from os import environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = '1'
import pygame
from adafruit_ht16k33.segments import Seg7x4
from openai import OpenAI
from colorama import Fore, Style
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
GPIO.setup(led1_pin, GPIO.OUT)
GPIO.output(led1_pin, GPIO.LOW)
i2c = board.I2C()
display = Seg7x4(i2c)
display.brightness = 1.0 #can be adjusted between 0.0 and 1.0
display.blink_rate = 0
display.colon = True
audio_stream = None
cobra = None
pa = None
polly = boto3.client('polly')
porcupine = None
recorder = None
wav_file = None
GPT_model = "gpt-4" # most capable GPT model and optimized for chat. You can substitute with gpt-3.5-turbo for lower cost and latency.
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 Edison. You are a helpful assistant and an internet clock radio. If asked about yourself, you include your name in your response."},
]
#Edison 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 Edison. You are a helpful assistant and an internet clock radio. If asked about yourself, you include your name in your response."},
]
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 change_station(transcript):
global Chat
global Radio
transcript_l = transcript.lower()
if any(word in transcript_l for word in ["change", "play", "switch"]) and \
any(word in transcript_l for word in ["radio", "station", "channel", "music"]):
print ("A request to change the radio station was detected")
if "classical" in transcript_l:
radio_url = "http://tuner.classical102.com:80/"
print ("Playing classical")
elif "mandarin" in transcript_l:
radio_url = "http://s1.voscast.com:10556/stream"
print ("Playing Mandarin")
elif "french" in transcript_l:
radio_url = "http://direct.franceinter.fr/live/franceinter-midfi.mp3"
print ("Playing French")
elif "acoustic" in transcript_l:
radio_url = "http://ec5.yesstreaming.net:1910/stream"
print ("Playing acoustic music")
elif "calm" in transcript_l:
radio_url = "http://streaming503.radionomy.com/calmradio-solo-piano-guitar"
print ("Playing calm music")
elif "rock" in transcript_l:
radio_url = "http://audio-edge-es6pf.mia.g.radiomast.io/c2cc99fb-efdf-485d-bcec-be38ba20e2bb"
print ("Playing Rock music")
elif "pop" in transcript_l:
radio_url = "http://pstnet6.shoutcastnet.com:60210/stream"
print ("Playing Pop music")
elif "guitar" in transcript_l:
radio_url = "http://radio.ericksons.net:8000/cgnw"
print ("Playing guitar music")
elif "spa" in transcript_l:
radio_url = "https://ec3.yesstreaming.net:1950/stream"
print ("Playing spa music")
else:
radio_url = "https://ec3.yesstreaming.net:1950/stream"
print ("Playing French")
print(radio_url)
media = instance.media_new(radio_url)
player.set_media(media)
player.audio_set_volume(70) #integer between 0-100
player.play()
Radio = 1
Chat = 0
else:
pass
def clock_request(transcript):
global Chat
transcript_l = transcript.lower()
if ("stop" in transcript_l or "off" in transcript_l or "start"\
in transcript_l or "on" in transcript_l or "dim" in transcript_l\
or "darken" in transcript_l or "brighten" in transcript_l)\
and ("clock" in transcript_l or "display" in transcript_l):
print ("A request to change the clock was detected")
if "stop" in transcript_l or "off" in transcript_l:
print ("\nTurning off the clock")
clock_event.set()
display.fill(0)
elif "dim" in transcript_l or "darken" in transcript_l:
print ("\nDimming the display")
display.brightness = 0.3
elif "brighten" in transcript_l:
print ("\nBrightening the display")
display.brightness = 1.0
if "start" in transcript_l or "on" in transcript_l:
print ("\nTurning on the clock")
clock_thread = threading.Thread(target=display_time, args=(clock_event,))
clock_thread.start()
else:
pass
voice("Done")
sleep(0.2)
Chat = 0
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)
cobra_audio_stream.stop_stream
cobra_audio_stream.close()
cobra.delete()
last_voice_time=None
break
def display_time(clock_event):
clock_event.clear()
while not clock_event.is_set():
now = datetime.datetime.now()
# now = datetime.datetime.now(pytz.timezone('Etc/GMT')) #uncomment to select timezone
time = now.strftime("%l:%M")
display.print(time)
sleep(0.2)
def fade_led_filament(event):
pwm1 = GPIO.PWM(led1_pin, 200)
event.clear()
while not event.is_set():
pwm1.start(0)
for dc in range(0, 101, 5):
pwm1.ChangeDutyCycle(dc)
time.sleep(0.05)
time.sleep(0.75)
for dc in range(100, -1, -5):
pwm1.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 radio_request(transcript):
global Chat
global Radio
transcript_l = transcript.lower()
if any(word in transcript_l for word in ["stop", "off", "start", "on", "mute"]) and \
any(word in transcript_l for word in ["radio", "music", "sound"]):
print ("A request to change the radio status was detected")
if "stop" in transcript_l or "off" in transcript_l or "mute" in transcript_l:
print ("\nTurning off the radio")
Radio = 0
player.stop()
voice("Done")
if "start" in transcript_l or "on" in transcript_l or "play" in transcript_l:
print ("\nTurning on the radio")
Radio = 1
player.play()
clock_thread = threading.Thread(target=display_time, args=(clock_event,))
clock_thread.start()
else:
pass
Chat = 0
sleep(0.5)
def voice(chat):
voiceResponse = polly.synthesize_speech(Text=chat, OutputFormat="mp3",
VoiceId="Matthew") #other options include Amy, Joey, Nicole, Raveena and Russell
if "AudioStream" in voiceResponse:
with voiceResponse["AudioStream"] as stream:
output_file = "speech.mp3"
try:
with open(output_file, "wb") as file:
file.write(stream.read())
except IOError as error:
print(error)
else:
print("did not work")
pygame.mixer.init()
pygame.mixer.music.load(output_file)
# pygame.mixer.music.set_volume(0.8) # uncomment to control the the playback volume (from 0.0 to 1.0
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pass
sleep(0.2)
def wake_word():
global Radio
porcupine = pvporcupine.create(keywords=["computer", "jarvis", "Edison",],
access_key=pv_access_key,
sensitivities=[0.1, 0.1, 0.4], #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() # pause the vlc player
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)
if Radio == 1:
player.stop()
else:
pass
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)
sleep (0.2)
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()
clock_event = threading.Event()
clock_thread = threading.Thread(target=display_time, args=(clock_event,))
clock_thread.start()
instance = vlc.Instance()
instance.log_unset()
player = instance.media_player_new()
media = instance.media_new('http://streaming503.radionomy.com/calmradio-solo-piano-guitar')
player.set_media(media)
player.audio_set_volume(70) #integer between 0-100
global Radio
Radio = 0
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()
# uncomment the next line if you want Edison 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_led_filament, args=(event,))
t_fade.start()
recorder.stop()
print("You said: " + transcript)
change_station(transcript)
radio_request(transcript)
clock_request(transcript)
if Chat == 1:
(res) = ChatGPT(transcript)
print("\nEdison'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()
if Radio == 1:
player.play()
print("Radio is on")
else:
print("Radio is off")
pass
event.set()
GPIO.output(led1_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()