-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathmain.py
More file actions
439 lines (352 loc) · 12.8 KB
/
Copy pathmain.py
File metadata and controls
439 lines (352 loc) · 12.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
import os
import sys
import shutil
import time
import wave
import gc
import re
import subprocess
import warnings
from datetime import datetime
# Suppress harmless library warnings
os.environ["TOKENIZERS_PARALLELISM"] = "false"
warnings.filterwarnings("ignore", category=UserWarning)
warnings.filterwarnings("ignore", category=FutureWarning)
try:
from mlx_audio.tts.utils import load_model
from mlx_audio.tts.generate import generate_audio
except ImportError:
print("Error: 'mlx_audio' library not found.")
print("Run: source .venv/bin/activate")
sys.exit(1)
# Configuration
BASE_OUTPUT_DIR = os.path.join(os.getcwd(), "outputs")
MODELS_DIR = os.path.join(os.getcwd(), "models")
VOICES_DIR = os.path.join(os.getcwd(), "voices")
# Settings
AUTO_PLAY = True
SAMPLE_RATE = 24000
FILENAME_MAX_LEN = 20
# Model Definitions
MODELS = {
# Pro (1.7B)
"1": {"name": "Custom Voice", "folder": "Qwen3-TTS-12Hz-1.7B-CustomVoice-8bit", "mode": "custom", "output_subfolder": "CustomVoice"},
"2": {"name": "Voice Design", "folder": "Qwen3-TTS-12Hz-1.7B-VoiceDesign-8bit", "mode": "design", "output_subfolder": "VoiceDesign"},
"3": {"name": "Voice Cloning", "folder": "Qwen3-TTS-12Hz-1.7B-Base-8bit", "mode": "clone_manager", "output_subfolder": "Clones"},
# Lite (0.6B)
"4": {"name": "Custom Voice", "folder": "Qwen3-TTS-12Hz-0.6B-CustomVoice-8bit", "mode": "custom", "output_subfolder": "CustomVoice"},
"5": {"name": "Voice Design", "folder": "Qwen3-TTS-12Hz-0.6B-VoiceDesign-8bit", "mode": "design", "output_subfolder": "VoiceDesign"},
"6": {"name": "Voice Cloning", "folder": "Qwen3-TTS-12Hz-0.6B-Base-8bit", "mode": "clone_manager", "output_subfolder": "Clones"},
}
SPEAKER_MAP = {
"English": ["Ryan", "Aiden", "Ethan", "Chelsie", "Serena", "Vivian"],
"Chinese": ["Vivian", "Serena", "Uncle_Fu", "Dylan", "Eric"],
"Japanese": ["Ono_Anna"],
"Korean": ["Sohee"]
}
EMOTION_EXAMPLES = [
"Sad and crying, speaking slowly",
"Excited and happy, speaking very fast",
"Angry and shouting",
"Whispering quietly"
]
def flush_input():
try:
import termios
termios.tcflush(sys.stdin, termios.TCIOFLUSH)
except (ImportError, OSError):
pass
def clean_memory():
gc.collect()
def make_temp_dir():
return f"temp_{int(time.time())}"
def get_smart_path(folder_name):
full_path = os.path.join(MODELS_DIR, folder_name)
if not os.path.exists(full_path):
return None
snapshots_dir = os.path.join(full_path, "snapshots")
if os.path.exists(snapshots_dir):
subfolders = [f for f in os.listdir(snapshots_dir) if not f.startswith('.')]
if subfolders:
return os.path.join(snapshots_dir, subfolders[0])
return full_path
def save_audio_file(temp_folder, subfolder, text_snippet):
save_path = os.path.join(BASE_OUTPUT_DIR, subfolder)
os.makedirs(save_path, exist_ok=True)
timestamp = datetime.now().strftime("%H-%M-%S")
clean_text = re.sub(r'[^\w\s-]', '', text_snippet)[:FILENAME_MAX_LEN].strip().replace(' ', '_') or "audio"
filename = f"{timestamp}_{clean_text}.wav"
final_path = os.path.join(save_path, filename)
source_file = os.path.join(temp_folder, "audio_000.wav")
if os.path.exists(source_file):
shutil.move(source_file, final_path)
print(f"Saved: outputs/{subfolder}/{filename}")
if AUTO_PLAY:
print("Playing...")
try:
subprocess.run(["afplay", final_path], check=False,
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except FileNotFoundError:
pass
if os.path.exists(temp_folder):
shutil.rmtree(temp_folder, ignore_errors=True)
def clean_path(user_input):
path = user_input.strip()
if len(path) > 1 and path[0] in ["'", '"'] and path[-1] == path[0]:
path = path[1:-1]
return path.replace("\\ ", " ")
def get_safe_input(prompt="\nEnter text (or drag .txt file): "):
try:
raw_input = input(prompt).strip()
if raw_input.lower() in ['exit', 'quit', 'q']:
return None
clean_p = clean_path(raw_input)
if os.path.exists(clean_p) and clean_p.endswith(".txt"):
print(f"Reading from: {os.path.basename(clean_p)}")
try:
with open(clean_p, 'r', encoding='utf-8') as f:
return f.read().strip()
except IOError as e:
print(f"Error reading file: {e}")
return None
return raw_input
except KeyboardInterrupt:
flush_input()
return None
def convert_audio_if_needed(input_path):
if not os.path.exists(input_path):
return None
filename = os.path.basename(input_path)
name, ext = os.path.splitext(filename)
if ext.lower() == ".wav":
try:
with wave.open(input_path, 'rb') as f:
if f.getnchannels() > 0:
return input_path
except wave.Error:
pass
temp_wav = os.path.join(os.getcwd(), f"temp_convert_{int(time.time())}.wav")
print(f"Converting '{ext}' to WAV...")
cmd = ["ffmpeg", "-y", "-v", "error", "-i", input_path,
"-ar", str(SAMPLE_RATE), "-ac", "1", "-c:a", "pcm_s16le", temp_wav]
try:
subprocess.run(cmd, check=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
return temp_wav
except (subprocess.CalledProcessError, FileNotFoundError):
print("Error: Could not convert audio. Is ffmpeg installed?")
return None
def get_saved_voices():
if not os.path.exists(VOICES_DIR):
return []
voices = [f.replace(".wav", "") for f in os.listdir(VOICES_DIR) if f.endswith(".wav")]
return sorted(voices)
def enroll_new_voice():
print("\n--- Enroll New Voice ---")
flush_input()
name = input("1. Voice name (e.g. Boss, Mom): ").strip()
if not name:
return
safe_name = re.sub(r'[^\w\s-]', '', name).strip().replace(' ', '_')
ref_input = input("2. Drag & Drop Reference File: ").strip()
raw_path = clean_path(ref_input)
if len(raw_path) > 300 or "\n" in raw_path:
print("Error: Input too long.")
flush_input()
return
clean_wav_path = convert_audio_if_needed(raw_path)
if not clean_wav_path:
return
print("3. Transcript (important for quality):")
ref_text = input(" Type EXACTLY what the audio says: ").strip()
if not os.path.exists(VOICES_DIR):
os.makedirs(VOICES_DIR)
target_wav = os.path.join(VOICES_DIR, f"{safe_name}.wav")
target_txt = os.path.join(VOICES_DIR, f"{safe_name}.txt")
shutil.copy(clean_wav_path, target_wav)
with open(target_txt, "w", encoding='utf-8') as f:
f.write(ref_text)
if clean_wav_path != raw_path and os.path.exists(clean_wav_path):
os.remove(clean_wav_path)
print(f"Voice saved as '{safe_name}'")
def run_custom_session(model_key):
info = MODELS[model_key]
model_path = get_smart_path(info["folder"])
if not model_path:
print("Error: Model not found.")
return
print(f"\nLoading {info['name']}...")
try:
model = load_model(model_path)
except Exception as e:
print(f"Load failed: {e}")
return
print(f"\n--- {info['name']} ---")
speaker = "Vivian"
all_speakers = [n for names in SPEAKER_MAP.values() for n in names]
print("Available Speakers: " + ", ".join(all_speakers))
user_choice = input("\nSelect Speaker (Name): ").strip()
for lang, names in SPEAKER_MAP.items():
if user_choice in names:
speaker = user_choice
break
print(f"Using: {speaker}")
print("\nEmotion Examples:")
for ex in EMOTION_EXAMPLES:
print(f" - {ex}")
base_instruct = input("Emotion Instruction: ").strip() or "Normal tone"
print("\nSpeed:")
print(" 1. Normal (1.0x)")
print(" 2. Fast (1.3x)")
print(" 3. Slow (0.8x)")
sp = input("Choice (1-3): ").strip()
speed = 1.0
if sp == "2":
speed = 1.3
elif sp == "3":
speed = 0.8
while True:
text = get_safe_input()
if text is None:
break
print("Generating...")
temp_dir = make_temp_dir()
try:
generate_audio(model=model, text=text, voice=speaker,
instruct=base_instruct, speed=speed, output_path=temp_dir)
save_audio_file(temp_dir, info["output_subfolder"], text)
except Exception as e:
print(f"Error: {e}")
clean_memory()
def run_design_session(model_key):
info = MODELS[model_key]
model_path = get_smart_path(info["folder"])
if not model_path:
print("Error: Model not found.")
return
print(f"\nLoading {info['name']}...")
try:
model = load_model(model_path)
except Exception as e:
print(f"Load failed: {e}")
return
print(f"\n--- {info['name']} ---")
instruct = input("Describe the voice: ").strip()
if not instruct:
return
while True:
text = get_safe_input()
if text is None:
break
print("Generating...")
temp_dir = make_temp_dir()
try:
generate_audio(model=model, text=text, instruct=instruct, output_path=temp_dir)
save_audio_file(temp_dir, info["output_subfolder"], text)
except Exception as e:
print(f"Error: {e}")
clean_memory()
def run_clone_manager(model_key):
print("\n--- Voice Cloning Manager ---")
print(" 1. Pick from Saved Voices")
print(" 2. Enroll New Voice")
print(" 3. Quick Clone")
print(" 4. Back")
sub_choice = input("\nChoice: ").strip()
if sub_choice == "2":
enroll_new_voice()
return
if sub_choice == "4":
return
info = MODELS[model_key]
model_path = get_smart_path(info["folder"])
if not model_path:
print("Error: Model not found.")
return
print("\nLoading Base Model...")
try:
model = load_model(model_path)
except Exception as e:
print(f"Load failed: {e}")
return
ref_audio, ref_text = None, None
if sub_choice == "1":
saved = get_saved_voices()
if not saved:
print("No saved voices found.")
return
print("\nSaved Voices:")
for i, v in enumerate(saved):
print(f" {i+1}. {v}")
try:
idx = int(input("\nPick Number: ")) - 1
if idx < 0 or idx >= len(saved):
print("Invalid selection.")
return
name = saved[idx]
ref_audio = os.path.join(VOICES_DIR, f"{name}.wav")
txt_path = os.path.join(VOICES_DIR, f"{name}.txt")
if os.path.exists(txt_path):
with open(txt_path, 'r', encoding='utf-8') as f:
ref_text = f.read().strip()
print(f"Loaded: {name}")
except (ValueError, IndexError):
print("Invalid selection.")
return
elif sub_choice == "3":
ref_input = input("\nDrag Reference Audio: ").strip()
raw_path = clean_path(ref_input)
ref_audio = convert_audio_if_needed(raw_path)
if not ref_audio:
return
ref_text = input(" Transcript (Optional): ").strip() or "."
else:
return
while True:
text = get_safe_input(f"\nText for '{os.path.basename(str(ref_audio))}' (or 'exit'): ")
if text is None:
break
print("Cloning...")
temp_dir = make_temp_dir()
try:
generate_audio(model=model, text=text, ref_audio=ref_audio,
ref_text=ref_text, output_path=temp_dir)
save_audio_file(temp_dir, info["output_subfolder"], text)
except Exception as e:
print(f"Error: {e}")
clean_memory()
def main_menu():
print("\n" + "=" * 40)
print(" Qwen3-TTS Manager")
print("=" * 40)
print("\n Pro Models (1.7B - Best Quality)")
print(" ---------------------------------")
print(" 1. Custom Voice")
print(" 2. Voice Design")
print(" 3. Voice Cloning")
print("\n Lite Models (0.6B - Faster)")
print(" ---------------------------")
print(" 4. Custom Voice")
print(" 5. Voice Design")
print(" 6. Voice Cloning")
print("\n q. Exit")
choice = input("\nSelect: ").strip().lower()
if choice == "q":
sys.exit()
if choice not in MODELS:
print("Invalid selection.")
flush_input()
return
mode = MODELS[choice]["mode"]
if mode == "custom":
run_custom_session(choice)
elif mode == "design":
run_design_session(choice)
elif mode == "clone_manager":
run_clone_manager(choice)
if __name__ == "__main__":
try:
os.makedirs(BASE_OUTPUT_DIR, exist_ok=True)
while True:
main_menu()
except KeyboardInterrupt:
print("\nExiting...")