forked from devnen/Chatterbox-TTS-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
272 lines (229 loc) · 9.46 KB
/
Copy pathengine.py
File metadata and controls
272 lines (229 loc) · 9.46 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
# File: engine.py
# Core TTS model loading and speech generation logic.
import logging
import random
import numpy as np
import torch
from typing import Optional, Tuple
from pathlib import Path
from chatterbox.tts import ChatterboxTTS # Main TTS engine class
from chatterbox.models.s3gen.const import (
S3GEN_SR,
) # Default sample rate from the engine
# Import the singleton config_manager
from config import config_manager
logger = logging.getLogger(__name__)
# --- Global Module Variables ---
chatterbox_model: Optional[ChatterboxTTS] = None
MODEL_LOADED: bool = False
model_device: Optional[str] = (
None # Stores the resolved device string ('cuda' or 'cpu')
)
def set_seed(seed_value: int):
"""
Sets the seed for torch, random, and numpy for reproducibility.
This is called if a non-zero seed is provided for generation.
"""
torch.manual_seed(seed_value)
if torch.cuda.is_available():
torch.cuda.manual_seed(seed_value)
torch.cuda.manual_seed_all(seed_value) # if using multi-GPU
if torch.backends.mps.is_available():
torch.mps.manual_seed(seed_value)
random.seed(seed_value)
np.random.seed(seed_value)
logger.info(f"Global seed set to: {seed_value}")
def _test_cuda_functionality() -> bool:
"""
Tests if CUDA is actually functional, not just available.
Returns:
bool: True if CUDA works, False otherwise.
"""
if not torch.cuda.is_available():
return False
try:
test_tensor = torch.tensor([1.0])
test_tensor = test_tensor.cuda()
test_tensor = test_tensor.cpu()
return True
except Exception as e:
logger.warning(f"CUDA functionality test failed: {e}")
return False
def _test_mps_functionality() -> bool:
"""
Tests if MPS is actually functional, not just available.
Returns:
bool: True if MPS works, False otherwise.
"""
if not torch.backends.mps.is_available():
return False
try:
test_tensor = torch.tensor([1.0])
test_tensor = test_tensor.to("mps")
test_tensor = test_tensor.cpu()
return True
except Exception as e:
logger.warning(f"MPS functionality test failed: {e}")
return False
def load_model() -> bool:
"""
Loads the TTS model.
This version directly attempts to load from the Hugging Face repository (or its cache)
using `from_pretrained`, bypassing the local `paths.model_cache` directory.
Updates global variables `chatterbox_model`, `MODEL_LOADED`, and `model_device`.
Returns:
bool: True if the model was loaded successfully, False otherwise.
"""
global chatterbox_model, MODEL_LOADED, model_device
if MODEL_LOADED:
logger.info("TTS model is already loaded.")
return True
try:
# Determine processing device with robust CUDA detection and intelligent fallback
device_setting = config_manager.get_string("tts_engine.device", "auto")
if device_setting == "auto":
if _test_cuda_functionality():
resolved_device_str = "cuda"
logger.info("CUDA functionality test passed. Using CUDA.")
elif _test_mps_functionality():
resolved_device_str = "mps"
logger.info("MPS functionality test passed. Using MPS.")
else:
resolved_device_str = "cpu"
logger.info("CUDA and MPS not functional or not available. Using CPU.")
elif device_setting == "cuda":
if _test_cuda_functionality():
resolved_device_str = "cuda"
logger.info("CUDA requested and functional. Using CUDA.")
else:
resolved_device_str = "cpu"
logger.warning(
"CUDA was requested in config but functionality test failed. "
"PyTorch may not be compiled with CUDA support. "
"Automatically falling back to CPU."
)
elif device_setting == "mps":
if _test_mps_functionality():
resolved_device_str = "mps"
logger.info("MPS requested and functional. Using MPS.")
else:
resolved_device_str = "cpu"
logger.warning(
"MPS was requested in config but functionality test failed. "
"PyTorch may not be compiled with MPS support. "
"Automatically falling back to CPU."
)
elif device_setting == "cpu":
resolved_device_str = "cpu"
logger.info("CPU device explicitly requested in config. Using CPU.")
else:
logger.warning(
f"Invalid device setting '{device_setting}' in config. "
f"Defaulting to auto-detection."
)
if _test_cuda_functionality():
resolved_device_str = "cuda"
elif _test_mps_functionality():
resolved_device_str = "mps"
else:
resolved_device_str = "cpu"
logger.info(f"Auto-detection resolved to: {resolved_device_str}")
model_device = resolved_device_str
logger.info(f"Final device selection: {model_device}")
# Get configured model_repo_id for logging and context,
# though from_pretrained might use its own internal default if not overridden.
model_repo_id_config = config_manager.get_string(
"model.repo_id", "ResembleAI/chatterbox"
)
logger.info(
f"Attempting to load model directly using from_pretrained (expected from Hugging Face repository: {model_repo_id_config} or library default)."
)
try:
# Directly use from_pretrained. This will utilize the standard Hugging Face cache.
# The ChatterboxTTS.from_pretrained method handles downloading if the model is not in the cache.
chatterbox_model = ChatterboxTTS.from_pretrained(device=model_device)
# The actual repo ID used by from_pretrained is often internal to the library,
# but logging the configured one provides user context.
logger.info(
f"Successfully loaded TTS model using from_pretrained on {model_device} (expected from '{model_repo_id_config}' or library default)."
)
except Exception as e_hf:
logger.error(
f"Failed to load model using from_pretrained (expected from '{model_repo_id_config}' or library default): {e_hf}",
exc_info=True,
)
chatterbox_model = None
MODEL_LOADED = False
return False
MODEL_LOADED = True
if chatterbox_model:
logger.info(
f"TTS Model loaded successfully on {model_device}. Engine sample rate: {chatterbox_model.sr} Hz."
)
else:
logger.error(
"Model loading sequence completed, but chatterbox_model is None. This indicates an unexpected issue."
)
MODEL_LOADED = False
return False
return True
except Exception as e:
logger.error(
f"An unexpected error occurred during model loading: {e}", exc_info=True
)
chatterbox_model = None
MODEL_LOADED = False
return False
def synthesize(
text: str,
audio_prompt_path: Optional[str] = None,
temperature: float = 0.8,
exaggeration: float = 0.5,
cfg_weight: float = 0.5,
seed: int = 0,
) -> Tuple[Optional[torch.Tensor], Optional[int]]:
"""
Synthesizes audio from text using the loaded TTS model.
Args:
text: The text to synthesize.
audio_prompt_path: Path to an audio file for voice cloning or predefined voice.
temperature: Controls randomness in generation.
exaggeration: Controls expressiveness.
cfg_weight: Classifier-Free Guidance weight.
seed: Random seed for generation. If 0, default randomness is used.
If non-zero, a global seed is set for reproducibility.
Returns:
A tuple containing the audio waveform (torch.Tensor) and the sample rate (int),
or (None, None) if synthesis fails.
"""
global chatterbox_model
if not MODEL_LOADED or chatterbox_model is None:
logger.error("TTS model is not loaded. Cannot synthesize audio.")
return None, None
try:
# Set seed globally if a specific seed value is provided and is non-zero.
if seed != 0:
logger.info(f"Applying user-provided seed for generation: {seed}")
set_seed(seed)
else:
logger.info(
"Using default (potentially random) generation behavior as seed is 0."
)
logger.debug(
f"Synthesizing with params: audio_prompt='{audio_prompt_path}', temp={temperature}, "
f"exag={exaggeration}, cfg_weight={cfg_weight}, seed_applied_globally_if_nonzero={seed}"
)
# Call the core model's generate method
wav_tensor = chatterbox_model.generate(
text=text,
audio_prompt_path=audio_prompt_path,
temperature=temperature,
exaggeration=exaggeration,
cfg_weight=cfg_weight,
)
# The ChatterboxTTS.generate method already returns a CPU tensor.
return wav_tensor, chatterbox_model.sr
except Exception as e:
logger.error(f"Error during TTS synthesis: {e}", exc_info=True)
return None, None
# --- End File: engine.py ---