-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reload --vosk-grammar-file JSON on SIGHUP #88
base: main
Are you sure you want to change the base?
Conversation
e180453
to
ebaf203
Compare
fixed merge conflict |
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]>
ebaf203
to
6859662
Compare
Fixed BTW, the diff shown by github isn't pretty because of the indented code (whitespace), but the logic change is small: Here is a diff ignoring whitespace: # git show -w HEAD
diff --git a/nerd-dictation b/nerd-dictation
index e5d8196..c4adc49 100755
--- a/nerd-dictation
+++ b/nerd-dictation
@@ -944,22 +944,30 @@ def text_from_vosk_pipe(
vosk.SetLogLevel(-1)
+ # Allow for loading the model to take some time:
+ if verbose >= 1:
+ sys.stderr.write("Loading model...\n")
+ model = vosk.Model(vosk_model_dir)
+
+ rec = None
+
+ def vosk_load_recognizer() -> None:
+ 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()
- # 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)
+ 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
|
Just realized I still have Scoping nerd-dictation:1052: error: Item "None" of "Optional[Any]" has no attribute "FinalResult"
nerd-dictation:1069: error: Item "None" of "Optional[Any]" has no attribute "PartialResult"
nerd-dictation:1100: error: Item "None" of "Optional[Any]" has no attribute "Reset"
nerd-dictation:1208: error: Item "None" of "Optional[Any]" has no attribute "AcceptWaveform" I tried a few things but I'm not very familiar with Python's typing. For example, changing Do you know what should be done here? |
In this case mypy needs to know that it's not
|
Signed-off-by: Eric Wheeler <[email protected]>
The only reason I set Questions:
|
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]