@@ -30,7 +30,7 @@ def __init__(
30
30
model_path : str ,
31
31
voices_path : str ,
32
32
espeak_config : EspeakConfig | None = None ,
33
- vocab_config : dict | str = None ,
33
+ vocab_config : dict | str | None = None ,
34
34
):
35
35
# Show useful information for bug reports
36
36
log .debug (
@@ -56,15 +56,7 @@ def __init__(
56
56
self .sess = rt .InferenceSession (model_path , providers = providers )
57
57
self .voices : np .ndarray = np .load (voices_path )
58
58
59
- vocab = None
60
-
61
- if isinstance (vocab_config , str ):
62
- with open (vocab_config , "r" , encoding = "utf-8" ) as fp :
63
- config = json .load (fp )
64
- vocab = config ["vocab" ]
65
- elif isinstance (vocab , dict ):
66
- vocab = vocab ["vocab" ]
67
-
59
+ vocab = self ._load_vocab (vocab_config )
68
60
self .tokenizer = Tokenizer (espeak_config , vocab = vocab )
69
61
70
62
@classmethod
@@ -73,15 +65,36 @@ def from_session(
73
65
session : rt .InferenceSession ,
74
66
voices_path : str ,
75
67
espeak_config : EspeakConfig | None = None ,
68
+ vocab_config : dict | str | None = None ,
76
69
):
77
70
instance = cls .__new__ (cls )
78
71
instance .sess = session
79
72
instance .config = KoKoroConfig (session ._model_path , voices_path , espeak_config )
80
73
instance .config .validate ()
81
74
instance .voices = np .load (voices_path )
82
- instance .tokenizer = Tokenizer (espeak_config )
75
+
76
+ vocab = instance ._load_vocab (vocab_config )
77
+ instance .tokenizer = Tokenizer (espeak_config , vocab = vocab )
83
78
return instance
84
79
80
+ def _load_vocab (self , vocab_config : dict | str | None ) -> dict :
81
+ """Load vocabulary from config file or dictionary.
82
+
83
+ Args:
84
+ vocab_config: Path to vocab config file or dictionary containing vocab.
85
+
86
+ Returns:
87
+ Loaded vocabulary dictionary or empty dictionary if no config provided.
88
+ """
89
+
90
+ if isinstance (vocab_config , str ):
91
+ with open (vocab_config , "r" , encoding = "utf-8" ) as fp :
92
+ config = json .load (fp )
93
+ return config ["vocab" ]
94
+ if isinstance (vocab_config , dict ):
95
+ return vocab_config ["vocab" ]
96
+ return {}
97
+
85
98
def _create_audio (
86
99
self , phonemes : str , voice : NDArray [np .float32 ], speed : float
87
100
) -> tuple [NDArray [np .float32 ], int ]:
0 commit comments