Skip to content

Commit

Permalink
Reload --vosk-grammar-file JSON on SIGHUP
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Eric Wheeler committed Feb 15, 2023
1 parent 2de40d2 commit ebaf203
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions nerd-dictation
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -1135,6 +1143,7 @@ def text_from_vosk_pipe(
if verbose >= 1:
sys.stderr.write("Reload.\n")
process_fn("")
vosk_load_recognizer()

import signal

Expand Down

0 comments on commit ebaf203

Please sign in to comment.