-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathChatter.py
More file actions
2087 lines (1842 loc) · 99.5 KB
/
Copy pathChatter.py
File metadata and controls
2087 lines (1842 loc) · 99.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import random
import numpy as np
import torch
import os
import re
import datetime
import torchaudio
import gradio as gr
import spaces
import subprocess
from pydub import AudioSegment
import ffmpeg
import librosa
import string
import difflib
import time
import gc
from chatterbox.src.chatterbox.tts import ChatterboxTTS
from concurrent.futures import ThreadPoolExecutor, as_completed
import whisper
import nltk
from nltk.tokenize import sent_tokenize
from faster_whisper import WhisperModel as FasterWhisperModel
import json
import csv
import argparse
import soundfile as sf
import inspect, traceback
from chatterbox.src.chatterbox.vc import ChatterboxVC
try:
import pyrnnoise
_PYRNNOISE_AVAILABLE = True
except Exception:
_PYRNNOISE_AVAILABLE = False
SETTINGS_PATH = "settings.json"
#THIS IS THE START
def load_settings():
if os.path.exists(SETTINGS_PATH):
with open(SETTINGS_PATH, "r", encoding="utf-8") as f:
try:
data = json.load(f)
d = default_settings()
d.update(data)
return d
except Exception:
return default_settings()
else:
return default_settings()
def save_settings(mapping):
# Ensure "whisper_model_dropdown" is always saved as the label, not code
whisper_model_map = {
"tiny (~1 GB VRAM OpenAI / ~0.5 GB faster-whisper)": "tiny",
"base (~1.2–2 GB OpenAI / ~0.7–1 GB faster-whisper)": "base",
"small (~2–3 GB OpenAI / ~1.2–1.7 GB faster-whisper)": "small",
"medium (~5–8 GB OpenAI / ~2.5–4.5 GB faster-whisper)": "medium",
"large (~10–13 GB OpenAI / ~4.5–6.5 GB faster-whisper)": "large"
}
v = mapping.get("whisper_model_dropdown", "")
if v not in whisper_model_map:
label = next((k for k, code in whisper_model_map.items() if code == v), v)
mapping["whisper_model_dropdown"] = label
# --- Add the extra "per-generation" fields for full compatibility ---
if "input_basename" not in mapping:
mapping["input_basename"] = "text_input_"
if "audio_prompt_path_input" not in mapping:
mapping["audio_prompt_path_input"] = None
if "generation_time" not in mapping:
import datetime
mapping["generation_time"] = datetime.datetime.now().isoformat()
if "output_audio_files" not in mapping:
mapping["output_audio_files"] = []
with open(SETTINGS_PATH, "w", encoding="utf-8") as f:
json.dump(mapping, f, indent=2, ensure_ascii=False)
def save_settings_csv(settings_dict, output_audio_files, csv_path):
"""
Save a dict of settings and a list of output audio files to a one-row CSV.
"""
# Prepare a flattened settings dict for CSV
flat_settings = {}
for k, v in settings_dict.items():
if isinstance(v, (list, tuple)):
flat_settings[k] = '|'.join(map(str, v))
else:
flat_settings[k] = v
flat_settings['output_audio_files'] = '|'.join(output_audio_files)
with open(csv_path, "w", newline='', encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=list(flat_settings.keys()))
writer.writeheader()
writer.writerow(flat_settings)
def save_settings_json(settings_dict, json_path):
"""
Save the settings dict as a JSON file.
"""
with open(json_path, "w", encoding="utf-8") as f:
json.dump(settings_dict, f, indent=2, ensure_ascii=False)
# === VC TAB (NEW) ===
VC_MODEL = None # Reuse the global DEVICE defined earlier
def get_or_load_vc_model():
global VC_MODEL
if VC_MODEL is None:
VC_MODEL = ChatterboxVC.from_pretrained(DEVICE)
return VC_MODEL
def voice_conversion(input_audio_path, target_voice_audio_path, chunk_sec=60, overlap_sec=0.1, disable_watermark=True, pitch_shift=0):
vc_model = get_or_load_vc_model()
model_sr = vc_model.sr
wav, sr = sf.read(input_audio_path)
if wav.ndim > 1:
wav = wav.mean(axis=1)
if sr != model_sr:
wav = librosa.resample(wav, orig_sr=sr, target_sr=model_sr)
sr = model_sr
total_sec = len(wav) / model_sr
if total_sec <= chunk_sec:
wav_out = vc_model.generate(
input_audio_path,
target_voice_path=target_voice_audio_path,
apply_watermark=not disable_watermark,
pitch_shift=pitch_shift
)
out_wav = wav_out.squeeze(0).numpy()
return model_sr, out_wav
# chunking logic for long files
chunk_samples = int(chunk_sec * model_sr)
overlap_samples = int(overlap_sec * model_sr)
step_samples = chunk_samples - overlap_samples
out_chunks = []
for start in range(0, len(wav), step_samples):
end = min(start + chunk_samples, len(wav))
chunk = wav[start:end]
temp_chunk_path = f"temp_vc_chunk_{start}_{end}.wav"
sf.write(temp_chunk_path, chunk, model_sr)
out_chunk = vc_model.generate(
temp_chunk_path,
target_voice_path=target_voice_audio_path,
apply_watermark=not disable_watermark,
pitch_shift=pitch_shift
)
out_chunk_np = out_chunk.squeeze(0).numpy()
out_chunks.append(out_chunk_np)
os.remove(temp_chunk_path)
# Crossfade join as before...
result = out_chunks[0]
for i in range(1, len(out_chunks)):
overlap = min(overlap_samples, len(out_chunks[i]), len(result))
if overlap > 0:
fade_out = np.linspace(1, 0, overlap)
fade_in = np.linspace(0, 1, overlap)
result[-overlap:] = result[-overlap:] * fade_out + out_chunks[i][:overlap] * fade_in
result = np.concatenate([result, out_chunks[i][overlap:]])
else:
result = np.concatenate([result, out_chunks[i]])
return model_sr, result
def default_settings():
return {
"text_input": """Three Rings for the Elven-kings under the sky,
Seven for the Dwarf-lords in their halls of stone,
Nine for Mortal Men doomed to die,
One for the Dark Lord on his dark throne
In the Land of Mordor where the Shadows lie.
One Ring to rule them all, One Ring to find them,
One Ring to bring them all and in the darkness bind them
In the Land of Mordor where the Shadows lie.""",
"separate_files_checkbox": False,
"export_format_checkboxes": ["flac", "mp3"],
"disable_watermark_checkbox": True,
"num_generations_input": 1,
"num_candidates_slider": 3,
"max_attempts_slider": 3,
"bypass_whisper_checkbox": False,
"whisper_model_dropdown": "medium (~5–8 GB OpenAI / ~2.5–4.5 GB faster-whisper)",
"use_faster_whisper_checkbox": True,
"enable_parallel_checkbox": True,
"use_longest_transcript_on_fail_checkbox": True,
"num_parallel_workers_slider": 4,
"exaggeration_slider": 0.5,
"cfg_weight_slider": 1.0,
"temp_slider": 0.75,
"seed_input": 0,
"enable_batching_checkbox": False,
"smart_batch_short_sentences_checkbox": True,
"to_lowercase_checkbox": True,
"normalize_spacing_checkbox": True,
"fix_dot_letters_checkbox": True,
"remove_reference_numbers_checkbox": True,
"use_auto_editor_checkbox": False,
"keep_original_checkbox": False,
"threshold_slider": 0.06,
"margin_slider": 0.2,
"normalize_audio_checkbox": False,
"normalize_method_dropdown": "ebu",
"normalize_level_slider": -24,
"normalize_tp_slider": -2,
"normalize_lra_slider": 7,
"sound_words_field": "",
"use_pyrnnoise_checkbox": False,
}
settings = load_settings()
# Download both punkt and punkt_tab if missing
try:
nltk.data.find('tokenizers/punkt')
except LookupError:
nltk.download('punkt')
#try:
# nltk.data.find('tokenizers/punkt_tab')
#except LookupError:
# nltk.download('punkt_tab')
os.environ["CUDA_LAUNCH_BLOCKING"] = "0"
# Select device: Apple Silicon GPU (MPS) if available, else fallback to CPU
if torch.cuda.is_available():
DEVICE = "cuda"
elif torch.backends.mps.is_available():
DEVICE = "mps"
else:
DEVICE = "cpu"
print(f"🚀 Running on device: {DEVICE}")
# ---- Determinism (CUDA / PyTorch) ----
import os as _os, torch as _torch
_torch.backends.cudnn.benchmark = False
if hasattr(_torch.backends.cudnn, "deterministic"):
_torch.backends.cudnn.deterministic = True
try:
_torch.use_deterministic_algorithms(True, warn_only=True)
except Exception:
pass
_os.environ.setdefault("CUBLAS_WORKSPACE_CONFIG", ":16:8")
if DEVICE == "cuda":
_torch.backends.cuda.matmul.allow_tf32 = False
_torch.backends.cudnn.allow_tf32 = False
# --------------------------------------
MODEL = None
def _free_vram():
"""
Best-effort VRAM/RAM cleanup before (re)initializing heavy models.
Safe to call on CPU-only systems.
"""
try:
torch.cuda.empty_cache()
except Exception:
pass
try:
gc.collect()
except Exception:
pass
def load_whisper_backend(model_name, use_faster_whisper, device):
"""
Load Whisper with VRAM-friendly fallbacks:
CUDA: try float16 -> int8_float16 -> int8
non-CUDA: try int8 -> float32
"""
if use_faster_whisper:
_free_vram() # free memory before constructing Faster-Whisper
if device == "cuda":
candidates = ["float16", "int8_float16", "int8"]
else:
candidates = ["int8", "float32"]
last_err = None
for ct in candidates:
try:
print(f"[DEBUG] Loading faster-whisper model: {model_name} (device={device}, compute_type={ct})")
return FasterWhisperModel(model_name, device=device, compute_type=ct)
except Exception as e:
last_err = e
print(f"[WARN] Failed loading faster-whisper ({ct}): {e}")
raise RuntimeError(
f"Failed to load Faster-Whisper '{model_name}' on device={device}. "
f"Tried compute_types={candidates}. Last error: {last_err}"
)
else:
print(f"[DEBUG] Loading openai-whisper model: {model_name}")
_free_vram() # also free before OpenAI-whisper to reduce fragmentation
return whisper.load_model(model_name, device=device)
def get_or_load_model():
global MODEL
if MODEL is None:
print("Model not loaded, initializing...")
MODEL = ChatterboxTTS.from_pretrained(DEVICE)
if hasattr(MODEL, 'to') and str(MODEL.device) != DEVICE:
MODEL.to(DEVICE)
if hasattr(MODEL, "eval"):
MODEL.eval()
print(f"Model loaded on device: {getattr(MODEL, 'device', 'unknown')}")
return MODEL
try:
get_or_load_model()
except Exception as e:
print(f"CRITICAL: Failed to load model. Error: {e}")
def set_seed(seed: int):
torch.manual_seed(seed)
if DEVICE == "cuda":
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
np.random.seed(seed)
def derive_seed(base_seed: int, chunk_idx: int, cand_idx: int, attempt_idx: int) -> int:
"""
Deterministically derive a 32-bit seed for each (chunk, candidate, attempt)
from the user-supplied base seed. This avoids any use of global random().
"""
# use 64-bit mixing then clamp to 32-bit
mix = (np.uint64(base_seed) * np.uint64(1000003)
+ np.uint64(chunk_idx) * np.uint64(10007)
+ np.uint64(cand_idx) * np.uint64(10009)
+ np.uint64(attempt_idx) * np.uint64(101))
s = int(mix & np.uint64(0xFFFFFFFF))
return s if s != 0 else 1
def normalize_whitespace(text: str) -> str:
return re.sub(r'\s{2,}', ' ', text.strip())
def replace_letter_period_sequences(text: str) -> str:
def replacer(match):
cleaned = match.group(0).rstrip('.')
letters = cleaned.split('.')
return ' '.join(letters)
return re.sub(r'\b(?:[A-Za-z]\.){2,}', replacer, text)
def remove_inline_reference_numbers(text):
# Remove reference numbers after sentence-ending punctuation, but keep the punctuation
pattern = r'([.!?,\"\'”’)\]])(\d+)(?=\s|$)'
return re.sub(pattern, r'\1', text)
def split_into_sentences(text):
# NLTK's Punkt tokenizer handles abbreviations and common English quirks
return sent_tokenize(text)
def split_long_sentence(sentence, max_len=300, seps=None):
"""
Recursively split a sentence into chunks of <= max_len using a sequence of separators.
Tries each separator in order, splitting further as needed.
"""
if seps is None:
seps = [';', ':', '-', ',', ' ']
sentence = sentence.strip()
if len(sentence) <= max_len:
return [sentence]
if not seps:
# Fallback: force split every max_len chars
return [sentence[i:i+max_len].strip() for i in range(0, len(sentence), max_len)]
sep = seps[0]
parts = sentence.split(sep)
if len(parts) == 1:
# Separator not found, try next separator
return split_long_sentence(sentence, max_len, seps=seps[1:])
# Now recursively process each part, joining separator back except for the first
chunks = []
current = parts[0].strip()
for part in parts[1:]:
candidate = (current + sep + part).strip()
if len(candidate) > max_len:
# Split current chunk further with the next separator
chunks.extend(split_long_sentence(current.strip(), max_len, seps=seps[1:]))
current = part.strip()
else:
current = candidate
# Process the last current
if current:
if len(current) > max_len:
chunks.extend(split_long_sentence(current.strip(), max_len, seps=seps[1:]))
else:
chunks.append(current.strip())
return chunks
# Fallback: force split every max_len chars
#return [sentence[i:i+max_len].strip() for i in range(0, len(sentence), max_len)]
def group_sentences(sentences, max_chars=300):
chunks = []
current_chunk = []
current_length = 0
for sentence in sentences:
if not sentence:
print(f"\033[32m[DEBUG] Skipping empty sentence\033[0m")
continue
sentence = sentence.strip()
sentence_len = len(sentence)
print(f"\033[32m[DEBUG] Processing sentence: len={sentence_len}, content='\033[33m{sentence}...'\033[0m")
if sentence_len > 300:
print(f"\033[32m[DEBUG] Splitting overlong sentence of {sentence_len} chars\033[0m")
for chunk in split_long_sentence(sentence, 300):
if len(chunk) > max_chars:
# For extremely long non-breakable segments, just chunk them
for i in range(0, len(chunk), max_chars):
chunks.append(chunk[i:i+max_chars])
else:
chunks.append(chunk)
current_chunk = []
current_length = 0
continue # Skip the rest of the loop for this sentence
if sentence_len > max_chars:
if current_chunk:
chunks.append(" ".join(current_chunk))
print(f"\033[32m[DEBUG] Finalized chunk: {' '.join(current_chunk)}...\033[0m")
chunks.append(sentence)
print(f"\033[32m[DEBUG] Added long sentence as chunk: {sentence}...\033[0m")
current_chunk = []
current_length = 0
elif current_length + sentence_len + (1 if current_chunk else 0) <= max_chars:
current_chunk.append(sentence)
current_length += sentence_len + (1 if current_chunk else 0)
print(f"\033[32m[DEBUG] Adding sentence to chunk: {sentence}...\033[0m")
else:
if current_chunk:
chunks.append(" ".join(current_chunk))
print(f"\033[32m[DEBUG] Finalized chunk: {' '.join(current_chunk)}...\033[0m")
current_chunk = [sentence]
current_length = sentence_len
print(f"\033[32m[DEBUG] Starting new chunk with: {sentence}...\033[0m")
if current_chunk:
chunks.append(" ".join(current_chunk))
print(f"\033[32m[DEBUG] Finalized final chunk: {' '.join(current_chunk)}...\033[0m")
print(f"\033[32m[DEBUG] Total chunks created: {len(chunks)}\033[0m")
for i, chunk in enumerate(chunks):
print(f"\033[32m[DEBUG] Chunk {i}: len={len(chunk)}, content='\033[33m{chunk}...'\033[0m")
return chunks
def smart_append_short_sentences(sentences, max_chars=300):
new_groups = []
i = 0
while i < len(sentences):
current = sentences[i].strip()
if len(current) >= 20:
new_groups.append(current)
i += 1
else:
appended = False
if i + 1 < len(sentences):
next_sentence = sentences[i + 1].strip()
if len(current + " " + next_sentence) <= max_chars:
new_groups.append(current + " " + next_sentence)
i += 2
appended = True
if not appended and new_groups:
if len(new_groups[-1] + " " + current) <= max_chars:
new_groups[-1] += " " + current
i += 1
appended = True
if not appended:
new_groups.append(current)
i += 1
return new_groups
def normalize_with_ffmpeg(input_wav, output_wav, method="ebu", i=-24, tp=-2, lra=7):
if method == "ebu":
loudnorm = f"loudnorm=I={i}:TP={tp}:LRA={lra}"
(
ffmpeg
.input(input_wav)
.output(output_wav, af=loudnorm)
.overwrite_output()
.run(quiet=True)
)
elif method == "peak":
(
ffmpeg
.input(input_wav)
.output(output_wav, af="alimiter=limit=-2dB")
.overwrite_output()
.run(quiet=True)
)
else:
raise ValueError("Unknown normalization method.")
os.replace(output_wav, input_wav)
def _convert_to_pcm48k_mono(input_wav, output_wav, sr=48000):
"""
Convert to 48kHz, mono, s16 PCM for RNNoise (pyrnnoise) best compatibility.
"""
subprocess.run([
"ffmpeg", "-y", "-i", input_wav,
"-ac", "2", "-ar", str(sr), "-sample_fmt", "s16", output_wav
], check=True)
def _run_pyrnnoise(input_wav, output_wav):
"""
Try the pyrnnoise CLI ('denoise') first; if missing or fails, fall back to Python API.
"""
if not _PYRNNOISE_AVAILABLE:
print("[DENOISE] pyrnnoise not available; skipping.")
return False
print("[DENOISE] Running pyrnnoise (RNNoise)…")
# Prefer CLI if present (often faster and lighter on Python mem)
try:
result = subprocess.run(["denoise", input_wav, output_wav], capture_output=True, text=True)
if result.returncode == 0 and os.path.exists(output_wav) and os.path.getsize(output_wav) > 1024:
print(f"[DENOISE] Saved: {output_wav}")
return True
else:
print("[DENOISE] pyrnnoise CLI failed, falling back to Python API…")
except FileNotFoundError:
print("[DENOISE] pyrnnoise CLI not found, using Python API…")
# Python API fallback
rate, data = sf.read(input_wav)
denoiser = pyrnnoise.RNNoise(rate)
denoised = denoiser.process_buffer(data)
sf.write(output_wav, denoised, rate)
print(f"[DENOISE] Saved: {output_wav}")
return True
def _apply_pyrnnoise_in_place(wav_output_path):
"""
Denoise wav_output_path with RNNoise, preserving the original path.
Converts to 48k mono s16 for processing, then converts back to the original sample rate.
"""
try:
original_sr = librosa.get_samplerate(wav_output_path)
except Exception:
# Fallback if librosa can't read it
original_sr = None
tmp_48kmono = wav_output_path.replace(".wav", "_48kmono.wav")
tmp_dn = wav_output_path.replace(".wav", "_dn.wav")
tmp_back = wav_output_path.replace(".wav", "_dn_resamp.wav")
try:
_convert_to_pcm48k_mono(wav_output_path, tmp_48kmono)
ok = _run_pyrnnoise(tmp_48kmono, tmp_dn)
if not ok:
return False
# Convert back to original sample rate (if known), keep mono
if original_sr:
subprocess.run([
"ffmpeg", "-y", "-i", tmp_dn, "-ar", str(original_sr), "-ac", "1", tmp_back
], check=True)
os.replace(tmp_back, wav_output_path)
else:
# If we don't know SR, just adopt the denoised file
os.replace(tmp_dn, wav_output_path)
print(f"[DENOISE] Denoised in-place: {wav_output_path}")
return True
except Exception as e:
print(f"[DENOISE] RNNoise failed: {e}")
return False
finally:
for p in [tmp_48kmono, tmp_dn, tmp_back]:
try:
if os.path.exists(p):
os.remove(p)
except Exception:
pass
def get_wav_duration(path):
try:
return librosa.get_duration(filename=path)
except Exception as e:
print(f"[ERROR] librosa.get_duration failed: {e}")
return float('inf')
def normalize_for_compare_all_punct(text):
text = re.sub(r'[–—-]', ' ', text)
text = re.sub(rf"[{re.escape(string.punctuation)}]", '', text)
text = re.sub(r'\s+', ' ', text)
return text.lower().strip()
def fuzzy_match(text1, text2, threshold=0.85):
t1 = normalize_for_compare_all_punct(text1)
t2 = normalize_for_compare_all_punct(text2)
seq = difflib.SequenceMatcher(None, t1, t2)
return seq.ratio() >= threshold
def parse_sound_word_field(user_input):
# Accepts comma or newline separated, allows 'sound=>replacement'
lines = [l.strip() for l in user_input.split('\n') if l.strip()]
result = []
for line in lines:
if '=>' in line:
pattern, replacement = line.split('=>', 1)
result.append((pattern.strip(), replacement.strip()))
else:
result.append((line, '')) # Remove (replace with empty string)
return result
def smart_remove_sound_words(text, sound_words):
for pattern, replacement in sound_words:
if replacement:
# 1. Handle possessive: "Baggins’" or "Baggins'" (optionally with s or S after apostrophe)
text = re.sub(
r'(?i)(%s)([’\']s?)' % re.escape(pattern),
lambda m: replacement + "'s" if m.group(2) else replacement,
text
)
# 2. Replace word in quotes
text = re.sub(
r'(["\'])%s(["\'])' % re.escape(pattern),
lambda m: f"{m.group(1)}{replacement}{m.group(2)}",
text,
flags=re.IGNORECASE
)
# If pattern is a punctuation character (like dash), replace all
if all(char in "-–—" for char in pattern.strip()):
text = re.sub(re.escape(pattern), replacement, text)
else:
# 3. Replace as whole word (not in quotes)
text = re.sub(
r'\b%s\b' % re.escape(pattern),
replacement,
text,
flags=re.IGNORECASE
)
else:
# Remove only the pattern itself, not adjacent spaces
text = re.sub(
r'%s' % re.escape(pattern),
'',
text,
flags=re.IGNORECASE
)
# --- Fix accidental joining of words caused by quote removal ---
# Add a space if a letter is next to a letter and was separated by removed quote
#text = re.sub(r'(\w)([’\'"“”‘’])(\w)', r'\1 \3', text)
# Add a space between lowercase and uppercase, likely joined words (e.g., rainbowPride)
text = re.sub(r'([a-z])([A-Z])', r'\1 \2', text)
# --- Clean up doubled-up commas and extra spaces ---
text = re.sub(r'([,\s]+,)+', ',', text)
text = re.sub(r',\s*,+', ',', text)
text = re.sub(r'\s{2,}', ' ', text)
text = re.sub(r'(\s+,|,\s+)', ', ', text)
text = re.sub(r'(^|[\.!\?]\s*),+', r'\1', text)
text = re.sub(r',+\s*([\.!\?])', r'\1', text)
return text.strip()
def whisper_check_mp(candidate_path, target_text, whisper_model, use_faster_whisper=False):
import difflib
import re
import string
import os
try:
print(f"\033[32m[DEBUG] Whisper checking: {candidate_path}\033[0m")
if use_faster_whisper:
segments, info = whisper_model.transcribe(candidate_path)
transcribed = "".join([seg.text for seg in segments]).strip().lower()
else:
result = whisper_model.transcribe(candidate_path)
transcribed = result['text'].strip().lower()
print(f"\033[32m[DEBUG] Whisper transcription: '\033[33m{transcribed}' for candidate '{os.path.basename(candidate_path)}'\033[0m")
score = difflib.SequenceMatcher(
None,
normalize_for_compare_all_punct(transcribed),
normalize_for_compare_all_punct(target_text.strip().lower())
).ratio()
print(f"\033[32m[DEBUG] Score: {score:.3f} (target: '\033[33m{target_text}')\033[0m")
return (candidate_path, score, transcribed)
except Exception as e:
print(f"[ERROR] Whisper transcription failed for {candidate_path}: {e}")
return (candidate_path, 0.0, f"ERROR: {e}")
def process_one_chunk(
model, sentence_group, idx, gen_index, this_seed,
audio_prompt_path_input, exaggeration_input, temperature_input, cfgw_input,
disable_watermark, num_candidates_per_chunk, max_attempts_per_candidate,
bypass_whisper_checking,
retry_attempt_number=1
):
candidates = []
try:
if not sentence_group.strip():
print(f"\033[32m[DEBUG] Skipping empty sentence group at index {idx}\033[0m")
return (idx, candidates)
if len(sentence_group) > 300:
print(f"\033[33m[WARNING] Very long sentence group at index {idx} (len={len(sentence_group)}); proceeding anyway.\033[0m")
print(f"\033[32m[DEBUG] Processing group {idx}: len={len(sentence_group)}:\033[33m {sentence_group}\033[0m")
for cand_idx in range(num_candidates_per_chunk):
for attempt in range(max_attempts_per_candidate):
candidate_seed = derive_seed(this_seed, idx, cand_idx, attempt)
set_seed(candidate_seed)
try:
print(f"\033[32m[DEBUG] Generating candidate {cand_idx+1} attempt {attempt+1} for chunk {idx}...\033[0m")
# print(f"[TTS DEBUG] audio_prompt_path passed: {audio_prompt_path_input!r}")
wav = model.generate(
sentence_group,
audio_prompt_path=audio_prompt_path_input,
exaggeration=min(exaggeration_input, 1.0),
temperature=temperature_input,
cfg_weight=cfgw_input,
apply_watermark=not disable_watermark
)
candidate_path = f"temp/gen{gen_index+1}_chunk_{idx:03d}_cand_{cand_idx+1}_try{retry_attempt_number}_seed{candidate_seed}.wav"
torchaudio.save(candidate_path, wav, model.sr)
for _ in range(10):
if os.path.exists(candidate_path) and os.path.getsize(candidate_path) > 1024:
break
time.sleep(0.05)
duration = get_wav_duration(candidate_path)
print(f"\033[32m[DEBUG] Saved candidate {cand_idx+1}, attempt {attempt+1}, duration={duration:.3f}s: {candidate_path}\033[0m")
candidates.append({
'path': candidate_path,
'duration': duration,
'sentence_group': sentence_group,
'cand_idx': cand_idx,
'attempt': attempt,
'seed': candidate_seed,
})
break
except Exception as e:
print(f"[ERROR] Candidate {cand_idx+1} generation attempt {attempt+1} failed: {e}")
except Exception as exc:
print(f"[ERROR] Exception in chunk {idx}: {exc}")
return (idx, candidates)
def process_one_chunk_deterministic(
model, sentence_group, idx, gen_index, this_seed,
audio_prompt_path_input, exaggeration_input, temperature_input, cfgw_input,
disable_watermark, num_candidates_per_chunk, max_attempts_per_candidate,
bypass_whisper_checking,
retry_attempt_number=1
):
"""
Deterministic per-chunk generation that does NOT mutate global RNG.
- If model.generate supports `generator`, use a per-call torch.Generator.
- Else, fallback to a forked RNG scope + manual seeds (still thread-local).
Also logs full tracebacks on failure so we can see the exact cause.
"""
import inspect, traceback
candidates = []
try:
if not sentence_group.strip():
print(f"\033[32m[DEBUG] Skipping empty sentence group at index {idx}\033[0m")
return (idx, candidates)
if len(sentence_group) > 300:
print(f"\033[33m[WARNING] Very long sentence group at index {idx} (len={len(sentence_group)}); proceeding anyway.\033[0m")
print(f"\033[32m[DEBUG] [DET] Processing group {idx}: len={len(sentence_group)}:\033[33m {sentence_group}\033[0m")
# Detect whether model.generate accepts a `generator` argument
supports_generator = False
try:
sig = inspect.signature(model.generate)
supports_generator = ("generator" in sig.parameters)
except Exception:
supports_generator = False
model_device = str(getattr(model, "device", "cpu"))
on_cuda = torch.cuda.is_available() and (model_device == "cuda")
devices = [torch.cuda.current_device()] if on_cuda else []
for cand_idx in range(num_candidates_per_chunk):
for attempt in range(max_attempts_per_candidate):
candidate_seed = derive_seed(this_seed, idx, cand_idx, attempt)
print(f"\033[32m[DEBUG] [DET] Generating cand {cand_idx+1} attempt {attempt+1} for chunk {idx} (seed={candidate_seed}).\033[0m")
try:
if supports_generator and (model_device != "mps"):
# Use a per-call generator on the matching device (CUDA→cuda, otherwise CPU)
gen_device = "cuda" if on_cuda else "cpu"
gen = torch.Generator(device=gen_device)
gen.manual_seed(int(candidate_seed) & 0xFFFFFFFFFFFFFFFF)
wav = model.generate(
sentence_group,
audio_prompt_path=audio_prompt_path_input,
exaggeration=min(exaggeration_input, 1.0),
temperature=temperature_input,
cfg_weight=cfgw_input,
apply_watermark=not disable_watermark,
generator=gen, # isolated RNG
)
else:
# Fallback: fork RNG state locally and seed inside the scope
with torch.random.fork_rng(devices=devices, enabled=True):
torch.manual_seed(int(candidate_seed))
if on_cuda:
torch.cuda.manual_seed_all(int(candidate_seed))
wav = model.generate(
sentence_group,
audio_prompt_path=audio_prompt_path_input,
exaggeration=min(exaggeration_input, 1.0),
temperature=temperature_input,
cfg_weight=cfgw_input,
apply_watermark=not disable_watermark,
)
candidate_path = f"temp/gen{gen_index+1}_chunk_{idx:03d}_cand_{cand_idx+1}_try{retry_attempt_number}_seed{candidate_seed}.wav"
torchaudio.save(candidate_path, wav, model.sr)
# Wait briefly for filesystem consistency
for _ in range(10):
if os.path.exists(candidate_path) and os.path.getsize(candidate_path) > 1024:
break
time.sleep(0.05)
duration = get_wav_duration(candidate_path)
print(f"\033[32m[DEBUG] [DET] Saved cand {cand_idx+1}, attempt {attempt+1}, duration={duration:.3f}s: {candidate_path}\033[0m")
candidates.append({
'path': candidate_path,
'duration': duration,
'sentence_group': sentence_group,
'cand_idx': cand_idx,
'attempt': attempt,
'seed': candidate_seed,
})
# If bypass is ON we can short-circuit after first successful candidate
if bypass_whisper_checking:
break
except Exception as e:
tb = traceback.format_exc()
print(f"[ERROR] Deterministic generation failed for chunk {idx}, cand {cand_idx+1}, attempt {attempt+1}: {e}\n{tb}")
# Continue to next attempt/candidate
except Exception as e:
tb = traceback.format_exc()
print(f"[ERROR] process_one_chunk_deterministic failed for index {idx}: {e}\n{tb}")
return (idx, candidates)
def generate_and_preview(*args):
output_paths = generate_batch_tts(*args)
audio_files = [p for p in output_paths if os.path.splitext(p)[1].lower() in [".wav", ".mp3", ".flac"]]
dropdown_value = audio_files[0] if audio_files else None
return output_paths, gr.update(choices=audio_files, value=dropdown_value), dropdown_value
def update_audio_preview(selected_path):
return selected_path
@spaces.GPU
def generate_batch_tts(
text: str,
text_file,
audio_prompt_path_input,
exaggeration_input: float,
temperature_input: float,
seed_num_input: int,
cfgw_input: float,
use_pyrnnoise: bool,
use_auto_editor: bool,
ae_threshold: float,
ae_margin: float,
export_formats: list,
enable_batching: bool,
to_lowercase: bool,
normalize_spacing: bool,
fix_dot_letters: bool,
remove_reference_numbers: bool,
keep_original_wav: bool,
smart_batch_short_sentences: bool,
disable_watermark: bool,
num_generations: int,
normalize_audio: bool,
normalize_method: str,
normalize_level: float,
normalize_tp: float,
normalize_lra: float,
num_candidates_per_chunk: int,
max_attempts_per_candidate: int,
bypass_whisper_checking: bool,
whisper_model_name: str,
enable_parallel: bool = True,
num_parallel_workers: int = 4,
use_longest_transcript_on_fail: bool = False,
sound_words_field: str = "",
use_faster_whisper: bool = False,
generate_separate_audio_files: bool = False,
) -> list[str]:
print(f"[DEBUG] Received audio_prompt_path_input: {audio_prompt_path_input!r}")
if not audio_prompt_path_input or (isinstance(audio_prompt_path_input, str) and not os.path.isfile(audio_prompt_path_input)):
audio_prompt_path_input = None
model = get_or_load_model()
# PATCH: Get file basename (to prepend) if a text file was uploaded
# Support for multiple file uploads
# PATCH: Get file basename (to prepend) if a text file was uploaded
# Support for multiple file uploads
input_basename = ""
# Robust handling for Gradio's file input (can be None, False, or list containing such)
files = []
if text_file:
files = text_file if isinstance(text_file, list) else [text_file]
# Remove any entry that's not a file-like object with a .name attribute (filters out None, False, bool)
files = [f for f in files if hasattr(f, "name") and isinstance(getattr(f, "name", None), str)]
if files:
# If generating separate audio files per text file:
if generate_separate_audio_files:
all_jobs = []
for fobj in files:
try:
fname = os.path.basename(fobj.name)
base = os.path.splitext(fname)[0]
base = re.sub(r'[^a-zA-Z0-9_\-]', '_', base + "_")
with open(fobj.name, "r", encoding="utf-8") as f:
file_text = f.read()
all_jobs.append((file_text, base))
except Exception as e:
print(f"[ERROR] Failed to read file: {getattr(fobj, 'name', repr(fobj))} | {e}")
# Now process each file separately and collect outputs
all_outputs = []
for job_text, base in all_jobs:
output_paths = process_text_for_tts(
job_text, base,
audio_prompt_path_input,
exaggeration_input, temperature_input, seed_num_input, cfgw_input,
use_pyrnnoise, # <-- add this
use_auto_editor, ae_threshold, ae_margin, export_formats, enable_batching,
to_lowercase, normalize_spacing, fix_dot_letters, remove_reference_numbers, keep_original_wav,
smart_batch_short_sentences, disable_watermark, num_generations,
normalize_audio, normalize_method, normalize_level, normalize_tp,
normalize_lra, num_candidates_per_chunk, max_attempts_per_candidate,
bypass_whisper_checking, whisper_model_name, enable_parallel,
num_parallel_workers, use_longest_transcript_on_fail, sound_words_field, use_faster_whisper
)
all_outputs.extend(output_paths)
return all_outputs # Return list of output files
# ELSE (default: join all text files as one, as before)
all_text = []
basenames = []
for fobj in files:
try:
fname = os.path.basename(fobj.name)
base = os.path.splitext(fname)[0]
base = re.sub(r'[^a-zA-Z0-9_\-]', '_', base)
basenames.append(base)
with open(fobj.name, "r", encoding="utf-8") as f:
all_text.append(f.read())
except Exception as e:
print(f"[ERROR] Failed to read file: {getattr(fobj, 'name', repr(fobj))} | {e}")