-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapple_tts_cloning.py
More file actions
1392 lines (1208 loc) · 57.8 KB
/
Copy pathapple_tts_cloning.py
File metadata and controls
1392 lines (1208 loc) · 57.8 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
#!/usr/bin/env python3
"""
NeuTTS-Air GUI - Optimized version with parallel processing, GPU acceleration, and session resume
Optimizations: GPU auto-detection (CUDA/MPS), parallel chunk processing (2-4x faster), reduced GC overhead
NOTE: Phonemizer protected by lock for thread-safety
"""
import os
import sys
import io
import json
import subprocess
import importlib
import zipfile
import urllib.request
from typing import Optional, Callable, Dict, List
import threading
import tkinter as tk
from tkinter import ttk, filedialog, messagebox, scrolledtext
import shutil
import glob
import traceback
import platform
from datetime import datetime
import re
import time
import statistics
import gc
from concurrent.futures import ThreadPoolExecutor, as_completed
import multiprocessing
# ==============================
# GPU Detection
# ==============================
def detect_best_device():
"""Detect the best available device (cuda, mps, or cpu)"""
try:
import torch
if torch.cuda.is_available():
print(f"[device] CUDA GPU detected: {torch.cuda.get_device_name(0)}")
return "cuda"
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
print("[device] Apple Metal (MPS) GPU detected")
return "mps"
except ImportError:
pass
print("[device] No GPU detected, using CPU")
return "cpu"
# ==============================
# tiny helpers
# ==============================
def run_cmd(args, check=False) -> int:
try:
subprocess.check_call(args)
return 0
except subprocess.CalledProcessError as e:
if check:
raise
return e.returncode
def pip_install(spec: str, quiet=False, extra_args=None):
cmd = [sys.executable, "-m", "pip", "install", spec]
if extra_args:
cmd.extend(extra_args)
if quiet:
subprocess.check_call(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else:
subprocess.check_call(cmd)
def ensure_dependency(import_name: str, install_spec: str, fallback: Optional[Callable] = None, quiet=False, extra_args=None) -> None:
try:
importlib.import_module(import_name)
return
except ModuleNotFoundError:
print(f"[deps] Missing '{import_name}'. Installing {install_spec} …")
try:
pip_install(install_spec, quiet=quiet, extra_args=extra_args)
except Exception as e:
print(f"[deps] pip install failed for '{import_name}': {e}")
if fallback:
fallback()
else:
raise
importlib.invalidate_caches()
def install_neuttsair_from_source():
print("[deps] Falling back to ZIP: downloading NeuTTS-Air source …")
repo_url = "https://github.com/neuphonic/neutts-air/archive/refs/heads/main.zip"
dest_dir = os.path.expanduser("~/.neuttsair_source")
os.makedirs(dest_dir, exist_ok=True)
zip_path = os.path.join(dest_dir, "neutts_air.zip")
urllib.request.urlretrieve(repo_url, zip_path)
with zipfile.ZipFile(zip_path, "r") as zf:
zf.extractall(dest_dir)
src_path = os.path.join(dest_dir, "neutts-air-main")
if src_path not in sys.path:
sys.path.append(src_path)
print(f"[deps] Added {src_path} to sys.path")
# ==============================
# eSpeak / eSpeak-NG
# ==============================
def _brew_prefix(formula: str) -> Optional[str]:
brew = shutil.which("brew") or "/opt/homebrew/bin/brew"
if not os.path.exists(brew):
return None
try:
out = subprocess.check_output([brew, "--prefix", formula], text=True).strip()
return out if out and os.path.exists(out) else None
except Exception:
return None
def _find_macos_espeak():
candidates = []
for formula in ("espeak", "espeak-ng"):
pref = _brew_prefix(formula)
if pref:
candidates.append(pref)
candidates += ["/opt/homebrew", "/usr/local"]
lib_candidates = []
for root in candidates:
for sub in ("lib", "Cellar/espeak", "opt/espeak/lib", "Cellar/espeak-ng", "opt/espeak-ng/lib"):
lib_candidates.append(os.path.join(root, sub))
dylib = None
for folder in lib_candidates:
for pattern in ("libespeak-ng*.dylib", "libespeak*.dylib"):
for f in glob.glob(os.path.join(folder, pattern)):
if os.path.isfile(f):
dylib = f
break
if dylib:
break
if dylib:
break
bin_dir = None
bins_to_try = [
shutil.which("espeak"),
shutil.which("espeak-ng"),
"/opt/homebrew/bin/espeak",
"/usr/local/bin/espeak",
"/opt/homebrew/bin/espeak-ng",
"/usr/local/bin/espeak-ng",
]
for b in bins_to_try:
if b and os.path.exists(b):
bin_dir = os.path.dirname(b)
break
return dylib, bin_dir
def _find_linux_espeak():
bin_dir = None
for b in (shutil.which("espeak"), shutil.which("espeak-ng")):
if b:
bin_dir = os.path.dirname(b)
break
libs = [
"/usr/lib/libespeak-ng.so",
"/usr/lib/x86_64-linux-gnu/libespeak-ng.so",
"/usr/lib/aarch64-linux-gnu/libespeak-ng.so",
"/usr/local/lib/libespeak-ng.so",
"/lib/x86_64-linux-gnu/libespeak-ng.so",
]
for l in libs:
if os.path.exists(l):
return l, bin_dir
for root in ("/usr/lib", "/usr/local/lib", "/lib"):
found = glob.glob(os.path.join(root, "libespeak*.so*"))
if found:
return found[0], bin_dir
return None, bin_dir
def set_phonemizer_env():
lib_path = None
bin_dir = None
if sys.platform.startswith("darwin"):
lib_path, bin_dir = _find_macos_espeak()
elif sys.platform.startswith("linux"):
lib_path, bin_dir = _find_linux_espeak()
elif os.name == "nt":
lib_path = os.environ.get("PHONEMIZER_ESPEAK_LIBRARY", None)
bin_dir = os.environ.get("PHONEMIZER_ESPEAK_PATH", None)
if lib_path:
os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = lib_path
os.environ["ESPEAK_LIBRARY"] = lib_path
if bin_dir:
os.environ["PHONEMIZER_ESPEAK_PATH"] = bin_dir
try:
if lib_path and sys.platform.startswith("darwin"):
from phonemizer.backend.espeak.wrapper import EspeakWrapper
EspeakWrapper.set_library(lib_path)
except Exception:
pass
def ensure_espeak_installed():
have_bin = shutil.which("espeak") or shutil.which("espeak-ng")
if not have_bin:
print("[deps] Installing eSpeak (required by phonemizer) …")
if sys.platform.startswith("darwin"):
brew = shutil.which("brew") or "/opt/homebrew/bin/brew"
if os.path.exists(brew):
run_cmd([brew, "install", "espeak"])
else:
try:
messagebox.showwarning(
"Manual step required",
"Homebrew not found. Install Homebrew, then run:\n brew install espeak"
)
except Exception:
print("Homebrew not found. Please install eSpeak via Homebrew.")
elif sys.platform.startswith("linux"):
mgr = shutil.which("apt") or shutil.which("apt-get") or shutil.which("dnf") or shutil.which("pacman")
if mgr and ("apt" in mgr or "apt-get" in mgr):
run_cmd(["sudo", "apt-get", "update"])
run_cmd(["sudo", "apt-get", "install", "-y", "espeak-ng", "libespeak-ng1"])
elif mgr and ("dnf" in mgr):
run_cmd(["sudo", "dnf", "install", "-y", "espeak-ng"])
elif mgr and ("pacman" in mgr):
run_cmd(["sudo", "pacman", "-Sy", "espeak-ng"])
else:
try:
messagebox.showwarning(
"Manual step required",
"Please install espeak-ng with your distribution's package manager."
)
except Exception:
print("Please install espeak-ng with your distribution's package manager.")
elif os.name == "nt":
try:
messagebox.showwarning(
"Manual step required",
"On Windows, install eSpeak NG from:\nhttps://github.com/espeak-ng/espeak-ng/releases"
)
except Exception:
print("On Windows, install eSpeak NG from: https://github.com/espeak-ng/espeak-ng/releases")
set_phonemizer_env()
# ==============================
# macOS C++ compilation fix
# ==============================
def setup_macos_compilation_env():
if sys.platform.startswith("darwin"):
xcode_path = "/Applications/Xcode.app/Contents/Developer"
cmdline_tools_path = "/Library/Developer/CommandLineTools"
if os.path.exists(xcode_path):
os.environ["CPLUS_INCLUDE_PATH"] = f"{xcode_path}/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1"
elif os.path.exists(cmdline_tools_path):
os.environ["CPLUS_INCLUDE_PATH"] = f"{cmdline_tools_path}/SDKs/MacOSX.sdk/usr/include/c++/v1"
os.environ["CXXFLAGS"] = "-stdlib=libc++ -std=c++17"
os.environ["LDFLAGS"] = "-stdlib=libc++"
os.environ["CXX"] = "clang++"
os.environ["CC"] = "clang"
def install_llama_cpp_fallback():
print("[deps] Attempting to install llama-cpp-python with pre-built wheel...")
if sys.platform.startswith("darwin") and platform.machine() == "arm64":
try:
pip_install("llama-cpp-python", extra_args=["--no-cache-dir", "--force-reinstall",
"--extra-index-url", "https://abetlen.github.io/llama-cpp-python/whl/metal"])
print("[deps] Successfully installed llama-cpp-python with Metal support")
return
except Exception as e:
print(f"[deps] Metal wheel installation failed: {e}")
try:
pip_install("llama-cpp-python", extra_args=["--no-cache-dir", "--force-reinstall",
"--prefer-binary"])
print("[deps] Successfully installed pre-built llama-cpp-python")
return
except Exception as e:
print(f"[deps] Pre-built wheel installation failed: {e}")
try:
pip_install("llama-cpp-python==0.2.90", extra_args=["--no-cache-dir"])
print("[deps] Successfully installed older version of llama-cpp-python")
return
except Exception:
pass
print("[deps] WARNING: Could not install llama-cpp-python. Will use PyTorch fallback for NeuTTS.")
# ==============================
# Python deps (GGUF-first)
# ==============================
def ensure_all_dependencies():
setup_macos_compilation_env()
ensure_dependency("numpy", "numpy")
ensure_dependency("soundfile", "soundfile")
ensure_dependency("phonemizer", "phonemizer>=3.3.0")
ensure_dependency("librosa", "librosa")
ensure_dependency("huggingface_hub", "huggingface_hub>=0.25.2")
ensure_dependency("safetensors", "safetensors")
try:
ensure_dependency("llama_cpp", "llama-cpp-python", fallback=install_llama_cpp_fallback)
except Exception as e:
print(f"[deps] llama-cpp-python installation failed: {e}")
print("[deps] Will use PyTorch backend instead (may be slower)")
ensure_dependency("perth", "resemble-perth==1.0.1")
ensure_dependency("neucodec", "neucodec>=0.0.4")
ensure_espeak_installed()
ensure_dependency("neuttsair", "git+https://github.com/neuphonic/neutts-air.git",
fallback=install_neuttsair_from_source)
# ==============================
# Session Management
# ==============================
class SessionManager:
def __init__(self):
self.session_dir = os.path.expanduser("~/.neuttsair_sessions")
self.session_file = os.path.join(self.session_dir, "last_session.json")
self.audio_dir = os.path.join(self.session_dir, "audio_chunks")
os.makedirs(self.session_dir, exist_ok=True)
os.makedirs(self.audio_dir, exist_ok=True)
def save_session(self, text: str, chunks: List[str], completed_indices: List[int],
ref_wav: str, ref_txt: str, voice_name: str):
"""Save current generation session"""
try:
session_data = {
"text": text,
"chunks": chunks,
"completed_indices": completed_indices,
"total_chunks": len(chunks),
"ref_wav": ref_wav,
"ref_txt": ref_txt,
"voice_name": voice_name,
"timestamp": datetime.now().isoformat()
}
with open(self.session_file, 'w') as f:
json.dump(session_data, f, indent=2)
print(f"[session] Saved session: {len(completed_indices)}/{len(chunks)} chunks")
except Exception as e:
print(f"[session] Failed to save session: {e}")
def load_session(self) -> Optional[Dict]:
"""Load last session if it exists and is incomplete"""
if not os.path.exists(self.session_file):
return None
try:
with open(self.session_file, 'r') as f:
session_data = json.load(f)
# Check if session is incomplete
if len(session_data['completed_indices']) < session_data['total_chunks']:
return session_data
else:
# Session was completed, clean it up
self.clear_session()
return None
except Exception as e:
print(f"[session] Failed to load session: {e}")
return None
def save_audio_chunk(self, index: int, audio_data: bytes):
"""Save individual audio chunk to disk"""
try:
chunk_path = os.path.join(self.audio_dir, f"chunk_{index:04d}.wav")
with open(chunk_path, 'wb') as f:
f.write(audio_data)
except Exception as e:
print(f"[session] Failed to save audio chunk {index}: {e}")
def load_audio_chunks(self, completed_indices: List[int]) -> List[bytes]:
"""Load completed audio chunks from disk"""
audio_chunks = []
for index in sorted(completed_indices):
chunk_path = os.path.join(self.audio_dir, f"chunk_{index:04d}.wav")
if os.path.exists(chunk_path):
try:
with open(chunk_path, 'rb') as f:
audio_chunks.append(f.read())
except Exception as e:
print(f"[session] Failed to load audio chunk {index}: {e}")
return audio_chunks
def clear_session(self):
"""Clear saved session and audio chunks"""
try:
if os.path.exists(self.session_file):
os.remove(self.session_file)
# Clean up audio chunks
for f in glob.glob(os.path.join(self.audio_dir, "chunk_*.wav")):
try:
os.remove(f)
except Exception:
pass
print("[session] Cleared session data")
except Exception as e:
print(f"[session] Failed to clear session: {e}")
# ==============================
# Voice Profile Management
# ==============================
class VoiceProfileManager:
def __init__(self):
self.profiles_dir = os.path.expanduser("~/.neuttsair_profiles")
self.profiles_file = os.path.join(self.profiles_dir, "profiles.json")
os.makedirs(self.profiles_dir, exist_ok=True)
self.profiles = self.load_profiles()
def load_profiles(self) -> Dict:
if os.path.exists(self.profiles_file):
try:
with open(self.profiles_file, 'r') as f:
return json.load(f)
except Exception:
return {}
return {}
def save_profiles(self):
try:
with open(self.profiles_file, 'w') as f:
json.dump(self.profiles, f, indent=2)
except Exception as e:
print(f"[profiles] Failed to save profiles: {e}")
def add_profile(self, name: str, wav_path: str, txt_path: str) -> bool:
try:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
safe_name = "".join(c for c in name if c.isalnum() or c in (' ', '-', '_')).rstrip()
profile_wav = os.path.join(self.profiles_dir, f"{safe_name}_{timestamp}.wav")
profile_txt = os.path.join(self.profiles_dir, f"{safe_name}_{timestamp}.txt")
shutil.copy2(wav_path, profile_wav)
shutil.copy2(txt_path, profile_txt)
self.profiles[name] = {
"wav": profile_wav,
"txt": profile_txt,
"created": timestamp
}
self.save_profiles()
return True
except Exception as e:
print(f"[profiles] Failed to add profile: {e}")
return False
def get_profile(self, name: str) -> Optional[tuple]:
if name in self.profiles:
profile = self.profiles[name]
if os.path.exists(profile["wav"]) and os.path.exists(profile["txt"]):
return (profile["wav"], profile["txt"])
return None
def list_profiles(self) -> List[str]:
return list(self.profiles.keys())
def delete_profile(self, name: str) -> bool:
if name in self.profiles:
try:
profile = self.profiles[name]
if os.path.exists(profile["wav"]):
os.remove(profile["wav"])
if os.path.exists(profile["txt"]):
os.remove(profile["txt"])
del self.profiles[name]
self.save_profiles()
return True
except Exception as e:
print(f"[profiles] Failed to delete profile: {e}")
return False
# ==============================
# NeuTTS-Air Engine (Singleton for speed)
# ==============================
class TTSEngine:
"""Singleton TTS engine that loads model once and reuses it"""
_instance = None
_phonemizer_lock = threading.Lock() # Lock to protect phonemizer (not thread-safe)
def __init__(self):
if TTSEngine._instance is not None:
raise Exception("Use TTSEngine.get_instance()")
self.tts = None
self.current_ref_wav = None
self.current_ref_txt = None
self.ref_codes = None
self.ref_text = None
@staticmethod
def get_instance():
if TTSEngine._instance is None:
TTSEngine._instance = TTSEngine()
return TTSEngine._instance
def initialize_model(self, force_cpu=False, retry_count=3):
"""Load the TTS model once"""
if self.tts is not None:
return # Already loaded
print("[tts] Loading TTS model (one-time initialization)...")
from neuttsair.neutts import NeuTTSAir
# Detect best device
device = "cpu" if force_cpu else detect_best_device()
gguf_available = False
try:
import llama_cpp
gguf_available = True
except ImportError:
print("[tts] llama-cpp-python not available, will use PyTorch backend")
# Try GGUF backend first (recommended)
if gguf_available:
for attempt in range(retry_count):
try:
# GGUF uses CPU for backbone, but can use GPU for codec
codec_device = "cpu" if force_cpu else device
self.tts = NeuTTSAir(
backbone_repo="neuphonic/neutts-air-q4-gguf",
backbone_device="cpu", # GGUF backbone always on CPU
codec_repo="neuphonic/neucodec",
codec_device=codec_device, # Codec can use GPU for massive speedup
)
print(f"[tts] Using GGUF backend (backbone: cpu, codec: {codec_device})")
print("[tts] Model loaded and ready!")
return
except Exception as _gguf_err:
if attempt < retry_count - 1:
print(f"[tts] GGUF load attempt {attempt + 1} failed: {_gguf_err}")
print(f"[tts] Retrying in 2 seconds...")
time.sleep(2)
else:
print(f"[tts] GGUF load failed after {retry_count} attempts: {_gguf_err}")
print("[tts] Attempting PyTorch fallback...")
gguf_available = False
# PyTorch fallback (if GGUF failed or not available)
if not gguf_available:
try:
ensure_dependency("torch", "torch")
ensure_dependency("transformers", "transformers")
except Exception as e:
raise Exception(
f"Failed to install PyTorch/transformers for fallback: {e}\n"
f"SOLUTION:\n"
f"1. Check your internet connection\n"
f"2. Manually install: pip install torch transformers\n"
f"3. If on macOS: pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cpu"
)
for attempt in range(retry_count):
try:
from neuttsair.neutts import NeuTTSAir as _NeuPT
# PyTorch can use GPU for both
self.tts = _NeuPT(
backbone_repo="neuphonic/neutts-air",
backbone_device=device,
codec_repo="neuphonic/neucodec",
codec_device=device,
)
print(f"[tts] Using PyTorch backend (device: {device})")
print("[tts] Model loaded and ready!")
return
except Exception as pt_err:
if attempt < retry_count - 1:
print(f"[tts] PyTorch load attempt {attempt + 1} failed: {pt_err}")
print(f"[tts] Retrying in 2 seconds...")
time.sleep(2)
else:
# Final failure - provide helpful error message
error_msg = (
f"Failed to load PyTorch backend after {retry_count} attempts.\n\n"
f"Original error: {pt_err}\n\n"
f"TROUBLESHOOTING:\n"
f"1. Check internet connection (needed to download models from HuggingFace)\n"
f"2. Clear HuggingFace cache: rm -rf ~/.cache/huggingface/\n"
f"3. Update transformers: pip install --upgrade transformers huggingface_hub\n"
f"4. Try again in a few minutes (HuggingFace API may be temporarily down)\n"
f"5. Check HuggingFace status: https://status.huggingface.co/\n\n"
f"If issue persists, the model files may need to be downloaded manually."
)
raise Exception(error_msg)
def load_reference(self, ref_wav: str, ref_txt: str):
"""Load and encode reference voice (cached)"""
if self.tts is None:
self.initialize_model()
# Only re-encode if reference changed
if ref_wav != self.current_ref_wav or ref_txt != self.current_ref_txt:
print(f"[tts] Encoding reference voice...")
self.ref_text = open(ref_txt, "r", encoding="utf-8").read().strip()
# CRITICAL: Truncate reference text if too long
# Model has 2048 token context window, reference should use max ~500 tokens (~250 chars)
if len(self.ref_text) > 250:
print(f"[tts] WARNING: Reference text too long ({len(self.ref_text)} chars), truncating to 250 chars")
# Find last complete sentence within 250 chars
truncated = self.ref_text[:250]
last_period = truncated.rfind('.')
if last_period > 100: # Keep at least 100 chars
self.ref_text = truncated[:last_period + 1].strip()
else:
self.ref_text = truncated.strip()
print(f"[tts] Reference text truncated to: '{self.ref_text[:50]}...'")
self.ref_codes = self.tts.encode_reference(ref_wav)
self.current_ref_wav = ref_wav
self.current_ref_txt = ref_txt
print(f"[tts] Reference voice ready (text length: {len(self.ref_text)} chars)")
def synthesize(self, text: str, skip_gc=False) -> Optional[bytes]:
"""Synthesize text using cached model and reference - THREAD-SAFE"""
import soundfile as sf
if self.tts is None or self.ref_codes is None:
raise Exception("Model or reference not loaded")
print(f"[tts] Synthesizing: '{text[:50]}...'")
try:
# Use lock to protect phonemizer (not thread-safe)
# This serializes phonemization but allows GPU codec work to overlap
with self._phonemizer_lock:
wav = self.tts.infer(text, self.ref_codes, self.ref_text)
# Convert to bytes (can happen in parallel after lock is released)
tmp = io.BytesIO()
sf.write(tmp, wav, 24000, format="WAV")
tmp.seek(0)
audio_bytes = tmp.getvalue()
# Cleanup - only GC if requested (reduces overhead)
del wav
del tmp
if not skip_gc:
gc.collect()
return audio_bytes
except Exception as e:
error_str = str(e).lower()
# Check if it's a token limit error
if any(keyword in error_str for keyword in ['token', 'length', 'too long', 'maximum', 'context']):
print(f"[tts] Token limit exceeded, text too long")
raise TokenLimitError(f"Text too long for model: {e}")
else:
raise
class TokenLimitError(Exception):
"""Raised when text exceeds model's token limit"""
pass
def load_default_reference():
base = os.path.expanduser("~/.neuttsair_reference")
os.makedirs(base, exist_ok=True)
wav_path = os.path.join(base, "dave.wav")
txt_path = os.path.join(base, "dave.txt")
if not os.path.exists(wav_path):
print("[ref] Downloading default reference audio …")
urllib.request.urlretrieve(
"https://raw.githubusercontent.com/neuphonic/neutts-air/main/samples/dave.wav",
wav_path
)
if not os.path.exists(txt_path):
urllib.request.urlretrieve(
"https://raw.githubusercontent.com/neuphonic/neutts-air/main/samples/dave.txt",
txt_path
)
return wav_path, txt_path
# ==============================
# GUI
# ==============================
class NeuTTSGui:
def __init__(self, root):
self.root = root
self.root.title("NeuTTS-Air GUI - Optimized (Parallel + GPU)")
self.root.geometry("750x570")
# Initialize components
self.profile_manager = VoiceProfileManager()
self.session_manager = SessionManager()
self.current_audio_sequence = []
self.ref_wav, self.ref_txt = load_default_reference()
self.generation_thread = None
self.is_generating = False
self.tts_engine = TTSEngine.get_instance()
# Timing tracking for ETA
self.chunk_times = []
self.start_time = None
# Session resume tracking
self.current_chunks = []
self.completed_chunk_indices = []
self.resuming_session = False
# Performance settings
# Use 2-4 workers for parallel synthesis
# Phonemizer access is protected by lock, so parallel processing is safe
cpu_count = multiprocessing.cpu_count()
self.max_workers = min(4, max(2, cpu_count // 2)) # 2-4 workers
print(f"[perf] Using {self.max_workers} parallel workers (phonemizer protected by lock)")
self.create_widgets()
self.load_voice_profiles()
# Check for incomplete session
self.check_for_incomplete_session()
# Preload model in background
threading.Thread(target=self._preload_model, daemon=True).start()
def check_for_incomplete_session(self):
"""Check if there's an incomplete session to resume"""
session = self.session_manager.load_session()
if session:
msg = (f"Found incomplete generation from {session['timestamp']}:\n\n"
f"Completed: {len(session['completed_indices'])}/{session['total_chunks']} chunks\n"
f"Voice: {session['voice_name']}\n\n"
f"Would you like to resume where you left off?")
if messagebox.askyesno("Resume Session?", msg):
self.resume_session(session)
def resume_session(self, session: Dict):
"""Resume an incomplete generation session"""
try:
# Restore text
self.textbox.delete("1.0", tk.END)
self.textbox.insert("1.0", session['text'])
# Restore voice profile
if session['voice_name'] != "Default":
# Try to find and select the voice
voices = list(self.voice_combo['values'])
if session['voice_name'] in voices:
self.voice_combo.set(session['voice_name'])
self.ref_wav, self.ref_txt = session['ref_wav'], session['ref_txt']
# Load completed audio chunks
self.current_audio_sequence = self.session_manager.load_audio_chunks(
session['completed_indices']
)
# Set resume state
self.current_chunks = session['chunks']
self.completed_chunk_indices = session['completed_indices']
self.resuming_session = True
# Update UI
completed = len(session['completed_indices'])
total = session['total_chunks']
self.progress_label.config(text=f"{completed}/{total}")
self.progress_var.set((completed / total) * 100)
if self.current_audio_sequence:
self.download_btn.config(state="normal")
self.status_label.config(
text=f"Session restored: {completed}/{total} chunks ready. Click Generate to continue."
)
except Exception as e:
print(f"[session] Failed to resume session: {e}")
traceback.print_exc()
self.session_manager.clear_session()
messagebox.showerror("Resume Failed", f"Could not resume session: {e}")
def _preload_model(self):
"""Preload TTS model in background for faster first generation"""
try:
self.root.after(0, lambda: self.status_label.config(text="Loading TTS model..."))
self.tts_engine.initialize_model()
self.tts_engine.load_reference(self.ref_wav, self.ref_txt)
self.root.after(0, lambda: self.status_label.config(text="Ready - Model loaded!"))
except Exception as e:
error_str = str(e)
print(f"[tts] Preload failed: {e}")
traceback.print_exc()
# Provide user-friendly error message
if "NoneType" in error_str or "HuggingFace" in error_str or "API" in error_str:
msg = (
"Failed to download model from HuggingFace.\n\n"
"This is usually caused by:\n"
"1. Internet connection issues\n"
"2. HuggingFace API being temporarily down\n"
"3. Firewall blocking downloads\n\n"
"SOLUTIONS:\n"
"• Check your internet connection\n"
"• Wait a few minutes and restart the app\n"
"• Check HuggingFace status: status.huggingface.co\n"
"• Clear cache: rm -rf ~/.cache/huggingface/\n\n"
"You can still use the app - it will try to load the model when you click Generate."
)
else:
msg = f"Model preload failed: {error_str}\n\nYou can still use the app - it will try to load when you click Generate."
self.root.after(0, lambda m=msg: messagebox.showwarning("Model Load Warning", m))
self.root.after(0, lambda: self.status_label.config(text="Ready (model not preloaded)"))
def create_widgets(self):
# Top frame - Voice Profile Selection
profile_frame = tk.LabelFrame(self.root, text="Voice Profile", padx=10, pady=5)
profile_frame.pack(fill="x", padx=10, pady=5)
tk.Label(profile_frame, text="Select Voice:").pack(side=tk.LEFT, padx=5)
self.voice_combo = ttk.Combobox(profile_frame, width=30, state="readonly")
self.voice_combo.pack(side=tk.LEFT, padx=5)
self.voice_combo.bind("<<ComboboxSelected>>", self.on_voice_selected)
tk.Button(profile_frame, text="Load Custom", command=self.load_custom_voice).pack(side=tk.LEFT, padx=5)
tk.Button(profile_frame, text="Save Voice", command=self.save_current_voice).pack(side=tk.LEFT, padx=5)
tk.Button(profile_frame, text="Delete", command=self.delete_voice).pack(side=tk.LEFT, padx=5)
# Text input area
text_frame = tk.LabelFrame(self.root, text="Text to Speak", padx=10, pady=5)
text_frame.pack(fill="both", expand=True, padx=10, pady=5)
self.textbox = scrolledtext.ScrolledText(text_frame, wrap=tk.WORD, width=80, height=12)
self.textbox.pack(fill="both", expand=True)
# SHORT default text to avoid token limit issues
self.textbox.insert("1.0", "Hello! Welcome to NeuTTS-Air. This is a test of the text-to-speech system.")
# Control buttons frame
controls_frame = tk.LabelFrame(self.root, text="Controls", padx=10, pady=5)
controls_frame.pack(fill="x", padx=10, pady=5)
# Buttons row
buttons_row = tk.Frame(controls_frame)
buttons_row.pack(fill="x", pady=5)
self.generate_btn = tk.Button(buttons_row, text="🎙️ Generate Audio",
command=self.generate_audio, width=20, height=2)
self.generate_btn.pack(side=tk.LEFT, padx=5)
self.download_btn = tk.Button(buttons_row, text="💾 Download WAV",
command=self.download_audio, width=20, height=2, state="disabled")
self.download_btn.pack(side=tk.LEFT, padx=5)
self.cancel_btn = tk.Button(buttons_row, text="❌ Cancel",
command=self.cancel_generation, width=15, height=2, state="disabled")
self.cancel_btn.pack(side=tk.LEFT, padx=5)
# Progress bar
progress_frame = tk.Frame(controls_frame)
progress_frame.pack(fill="x", pady=5)
tk.Label(progress_frame, text="Progress:").pack(side=tk.LEFT, padx=5)
self.progress_var = tk.DoubleVar()
self.progress_bar = ttk.Progressbar(progress_frame, variable=self.progress_var,
maximum=100, mode='determinate')
self.progress_bar.pack(side=tk.LEFT, fill="x", expand=True, padx=5)
self.progress_label = tk.Label(progress_frame, text="0/0", width=10)
self.progress_label.pack(side=tk.LEFT, padx=5)
# ETA display
eta_frame = tk.Frame(controls_frame)
eta_frame.pack(fill="x", pady=2)
tk.Label(eta_frame, text="ETA:").pack(side=tk.LEFT, padx=5)
self.eta_label = tk.Label(eta_frame, text="--:--", anchor=tk.W, font=("TkDefaultFont", 9))
self.eta_label.pack(side=tk.LEFT, padx=5)
# Status bar
self.status_label = tk.Label(self.root, text="Ready", relief=tk.SUNKEN, anchor=tk.W)
self.status_label.pack(side=tk.BOTTOM, fill=tk.X)
def load_voice_profiles(self):
profiles = ["Default"] + self.profile_manager.list_profiles()
self.voice_combo['values'] = profiles
if profiles:
self.voice_combo.current(0)
def on_voice_selected(self, event=None):
selected = self.voice_combo.get()
if selected == "Default":
self.ref_wav, self.ref_txt = load_default_reference()
self.status_label.config(text="Using default voice")
else:
profile = self.profile_manager.get_profile(selected)
if profile:
self.ref_wav, self.ref_txt = profile
self.status_label.config(text=f"Loaded voice: {selected}")
# Reload reference in background
threading.Thread(target=self._reload_reference, daemon=True).start()
def _reload_reference(self):
"""Reload reference voice in background"""
try:
self.tts_engine.load_reference(self.ref_wav, self.ref_txt)
except Exception as e:
print(f"[tts] Failed to load reference: {e}")
def load_custom_voice(self):
wav_path = filedialog.askopenfilename(
title="Select Reference Audio",
filetypes=[("WAV files", "*.wav"), ("All files", "*.*")]
)
if not wav_path:
return
txt_path = filedialog.askopenfilename(
title="Select Reference Text",
filetypes=[("Text files", "*.txt"), ("All files", "*.*")]
)
if not txt_path:
return
self.ref_wav = wav_path
self.ref_txt = txt_path
self.status_label.config(text="Custom voice loaded")
# Reload reference in background
threading.Thread(target=self._reload_reference, daemon=True).start()
def save_current_voice(self):
if not self.ref_wav or not self.ref_txt:
messagebox.showwarning("No Voice", "Please load a voice first")
return
name = tk.simpledialog.askstring("Save Voice", "Enter a name for this voice:")
if not name:
return
if self.profile_manager.add_profile(name, self.ref_wav, self.ref_txt):
self.load_voice_profiles()
self.voice_combo.set(name)
self.status_label.config(text=f"Saved voice: {name}")
else:
messagebox.showerror("Error", "Failed to save voice profile")
def delete_voice(self):
selected = self.voice_combo.get()
if selected == "Default":
messagebox.showinfo("Info", "Cannot delete default voice")
return
if messagebox.askyesno("Delete Voice", f"Delete voice profile '{selected}'?"):
if self.profile_manager.delete_profile(selected):
self.load_voice_profiles()
self.voice_combo.current(0)
self.status_label.config(text=f"Deleted voice: {selected}")
def _split_into_sentence_chunks(self, text: str, max_chars: int = 150) -> List[str]:
"""Split text into chunks of up to max_chars, ending at sentence boundaries"""
chunks = []
current_pos = 0
text_length = len(text)
while current_pos < text_length:
# Calculate the end position for this chunk
chunk_end = min(current_pos + max_chars, text_length)
# If we're at the end of the text, take everything
if chunk_end == text_length:
chunk = text[current_pos:].strip()
if chunk:
chunks.append(chunk)
break
# Look for the last sentence ending before chunk_end
chunk_text = text[current_pos:chunk_end]
# Find all sentence endings (. ! ?) in this chunk
sentence_endings = []
for match in re.finditer(r'[.!?]\s', chunk_text):
sentence_endings.append(match.end())
if sentence_endings:
# Use the last sentence ending
actual_end = current_pos + sentence_endings[-1]
chunk = text[current_pos:actual_end].strip()
if chunk:
chunks.append(chunk)
current_pos = actual_end
else:
# No sentence ending found, look for other break points (comma, semicolon)
break_points = []
for match in re.finditer(r'[,;:]\s', chunk_text):
break_points.append(match.end())
if break_points:
actual_end = current_pos + break_points[-1]
chunk = text[current_pos:actual_end].strip()
if chunk:
chunks.append(chunk)
current_pos = actual_end
else:
# No good break point, just take up to max_chars at a word boundary
words = chunk_text.split()
if len(words) > 1:
# Take all but the last word to avoid cutting mid-word
safe_text = ' '.join(words[:-1])
actual_end = current_pos + len(safe_text)