daanzu / kaldi-active-grammar

Python Kaldi speech recognition with grammars that can be set active/inactive dynamically at decode-time
GNU Affero General Public License v3.0
336 stars 50 forks source link

Whitespace in model path #54

Closed etfre closed 2 years ago

etfre commented 3 years ago

I'm running into the following error when I try to use a model that contains a whitespace in its path:

[StardewSpeak] Speech engine error: ERR:     self._compiler = KaldiCompiler(self._options['model_dir'], tmp_dir=self._options['tmp_dir'],
[StardewSpeak] Speech engine error: ERR:   File "C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley\Mods\StardewSpeak\StardewSpeak\lib\speech-client\lib\site-packages\dragonfly\engines\backend_kaldi\compiler.py", line 70, in __init__
[StardewSpeak] Speech engine error: ERR:     KaldiAGCompiler.__init__(self, model_dir=model_dir, tmp_dir=tmp_dir, **kwargs)
[StardewSpeak] Speech engine error: ERR:   File "C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley\Mods\StardewSpeak\StardewSpeak\lib\speech-client\lib\site-packages\kaldi_active_grammar\compiler.py", line 201, in __init__
[StardewSpeak] Speech engine error: ERR:     self.model = Model(model_dir, tmp_dir)
[StardewSpeak] Speech engine error: ERR:   File "C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley\Mods\StardewSpeak\StardewSpeak\lib\speech-client\lib\site-packages\kaldi_active_grammar\model.py", line 221, in __init__
[StardewSpeak] Speech engine error: ERR:     self.phone_to_int_dict = { phone: i for phone, i in load_symbol_table(self.files_dict['phones.txt']) }
[StardewSpeak] Speech engine error: ERR:   File "C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley\Mods\StardewSpeak\StardewSpeak\lib\speech-client\lib\site-packages\kaldi_active_grammar\utils.py", line 187, in load_symbol_table
[StardewSpeak] Speech engine error: ERR:     with open(filename, 'r', encoding='utf-8') as f:
[StardewSpeak] Speech engine error: ERR: OSError: [Errno 22] Invalid argument: '"C:\\Users\\evfre\\.stardew speak\\models\\kaldi_model\\phones.txt"'

However, when I comment out the line self.files_dict.update({ k: '"%s"' % v for (k, v) in self.files_dict.items() if v and ' ' in v }) in the Model constructor, everything works as expected.

daanzu commented 3 years ago

Good find! However, I think this may now be moot in the latest versions, which don't need to use the command line utilities. Can you give it a try?

Relatedly, I am working on improving freezing. I haven't tried cx_freeze, but I have had success with using winpython.

etfre commented 3 years ago

Sorry for the late response. I can reproduce this in 2.1.0 as well, even without freezing:

File "C:\Program Files (x86)\GOG Galaxy\Games\Stardew Valley\Mods\StardewSpeak\StardewSpeak\lib\speech-client\lib\site-packages\kaldi_active_grammar\utils.py", line 194, in load_symbol_table
    with open(filename, 'r', encoding='utf-8') as f:
OSError: [Errno 22] Invalid argument: '"C:\\Program Files (x86)\\GOG Galaxy\\Games\\Stardew Valley\\Mods\\StardewSpeak\\StardewSpeak\\lib\\speech-client\\models\\kaldi_model\\phones.txt"'

Removing the above line in the Model __init__ method still fixes the issue for me.

daanzu commented 3 years ago

@evfredericksen Hmm, strange. Thanks for the report and retest! I will try to figure out what is causing it.

etfre commented 3 years ago

No problem. I don't have context for why the line was added originally, but I would think that the quotation marks around the path are unnecessary as Python should be able to handle whitespace in paths without any extra logic.

https://stackoverflow.com/questions/14852140/whitespaces-in-the-path-of-windows-filepath

Sample script:

import os
path_with_whitespace = os.path.join(os.getcwd(), "text.txt")
print(path_with_whitespace)
with open(path_with_whitespace) as f:
    pass
with open(f'"{path_with_whitespace}"') as f:
    pass

Result:

C:\Users\evfre\wp\test path\text.txt
Traceback (most recent call last):
  File "r.py", line 6, in <module>
    with open(f'"{path_with_whitespace}"') as f:
OSError: [Errno 22] Invalid argument: '"C:\\Users\\evfre\\wp\\test path\\text.txt"'