From 3fbf2aa36223ab806e8e367adbfbb991550d4be2 Mon Sep 17 00:00:00 2001 From: Abdullah Gohar Date: Fri, 12 Apr 2024 09:03:48 +0500 Subject: [PATCH 1/3] Fixed the ctrl-c bug --- software/source/clients/base_device.py | 27 +++++++++++++++---- software/source/server/utils/process_utils.py | 6 ++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/software/source/clients/base_device.py b/software/source/clients/base_device.py index 3bf900e..a614498 100644 --- a/software/source/clients/base_device.py +++ b/software/source/clients/base_device.py @@ -72,6 +72,7 @@ class Device: self.captured_images = [] self.audiosegments = [] self.server_url = "" + self.ctrl_pressed = False def fetch_image_from_camera(self, camera_index=CAMERA_DEVICE_INDEX): """Captures an image from the specified camera device and saves it to a temporary file. Adds the image to the captured_images list.""" @@ -256,23 +257,39 @@ class Device: def on_press(self, key): """Detect spacebar press and Ctrl+C combination.""" self.pressed_keys.add(key) # Add the pressed key to the set + if keyboard.Key.space in self.pressed_keys: self.toggle_recording(True) - elif {keyboard.Key.ctrl, keyboard.KeyCode.from_char("c")} <= self.pressed_keys: + elif {keyboard.Key.ctrl, keyboard.KeyCode.from_char('c')} <= self.pressed_keys: logger.info("Ctrl+C pressed. Exiting...") kill_process_tree() os._exit(0) + + # Windows alternative to the above + if key == keyboard.Key.ctrl_l: + self.ctrl_pressed = True + + try: + if key.vk == 67 and self.ctrl_pressed: + logger.info("Ctrl+C pressed. Exiting...") + kill_process_tree() + os._exit(0) + # For non-character keys + except: + pass + + def on_release(self, key): """Detect spacebar release and 'c' key press for camera, and handle key release.""" - self.pressed_keys.discard( - key - ) # Remove the released key from the key press tracking set + self.pressed_keys.discard(key) # Remove the released key from the key press tracking set + if key == keyboard.Key.ctrl_l: + self.ctrl_pressed = False if key == keyboard.Key.space: self.toggle_recording(False) - elif CAMERA_ENABLED and key == keyboard.KeyCode.from_char("c"): + elif CAMERA_ENABLED and key == keyboard.KeyCode.from_char('c'): self.fetch_image_from_camera() async def message_sender(self, websocket): diff --git a/software/source/server/utils/process_utils.py b/software/source/server/utils/process_utils.py index 586e4c6..5337bae 100644 --- a/software/source/server/utils/process_utils.py +++ b/software/source/server/utils/process_utils.py @@ -7,7 +7,11 @@ def kill_process_tree(): pid = os.getpid() # Get the current process ID try: # Send SIGTERM to the entire process group to ensure all processes are targeted - os.killpg(os.getpgid(pid), signal.SIGKILL) + try: + os.killpg(os.getpgid(pid), signal.SIGKILL) + # Windows implementation + except AttributeError: + os.kill(pid, signal.SIGTERM) parent = psutil.Process(pid) children = parent.children(recursive=True) for child in children: From f365b6a515b3b1b5125277a66256cccd551400e3 Mon Sep 17 00:00:00 2001 From: Abdullah Gohar Date: Sun, 14 Apr 2024 07:50:03 +0500 Subject: [PATCH 2/3] Fixed non-connection issue --- software/source/clients/base_device.py | 15 +++++++++++++-- software/start.py | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/software/source/clients/base_device.py b/software/source/clients/base_device.py index a614498..5d60c50 100644 --- a/software/source/clients/base_device.py +++ b/software/source/clients/base_device.py @@ -3,6 +3,7 @@ from dotenv import load_dotenv load_dotenv() # take environment variables from .env. import os +import sys import asyncio import threading import pyaudio @@ -58,7 +59,17 @@ CAMERA_WARMUP_SECONDS = float(os.getenv("CAMERA_WARMUP_SECONDS", 0)) # Specify OS current_platform = get_system_info() -is_win10 = lambda: platform.system() == "Windows" and "10" in platform.version() + +def is_win11(): + return sys.getwindowsversion().build >= 22000 + +def is_win10(): + try: + return platform.system() == "Windows" and "10" in platform.version() and not is_win11() + except: + return False + +print(platform.system(), platform.version()) # Initialize PyAudio p = pyaudio.PyAudio() @@ -359,7 +370,7 @@ class Device: code = message["content"] result = interpreter.computer.run(language, code) send_queue.put(result) - + if is_win10(): logger.info("Windows 10 detected") # Workaround for Windows 10 not latching to the websocket server. diff --git a/software/start.py b/software/start.py index 4f3377f..6016d17 100644 --- a/software/start.py +++ b/software/start.py @@ -122,6 +122,10 @@ def _run( # llm_service = "llamafile" stt_service = "local-whisper" select_local_model() + + system_type = platform.system() + if system_type == "Windows": + server_host = "localhost" if not server_url: server_url = f"{server_host}:{server_port}" @@ -129,6 +133,8 @@ def _run( if not server and not client: server = True client = True + + def handle_exit(signum, frame): os._exit(0) From 325fcea15bb8d2b1cf2cbd3ff98a39a0d17d7666 Mon Sep 17 00:00:00 2001 From: Abdullah Gohar Date: Sun, 14 Apr 2024 07:58:16 +0500 Subject: [PATCH 3/3] Removed redundant logging lines --- software/source/clients/base_device.py | 1 - 1 file changed, 1 deletion(-) diff --git a/software/source/clients/base_device.py b/software/source/clients/base_device.py index 5d60c50..087ca55 100644 --- a/software/source/clients/base_device.py +++ b/software/source/clients/base_device.py @@ -69,7 +69,6 @@ def is_win10(): except: return False -print(platform.system(), platform.version()) # Initialize PyAudio p = pyaudio.PyAudio()