Feat: add editor server backend
This commit is contained in:
@@ -19,14 +19,25 @@ def cleaned_text_to_sequence(cleaned_text, tones, language):
|
||||
|
||||
|
||||
def get_bert(
|
||||
norm_text, word2ph, language, device, assist_text=None, assist_text_weight=0.7
|
||||
text,
|
||||
word2ph,
|
||||
language,
|
||||
device,
|
||||
assist_text=None,
|
||||
assist_text_weight=0.7,
|
||||
ignore_unknown=False,
|
||||
):
|
||||
from .chinese_bert import get_bert_feature as zh_bert
|
||||
from .english_bert_mock import get_bert_feature as en_bert
|
||||
from .japanese_bert import get_bert_feature as jp_bert
|
||||
|
||||
lang_bert_func_map = {"ZH": zh_bert, "EN": en_bert, "JP": jp_bert}
|
||||
bert = lang_bert_func_map[language](
|
||||
norm_text, word2ph, device, assist_text, assist_text_weight
|
||||
)
|
||||
if language == "JP":
|
||||
bert = lang_bert_func_map[language](
|
||||
text, word2ph, device, assist_text, assist_text_weight, ignore_unknown
|
||||
)
|
||||
else:
|
||||
bert = lang_bert_func_map[language](
|
||||
text, word2ph, device, assist_text, assist_text_weight
|
||||
)
|
||||
return bert
|
||||
|
||||
@@ -4,11 +4,13 @@ from text import chinese, japanese, english, cleaned_text_to_sequence
|
||||
language_module_map = {"ZH": chinese, "JP": japanese, "EN": english}
|
||||
|
||||
|
||||
def clean_text(text, language, use_jp_extra=True):
|
||||
def clean_text(text, language, use_jp_extra=True, ignore_unknown=False):
|
||||
language_module = language_module_map[language]
|
||||
norm_text = language_module.text_normalize(text)
|
||||
if language == "JP":
|
||||
phones, tones, word2ph = language_module.g2p(norm_text, use_jp_extra)
|
||||
phones, tones, word2ph = language_module.g2p(
|
||||
norm_text, use_jp_extra, ignore_unknown=ignore_unknown
|
||||
)
|
||||
else:
|
||||
phones, tones, word2ph = language_module.g2p(norm_text)
|
||||
return norm_text, phones, tones, word2ph
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
# compatible with Julius https://github.com/julius-speech/segmentation-kit
|
||||
import re
|
||||
import unicodedata
|
||||
from pathlib import Path
|
||||
|
||||
import pyopenjtalk
|
||||
from num2words import num2words
|
||||
from transformers import AutoTokenizer
|
||||
|
||||
from common.constants import USER_DICT_PATH, USER_DICT_CSV_PATH
|
||||
from common.log import logger
|
||||
from text import punctuation
|
||||
from text.japanese_mora_list import (
|
||||
@@ -14,6 +16,11 @@ from text.japanese_mora_list import (
|
||||
mora_phonemes_to_mora_kata,
|
||||
)
|
||||
|
||||
if Path(USER_DICT_CSV_PATH).exists():
|
||||
pyopenjtalk.mecab_dict_index(USER_DICT_CSV_PATH, USER_DICT_PATH)
|
||||
if Path(USER_DICT_PATH).exists():
|
||||
pyopenjtalk.update_global_jtalk_with_user_dict(USER_DICT_PATH)
|
||||
|
||||
# 子音の集合
|
||||
COSONANTS = set(
|
||||
[
|
||||
@@ -160,7 +167,7 @@ def japanese_convert_numbers_to_words(text: str) -> str:
|
||||
|
||||
|
||||
def g2p(
|
||||
norm_text: str, use_jp_extra: bool = True
|
||||
norm_text: str, use_jp_extra: bool = True, ignore_unknown: bool = False
|
||||
) -> tuple[list[str], list[int], list[int]]:
|
||||
"""
|
||||
他で使われるメインの関数。`text_normalize()`で正規化された`norm_text`を受け取り、
|
||||
@@ -182,7 +189,7 @@ def g2p(
|
||||
|
||||
# sep_text: 単語単位の単語のリスト
|
||||
# sep_kata: 単語単位の単語のカタカナ読みのリスト
|
||||
sep_text, sep_kata = text2sep_kata(norm_text)
|
||||
sep_text, sep_kata = text2sep_kata(norm_text, ignore_unknown=ignore_unknown)
|
||||
|
||||
# sep_phonemes: 各単語ごとの音素のリストのリスト
|
||||
sep_phonemes = handle_long([kata2phoneme_list(i) for i in sep_kata])
|
||||
@@ -231,8 +238,8 @@ def g2p(
|
||||
return phones, tones, word2ph
|
||||
|
||||
|
||||
def g2kata_tone(norm_text: str) -> list[tuple[str, int]]:
|
||||
phones, tones, _ = g2p(norm_text, use_jp_extra=True)
|
||||
def g2kata_tone(norm_text: str, ignore_unknown: bool = False) -> list[tuple[str, int]]:
|
||||
phones, tones, _ = g2p(norm_text, use_jp_extra=True, ignore_unknown=ignore_unknown)
|
||||
return phone_tone2kata_tone(list(zip(phones, tones)))
|
||||
|
||||
|
||||
@@ -325,7 +332,9 @@ def g2phone_tone_wo_punct(text: str) -> list[tuple[str, int]]:
|
||||
return result
|
||||
|
||||
|
||||
def text2sep_kata(norm_text: str) -> tuple[list[str], list[str]]:
|
||||
def text2sep_kata(
|
||||
norm_text: str, ignore_unknown: bool = False
|
||||
) -> tuple[list[str], list[str]]:
|
||||
"""
|
||||
`text_normalize`で正規化済みの`norm_text`を受け取り、それを単語分割し、
|
||||
分割された単語リストとその読み(カタカナor記号1文字)のリストのタプルを返す。
|
||||
@@ -361,6 +370,9 @@ def text2sep_kata(norm_text: str) -> tuple[list[str], list[str]]:
|
||||
# wordは正規化されているので、`.`, `,`, `!`, `'`, `-`, `--` のいずれか
|
||||
if not set(word).issubset(set(punctuation)): # 記号繰り返しか判定
|
||||
# ここはpyopenjtalkが読めない文字等のときに起こる
|
||||
if ignore_unknown:
|
||||
logger.error(f"Ignoring unknown: {word} in:\n{norm_text}")
|
||||
continue
|
||||
raise ValueError(f"Cannot read: {word} in:\n{norm_text}")
|
||||
# yomiは元の記号のままに変更
|
||||
yomi = word
|
||||
@@ -500,6 +512,9 @@ def handle_long(sep_phonemes: list[list[str]]) -> list[list[str]]:
|
||||
おそらく長音記号とダッシュを勘違いしていると思われるので、ダッシュに対応する音素`-`に変換する。
|
||||
"""
|
||||
for i in range(len(sep_phonemes)):
|
||||
if len(sep_phonemes[i]) == 0:
|
||||
# 空白文字等でリストが空の場合
|
||||
continue
|
||||
if sep_phonemes[i][0] == "ー":
|
||||
if i != 0:
|
||||
prev_phoneme = sep_phonemes[i - 1][-1]
|
||||
|
||||
@@ -4,7 +4,7 @@ import torch
|
||||
from transformers import AutoModelForMaskedLM, AutoTokenizer
|
||||
|
||||
from config import config
|
||||
from text.japanese import text2sep_kata
|
||||
from text.japanese import text2sep_kata, text_normalize
|
||||
|
||||
LOCAL_PATH = "./bert/deberta-v2-large-japanese-char-wwm"
|
||||
|
||||
@@ -19,8 +19,10 @@ def get_bert_feature(
|
||||
device=config.bert_gen_config.device,
|
||||
assist_text=None,
|
||||
assist_text_weight=0.7,
|
||||
ignore_unknown=False,
|
||||
):
|
||||
text = "".join(text2sep_kata(text)[0])
|
||||
text = "".join(text2sep_kata(text, ignore_unknown=ignore_unknown)[0])
|
||||
# text = text_normalize(text)
|
||||
if assist_text:
|
||||
assist_text = "".join(text2sep_kata(assist_text)[0])
|
||||
if (
|
||||
|
||||
Reference in New Issue
Block a user