Update README.md
Browse files
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 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
-
|
| 65 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|