parent
849282ab50
commit
3f92df120b
@ -1,27 +1,104 @@
|
|||||||
while True:
|
import redis
|
||||||
|
import RPi.GPIO as GPIO
|
||||||
if button.is_pressed():
|
import asyncio
|
||||||
send_to_main(user_start_message)
|
import websockets
|
||||||
send_to_websocket(user_start_message)
|
import sounddevice as sd
|
||||||
|
import numpy as np
|
||||||
audio_chunks = []
|
import time
|
||||||
for audio_chunk in listen():
|
import re
|
||||||
audio_chunks.append(chunk)
|
|
||||||
if not button.is_pressed():
|
def transcribe(audio_chunks):
|
||||||
break
|
pass # (todo)
|
||||||
|
|
||||||
text = stt(audio_chunks)
|
def say(text):
|
||||||
send_to_main(text)
|
# This should immediatly stop if button is pressed (if GPIO.input(18))
|
||||||
send_to_websocket(user_end_message)
|
pass # (todo)
|
||||||
|
|
||||||
chunk = get_from_queue('to_io')
|
# Connect to button
|
||||||
if chunk:
|
GPIO.setmode(GPIO.BCM)
|
||||||
send_to_websocket(chunk)
|
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||||
sentence += chunk["content"]
|
|
||||||
if is_full_sentence(sentence)
|
# Set the duration and sample rate for the mic
|
||||||
tts(sentence)
|
chunk_duration = 0.5 # seconds
|
||||||
sentence = []
|
sample_rate = 44100 # Hz
|
||||||
|
|
||||||
message = check_websocket()
|
# Set up Redis connection
|
||||||
if message:
|
r = redis.Redis(host='localhost', port=6379, db=0)
|
||||||
send_to_main(message)
|
|
||||||
|
# Define some standard, useful messages
|
||||||
|
user_start_message = {"role": "user", "type": "message", "start": True}
|
||||||
|
user_start_message = {"role": "user", "type": "message", "start": True}
|
||||||
|
|
||||||
|
# Set up websocket connection
|
||||||
|
websocket = websockets.connect('ws://localhost:8765')
|
||||||
|
|
||||||
|
# This is so we only say() full sentences
|
||||||
|
accumulated_text = ""
|
||||||
|
def is_full_sentence(text):
|
||||||
|
return text.endswith(('.', '!', '?'))
|
||||||
|
def split_into_sentences(text):
|
||||||
|
return re.split(r'(?<=[.!?])\s+', text)
|
||||||
|
|
||||||
|
async def send_to_websocket(message):
|
||||||
|
async with websocket as ws:
|
||||||
|
await ws.send(message)
|
||||||
|
|
||||||
|
async def check_websocket():
|
||||||
|
async with websocket as ws:
|
||||||
|
message = await ws.recv()
|
||||||
|
return message
|
||||||
|
|
||||||
|
def main():
|
||||||
|
while True:
|
||||||
|
|
||||||
|
# If the button is pushed down
|
||||||
|
if not GPIO.input(18):
|
||||||
|
|
||||||
|
# Send start message to core and websocket
|
||||||
|
r.rpush('to_core', user_start_message)
|
||||||
|
send_to_websocket(user_start_message)
|
||||||
|
|
||||||
|
# Record audio from the microphone in chunks
|
||||||
|
audio_chunks = []
|
||||||
|
|
||||||
|
# Continue recording until the button is released
|
||||||
|
while not GPIO.input(18):
|
||||||
|
chunk = sd.rec(int(chunk_duration * sample_rate), samplerate=sample_rate, channels=2)
|
||||||
|
sd.wait() # Wait until recording is finished
|
||||||
|
audio_chunks.append(chunk)
|
||||||
|
|
||||||
|
# Transcribe
|
||||||
|
text = transcribe(audio_chunks)
|
||||||
|
|
||||||
|
message = {"role": "user", "type": "message", "content": text, "time": time.time()}
|
||||||
|
|
||||||
|
# Send message to core and websocket
|
||||||
|
r.rpush('to_core', message)
|
||||||
|
send_to_websocket(message)
|
||||||
|
|
||||||
|
# Send out anything in the to_interface queue
|
||||||
|
chunk = r.lpop('to_interface')
|
||||||
|
if chunk:
|
||||||
|
send_to_websocket(chunk)
|
||||||
|
accumulated_text += chunk["content"]
|
||||||
|
|
||||||
|
# Speak full sentences out loud
|
||||||
|
sentences = split_into_sentences(accumulated_text)
|
||||||
|
if is_full_sentence(sentences[-1]):
|
||||||
|
for sentence in sentences:
|
||||||
|
say(sentence)
|
||||||
|
accumulated_text = ""
|
||||||
|
else:
|
||||||
|
for sentence in sentences[:-1]:
|
||||||
|
say(sentence)
|
||||||
|
accumulated_text = sentences[-1]
|
||||||
|
else:
|
||||||
|
say(accumulated_text)
|
||||||
|
accumulated_text = ""
|
||||||
|
|
||||||
|
message = check_websocket()
|
||||||
|
if message:
|
||||||
|
r.rpush('to_core', message)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
Loading…
Reference in new issue