MZhaovo commited on
Commit
5df1b32
1 Parent(s): 36416cb

Upload folder using huggingface_hub

Browse files
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import nltk
2
  import ssl
3
 
@@ -27,11 +28,9 @@ device = config.webui_config.device
27
  if device == "mps":
28
  os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
29
 
30
-
31
  def speak_fn(
32
  text: str,
33
  exceed_flag,
34
- bubble_text,
35
  speaker="TalkFlower_CNzh",
36
  sdp_ratio=0.2, # SDP/DP混合比
37
  noise_scale=0.6, # 感情
@@ -45,11 +44,12 @@ def speak_fn(
45
  text = text.replace("\n\n", "\n")
46
  if len(text) > 100:
47
  print(f"Too Long Text: {text}")
48
- gr.Warning("Too long! No more than 100 characters. 一口气不要超过 100 个字,憋坏我了。")
49
  if exceed_flag:
50
- return gr.update(value="./assets/audios/nomorethan100.wav", autoplay=True), False, "不要超过100个字!"
 
51
  else:
52
- return gr.update(value="./assets/audios/overlength.wav", autoplay=True), True, "这句太长啦,憋坏我了!"
 
53
  audio_list = []
54
  if len(text) > 42:
55
  print(f"Long Text: {text}")
@@ -74,13 +74,9 @@ def speak_fn(
74
  silence = np.zeros((int)(44100 * interval_between_sent))
75
  audio_list_sent.append(silence)
76
  if (interval_between_para - interval_between_sent) > 0:
77
- silence = np.zeros(
78
- (int)(44100 * (interval_between_para - interval_between_sent))
79
- )
80
  audio_list_sent.append(silence)
81
- audio16bit = gr.processing_utils.convert_to_16_bit_wav(
82
- np.concatenate(audio_list_sent)
83
- ) # 对完整句子做音量归一
84
  audio_list.append(audio16bit)
85
  else:
86
  print(f"Short Text: {text}")
@@ -104,17 +100,29 @@ def speak_fn(
104
  audio_list.append(silence) # 将静音添加到列表中
105
 
106
  audio_concat = np.concatenate(audio_list)
107
- return (hps.data.sampling_rate, audio_concat), exceed_flag, text
108
 
109
 
110
- def update_bubble_fn(text):
111
- return gr.update(value=get_character_html(text))
112
 
113
 
114
  def init_fn():
115
- gr.Info("2023-11-23: 有什么建议可以在 Community 中提哦。")
116
  gr.Info("2023-11-24: 优化长句生成效果;增加示例;更新了一些小彩蛋;画了一些大饼)")
117
- gr.Info("Only support Chinese now. Trying to train a mutilingual model.")
 
 
 
 
 
 
 
 
 
 
 
 
 
118
 
119
 
120
  with open("./css/style.css", "r", encoding="utf-8") as f:
@@ -122,8 +130,8 @@ with open("./css/style.css", "r", encoding="utf-8") as f:
122
 
123
  with gr.Blocks(css=customCSS) as demo:
124
  exceed_flag = gr.State(value=False)
125
- bubble_text = gr.State(value="你好呀!")
126
- character_area = gr.HTML(get_character_html("你好呀!"), elem_id="character_area")
127
  with gr.Tab("Speak", elem_id="tab-speak"):
128
  speak_input = gr.Textbox(lines=1, label="Talking Flower will say:", elem_classes="wonder-card", elem_id="input_text")
129
  speak_button = gr.Button("Speak!", elem_id="speak_button", elem_classes="main-button wonder-card")
@@ -136,22 +144,21 @@ with gr.Blocks(css=customCSS) as demo:
136
  mimic_button = gr.Button("Mimic!", elem_id="mimic_button", elem_classes="main-button wonder-card")
137
  audio_output = gr.Audio(label="输出音频", show_label=False, autoplay=True, elem_id="audio_output", elem_classes="wonder-card")
138
 
139
-
140
  demo.load(
141
  init_fn,
142
  inputs=[],
143
- outputs=[]
144
  )
145
- speak_input.submit(
146
  speak_fn,
147
- inputs=[speak_input, exceed_flag, bubble_text],
148
- outputs=[audio_output, exceed_flag, bubble_text],
149
- ).then(update_bubble_fn, inputs=[speak_input], outputs=[character_area], show_progress=False)
150
- speak_button.click(
151
  speak_fn,
152
- inputs=[speak_input, exceed_flag, bubble_text],
153
- outputs=[audio_output, exceed_flag, bubble_text],
154
- ).then(update_bubble_fn, inputs=[speak_input], outputs=[character_area], show_progress=False)
155
 
156
 
157
  if __name__ == "__main__":
 
1
+ import random
2
  import nltk
3
  import ssl
4
 
 
28
  if device == "mps":
29
  os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
30
 
 
31
  def speak_fn(
32
  text: str,
33
  exceed_flag,
 
34
  speaker="TalkFlower_CNzh",
35
  sdp_ratio=0.2, # SDP/DP混合比
36
  noise_scale=0.6, # 感情
 
44
  text = text.replace("\n\n", "\n")
45
  if len(text) > 100:
46
  print(f"Too Long Text: {text}")
 
47
  if exceed_flag:
48
+ text = "不要超过100字!"
49
+ return gr.update(value="./assets/audios/nomorethan100.wav"), get_character_html(text), False, gr.update(interactive=True)
50
  else:
51
+ text = "这句太长了,憋坏我啦!"
52
+ return gr.update(value="./assets/audios/overlength.wav"), get_character_html(text), True, gr.update(interactive=True)
53
  audio_list = []
54
  if len(text) > 42:
55
  print(f"Long Text: {text}")
 
74
  silence = np.zeros((int)(44100 * interval_between_sent))
75
  audio_list_sent.append(silence)
76
  if (interval_between_para - interval_between_sent) > 0:
77
+ silence = np.zeros((int)(44100 * (interval_between_para - interval_between_sent)))
 
 
78
  audio_list_sent.append(silence)
79
+ audio16bit = gr.processing_utils.convert_to_16_bit_wav(np.concatenate(audio_list_sent)) # 对完整句子做音量归一
 
 
80
  audio_list.append(audio16bit)
81
  else:
82
  print(f"Short Text: {text}")
 
100
  audio_list.append(silence) # 将静音添加到列表中
101
 
102
  audio_concat = np.concatenate(audio_list)
103
+ return (hps.data.sampling_rate, audio_concat), get_character_html(text), exceed_flag, gr.update(interactive=True)
104
 
105
 
106
+ def submit_lock_fn():
107
+ return gr.update(interactive=False)
108
 
109
 
110
  def init_fn():
 
111
  gr.Info("2023-11-24: 优化长句生成效果;增加示例;更新了一些小彩蛋;画了一些大饼)")
112
+ gr.Info("Only support Chinese now. Trying to train a mutilingual model. 欢迎在 Community 中提建议~")
113
+
114
+ index = random.randint(1,7)
115
+ welcome_dict = {
116
+ 1: "哇!你来找我玩啦!",
117
+ 2: "你好呀!",
118
+ 3: "哇!你来啦~",
119
+ 4: "真高兴见到你!",
120
+ 5: "我一朵花好无聊啊!",
121
+ 6: "欢迎你!",
122
+ 7: "来找我玩啦!",
123
+ }
124
+
125
+ return gr.update(value=f"./assets/audios/Welcome{index}.wav"), get_character_html(welcome_dict[index])
126
 
127
 
128
  with open("./css/style.css", "r", encoding="utf-8") as f:
 
130
 
131
  with gr.Blocks(css=customCSS) as demo:
132
  exceed_flag = gr.State(value=False)
133
+ bubble_text = gr.State(value="感谢你来找我玩!")
134
+ character_area = gr.HTML(get_character_html("感谢你来找我玩!"), elem_id="character_area")
135
  with gr.Tab("Speak", elem_id="tab-speak"):
136
  speak_input = gr.Textbox(lines=1, label="Talking Flower will say:", elem_classes="wonder-card", elem_id="input_text")
137
  speak_button = gr.Button("Speak!", elem_id="speak_button", elem_classes="main-button wonder-card")
 
144
  mimic_button = gr.Button("Mimic!", elem_id="mimic_button", elem_classes="main-button wonder-card")
145
  audio_output = gr.Audio(label="输出音频", show_label=False, autoplay=True, elem_id="audio_output", elem_classes="wonder-card")
146
 
 
147
  demo.load(
148
  init_fn,
149
  inputs=[],
150
+ outputs=[audio_output, character_area]
151
  )
152
+ speak_input.submit(submit_lock_fn, show_progress=False).then(
153
  speak_fn,
154
+ inputs=[speak_input, exceed_flag],
155
+ outputs=[audio_output, character_area, exceed_flag, speak_button],
156
+ )
157
+ speak_button.click(submit_lock_fn, show_progress=False).then(
158
  speak_fn,
159
+ inputs=[speak_input, exceed_flag],
160
+ outputs=[audio_output, character_area, exceed_flag, speak_button],
161
+ )
162
 
163
 
164
  if __name__ == "__main__":
assets/audios/Welcome1.wav ADDED
Binary file (222 kB). View file
 
assets/audios/Welcome2.wav ADDED
Binary file (113 kB). View file
 
assets/audios/Welcome3.wav ADDED
Binary file (174 kB). View file
 
assets/audios/Welcome4.wav ADDED
Binary file (155 kB). View file
 
assets/audios/Welcome5.wav ADDED
Binary file (184 kB). View file
 
assets/audios/Welcome6.wav ADDED
Binary file (109 kB). View file
 
assets/audios/Welcome7.wav ADDED
Binary file (149 kB). View file
 
css/style.css CHANGED
@@ -67,6 +67,12 @@ gradio-app {
67
  width: var(--card-width) !important;
68
  align-self: center;
69
  }
 
 
 
 
 
 
70
  .character {
71
  display: flex;
72
  flex-direction: row;
@@ -191,6 +197,9 @@ gradio-app {
191
  #input_text label textarea {
192
  width: 80%;
193
  }
 
 
 
194
  #input_text {
195
  display: flex;
196
  background: #ffffff !important;
@@ -231,6 +240,10 @@ gradio-app {
231
 
232
  transform: translate(-2px,-2px) !important;
233
  }
 
 
 
 
234
  #speak_button {
235
  background: var(--color-yellow) !important;
236
  color: var(--color-accent-black) !important;
 
67
  width: var(--card-width) !important;
68
  align-self: center;
69
  }
70
+ #character_area .pending {
71
+ opacity: 1 !important;
72
+ }
73
+ #character_area div.wrap {
74
+ display: none;
75
+ }
76
  .character {
77
  display: flex;
78
  flex-direction: row;
 
197
  #input_text label textarea {
198
  width: 80%;
199
  }
200
+ #input_text div.wrap {
201
+ display: none;
202
+ }
203
  #input_text {
204
  display: flex;
205
  background: #ffffff !important;
 
240
 
241
  transform: translate(-2px,-2px) !important;
242
  }
243
+ .main-button:disabled,
244
+ .main-button[disabled]{
245
+ opacity: 100% !important;
246
+ }
247
  #speak_button {
248
  background: var(--color-yellow) !important;
249
  color: var(--color-accent-black) !important;