From 7863497ac8ab66cbb23146a079af61a29efd0653 Mon Sep 17 00:00:00 2001 From: Chaitanya Rahalkar Date: Sun, 24 Mar 2024 12:08:44 -0500 Subject: [PATCH 1/4] Fix empty message array error when running on Windows --- software/source/server/utils/kernel.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/software/source/server/utils/kernel.py b/software/source/server/utils/kernel.py index b443bba..e8ac7fb 100644 --- a/software/source/server/utils/kernel.py +++ b/software/source/server/utils/kernel.py @@ -45,15 +45,16 @@ last_messages = "" def check_filtered_kernel(): messages = get_kernel_messages() - messages.replace(last_messages, "") - messages = messages.split("\n") - - filtered_messages = [] - for message in messages: - if custom_filter(message): - filtered_messages.append(message) - - return "\n".join(filtered_messages) + if messages: + messages.replace(last_messages, "") + messages = messages.split("\n") + + filtered_messages = [] + for message in messages: + if custom_filter(message): + filtered_messages.append(message) + + return "\n".join(filtered_messages) async def put_kernel_messages_into_queue(queue): while True: From f7a2f0317cd7c0ed876551c8d0c5b4f018da06c6 Mon Sep 17 00:00:00 2001 From: Tasha Upchurch Date: Tue, 26 Mar 2024 17:18:40 -0400 Subject: [PATCH 2/4] Implement cross-platform compatibility in stt.py by replacing system-specific commands with Python's built-in functions --- .../server/services/stt/local-whisper/stt.py | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/software/source/server/services/stt/local-whisper/stt.py b/software/source/server/services/stt/local-whisper/stt.py index b318e8e..727154d 100644 --- a/software/source/server/services/stt/local-whisper/stt.py +++ b/software/source/server/services/stt/local-whisper/stt.py @@ -12,6 +12,8 @@ import subprocess import os import subprocess +import platform +import urllib.request class Stt: @@ -23,7 +25,6 @@ class Stt: return stt(self.service_directory, audio_file_path) - def install(service_dir): ### INSTALL @@ -42,27 +43,29 @@ def install(service_dir): # Check if whisper-rust executable exists before attempting to build if not os.path.isfile(os.path.join(WHISPER_RUST_PATH, "target/release/whisper-rust")): # Check if Rust is installed. Needed to build whisper executable - rust_check = subprocess.call('command -v rustc', shell=True) - if rust_check != 0: + rustc_path = shutil.which('rustc') + if rustc_path is None: print("Rust is not installed or is not in system PATH. Please install Rust before proceeding.") exit(1) - + # Build Whisper Rust executable if not found - subprocess.call('cargo build --release', shell=True) + subprocess.run(['cargo', 'build', '--release'], check=True) else: print("Whisper Rust executable already exists. Skipping build.") WHISPER_MODEL_PATH = os.path.join(service_dir, "model") - + WHISPER_MODEL_NAME = os.getenv('WHISPER_MODEL_NAME', 'ggml-tiny.en.bin') WHISPER_MODEL_URL = os.getenv('WHISPER_MODEL_URL', 'https://huggingface.co/ggerganov/whisper.cpp/resolve/main/') - + if not os.path.isfile(os.path.join(WHISPER_MODEL_PATH, WHISPER_MODEL_NAME)): os.makedirs(WHISPER_MODEL_PATH, exist_ok=True) - subprocess.call(f'curl -L "{WHISPER_MODEL_URL}{WHISPER_MODEL_NAME}" -o "{os.path.join(WHISPER_MODEL_PATH, WHISPER_MODEL_NAME)}"', shell=True) + urllib.request.urlretrieve(f"{WHISPER_MODEL_URL}{WHISPER_MODEL_NAME}", + os.path.join(WHISPER_MODEL_PATH, WHISPER_MODEL_NAME)) else: print("Whisper model already exists. Skipping download.") + def convert_mime_type_to_format(mime_type: str) -> str: if mime_type == "audio/x-wav" or mime_type == "audio/wav": return "wav" @@ -73,6 +76,7 @@ def convert_mime_type_to_format(mime_type: str) -> str: return mime_type + @contextlib.contextmanager def export_audio_to_wav_ffmpeg(audio: bytearray, mime_type: str) -> str: temp_dir = tempfile.gettempdir() @@ -105,10 +109,12 @@ def export_audio_to_wav_ffmpeg(audio: bytearray, mime_type: str) -> str: os.remove(input_path) os.remove(output_path) + def run_command(command): result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) return result.stdout, result.stderr + def get_transcription_file(service_directory, wav_file_path: str): local_path = os.path.join(service_directory, 'model') whisper_rust_path = os.path.join(service_directory, 'whisper-rust', 'target', 'release') @@ -124,14 +130,15 @@ def get_transcription_file(service_directory, wav_file_path: str): def stt_wav(service_directory, wav_file_path: str): - temp_dir = tempfile.gettempdir() - output_path = os.path.join(temp_dir, f"output_stt_{datetime.now().strftime('%Y%m%d%H%M%S%f')}.wav") - ffmpeg.input(wav_file_path).output(output_path, acodec='pcm_s16le', ac=1, ar='16k').run() - try: - transcript = get_transcription_file(service_directory, output_path) - finally: - os.remove(output_path) - return transcript + temp_dir = tempfile.gettempdir() + output_path = os.path.join(temp_dir, f"output_stt_{datetime.now().strftime('%Y%m%d%H%M%S%f')}.wav") + ffmpeg.input(wav_file_path).output(output_path, acodec='pcm_s16le', ac=1, ar='16k').run() + try: + transcript = get_transcription_file(service_directory, output_path) + finally: + os.remove(output_path) + return transcript + def stt(service_directory, input_data): - return stt_wav(service_directory, input_data) \ No newline at end of file + return stt_wav(service_directory, input_data) From aca34eeeceb0f406749e162b52b3ac7c7dc19b03 Mon Sep 17 00:00:00 2001 From: Martin Moehle Date: Wed, 27 Mar 2024 18:28:10 +0100 Subject: [PATCH 3/4] Adjusted piper download link for windows --- software/source/server/services/tts/piper/tts.py | 11 ++++++----- software/source/server/utils/kernel.py | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/software/source/server/services/tts/piper/tts.py b/software/source/server/services/tts/piper/tts.py index a7f91c7..c7e65b0 100644 --- a/software/source/server/services/tts/piper/tts.py +++ b/software/source/server/services/tts/piper/tts.py @@ -49,7 +49,7 @@ class Tts: return elif OS == "Windows": if ARCH == "AMD64": - ARCH = "x64" + ARCH = "amd64" else: print("Piper: unsupported architecture") return @@ -57,14 +57,15 @@ class Tts: PIPER_ASSETNAME = f"piper_{OS}_{ARCH}.tar.gz" PIPER_URL = "https://github.com/rhasspy/piper/releases/latest/download/" - if OS == "windows": - asset_url = f"{PIPER_URL}{PIPER_ASSETNAME}".replace(".tar.gz", ".zip") + asset_url = f"{PIPER_URL}{PIPER_ASSETNAME}" + if OS == "Windows": + asset_url = asset_url.replace(".tar.gz", ".zip") # Download and extract Piper urllib.request.urlretrieve(asset_url, os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME)) # Extract the downloaded file - if OS == "windows": + if OS == "Windows": import zipfile with zipfile.ZipFile(os.path.join(PIPER_FOLDER_PATH, PIPER_ASSETNAME), 'r') as zip_ref: zip_ref.extractall(path=PIPER_FOLDER_PATH) @@ -105,4 +106,4 @@ class Tts: print("Piper setup completed.") else: - print("Piper already set up. Skipping download.") + print("Piper already set up. Skipping download.") \ No newline at end of file diff --git a/software/source/server/utils/kernel.py b/software/source/server/utils/kernel.py index b443bba..5d2f093 100644 --- a/software/source/server/utils/kernel.py +++ b/software/source/server/utils/kernel.py @@ -45,6 +45,10 @@ last_messages = "" def check_filtered_kernel(): messages = get_kernel_messages() + if messages is None: + return "" # Handle unsupported platform or error in fetching kernel messages + + global last_messages messages.replace(last_messages, "") messages = messages.split("\n") From 547c4afb57828f8c36c97b6591c277a167f6ad46 Mon Sep 17 00:00:00 2001 From: Martin Moehle Date: Wed, 27 Mar 2024 18:28:59 +0100 Subject: [PATCH 4/4] Adjusted rust installation check to work with windows --- software/source/server/services/stt/local-whisper/stt.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/software/source/server/services/stt/local-whisper/stt.py b/software/source/server/services/stt/local-whisper/stt.py index b318e8e..4ceaf10 100644 --- a/software/source/server/services/stt/local-whisper/stt.py +++ b/software/source/server/services/stt/local-whisper/stt.py @@ -42,8 +42,8 @@ def install(service_dir): # Check if whisper-rust executable exists before attempting to build if not os.path.isfile(os.path.join(WHISPER_RUST_PATH, "target/release/whisper-rust")): # Check if Rust is installed. Needed to build whisper executable - rust_check = subprocess.call('command -v rustc', shell=True) - if rust_check != 0: + rustc_path = shutil.which("rustc") + if rustc_path is None: print("Rust is not installed or is not in system PATH. Please install Rust before proceeding.") exit(1)