pocket_tts.models.tts_model.TTSModel
Bases: Module
Source code in pocket_tts/models/tts_model.py
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 | |
generate_audio(model_state, text_to_generate, max_tokens=MAX_TOKEN_PER_CHUNK, frames_after_eos=None, copy_state=True)
Generate complete audio tensor from text input.
This method generates the full audio output for the given text prompt and returns it as a single tensor. It internally uses the streaming generation method but collects all chunks before returning.
This method is NOT thread-safe; separate model instances should be used for concurrent generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_state
|
dict
|
Model state dictionary containing hidden states and positional information. Can be obtained from get_state_for_audio_prompt() or init_states(). The state may be modified during generation. |
required |
text_to_generate
|
str
|
Input text to convert to speech. The text will be automatically formatted (capitalization, punctuation) for optimal generation quality. |
required |
frames_after_eos
|
int | None
|
Number of additional frames to generate after detecting end-of-sequence. If None, automatically determined based on text length (1-3 frames). |
None
|
copy_state
|
bool
|
Whether to create a deep copy of the model state before generation. If True, preserves the original state for reuse. If False, modifies the input state in-place. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: Generated audio tensor with shape [channels, samples]
at the model's sample rate (typically 24kHz). The audio is
normalized and ready for playback or saving.
You can get the sample rate from the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If text_to_generate is empty or invalid. |
RuntimeError
|
If generation fails due to model errors. |
Example
from pocket_tts import TTSModel
model = TTSModel.load_model()
voice_state = model.get_state_for_audio_prompt("hf://kyutai/tts-voices/alba-mackenna/casual.wav")
# Generate audio
audio = model.generate_audio(voice_state, "Hello world!", frames_after_eos=2, copy_state=True)
print(f"Generated audio shape: {audio.shape}")
print(f"Audio duration: {audio.shape[-1] / model.sample_rate:.2f} seconds")
Source code in pocket_tts/models/tts_model.py
generate_audio_stream(model_state, text_to_generate, max_tokens=MAX_TOKEN_PER_CHUNK, frames_after_eos=None, copy_state=True)
Generate audio streaming chunks from text input.
This method generates audio from text and yields chunks as they become available, enabling real-time playback or processing. It uses multithreading to parallelize generation and decoding for optimal performance. This method is NOT thread-safe; separate model instances should be used for concurrent generation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_state
|
dict
|
Model state dictionary containing hidden states and positional information. Can be obtained from get_state_for_audio_prompt() or init_states(). The state may be modified during generation. |
required |
text_to_generate
|
str
|
Input text to convert to speech. The text will be automatically formatted (capitalization, punctuation) for optimal generation quality. |
required |
frames_after_eos
|
int | None
|
Number of additional frames to generate after detecting end-of-sequence. If None, automatically determined based on text length (1-3 frames). Defaults to None. |
None
|
copy_state
|
bool
|
Whether to create a deep copy of the model state before generation. If True, preserves the original state for reuse. If False, modifies the input state in-place. Defaults to True. |
True
|
Yields:
| Type | Description |
|---|---|
|
torch.Tensor: Audio chunks with shape [samples] at the model's sample rate (typically 24kHz). Chunks are yielded as soon as they are decoded, enabling real-time streaming. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If text_to_generate is empty or invalid. |
RuntimeError
|
If generation fails due to model errors or threading issues. |
Example
from pocket_tts import TTSModel
model = TTSModel.load_model()
voice_state = model.get_state_for_audio_prompt("hf://kyutai/tts-voices/alba-mackenna/casual.wav")
# Stream generation
for chunk in model.generate_audio_stream(voice_state, "Long text content..."):
# Process each chunk as it's generated
print(f"Generated chunk: {chunk.shape[0]} samples")
# Could save chunks to file or play in real-time
Note
This method uses multithreading to parallelize latent generation and audio decoding. Generation performance is logged including real-time factor (RTF) metrics.
Source code in pocket_tts/models/tts_model.py
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 | |
get_state_for_audio_prompt(audio_conditioning, truncate=False)
Create model state conditioned on audio prompt for continuation.
This method processes an audio prompt and creates a model state that captures the acoustic characteristics (speaker voice, style, prosody) for use in subsequent text-to-speech generation. The resulting state enables voice cloning and audio continuation with speaker consistency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
audio_conditioning
|
Path | str | Tensor
|
Audio prompt to condition (or .safetensors to load). Can be: - Path: Local file path to audio file (or .safetensors) - str: URL to download audio file (or .safetensors) from - torch.Tensor: Pre-loaded audio tensor with shape [channels, samples] |
required |
truncate
|
bool
|
Whether to truncate long audio prompts to 30 seconds. Helps prevent memory issues with very long inputs. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Model state dictionary containing hidden states and positional
information conditioned on the audio prompt. This state can be
passed to |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If audio file path doesn't exist. |
ValueError
|
If audio tensor is invalid or empty. |
RuntimeError
|
If audio processing or encoding fails. |
Example
from pocket_tts import TTSModel
model = TTSModel.load_model()
# From HuggingFace URL
voice_state = model.get_state_for_audio_prompt("hf://kyutai/tts-voices/alba-mackenna/casual.wav")
# From local file
voice_state = model.get_state_for_audio_prompt("./my_voice.wav")
# Reload state from a .safetensors file (much faster than extracting from an audio file)
voice_state = model.get_state_for_audio_prompt("./my_voices.safetensors")
# From HTTP URL
voice_state = model.get_state_for_audio_prompt(
"https://huggingface.co/kyutai/tts-voices/resolve"
"/main/expresso/ex01-ex02_default_001_channel1_168s.wav"
)
Note
- Audio is automatically resampled to the model's sample rate (24kHz)
- The audio is encoded using the Mimi compression model and projected to the flow model's latent space
- Processing time is logged for performance monitoring
- The state preserves speaker characteristics for voice cloning
Source code in pocket_tts/models/tts_model.py
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 | |
load_model(config=DEFAULT_VARIANT, temp=DEFAULT_TEMPERATURE, lsd_decode_steps=DEFAULT_LSD_DECODE_STEPS, noise_clamp=DEFAULT_NOISE_CLAMP, eos_threshold=DEFAULT_EOS_THRESHOLD)
classmethod
Load a pre-trained TTS model with specified configuration.
This class method loads a complete TTS model including the flow language model and Mimi compression model from pre-trained weights. The model is initialized with the specified generation parameters and ready for inference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
str | Path
|
a path to a custom YAML config file saved locally (e.g., C://pocket_tts/pocket_tts_config.yaml) or a model variant identifier (e.g., '610b0b2c'; must match a YAML file in the config directory). |
DEFAULT_VARIANT
|
temp
|
float | int
|
Sampling temperature for generation. Higher values produce more diverse but potentially lower quality output. |
DEFAULT_TEMPERATURE
|
lsd_decode_steps
|
int
|
Number of steps for Lagrangian Self Distillation decoding. More steps can improve quality but increase computation. |
DEFAULT_LSD_DECODE_STEPS
|
noise_clamp
|
float | int | None
|
Maximum value for noise sampling. If None, no clamping is applied. Helps prevent extreme values in generation. |
DEFAULT_NOISE_CLAMP
|
eos_threshold
|
float
|
Threshold for end-of-sequence detection. Higher values make the model more likely to continue generating. |
DEFAULT_EOS_THRESHOLD
|
Returns:
| Name | Type | Description |
|---|---|---|
TTSModel |
Self
|
Fully initialized model with loaded weights on cpu, ready for text-to-speech generation. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified config file or model weights are not found. |
ValueError
|
If the configuration is invalid or incompatible. |