From a54f9cbed78ce3df4252c209553f4f318bc2b81e Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Wed, 20 Nov 2024 13:46:44 +0100 Subject: cleanup pipo when exiting --- __main__.py | 31 ++++++++++++++++++------------- modules/input_handlers/pipewire_record.py | 11 +++++++---- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/__main__.py b/__main__.py index 4aa3117..b51e70b 100644 --- a/__main__.py +++ b/__main__.py @@ -3,12 +3,13 @@ import sys import yaml from pathlib import Path + from .modules.config import load_config from .modules.hassil.recognize import recognize from .modules.hassil.util import merge_dict from .modules.hassil.intents import Intents, TextSlotList from .modules.input_handlers.stdin_input import get_input_stdin -from .modules.input_handlers.pipewire_record import get_input_pw_record +from .modules.input_handlers.pipewire_record import get_input_pw_record, cleanup from .modules.intents import * @@ -39,18 +40,22 @@ def main(): intents = Intents.from_dict(input_dict) - for input_text in get_input_pw_record(): - result = recognize(input_text, intents, slot_lists=slot_lists) - if result is not None: - result_dict = { - "intent": result.intent.name, - **{e.name: e.value for e in result.entities_list}, - } - print(result_dict) - handler = getattr(globals()[result_dict["domain"]], result_dict["intent"]) - handler(result_dict, config) - else: - print("") + try: + # TODO select input type from config + for input_text in get_input_pw_record(): + result = recognize(input_text, intents, slot_lists=slot_lists) + if result is not None: + result_dict = { + "intent": result.intent.name, + **{e.name: e.value for e in result.entities_list}, + } + print(result_dict) + handler = getattr(globals()[result_dict["domain"]], result_dict["intent"]) + handler(result_dict, config) + else: + print("") + finally: + cleanup() if __name__ == '__main__': main() \ No newline at end of file diff --git a/modules/input_handlers/pipewire_record.py b/modules/input_handlers/pipewire_record.py index 8584ad3..147d77b 100644 --- a/modules/input_handlers/pipewire_record.py +++ b/modules/input_handlers/pipewire_record.py @@ -9,12 +9,15 @@ import whisper FIFO_PATH = "/tmp/hestia-listening" RECORD_PATH = "/tmp/hestia-record.mp3" -def get_input_pw_record(): - device = get_device() - +def cleanup(): if os.path.exists(FIFO_PATH): os.remove(FIFO_PATH) + +def get_input_pw_record(): + device = get_device() + + cleanup() os.mkfifo(FIFO_PATH) while True: @@ -33,7 +36,7 @@ def get_input_pw_record(): ps.kill() # TODO "error" # TODO exit gracefully or try to recover - sys.exit() + raise StopIteration model = whisper.load_model("base") -- cgit v1.2.3-70-g09d2