Li Wei Chen commited on
Commit
14e913b
·
1 Parent(s): 853fa9b

feat: add optional G2P conversion using formog2p

Browse files

Add a checkbox to toggle G2P conversion (default off). When enabled,
formog2p converts Hakka Chinese characters to uppercase pinyin before
passing to the model, with dialect-aware lang_group mapping.

Files changed (2) hide show
  1. app.py +35 -4
  2. requirements.txt +2 -1
app.py CHANGED
@@ -24,6 +24,14 @@ DIALECT_LABELS = [
24
  "客語詔安腔",
25
  "客語南四縣腔",
26
  ]
 
 
 
 
 
 
 
 
27
  DEFAULT_SPEED = 1.0
28
  DEFAULT_STEPS = 32
29
  EXAMPLES = [
@@ -34,6 +42,7 @@ EXAMPLES = [
34
  "恁早。",
35
  DEFAULT_SPEED,
36
  DEFAULT_STEPS,
 
37
  ],
38
  [
39
  "客語四縣腔",
@@ -42,6 +51,7 @@ EXAMPLES = [
42
  "你今晡日著到恁派頭。",
43
  DEFAULT_SPEED,
44
  DEFAULT_STEPS,
 
45
  ],
46
  [
47
  "客語四縣腔",
@@ -50,6 +60,7 @@ EXAMPLES = [
50
  "你今晡日著到恁派頭。",
51
  DEFAULT_SPEED,
52
  DEFAULT_STEPS,
 
53
  ],
54
  ]
55
 
@@ -142,6 +153,14 @@ def startup_status() -> str:
142
  )
143
 
144
 
 
 
 
 
 
 
 
 
145
  def validate_inputs(
146
  dialect: str | None,
147
  text: str,
@@ -175,6 +194,7 @@ def synthesize(
175
  ref_text: str,
176
  speed: float,
177
  num_step: int,
 
178
  ) -> tuple[tuple[int, np.ndarray] | None, str]:
179
  error = validate_inputs(dialect, text, ref_audio, ref_text)
180
  if error:
@@ -188,6 +208,12 @@ def synthesize(
188
  return None, startup_status()
189
 
190
  try:
 
 
 
 
 
 
191
  generation_config = RUNTIME.generation_config_cls(
192
  num_step=int(num_step),
193
  guidance_scale=2.0,
@@ -201,7 +227,7 @@ def synthesize(
201
  preprocess_prompt=True,
202
  )
203
  generate_kwargs: dict[str, Any] = {
204
- "text": text.strip(),
205
  "voice_clone_prompt": voice_clone_prompt,
206
  "instruct": dialect,
207
  "generation_config": generation_config,
@@ -216,7 +242,7 @@ def synthesize(
216
 
217
  return (
218
  to_audio_output(audio[0], int(RUNTIME.sampling_rate or 24000)),
219
- f"合成完成。腔調:{dialect};speed={speed:.2f};steps={int(num_step)}",
220
  )
221
  except Exception as exc:
222
  return None, f"合成失敗:{type(exc).__name__}: {exc}"
@@ -260,6 +286,11 @@ def build_demo() -> gr.Blocks:
260
  lines=2,
261
  placeholder="請填寫參考音檔對應的逐字文本。",
262
  )
 
 
 
 
 
263
  with gr.Accordion("進階設定", open=False):
264
  speed = gr.Slider(
265
  minimum=0.5,
@@ -293,13 +324,13 @@ def build_demo() -> gr.Blocks:
293
 
294
  submit.click(
295
  fn=synthesize,
296
- inputs=[dialect, text, ref_audio, ref_text, speed, num_step],
297
  outputs=[output_audio, status],
298
  )
299
 
300
  gr.Examples(
301
  examples=EXAMPLES,
302
- inputs=[dialect, text, ref_audio, ref_text, speed, num_step],
303
  label="範例",
304
  )
305
 
 
24
  "客語詔安腔",
25
  "客語南四縣腔",
26
  ]
27
+ DIALECT_TO_LANG_GROUP = {
28
+ "客語四縣腔": "hak_sx",
29
+ "客語海陸腔": "hak_hl",
30
+ "客語大埔腔": "hak_dp",
31
+ "客語饒平腔": "hak_rp",
32
+ "客語詔安腔": "hak_za",
33
+ "客語南四縣腔": "hak_nsx",
34
+ }
35
  DEFAULT_SPEED = 1.0
36
  DEFAULT_STEPS = 32
37
  EXAMPLES = [
 
42
  "恁早。",
43
  DEFAULT_SPEED,
44
  DEFAULT_STEPS,
45
+ False,
46
  ],
47
  [
48
  "客語四縣腔",
 
51
  "你今晡日著到恁派頭。",
52
  DEFAULT_SPEED,
53
  DEFAULT_STEPS,
54
+ False,
55
  ],
56
  [
57
  "客語四縣腔",
 
60
  "你今晡日著到恁派頭。",
61
  DEFAULT_SPEED,
62
  DEFAULT_STEPS,
63
+ False,
64
  ],
65
  ]
66
 
 
153
  )
154
 
155
 
156
+ def apply_g2p(text: str, dialect: str) -> str:
157
+ from formog2p.hakka.g2p import g2p
158
+
159
+ lang_group = DIALECT_TO_LANG_GROUP.get(dialect, "hak_sx")
160
+ result = g2p(text, lang_group=lang_group, pronunciation_type="pinyin")
161
+ return " ".join(result.pronunciations).upper()
162
+
163
+
164
  def validate_inputs(
165
  dialect: str | None,
166
  text: str,
 
194
  ref_text: str,
195
  speed: float,
196
  num_step: int,
197
+ use_g2p: bool,
198
  ) -> tuple[tuple[int, np.ndarray] | None, str]:
199
  error = validate_inputs(dialect, text, ref_audio, ref_text)
200
  if error:
 
208
  return None, startup_status()
209
 
210
  try:
211
+ input_text = text.strip()
212
+ g2p_note = ""
213
+ if use_g2p:
214
+ input_text = apply_g2p(input_text, dialect)
215
+ g2p_note = f";G2P 轉換:{input_text}"
216
+
217
  generation_config = RUNTIME.generation_config_cls(
218
  num_step=int(num_step),
219
  guidance_scale=2.0,
 
227
  preprocess_prompt=True,
228
  )
229
  generate_kwargs: dict[str, Any] = {
230
+ "text": input_text,
231
  "voice_clone_prompt": voice_clone_prompt,
232
  "instruct": dialect,
233
  "generation_config": generation_config,
 
242
 
243
  return (
244
  to_audio_output(audio[0], int(RUNTIME.sampling_rate or 24000)),
245
+ f"合成完成。腔調:{dialect};speed={speed:.2f};steps={int(num_step)}{g2p_note}",
246
  )
247
  except Exception as exc:
248
  return None, f"合成失敗:{type(exc).__name__}: {exc}"
 
286
  lines=2,
287
  placeholder="請填寫參考音檔對應的逐字文本。",
288
  )
289
+ use_g2p = gr.Checkbox(
290
+ value=False,
291
+ label="使用 G2P 轉換",
292
+ info="勾選後會先用 formog2p 將漢字轉成拼音(大寫)再輸入模型;不勾選則直接輸入原文。",
293
+ )
294
  with gr.Accordion("進階設定", open=False):
295
  speed = gr.Slider(
296
  minimum=0.5,
 
324
 
325
  submit.click(
326
  fn=synthesize,
327
+ inputs=[dialect, text, ref_audio, ref_text, speed, num_step, use_g2p],
328
  outputs=[output_audio, status],
329
  )
330
 
331
  gr.Examples(
332
  examples=EXAMPLES,
333
+ inputs=[dialect, text, ref_audio, ref_text, speed, num_step, use_g2p],
334
  label="範例",
335
  )
336
 
requirements.txt CHANGED
@@ -1,3 +1,4 @@
1
  git+https://github.com/txya900619/OmniVoice-hakka.git
2
  torch==2.8.0
3
- torchaudio==2.8.0
 
 
1
  git+https://github.com/txya900619/OmniVoice-hakka.git
2
  torch==2.8.0
3
+ torchaudio==2.8.0
4
+ formog2p