BadriNarayanan commited on
Commit
8431405
·
verified ·
1 Parent(s): b79c079

deleted app file

Browse files
Files changed (1) hide show
  1. app.py +0 -795
app.py DELETED
@@ -1,795 +0,0 @@
1
-
2
- import re
3
- import torch
4
- import torchaudio
5
- import gradio as gr
6
- import numpy as np
7
- import tempfile
8
- from einops import rearrange
9
- from vocos import Vocos
10
- from pydub import AudioSegment, silence
11
- from model import CFM, UNetT, DiT, MMDiT
12
- from cached_path import cached_path
13
- from model.utils import (
14
- load_checkpoint,
15
- get_tokenizer,
16
- convert_char_to_pinyin,
17
- save_spectrogram,
18
- )
19
- from transformers import pipeline
20
- import click
21
- import soundfile as sf
22
-
23
- try:
24
- import spaces
25
- USING_SPACES = True
26
- except ImportError:
27
- USING_SPACES = False
28
-
29
- def gpu_decorator(func):
30
- if USING_SPACES:
31
- return spaces.GPU(func)
32
- else:
33
- return func
34
-
35
- device = (
36
- "cuda"
37
- if torch.cuda.is_available()
38
- else "mps" if torch.backends.mps.is_available() else "cpu"
39
- )
40
-
41
- print(f"Using {device} device")
42
-
43
- pipe = pipeline(
44
- "automatic-speech-recognition",
45
- model="openai/whisper-large-v3-turbo",
46
- torch_dtype=torch.float16,
47
- device=device,
48
- )
49
- vocos = Vocos.from_pretrained("charactr/vocos-mel-24khz")
50
-
51
- # --------------------- Settings -------------------- #
52
-
53
- target_sample_rate = 24000
54
- n_mel_channels = 100
55
- hop_length = 256
56
- target_rms = 0.1
57
- nfe_step = 32 # 16, 32
58
- cfg_strength = 2.0
59
- ode_method = "euler"
60
- sway_sampling_coef = -1.0
61
- speed = 1.0
62
- fix_duration = None
63
-
64
-
65
- def load_model(repo_name, exp_name, model_cls, model_cfg, ckpt_step):
66
- ckpt_path = str(cached_path(f"hf://SWivid/{repo_name}/{exp_name}/model_{ckpt_step}.safetensors"))
67
- # ckpt_path = f"ckpts/{exp_name}/model_{ckpt_step}.pt" # .pt | .safetensors
68
- vocab_char_map, vocab_size = get_tokenizer("Emilia_ZH_EN", "pinyin")
69
- model = CFM(
70
- transformer=model_cls(
71
- **model_cfg, text_num_embeds=vocab_size, mel_dim=n_mel_channels
72
- ),
73
- mel_spec_kwargs=dict(
74
- target_sample_rate=target_sample_rate,
75
- n_mel_channels=n_mel_channels,
76
- hop_length=hop_length,
77
- ),
78
- odeint_kwargs=dict(
79
- method=ode_method,
80
- ),
81
- vocab_char_map=vocab_char_map,
82
- ).to(device)
83
-
84
- model = load_checkpoint(model, ckpt_path, device, use_ema = True)
85
-
86
- return model
87
-
88
-
89
- # load models
90
- F5TTS_model_cfg = dict(
91
- dim=1024, depth=22, heads=16, ff_mult=2, text_dim=512, conv_layers=4
92
- )
93
- E2TTS_model_cfg = dict(dim=1024, depth=24, heads=16, ff_mult=4)
94
-
95
- F5TTS_ema_model = load_model(
96
- "F5-TTS", "F5TTS_Base", DiT, F5TTS_model_cfg, 1200000
97
- )
98
- E2TTS_ema_model = load_model(
99
- "E2-TTS", "E2TTS_Base", UNetT, E2TTS_model_cfg, 1200000
100
- )
101
-
102
- def chunk_text(text, max_chars=135):
103
- """
104
- Splits the input text into chunks, each with a maximum number of characters.
105
-
106
- Args:
107
- text (str): The text to be split.
108
- max_chars (int): The maximum number of characters per chunk.
109
-
110
- Returns:
111
- List[str]: A list of text chunks.
112
- """
113
- chunks = []
114
- current_chunk = ""
115
- # Split the text into sentences based on punctuation followed by whitespace
116
- sentences = re.split(r'(?<=[;:,.!?])\s+|(?<=[;:,。!?])', text)
117
-
118
- for sentence in sentences:
119
- if len(current_chunk.encode('utf-8')) + len(sentence.encode('utf-8')) <= max_chars:
120
- current_chunk += sentence + " " if sentence and len(sentence[-1].encode('utf-8')) == 1 else sentence
121
- else:
122
- if current_chunk:
123
- chunks.append(current_chunk.strip())
124
- current_chunk = sentence + " " if sentence and len(sentence[-1].encode('utf-8')) == 1 else sentence
125
-
126
- if current_chunk:
127
- chunks.append(current_chunk.strip())
128
-
129
- return chunks
130
-
131
- @gpu_decorator
132
- def infer_batch(ref_audio, ref_text, gen_text_batches, exp_name, remove_silence, cross_fade_duration=0.15, progress=gr.Progress()):
133
- if exp_name == "F5-TTS":
134
- ema_model = F5TTS_ema_model
135
- elif exp_name == "E2-TTS":
136
- ema_model = E2TTS_ema_model
137
-
138
- audio, sr = ref_audio
139
- if audio.shape[0] > 1:
140
- audio = torch.mean(audio, dim=0, keepdim=True)
141
-
142
- rms = torch.sqrt(torch.mean(torch.square(audio)))
143
- if rms < target_rms:
144
- audio = audio * target_rms / rms
145
- if sr != target_sample_rate:
146
- resampler = torchaudio.transforms.Resample(sr, target_sample_rate)
147
- audio = resampler(audio)
148
- audio = audio.to(device)
149
-
150
- generated_waves = []
151
- spectrograms = []
152
-
153
- for i, gen_text in enumerate(progress.tqdm(gen_text_batches)):
154
- # Prepare the text
155
- if len(ref_text[-1].encode('utf-8')) == 1:
156
- ref_text = ref_text + " "
157
- text_list = [ref_text + gen_text]
158
- final_text_list = convert_char_to_pinyin(text_list)
159
-
160
- # Calculate duration
161
- ref_audio_len = audio.shape[-1] // hop_length
162
- zh_pause_punc = r"。,、;:?!"
163
- ref_text_len = len(ref_text.encode('utf-8')) + 3 * len(re.findall(zh_pause_punc, ref_text))
164
- gen_text_len = len(gen_text.encode('utf-8')) + 3 * len(re.findall(zh_pause_punc, gen_text))
165
- duration = ref_audio_len + int(ref_audio_len / ref_text_len * gen_text_len / speed)
166
-
167
- # inference
168
- with torch.inference_mode():
169
- generated, _ = ema_model.sample(
170
- cond=audio,
171
- text=final_text_list,
172
- duration=duration,
173
- steps=nfe_step,
174
- cfg_strength=cfg_strength,
175
- sway_sampling_coef=sway_sampling_coef,
176
- )
177
-
178
- generated = generated[:, ref_audio_len:, :]
179
- generated_mel_spec = rearrange(generated, "1 n d -> 1 d n")
180
- generated_wave = vocos.decode(generated_mel_spec.cpu())
181
- if rms < target_rms:
182
- generated_wave = generated_wave * rms / target_rms
183
-
184
- # wav -> numpy
185
- generated_wave = generated_wave.squeeze().cpu().numpy()
186
-
187
- generated_waves.append(generated_wave)
188
- spectrograms.append(generated_mel_spec[0].cpu().numpy())
189
-
190
- # Combine all generated waves with cross-fading
191
- if cross_fade_duration <= 0:
192
- # Simply concatenate
193
- final_wave = np.concatenate(generated_waves)
194
- else:
195
- final_wave = generated_waves[0]
196
- for i in range(1, len(generated_waves)):
197
- prev_wave = final_wave
198
- next_wave = generated_waves[i]
199
-
200
- # Calculate cross-fade samples, ensuring it does not exceed wave lengths
201
- cross_fade_samples = int(cross_fade_duration * target_sample_rate)
202
- cross_fade_samples = min(cross_fade_samples, len(prev_wave), len(next_wave))
203
-
204
- if cross_fade_samples <= 0:
205
- # No overlap possible, concatenate
206
- final_wave = np.concatenate([prev_wave, next_wave])
207
- continue
208
-
209
- # Overlapping parts
210
- prev_overlap = prev_wave[-cross_fade_samples:]
211
- next_overlap = next_wave[:cross_fade_samples]
212
-
213
- # Fade out and fade in
214
- fade_out = np.linspace(1, 0, cross_fade_samples)
215
- fade_in = np.linspace(0, 1, cross_fade_samples)
216
-
217
- # Cross-faded overlap
218
- cross_faded_overlap = prev_overlap * fade_out + next_overlap * fade_in
219
-
220
- # Combine
221
- new_wave = np.concatenate([
222
- prev_wave[:-cross_fade_samples],
223
- cross_faded_overlap,
224
- next_wave[cross_fade_samples:]
225
- ])
226
-
227
- final_wave = new_wave
228
-
229
- # Remove silence
230
- if remove_silence:
231
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
232
- sf.write(f.name, final_wave, target_sample_rate)
233
- aseg = AudioSegment.from_file(f.name)
234
- non_silent_segs = silence.split_on_silence(aseg, min_silence_len=1000, silence_thresh=-50, keep_silence=500)
235
- non_silent_wave = AudioSegment.silent(duration=0)
236
- for non_silent_seg in non_silent_segs:
237
- non_silent_wave += non_silent_seg
238
- aseg = non_silent_wave
239
- aseg.export(f.name, format="wav")
240
- final_wave, _ = torchaudio.load(f.name)
241
- final_wave = final_wave.squeeze().cpu().numpy()
242
-
243
- # Create a combined spectrogram
244
- combined_spectrogram = np.concatenate(spectrograms, axis=1)
245
-
246
- with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_spectrogram:
247
- spectrogram_path = tmp_spectrogram.name
248
- save_spectrogram(combined_spectrogram, spectrogram_path)
249
-
250
- return (target_sample_rate, final_wave), spectrogram_path
251
-
252
- @gpu_decorator
253
- def infer(ref_audio_orig, ref_text, gen_text, exp_name, remove_silence, cross_fade_duration=0.15):
254
-
255
- print(gen_text)
256
-
257
- gr.Info("Converting audio...")
258
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as f:
259
- aseg = AudioSegment.from_file(ref_audio_orig)
260
-
261
- non_silent_segs = silence.split_on_silence(
262
- aseg, min_silence_len=1000, silence_thresh=-50, keep_silence=1000
263
- )
264
- non_silent_wave = AudioSegment.silent(duration=0)
265
- for non_silent_seg in non_silent_segs:
266
- non_silent_wave += non_silent_seg
267
- aseg = non_silent_wave
268
-
269
- audio_duration = len(aseg)
270
- if audio_duration > 15000:
271
- gr.Warning("Audio is over 15s, clipping to only first 15s.")
272
- aseg = aseg[:15000]
273
- aseg.export(f.name, format="wav")
274
- ref_audio = f.name
275
-
276
- if not ref_text.strip():
277
- gr.Info("No reference text provided, transcribing reference audio...")
278
- ref_text = pipe(
279
- ref_audio,
280
- chunk_length_s=30,
281
- batch_size=128,
282
- generate_kwargs={"task": "transcribe"},
283
- return_timestamps=False,
284
- )["text"].strip()
285
- gr.Info("Finished transcription")
286
- else:
287
- gr.Info("Using custom reference text...")
288
-
289
- # Add the functionality to ensure it ends with ". "
290
- if not ref_text.endswith(". "):
291
- if ref_text.endswith("."):
292
- ref_text += " "
293
- else:
294
- ref_text += ". "
295
-
296
- audio, sr = torchaudio.load(ref_audio)
297
-
298
- # Use the new chunk_text function to split gen_text
299
- max_chars = int(len(ref_text.encode('utf-8')) / (audio.shape[-1] / sr) * (25 - audio.shape[-1] / sr))
300
- gen_text_batches = chunk_text(gen_text, max_chars=max_chars)
301
- print('ref_text', ref_text)
302
- for i, batch_text in enumerate(gen_text_batches):
303
- print(f'gen_text {i}', batch_text)
304
-
305
- gr.Info(f"Generating audio using {exp_name} in {len(gen_text_batches)} batches")
306
- return infer_batch((audio, sr), ref_text, gen_text_batches, exp_name, remove_silence, cross_fade_duration)
307
-
308
-
309
- @gpu_decorator
310
- def generate_podcast(script, speaker1_name, ref_audio1, ref_text1, speaker2_name, ref_audio2, ref_text2, exp_name, remove_silence):
311
- # Split the script into speaker blocks
312
- speaker_pattern = re.compile(f"^({re.escape(speaker1_name)}|{re.escape(speaker2_name)}):", re.MULTILINE)
313
- speaker_blocks = speaker_pattern.split(script)[1:] # Skip the first empty element
314
-
315
- generated_audio_segments = []
316
-
317
- for i in range(0, len(speaker_blocks), 2):
318
- speaker = speaker_blocks[i]
319
- text = speaker_blocks[i+1].strip()
320
-
321
- # Determine which speaker is talking
322
- if speaker == speaker1_name:
323
- ref_audio = ref_audio1
324
- ref_text = ref_text1
325
- elif speaker == speaker2_name:
326
- ref_audio = ref_audio2
327
- ref_text = ref_text2
328
- else:
329
- continue # Skip if the speaker is neither speaker1 nor speaker2
330
-
331
- # Generate audio for this block
332
- audio, _ = infer(ref_audio, ref_text, text, exp_name, remove_silence)
333
-
334
- # Convert the generated audio to a numpy array
335
- sr, audio_data = audio
336
-
337
- # Save the audio data as a WAV file
338
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
339
- sf.write(temp_file.name, audio_data, sr)
340
- audio_segment = AudioSegment.from_wav(temp_file.name)
341
-
342
- generated_audio_segments.append(audio_segment)
343
-
344
- # Add a short pause between speakers
345
- pause = AudioSegment.silent(duration=500) # 500ms pause
346
- generated_audio_segments.append(pause)
347
-
348
- # Concatenate all audio segments
349
- final_podcast = sum(generated_audio_segments)
350
-
351
- # Export the final podcast
352
- with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
353
- podcast_path = temp_file.name
354
- final_podcast.export(podcast_path, format="wav")
355
-
356
- return podcast_path
357
-
358
- def parse_speechtypes_text(gen_text):
359
- # Pattern to find (Emotion)
360
- pattern = r'\((.*?)\)'
361
-
362
- # Split the text by the pattern
363
- tokens = re.split(pattern, gen_text)
364
-
365
- segments = []
366
-
367
- current_emotion = 'Regular'
368
-
369
- for i in range(len(tokens)):
370
- if i % 2 == 0:
371
- # This is text
372
- text = tokens[i].strip()
373
- if text:
374
- segments.append({'emotion': current_emotion, 'text': text})
375
- else:
376
- # This is emotion
377
- emotion = tokens[i].strip()
378
- current_emotion = emotion
379
-
380
- return segments
381
-
382
- def update_speed(new_speed):
383
- global speed
384
- speed = new_speed
385
- return f"Speed set to: {speed}"
386
-
387
- with gr.Blocks() as app_credits:
388
- gr.Markdown("""
389
- # Credits
390
-
391
- * [mrfakename](https://github.com/fakerybakery) for the original [online demo](https://huggingface.co/spaces/mrfakename/E2-F5-TTS)
392
- * [RootingInLoad](https://github.com/RootingInLoad) for the podcast generation
393
- * [jpgallegoar](https://github.com/jpgallegoar) for multiple speech-type generation
394
- """)
395
- with gr.Blocks() as app_tts:
396
- gr.Markdown("# Batched TTS")
397
- ref_audio_input = gr.Audio(label="Reference Audio", type="filepath")
398
- gen_text_input = gr.Textbox(label="Text to Generate", lines=10)
399
- model_choice = gr.Radio(
400
- choices=["F5-TTS", "E2-TTS"], label="Choose TTS Model", value="F5-TTS"
401
- )
402
- generate_btn = gr.Button("Synthesize", variant="primary")
403
- with gr.Accordion("Advanced Settings", open=False):
404
- ref_text_input = gr.Textbox(
405
- label="Reference Text",
406
- info="Leave blank to automatically transcribe the reference audio. If you enter text it will override automatic transcription.",
407
- lines=2,
408
- )
409
- remove_silence = gr.Checkbox(
410
- label="Remove Silences",
411
- info="The model tends to produce silences, especially on longer audio. We can manually remove silences if needed. Note that this is an experimental feature and may produce strange results. This will also increase generation time.",
412
- value=False,
413
- )
414
- speed_slider = gr.Slider(
415
- label="Speed",
416
- minimum=0.3,
417
- maximum=2.0,
418
- value=speed,
419
- step=0.1,
420
- info="Adjust the speed of the audio.",
421
- )
422
- cross_fade_duration_slider = gr.Slider(
423
- label="Cross-Fade Duration (s)",
424
- minimum=0.0,
425
- maximum=1.0,
426
- value=0.15,
427
- step=0.01,
428
- info="Set the duration of the cross-fade between audio clips.",
429
- )
430
- speed_slider.change(update_speed, inputs=speed_slider)
431
-
432
- audio_output = gr.Audio(label="Synthesized Audio")
433
- spectrogram_output = gr.Image(label="Spectrogram")
434
-
435
- generate_btn.click(
436
- infer,
437
- inputs=[
438
- ref_audio_input,
439
- ref_text_input,
440
- gen_text_input,
441
- model_choice,
442
- remove_silence,
443
- cross_fade_duration_slider,
444
- ],
445
- outputs=[audio_output, spectrogram_output],
446
- )
447
-
448
- with gr.Blocks() as app_podcast:
449
- gr.Markdown("# Podcast Generation")
450
- speaker1_name = gr.Textbox(label="Speaker 1 Name")
451
- ref_audio_input1 = gr.Audio(label="Reference Audio (Speaker 1)", type="filepath")
452
- ref_text_input1 = gr.Textbox(label="Reference Text (Speaker 1)", lines=2)
453
-
454
- speaker2_name = gr.Textbox(label="Speaker 2 Name")
455
- ref_audio_input2 = gr.Audio(label="Reference Audio (Speaker 2)", type="filepath")
456
- ref_text_input2 = gr.Textbox(label="Reference Text (Speaker 2)", lines=2)
457
-
458
- script_input = gr.Textbox(label="Podcast Script", lines=10,
459
- placeholder="Enter the script with speaker names at the start of each block, e.g.:\nSean: How did you start studying...\n\nMeghan: I came to my interest in technology...\nIt was a long journey...\n\nSean: That's fascinating. Can you elaborate...")
460
-
461
- podcast_model_choice = gr.Radio(
462
- choices=["F5-TTS", "E2-TTS"], label="Choose TTS Model", value="F5-TTS"
463
- )
464
- podcast_remove_silence = gr.Checkbox(
465
- label="Remove Silences",
466
- value=True,
467
- )
468
- generate_podcast_btn = gr.Button("Generate Podcast", variant="primary")
469
- podcast_output = gr.Audio(label="Generated Podcast")
470
-
471
- def podcast_generation(script, speaker1, ref_audio1, ref_text1, speaker2, ref_audio2, ref_text2, model, remove_silence):
472
- return generate_podcast(script, speaker1, ref_audio1, ref_text1, speaker2, ref_audio2, ref_text2, model, remove_silence)
473
-
474
- generate_podcast_btn.click(
475
- podcast_generation,
476
- inputs=[
477
- script_input,
478
- speaker1_name,
479
- ref_audio_input1,
480
- ref_text_input1,
481
- speaker2_name,
482
- ref_audio_input2,
483
- ref_text_input2,
484
- podcast_model_choice,
485
- podcast_remove_silence,
486
- ],
487
- outputs=podcast_output,
488
- )
489
-
490
- def parse_emotional_text(gen_text):
491
- # Pattern to find (Emotion)
492
- pattern = r'\((.*?)\)'
493
-
494
- # Split the text by the pattern
495
- tokens = re.split(pattern, gen_text)
496
-
497
- segments = []
498
-
499
- current_emotion = 'Regular'
500
-
501
- for i in range(len(tokens)):
502
- if i % 2 == 0:
503
- # This is text
504
- text = tokens[i].strip()
505
- if text:
506
- segments.append({'emotion': current_emotion, 'text': text})
507
- else:
508
- # This is emotion
509
- emotion = tokens[i].strip()
510
- current_emotion = emotion
511
-
512
- return segments
513
-
514
- with gr.Blocks() as app_emotional:
515
- # New section for emotional generation
516
- gr.Markdown(
517
- """
518
- # Multiple Speech-Type Generation
519
-
520
- This section allows you to upload different audio clips for each speech type. 'Regular' emotion is mandatory. You can add additional speech types by clicking the "Add Speech Type" button. Enter your text in the format shown below, and the system will generate speech using the appropriate emotions. If unspecified, the model will use the regular speech type. The current speech type will be used until the next speech type is specified.
521
-
522
- **Example Input:**
523
-
524
- (Regular) Hello, I'd like to order a sandwich please. (Surprised) What do you mean you're out of bread? (Sad) I really wanted a sandwich though... (Angry) You know what, darn you and your little shop, you suck! (Whisper) I'll just go back home and cry now. (Shouting) Why me?!
525
- """
526
- )
527
-
528
- gr.Markdown("Upload different audio clips for each speech type. 'Regular' emotion is mandatory. You can add additional speech types by clicking the 'Add Speech Type' button.")
529
-
530
- # Regular speech type (mandatory)
531
- with gr.Row():
532
- regular_name = gr.Textbox(value='Regular', label='Speech Type Name', interactive=False)
533
- regular_audio = gr.Audio(label='Regular Reference Audio', type='filepath')
534
- regular_ref_text = gr.Textbox(label='Reference Text (Regular)', lines=2)
535
-
536
- # Additional speech types (up to 99 more)
537
- max_speech_types = 100
538
- speech_type_names = []
539
- speech_type_audios = []
540
- speech_type_ref_texts = []
541
- speech_type_delete_btns = []
542
-
543
- for i in range(max_speech_types - 1):
544
- with gr.Row():
545
- name_input = gr.Textbox(label='Speech Type Name', visible=False)
546
- audio_input = gr.Audio(label='Reference Audio', type='filepath', visible=False)
547
- ref_text_input = gr.Textbox(label='Reference Text', lines=2, visible=False)
548
- delete_btn = gr.Button("Delete", variant="secondary", visible=False)
549
- speech_type_names.append(name_input)
550
- speech_type_audios.append(audio_input)
551
- speech_type_ref_texts.append(ref_text_input)
552
- speech_type_delete_btns.append(delete_btn)
553
-
554
- # Button to add speech type
555
- add_speech_type_btn = gr.Button("Add Speech Type")
556
-
557
- # Keep track of current number of speech types
558
- speech_type_count = gr.State(value=0)
559
-
560
- # Function to add a speech type
561
- def add_speech_type_fn(speech_type_count):
562
- if speech_type_count < max_speech_types - 1:
563
- speech_type_count += 1
564
- # Prepare updates for the components
565
- name_updates = []
566
- audio_updates = []
567
- ref_text_updates = []
568
- delete_btn_updates = []
569
- for i in range(max_speech_types - 1):
570
- if i < speech_type_count:
571
- name_updates.append(gr.update(visible=True))
572
- audio_updates.append(gr.update(visible=True))
573
- ref_text_updates.append(gr.update(visible=True))
574
- delete_btn_updates.append(gr.update(visible=True))
575
- else:
576
- name_updates.append(gr.update())
577
- audio_updates.append(gr.update())
578
- ref_text_updates.append(gr.update())
579
- delete_btn_updates.append(gr.update())
580
- else:
581
- # Optionally, show a warning
582
- # gr.Warning("Maximum number of speech types reached.")
583
- name_updates = [gr.update() for _ in range(max_speech_types - 1)]
584
- audio_updates = [gr.update() for _ in range(max_speech_types - 1)]
585
- ref_text_updates = [gr.update() for _ in range(max_speech_types - 1)]
586
- delete_btn_updates = [gr.update() for _ in range(max_speech_types - 1)]
587
- return [speech_type_count] + name_updates + audio_updates + ref_text_updates + delete_btn_updates
588
-
589
- add_speech_type_btn.click(
590
- add_speech_type_fn,
591
- inputs=speech_type_count,
592
- outputs=[speech_type_count] + speech_type_names + speech_type_audios + speech_type_ref_texts + speech_type_delete_btns
593
- )
594
-
595
- # Function to delete a speech type
596
- def make_delete_speech_type_fn(index):
597
- def delete_speech_type_fn(speech_type_count):
598
- # Prepare updates
599
- name_updates = []
600
- audio_updates = []
601
- ref_text_updates = []
602
- delete_btn_updates = []
603
-
604
- for i in range(max_speech_types - 1):
605
- if i == index:
606
- name_updates.append(gr.update(visible=False, value=''))
607
- audio_updates.append(gr.update(visible=False, value=None))
608
- ref_text_updates.append(gr.update(visible=False, value=''))
609
- delete_btn_updates.append(gr.update(visible=False))
610
- else:
611
- name_updates.append(gr.update())
612
- audio_updates.append(gr.update())
613
- ref_text_updates.append(gr.update())
614
- delete_btn_updates.append(gr.update())
615
-
616
- speech_type_count = max(0, speech_type_count - 1)
617
-
618
- return [speech_type_count] + name_updates + audio_updates + ref_text_updates + delete_btn_updates
619
-
620
- return delete_speech_type_fn
621
-
622
- for i, delete_btn in enumerate(speech_type_delete_btns):
623
- delete_fn = make_delete_speech_type_fn(i)
624
- delete_btn.click(
625
- delete_fn,
626
- inputs=speech_type_count,
627
- outputs=[speech_type_count] + speech_type_names + speech_type_audios + speech_type_ref_texts + speech_type_delete_btns
628
- )
629
-
630
- # Text input for the prompt
631
- gen_text_input_emotional = gr.Textbox(label="Text to Generate", lines=10)
632
-
633
- # Model choice
634
- model_choice_emotional = gr.Radio(
635
- choices=["F5-TTS", "E2-TTS"], label="Choose TTS Model", value="F5-TTS"
636
- )
637
-
638
- with gr.Accordion("Advanced Settings", open=False):
639
- remove_silence_emotional = gr.Checkbox(
640
- label="Remove Silences",
641
- value=True,
642
- )
643
-
644
- # Generate button
645
- generate_emotional_btn = gr.Button("Generate Emotional Speech", variant="primary")
646
-
647
- # Output audio
648
- audio_output_emotional = gr.Audio(label="Synthesized Audio")
649
- @gpu_decorator
650
- def generate_emotional_speech(
651
- regular_audio,
652
- regular_ref_text,
653
- gen_text,
654
- *args,
655
- ):
656
- num_additional_speech_types = max_speech_types - 1
657
- speech_type_names_list = args[:num_additional_speech_types]
658
- speech_type_audios_list = args[num_additional_speech_types:2 * num_additional_speech_types]
659
- speech_type_ref_texts_list = args[2 * num_additional_speech_types:3 * num_additional_speech_types]
660
- model_choice = args[3 * num_additional_speech_types]
661
- remove_silence = args[3 * num_additional_speech_types + 1]
662
-
663
- # Collect the speech types and their audios into a dict
664
- speech_types = {'Regular': {'audio': regular_audio, 'ref_text': regular_ref_text}}
665
-
666
- for name_input, audio_input, ref_text_input in zip(speech_type_names_list, speech_type_audios_list, speech_type_ref_texts_list):
667
- if name_input and audio_input:
668
- speech_types[name_input] = {'audio': audio_input, 'ref_text': ref_text_input}
669
-
670
- # Parse the gen_text into segments
671
- segments = parse_speechtypes_text(gen_text)
672
-
673
- # For each segment, generate speech
674
- generated_audio_segments = []
675
- current_emotion = 'Regular'
676
-
677
- for segment in segments:
678
- emotion = segment['emotion']
679
- text = segment['text']
680
-
681
- if emotion in speech_types:
682
- current_emotion = emotion
683
- else:
684
- # If emotion not available, default to Regular
685
- current_emotion = 'Regular'
686
-
687
- ref_audio = speech_types[current_emotion]['audio']
688
- ref_text = speech_types[current_emotion].get('ref_text', '')
689
-
690
- # Generate speech for this segment
691
- audio, _ = infer(ref_audio, ref_text, text, model_choice, remove_silence, 0)
692
- sr, audio_data = audio
693
-
694
- generated_audio_segments.append(audio_data)
695
-
696
- # Concatenate all audio segments
697
- if generated_audio_segments:
698
- final_audio_data = np.concatenate(generated_audio_segments)
699
- return (sr, final_audio_data)
700
- else:
701
- gr.Warning("No audio generated.")
702
- return None
703
-
704
- generate_emotional_btn.click(
705
- generate_emotional_speech,
706
- inputs=[
707
- regular_audio,
708
- regular_ref_text,
709
- gen_text_input_emotional,
710
- ] + speech_type_names + speech_type_audios + speech_type_ref_texts + [
711
- model_choice_emotional,
712
- remove_silence_emotional,
713
- ],
714
- outputs=audio_output_emotional,
715
- )
716
-
717
- # Validation function to disable Generate button if speech types are missing
718
- def validate_speech_types(
719
- gen_text,
720
- regular_name,
721
- *args
722
- ):
723
- num_additional_speech_types = max_speech_types - 1
724
- speech_type_names_list = args[:num_additional_speech_types]
725
-
726
- # Collect the speech types names
727
- speech_types_available = set()
728
- if regular_name:
729
- speech_types_available.add(regular_name)
730
- for name_input in speech_type_names_list:
731
- if name_input:
732
- speech_types_available.add(name_input)
733
-
734
- # Parse the gen_text to get the speech types used
735
- segments = parse_emotional_text(gen_text)
736
- speech_types_in_text = set(segment['emotion'] for segment in segments)
737
-
738
- # Check if all speech types in text are available
739
- missing_speech_types = speech_types_in_text - speech_types_available
740
-
741
- if missing_speech_types:
742
- # Disable the generate button
743
- return gr.update(interactive=False)
744
- else:
745
- # Enable the generate button
746
- return gr.update(interactive=True)
747
-
748
- gen_text_input_emotional.change(
749
- validate_speech_types,
750
- inputs=[gen_text_input_emotional, regular_name] + speech_type_names,
751
- outputs=generate_emotional_btn
752
- )
753
- with gr.Blocks() as app:
754
- gr.Markdown(
755
- """
756
- # Antriksh AI
757
- """
758
- )
759
-
760
- # Add the image here
761
- gr.Image(
762
- value="C:\\Users\\USER\\Downloads\\logo-removebg-preview.png",
763
- label="AI System Logo",
764
- show_label=False,
765
- width=300,
766
- height=150
767
- )
768
-
769
- gr.TabbedInterface([app_tts, app_podcast, app_emotional, app_credits], ["TTS", "Podcast", "Multi-Style", "Credits"])
770
-
771
-
772
- @click.command()
773
- @click.option("--port", "-p", default=None, type=int, help="Port to run the app on")
774
- @click.option("--host", "-H", default=None, help="Host to run the app on")
775
- @click.option(
776
- "--share",
777
- "-s",
778
- default=False,
779
- is_flag=True,
780
- help="Share the app via Gradio share link",
781
- )
782
- @click.option("--api", "-a", default=True, is_flag=True, help="Allow API access")
783
- def main(port, host, share, api):
784
- global app
785
- print(f"Starting app...")
786
- app.queue(api_open=api).launch(
787
- server_name=host, server_port=port, share=share, show_api=api
788
- )
789
-
790
-
791
- if __name__ == "__main__":
792
- if not USING_SPACES:
793
- main()
794
- else:
795
- app.queue().launch()