From ebaf203071b708f79d8046098b30ab34abf676f9 Mon Sep 17 00:00:00 2001 From: Eric Wheeler Date: Thu, 9 Feb 2023 14:00:59 -0800 Subject: [PATCH] Reload --vosk-grammar-file JSON on SIGHUP This commit adds support for reloading the grammar configuration on SIGHUP. This is accomplished by moving the recognizer initialization into its own function so that the signal handler can call it on reload. A reload of the recognizer is fast because it does not reload the model and only applies the grammar. Signed-off-by: Eric Wheeler --- nerd-dictation | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/nerd-dictation b/nerd-dictation index e5d8196..2d62e75 100755 --- a/nerd-dictation +++ b/nerd-dictation @@ -944,21 +944,29 @@ def text_from_vosk_pipe( vosk.SetLogLevel(-1) - if not vosk_grammar_file: - grammar_json = "" - else: - with open(vosk_grammar_file, encoding="utf-8") as fh: - grammar_json = fh.read() - # Allow for loading the model to take some time: if verbose >= 1: sys.stderr.write("Loading model...\n") model = vosk.Model(vosk_model_dir) - if grammar_json == "": - rec = vosk.KaldiRecognizer(model, sample_rate) - else: - rec = vosk.KaldiRecognizer(model, sample_rate, grammar_json) + rec = None + + def vosk_load_recognizer(): + nonlocal vosk_grammar_file + nonlocal rec + + if not vosk_grammar_file: + grammar_json = "" + else: + with open(vosk_grammar_file, encoding="utf-8") as fh: + grammar_json = fh.read() + + if grammar_json == "": + rec = vosk.KaldiRecognizer(model, sample_rate) + else: + rec = vosk.KaldiRecognizer(model, sample_rate, grammar_json) + + vosk_load_recognizer() if verbose >= 1: sys.stderr.write("Model loaded.\n") @@ -1135,6 +1143,7 @@ def text_from_vosk_pipe( if verbose >= 1: sys.stderr.write("Reload.\n") process_fn("") + vosk_load_recognizer() import signal