Views
No views yet

1import torch
2import soundfile as sf
3from chatterbox.tts import ChatterboxTTS
4from huggingface_hub import hf_hub_download
5from safetensors.torch import load_file
6
7# Configuration
8MODEL_REPO = "Thomcles/Chatterbox-TTS-French"
9CHECKPOINT_FILENAME = "t3_cfg.safetensors"
10OUTPUT_PATH = "output_cloned_voice.wav"
11TEXT_TO_SYNTHESIZE = "Jean-Paul Sartre laisse à la postérité une œuvre considérable, tant littéraire que philosophique, ayant influencée à la fois la vie politique française d'après-guerre et les penseurs de son temps (Merleau-Ponty et Alain Badiou notamment)."
12
13def get_device() -> str:
14 return "cuda" if torch.cuda.is_available() else "cpu"
15
16def download_checkpoint(repo: str, filename: str) -> str:
17 return hf_hub_download(repo_id=repo, filename=filename)
18
19def load_tts_model(repo: str, checkpoint_file: str, device: str) -> ChatterboxTTS:
20 model = ChatterboxTTS.from_pretrained(device=device)
21 checkpoint_path = download_checkpoint(repo, checkpoint_file)
22 t3_state = load_file(checkpoint_path, device="cpu")
23 model.t3.load_state_dict(t3_state)
24 return model
25
26def synthesize_speech(model: ChatterboxTTS, text: str, audio_prompt_path:str, **kwargs) -> torch.Tensor:
27 with torch.inference_mode():
28 return model.generate(
29 text=text,
30 audio_prompt_path=audio_prompt_path,
31 **kwargs
32 )
33
34def save_audio(waveform: torch.Tensor, path: str, sample_rate: int):
35 sf.write(path, waveform.squeeze().cpu().numpy(), sample_rate)
36
37def main():
38 print("Loading model...")
39 device = get_device()
40 model = load_tts_model(MODEL_REPO, CHECKPOINT_FILENAME, device)
41
42 print(f"Generating speech on {device}...")
43 wav = synthesize_speech(
44 model,
45 TEXT_TO_SYNTHESIZE,
46 audio_prompt_path=None,
47 exaggeration=0.5,
48 temperature=0.6,
49 cfg_weight=0.3
50 )
51
52 print(f"Saving output to: {OUTPUT_PATH}")
53 save_audio(wav, OUTPUT_PATH, model.sr)
54 print("Done.")
55
56if __name__ == "__main__":
57 main()