-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: add support for dynamic module loading
- Loading branch information
Showing
4 changed files
with
57 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import importlib | ||
|
||
|
||
class BertHandler: | ||
_bert_functions = {} | ||
|
||
BERT_IMPORT_MAP = { | ||
"zh": "bert_vits2.text.chinese_bert.get_bert_feature", | ||
"en": "bert_vits2.text.english_bert_mock.get_bert_feature", | ||
"ja": "bert_vits2.text.japanese_bert.get_bert_feature", | ||
} | ||
|
||
def __init__(self, languages): | ||
for lang in languages: | ||
if lang not in BertHandler._bert_functions: | ||
self.load_bert_function(lang) | ||
|
||
def load_bert_function(self, language): | ||
if language not in BertHandler.BERT_IMPORT_MAP: | ||
raise ValueError(f"Unsupported language: {language}") | ||
|
||
module_path, function_name = BertHandler.BERT_IMPORT_MAP[language].rsplit('.', 1) | ||
module = importlib.import_module(module_path, package=__package__) | ||
bert_function = getattr(module, function_name) | ||
|
||
BertHandler._bert_functions[language] = bert_function | ||
|
||
def get_bert(self, norm_text, word2ph, language): | ||
if language not in BertHandler._bert_functions: | ||
raise ValueError(f"BERT for {language} has not been initialized. Please initialize first.") | ||
|
||
bert_func = BertHandler._bert_functions[language] | ||
return bert_func(norm_text, word2ph) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters