Xinonria commited on
Commit
b818867
1 Parent(s): 8358abb

更新长度检测

Browse files
Files changed (2) hide show
  1. app.py +14 -7
  2. utils.py +22 -0
app.py CHANGED
@@ -9,6 +9,7 @@ from pypinyin import lazy_pinyin
9
  from gradio_i18n import gettext, Translate
10
 
11
  from api import generate_api, get_audio
 
12
 
13
  # 翻译文件位置
14
  trans_file = os.path.join(os.path.dirname(__file__),"i18n", "translations.json")
@@ -104,15 +105,15 @@ async def generate(selected_character = None, selected_characters = [], text = "
104
  elif lang == "ko":
105
  raise gr.Error("합성할 텍스트를 입력하세요")
106
 
107
- if (lang == "en" and len(text.split()) > 200) or len(text) > 512:
108
  if lang == "zh":
109
- raise gr.Error("长度请控制在512个字符以内")
110
  elif lang == "en":
111
- raise gr.Error("The text length exceeds 200 words")
112
  elif lang == "ja":
113
- raise gr.Error("テキストの長さが512文字を超えています")
114
  elif lang == "ko":
115
- raise gr.Error("텍스트 길이가 512자를 초과합니다")
116
 
117
  audio = await generate_api(voice_ids, text)
118
  end_time = time.time()
@@ -218,8 +219,14 @@ def on_select(evt: gr.SelectData, characters, selected_characters, all_character
218
 
219
  selected = characters[evt.index]
220
  emotions = get_character_emotions(selected, all_characters)
221
- default_emotion = emotions[0]["情绪"] if emotions else ""
222
- default_voice_id = emotions[0]["voice_id"] if emotions else ""
 
 
 
 
 
 
223
 
224
  character_dict = selected.copy()
225
  character_dict['情绪'] = default_emotion
 
9
  from gradio_i18n import gettext, Translate
10
 
11
  from api import generate_api, get_audio
12
+ from utils import get_length
13
 
14
  # 翻译文件位置
15
  trans_file = os.path.join(os.path.dirname(__file__),"i18n", "translations.json")
 
105
  elif lang == "ko":
106
  raise gr.Error("합성할 텍스트를 입력하세요")
107
 
108
+ if get_length(text) > 1024:
109
  if lang == "zh":
110
+ raise gr.Error("长度请控制在1024个字符以内")
111
  elif lang == "en":
112
+ raise gr.Error("The text length exceeds 1024 words")
113
  elif lang == "ja":
114
+ raise gr.Error("テキストの長さが1024文字を超えています")
115
  elif lang == "ko":
116
+ raise gr.Error("텍스트 길이가 1024자를 초과합니다")
117
 
118
  audio = await generate_api(voice_ids, text)
119
  end_time = time.time()
 
219
 
220
  selected = characters[evt.index]
221
  emotions = get_character_emotions(selected, all_characters)
222
+ normal_index = 0
223
+ for index, emotion in enumerate(emotions):
224
+ if emotion["情绪"] == "正常" or emotion["情绪"] == "보통" or emotion["情绪"] == "normal":
225
+ normal_index = index
226
+ break
227
+
228
+ default_emotion = emotions[normal_index]["情绪"] if emotions else ""
229
+ default_voice_id = emotions[normal_index]["voice_id"] if emotions else ""
230
 
231
  character_dict = selected.copy()
232
  character_dict['情绪'] = default_emotion
utils.py CHANGED
@@ -1,6 +1,7 @@
1
  import io
2
  import os
3
  import pickle
 
4
 
5
  import soundfile as sf
6
  import numpy as np
@@ -37,5 +38,26 @@ def normalize_audio_loudness(data: bytes, target_loudness: float = -23.0) -> byt
37
 
38
  return normalized_audio_bytes
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  if __name__ == "__main__":
41
  normalize_audio_loudness()
 
1
  import io
2
  import os
3
  import pickle
4
+ import re
5
 
6
  import soundfile as sf
7
  import numpy as np
 
38
 
39
  return normalized_audio_bytes
40
 
41
+ def get_length(text: str) -> float:
42
+ def calculate_string_length(text: str) -> float:
43
+ def split_into_words(s: str) -> list[str]:
44
+ return re.findall(r"\b\w+\b|[^\w\s]|\s+", s)
45
+
46
+ def calculate_effective_length(words: list[str]) -> float:
47
+ length = 0
48
+ for word in words:
49
+ if re.match(r"^[\u4e00-\u9fff\u3040-\u30ff\u3400-\u4dbf]+$", word):
50
+ length += len(word)
51
+ elif re.match(r"^\w+$", word):
52
+ length += 1
53
+ else:
54
+ length += len(word) * 0.5
55
+ return length
56
+
57
+ words = split_into_words(text)
58
+ return calculate_effective_length(words)
59
+
60
+ return calculate_string_length(text)
61
+
62
  if __name__ == "__main__":
63
  normalize_audio_loudness()