Manmay commited on
Commit
59b9fc8
·
verified ·
1 Parent(s): 4e9199e

Initial commit: per-language Chatterbox TTS demo

Browse files
Files changed (2) hide show
  1. app.py +24 -11
  2. chatterbox/src/chatterbox/tts.py +3 -1
app.py CHANGED
@@ -12,14 +12,20 @@ DEFAULT_CONFIG = {
12
  "text": 'Last month, we reached a new milestone with two billion views on our YouTube channel.',
13
  }
14
 
 
 
 
 
15
  EXAMPLES = [
16
- ['Last month, we reached a new milestone with two billion views on our YouTube channel.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/en_f1.flac', 0.5, 0.8, 0, 0.5],
17
- ["Bonjour, comment allez-vous? Aujourd'hui, il fait un temps magnifique pour se promener.", 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/fr_f1.flac', 0.5, 0.8, 0, 0.5],
18
- ['Guten Tag! Heute ist ein wunderschöner Tag für einen Spaziergang im Park.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/de_f1.flac', 0.5, 0.8, 0, 0.5],
19
- ['Hola, ¿cómo estás? Me encanta visitar nuevos lugares y conocer culturas diferentes.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/es_f1.flac', 0.5, 0.8, 0, 0.5],
20
- ['Ciao! Oggi è una giornata perfetta per una passeggiata a Roma.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/it_m1.flac', 0.5, 0.8, 0, 0.5],
21
- ['Olá, tudo bem? O Brasil é um país incrível com paisagens deslumbrantes.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/pt_m1.flac', 0.5, 0.8, 0, 0.5],
22
- ['नमस्ते, आज का दिन बहुत खूबसूरत है।', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/hi_f1.flac', 0.5, 0.8, 0, 0.5]
 
 
23
  ]
24
 
25
 
@@ -53,6 +59,7 @@ def set_seed(seed: int, device: str):
53
  def generate_tts_audio(
54
  text_input: str,
55
  audio_prompt_path_input: str = None,
 
56
  exaggeration_input: float = 0.5,
57
  temperature_input: float = 0.8,
58
  seed_num_input: int = 0,
@@ -64,16 +71,17 @@ def generate_tts_audio(
64
  current_model.to(device)
65
  if seed_num_input != 0:
66
  set_seed(int(seed_num_input), device)
67
- print(f"Generating on {device} for text: '{text_input[:50]}...'")
68
  chosen_prompt = audio_prompt_path_input or default_audio_for_ui()
 
 
69
  generate_kwargs = {
70
  "exaggeration": exaggeration_input,
71
  "temperature": temperature_input,
72
  "cfg_weight": cfgw_input,
 
73
  }
74
  if chosen_prompt:
75
  generate_kwargs["audio_prompt_path"] = chosen_prompt
76
- print(f"Using audio prompt: {chosen_prompt}")
77
  wav = current_model.generate(text_input[:300], **generate_kwargs)
78
  return (current_model.sr, wav.squeeze(0).cpu().numpy())
79
 
@@ -99,6 +107,11 @@ with gr.Blocks() as demo:
99
  label="Reference Audio File (Optional)",
100
  value=default_audio_for_ui(),
101
  )
 
 
 
 
 
102
  exaggeration = gr.Slider(0.25, 2, step=.05, label="Exaggeration (Neutral = 0.5)", value=.5)
103
  cfg_weight = gr.Slider(0.2, 1, step=.05, label="CFG/Pace", value=0.5)
104
  with gr.Accordion("More options", open=False):
@@ -108,12 +121,12 @@ with gr.Blocks() as demo:
108
  with gr.Column():
109
  audio_output = gr.Audio(label="Output Audio")
110
 
111
- inputs = [text, ref_wav, exaggeration, temp, seed_num, cfg_weight]
112
  run_btn.click(fn=generate_tts_audio, inputs=inputs, outputs=[audio_output])
113
 
114
  gr.Examples(
115
  examples=EXAMPLES,
116
- inputs=[text, ref_wav, exaggeration, temp, seed_num, cfg_weight],
117
  label="Examples",
118
  )
119
 
 
12
  "text": 'Last month, we reached a new milestone with two billion views on our YouTube channel.',
13
  }
14
 
15
+ LANGUAGE_CHOICES = [('en', 'English'), ('ar', 'Arabic'), ('da', 'Danish'), ('de', 'German'), ('el', 'Greek'), ('es', 'Spanish'), ('fi', 'Finnish'), ('fr', 'French'), ('he', 'Hebrew'), ('hi', 'Hindi'), ('it', 'Italian'), ('ja', 'Japanese'), ('ko', 'Korean'), ('ms', 'Malay'), ('nl', 'Dutch'), ('no', 'Norwegian'), ('pl', 'Polish'), ('pt', 'Portuguese'), ('ru', 'Russian'), ('sv', 'Swedish'), ('sw', 'Swahili'), ('tr', 'Turkish'), ('zh', 'Chinese')]
16
+ DEFAULT_LANGUAGE = 'en'
17
+ FIXED_LANGUAGE_ID = None
18
+
19
  EXAMPLES = [
20
+ ['Last month, we reached a new milestone with two billion views on our YouTube channel.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/en_f1.flac', 'en', 0.5, 0.8, 0, 0.5],
21
+ ["Bonjour, comment allez-vous? Aujourd'hui, il fait un temps magnifique pour se promener.", 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/fr_f1.flac', 'fr', 0.5, 0.8, 0, 0.5],
22
+ ['Guten Tag! Heute ist ein wunderschöner Tag für einen Spaziergang im Park.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/de_f1.flac', 'de', 0.5, 0.8, 0, 0.5],
23
+ ['Hola, ¿cómo estás? Me encanta visitar nuevos lugares y conocer culturas diferentes.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/es_f1.flac', 'es', 0.5, 0.8, 0, 0.5],
24
+ ['Ciao! Oggi è una giornata perfetta per una passeggiata a Roma.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/it_m1.flac', 'it', 0.5, 0.8, 0, 0.5],
25
+ ['Olá, tudo bem? O Brasil é um país incrível com paisagens deslumbrantes.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/pt_m1.flac', 'pt', 0.5, 0.8, 0, 0.5],
26
+ ['नमस्ते, आज का दिन बहुत खूबसूरत है।', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/hi_f1.flac', 'hi', 0.5, 0.8, 0, 0.5],
27
+ ['你好,今天天气真不错,我们一起去公园散步吧。', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/zh_f1.flac', 'zh', 0.5, 0.8, 0, 0.5],
28
+ ['مرحبا، كيف حالك اليوم؟ الطقس جميل للنزهة في الحديقة.', 'https://storage.googleapis.com/chatterbox-demo-samples/mtl_prompts/ar_m1.flac', 'ar', 0.5, 0.8, 0, 0.5]
29
  ]
30
 
31
 
 
59
  def generate_tts_audio(
60
  text_input: str,
61
  audio_prompt_path_input: str = None,
62
+ language_id_input: str = None,
63
  exaggeration_input: float = 0.5,
64
  temperature_input: float = 0.8,
65
  seed_num_input: int = 0,
 
71
  current_model.to(device)
72
  if seed_num_input != 0:
73
  set_seed(int(seed_num_input), device)
 
74
  chosen_prompt = audio_prompt_path_input or default_audio_for_ui()
75
+ lang = language_id_input or DEFAULT_LANGUAGE
76
+ print(f"Generating on {device} (lang={lang}) for text: '{text_input[:50]}...'")
77
  generate_kwargs = {
78
  "exaggeration": exaggeration_input,
79
  "temperature": temperature_input,
80
  "cfg_weight": cfgw_input,
81
+ "language_id": lang,
82
  }
83
  if chosen_prompt:
84
  generate_kwargs["audio_prompt_path"] = chosen_prompt
 
85
  wav = current_model.generate(text_input[:300], **generate_kwargs)
86
  return (current_model.sr, wav.squeeze(0).cpu().numpy())
87
 
 
107
  label="Reference Audio File (Optional)",
108
  value=default_audio_for_ui(),
109
  )
110
+ language = gr.Dropdown(
111
+ choices=[(name, code) for code, name in LANGUAGE_CHOICES],
112
+ value=DEFAULT_LANGUAGE,
113
+ label="Language",
114
+ )
115
  exaggeration = gr.Slider(0.25, 2, step=.05, label="Exaggeration (Neutral = 0.5)", value=.5)
116
  cfg_weight = gr.Slider(0.2, 1, step=.05, label="CFG/Pace", value=0.5)
117
  with gr.Accordion("More options", open=False):
 
121
  with gr.Column():
122
  audio_output = gr.Audio(label="Output Audio")
123
 
124
+ inputs = [text, ref_wav, language, exaggeration, temp, seed_num, cfg_weight]
125
  run_btn.click(fn=generate_tts_audio, inputs=inputs, outputs=[audio_output])
126
 
127
  gr.Examples(
128
  examples=EXAMPLES,
129
+ inputs=inputs,
130
  label="Examples",
131
  )
132
 
chatterbox/src/chatterbox/tts.py CHANGED
@@ -247,6 +247,7 @@ class ChatterboxTTS:
247
  exaggeration=0.5,
248
  cfg_weight=0.5,
249
  temperature=0.8,
 
250
  ):
251
  """
252
  Generate speech from text using the language-agnostic model.
@@ -278,7 +279,8 @@ class ChatterboxTTS:
278
 
279
  # Norm and tokenize text
280
  text = punc_norm(text)
281
- text_tokens = self.tokenizer.text_to_tokens(text, language_id="en").to(self.device)
 
282
  text_tokens = torch.cat([text_tokens, text_tokens], dim=0) # Need two seqs for CFG
283
 
284
  sot = self.t3.hp.start_text_token
 
247
  exaggeration=0.5,
248
  cfg_weight=0.5,
249
  temperature=0.8,
250
+ language_id="en",
251
  ):
252
  """
253
  Generate speech from text using the language-agnostic model.
 
279
 
280
  # Norm and tokenize text
281
  text = punc_norm(text)
282
+ lang = (language_id or "en").lower() if language_id else None
283
+ text_tokens = self.tokenizer.text_to_tokens(text, language_id=lang).to(self.device)
284
  text_tokens = torch.cat([text_tokens, text_tokens], dim=0) # Need two seqs for CFG
285
 
286
  sot = self.t3.hp.start_text_token