From c75fa8c322a0eff0bbd923d185e223248db9a11d Mon Sep 17 00:00:00 2001 From: Ty Fiero Date: Sat, 10 Feb 2024 21:32:13 -0800 Subject: [PATCH 1/4] Use a button if a raspberry pi is connected --- OS/01/device.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/OS/01/device.py b/OS/01/device.py index edf7a60..f4462e9 100644 --- a/OS/01/device.py +++ b/OS/01/device.py @@ -17,6 +17,7 @@ import tempfile from datetime import datetime from interpreter import interpreter # Just for code execution. Maybe we should let people do from interpreter.computer import run? from utils.kernel import put_kernel_messages_into_queue +from utils.get_system_info import get_system_info from stt import stt_wav import asyncio @@ -33,6 +34,9 @@ RECORDING = False # Flag to control recording state SPACEBAR_PRESSED = False # Flag to track spacebar press state +# Specify OS +current_platform = get_system_info() + # Initialize PyAudio p = pyaudio.PyAudio() @@ -186,8 +190,8 @@ async def websocket_communication(WS_URL): except Exception as e: - logging.exception(f"An error occurred during websocket communication. {e}") - logging.info(f"Connecting to `{WS_URL}`...") + logger.exception(f"An error occurred during websocket communication. {e}") + logger.info(f"Connecting to `{WS_URL}`...") await asyncio.sleep(2) @@ -205,9 +209,30 @@ if __name__ == "__main__": if os.getenv('CODE_RUNNER') == "device": asyncio.create_task(put_kernel_messages_into_queue(send_queue)) - # Keyboard listener for spacebar press/release - listener = keyboard.Listener(on_press=on_press, on_release=on_release) - listener.start() + + #If Raspberry Pi, add the button listener, otherwise use the spacebar + if current_platform.startswith("raspberry-pi"): + logger.info("Raspberry Pi detected, using button on GPIO pin 15") + # Use GPIO pin 15 + pindef = ["gpiochip4", "15"] # gpiofind PIN15 + print("PINDEF", pindef) + + # HACK: needs passwordless sudo + process = await asyncio.create_subprocess_exec("sudo", "gpiomon", "-brf", *pindef, stdout=asyncio.subprocess.PIPE) + while True: + line = await process.stdout.readline() + if line: + line = line.decode().strip() + if "FALLING" in line: + toggle_recording(False) + elif "RISING" in line: + toggle_recording(True) + else: + break + else: + # Keyboard listener for spacebar press/release + listener = keyboard.Listener(on_press=on_press, on_release=on_release) + listener.start() asyncio.run(main()) p.terminate() \ No newline at end of file From ecf4a9385fab92e2570986ad44af20b7fc96eaad Mon Sep 17 00:00:00 2001 From: Ty Fiero Date: Sat, 10 Feb 2024 21:33:34 -0800 Subject: [PATCH 2/4] Small changes to the start script. It was downloading local whisper every time --- OS/01/start.sh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/OS/01/start.sh b/OS/01/start.sh index 18187bf..5904aa8 100755 --- a/OS/01/start.sh +++ b/OS/01/start.sh @@ -1,3 +1,5 @@ +#!/usr/bin/env bash + ### SETTINGS # If ALL_LOCAL is False, we'll use OpenAI's services @@ -33,7 +35,7 @@ export STT_RUNNER=device # If server, audio will be sent over websocket. export SERVER_EXPOSE_PUBLICALLY=False # Debug level -# export LOG_LEVEL=DEBUG +# export LOG_LEVEL="DEBUG" export LOG_LEVEL="INFO" @@ -43,9 +45,9 @@ export LOG_LEVEL="INFO" WHISPER_MODEL_URL="https://huggingface.co/ggerganov/whisper.cpp/resolve/main/" WHISPER_RUST_PATH="`pwd`/local_stt/whisper-rust" -curl -OL "${WHISPER_MODEL_URL}${WHISPER_MODEL_NAME}" --output-dir ${WHISPER_RUST_PATH} if [[ "$ALL_LOCAL" == "True" ]]; then + curl -OL "${WHISPER_MODEL_URL}${WHISPER_MODEL_NAME}" --output-dir ${WHISPER_RUST_PATH} OS=$(uname -s) ARCH=$(uname -m) if [ "$OS" = "Darwin" ]; then @@ -88,7 +90,7 @@ fi SERVER_PORT=$(echo $SERVER_URL | grep -oE "[0-9]+") if [ -n "$SERVER_PORT" ]; then - lsof -ti tcp:$SERVER_PORT | xargs kill + lsof -ti tcp:$SERVER_PORT | xargs kill 2>/dev/null || true fi ### START From d9fe81cc8d5899b540397386d4145b184b3b8c4b Mon Sep 17 00:00:00 2001 From: Ty Fiero Date: Sat, 10 Feb 2024 21:34:04 -0800 Subject: [PATCH 3/4] Comment out some of the kernel filter code. --- OS/01/utils/kernel.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/OS/01/utils/kernel.py b/OS/01/utils/kernel.py index 7f1bb3c..d7be2ba 100644 --- a/OS/01/utils/kernel.py +++ b/OS/01/utils/kernel.py @@ -29,11 +29,12 @@ def custom_filter(message): end = message.find('}TO_INTERPRETER}', start) return message[start:end] # Check for USB mention - elif 'USB' in message: - return message - # Check for network related keywords - elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']) and "networkStatusForFlags" not in message: - return message + # elif 'USB' in message: + # return message + # # Check for network related keywords + # elif any(keyword in message for keyword in ['network', 'IP', 'internet', 'LAN', 'WAN', 'router', 'switch']) and "networkStatusForFlags" not in message: + + # return message else: return None From e446e2765b9848dee9b7c39e1dcbfe4a2f601d6f Mon Sep 17 00:00:00 2001 From: Ty Fiero Date: Sat, 10 Feb 2024 21:34:25 -0800 Subject: [PATCH 4/4] Added a get system message function to determine if its a raspberry pi --- OS/01/utils/get_system_info.py | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 OS/01/utils/get_system_info.py diff --git a/OS/01/utils/get_system_info.py b/OS/01/utils/get_system_info.py new file mode 100644 index 0000000..eeca56a --- /dev/null +++ b/OS/01/utils/get_system_info.py @@ -0,0 +1,38 @@ +import os +import platform + + +def get_system_info(): + system = platform.system() + + if system == "Linux": + # Attempt to identify specific Linux distribution + distro = "linux" # Default to generic 'linux' + try: + with open("/etc/os-release") as f: + os_release_info = f.read().lower() + if "ubuntu" in os_release_info: + return "raspberry-pi-ubuntu" + elif "raspbian" in os_release_info: + return "raspberry-pi-os" + except FileNotFoundError: + pass + + # Check for Raspberry Pi hardware + try: + with open("/proc/device-tree/model") as f: + model_info = f.read() + if "Raspberry Pi" in model_info: + if distro == "ubuntu": + return "raspberry-pi-ubuntu" + return "raspberry-pi" + except FileNotFoundError: + pass + + return distro + elif system == "Darwin": + return "darwin" + elif system == "Windows": + return "windows" + else: + return "unknown"