Skip to content

Commit

Permalink
Update: Support for limiting language range in GPT-SoVITS
Browse files Browse the repository at this point in the history
  • Loading branch information
Artrajz committed Jul 19, 2024
1 parent 253faa7 commit 53925de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
18 changes: 10 additions & 8 deletions gpt_sovits/gpt_sovits.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,13 @@ def get_bert_feature(self, text, phones, word2ph, language):

return bert

def get_bert_and_cleaned_text_multilang(self, text: list):
sentences = split_languages(text, expand_abbreviations=True, expand_hyphens=True)
def get_bert_and_cleaned_text_multilang(self, text: str, lang_list: list):
target_languages = lang_list
if len(lang_list) == 1 and lang_list[0] == "auto":
target_languages = self.lang

sentences = split_languages(text, expand_abbreviations=True, expand_hyphens=True,
target_languages=target_languages)

phones_list, word2ph_list, norm_text_list, bert_list = [], [], [], []

Expand Down Expand Up @@ -253,8 +258,9 @@ def get_first(self, text):
text = re.split(pattern, text)[0].strip()
return text

def preprocess_text(self, text: str, lang: str, segment_size: int):
def preprocess_text(self, text: str, lang_list: list, segment_size: int):
texts = sentence_split(text, segment_size)
lang = lang_list[0] # main language

result = []
for text in texts:
Expand All @@ -264,11 +270,7 @@ def preprocess_text(self, text: str, lang: str, segment_size: int):
if (text[-1] not in splits):
text += "。" if lang != "en" else "."

if lang == "auto":
phones, word2ph, norm_text, bert_features = self.get_bert_and_cleaned_text_multilang(text)
else:
phones, word2ph, norm_text = self.get_cleaned_text(text, lang)
bert_features = self.get_bert_feature(text, phones, word2ph, norm_text).to(self.device)
phones, word2ph, norm_text, bert_features = self.get_bert_and_cleaned_text_multilang(text, lang_list)

res = {
"phones": phones,
Expand Down
15 changes: 12 additions & 3 deletions tts_app/voice_api/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import os
import re
import time
import uuid
from io import BytesIO
Expand Down Expand Up @@ -605,10 +606,18 @@ def voice_gpt_sovits_api():
logger.info(f"[{ModelType.GPT_SOVITS.value}] speaker id {id} does not exist")
return make_response(jsonify({"status": "error", "message": f"id {id} does not exist"}), 400)

lang_list = re.split(r'[,,\s]+', lang)
# 校验模型是否支持输入的语言
speaker_lang = model_manager.voice_speakers[ModelType.GPT_SOVITS.value][id].get('lang')
if lang not in ["auto", "mix"] and len(speaker_lang) > 1 and lang not in speaker_lang:
logger.info(f"[{ModelType.GPT_SOVITS.value}] lang \"{lang}\" is not in {speaker_lang}")
for idx in range(len(lang_list)):
lang_list[idx] = lang_list[idx].lower()
lang = lang_list[idx]
if lang not in ["auto", "mix"] and len(speaker_lang) > 1 and lang not in speaker_lang:
logger.info(f"[{ModelType.GPT_SOVITS.value}] lang \"{lang}\" is not in {speaker_lang}")
return make_response(jsonify({"status": "error", "message": f"lang '{lang}' is not in {speaker_lang}"}),
400)

if "auto" in lang_list and len(lang_list) > 1:
return make_response(jsonify({"status": "error", "message": f"lang '{lang}' is not in {speaker_lang}"}),
400)

Expand All @@ -629,7 +638,7 @@ def voice_gpt_sovits_api():
"id": id,
"format": format,
"segment_size": segment_size,
"lang": lang,
"lang": lang_list,
"speaker_lang": speaker_lang,
"reference_audio": reference_audio,
# "reference_audio_sr": reference_audio_sr,
Expand Down

0 comments on commit 53925de

Please sign in to comment.