Thomcles commited on
Commit
38e0d26
·
verified ·
1 Parent(s): 43d5156

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +47 -22
README.md CHANGED
@@ -40,29 +40,54 @@ import soundfile as sf
40
  from chatterbox.tts import ChatterboxTTS
41
  from huggingface_hub import hf_hub_download
42
  from safetensors.torch import load_file
43
- MODEL_REPO = "Thomcles/Chatterbox-TTS-French"
44
- T3_CHECKPOINT_FILE = "t3_cfg.safetensors"
45
- device = "cuda" if torch.cuda.is_available() else "cpu"
46
- model = ChatterboxTTS.from_pretrained(device=device)
47
- print("Downloading and applying German patch...")
48
- checkpoint_path = hf_hub_download(repo_id=MODEL_REPO, filename=T3_CHECKPOINT_FILE)
49
- t3_state = load_file(checkpoint_path, device="cpu")
50
- model.t3.load_state_dict(t3_state)
51
- print("Patch applied successfully.")
52
- text = "Tief im verwunschenen Wald, wo die Bäume uralte Geheimnisse flüsterten, lebte ein kleiner Gnom namens Fips, der die Sprache der Tiere verstand."
53
- reference_audio_path = "/content/uitoll.mp3"
54
- output_path = "output_cloned_voice.wav"
55
- print("Generating speech...")
56
- with torch.inference_mode():
57
- wav = model.generate(
58
- text,
59
- audio_prompt_path=reference_audio_path,
60
- exaggeration=0.5,
61
- temperature=0.6,
62
- cfg_weight=0.3,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  )
64
- sf.write(output_path, wav.squeeze().cpu().numpy(), model.sr)
65
- print(f"Audio saved to {output_path}")
 
 
 
 
 
66
  ```
67
 
68
 
 
40
  from chatterbox.tts import ChatterboxTTS
41
  from huggingface_hub import hf_hub_download
42
  from safetensors.torch import load_file
43
+
44
+ # Configuration
45
+ MODEL_REPO = "Thomcles/Chatterbox-TTS-French"
46
+ CHECKPOINT_FILENAME = "t3_cfg.safetensors"
47
+ OUTPUT_PATH = "output_cloned_voice.wav"
48
+ TEXT_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)."
49
+
50
+ def get_device() -> str:
51
+ return "cuda" if torch.cuda.is_available() else "cpu"
52
+
53
+ def download_checkpoint(repo: str, filename: str) -> str:
54
+ return hf_hub_download(repo_id=repo, filename=filename)
55
+
56
+ def load_tts_model(repo: str, checkpoint_file: str, device: str) -> ChatterboxTTS:
57
+ model = ChatterboxTTS.from_pretrained(device=device)
58
+ checkpoint_path = download_checkpoint(repo, checkpoint_file)
59
+ t3_state = load_file(checkpoint_path, device="cpu")
60
+ model.t3.load_state_dict(t3_state)
61
+ return model
62
+
63
+ def synthesize_speech(model: ChatterboxTTS, text: str, audio_prompt_path:str, **kwargs) -> torch.Tensor:
64
+ with torch.inference_mode():
65
+ return model.generate(text, audio_prompt_path, **kwargs)
66
+
67
+ def save_audio(waveform: torch.Tensor, path: str, sample_rate: int):
68
+ sf.write(path, waveform.squeeze().cpu().numpy(), sample_rate)
69
+
70
+ def main():
71
+ print("Loading model...")
72
+ device = get_device()
73
+ model = load_tts_model(MODEL_REPO, CHECKPOINT_FILENAME, device)
74
+
75
+ print(f"Generating speech on {device}...")
76
+ wav = synthesize_speech(
77
+ model,
78
+ TEXT_TO_SYNTHESIZE,
79
+ audio_prompt_path=None
80
+ exaggeration=0.5,
81
+ temperature=0.6,
82
+ cfg_weight=0.3
83
  )
84
+
85
+ print(f"Saving output to: {OUTPUT_PATH}")
86
+ save_audio(wav, OUTPUT_PATH, model.sr)
87
+ print("Done.")
88
+
89
+ if __name__ == "__main__":
90
+ main()
91
  ```
92
 
93