Views
No views yet
pip install chatterbox-tts==0.1.4download thechatterbox_dhivehi.pyin this repo
1# Assumes chatterbox-tts==0.1.4 and a local chatterbox_dhivehi.py that adds Dhivehi support.
2
3from chatterbox.tts import ChatterboxTTS
4import chatterbox_dhivehi
5from pathlib import Path
6import torchaudio
7import torch
8import numpy as np
9import random
10
11# User settings (edit these)
12CKPT_DIR = "/models/lab/whisper/chatterbox_test/kn_cbox" # checkpoint dir
13REF_WAV = "reference_audio.wav" # optional 3–10s clean reference; "" to disable
14#REF_WAV = ""
15TEXT = "މި ރިޕޯޓާ ގުޅޭ ގޮތުން އެނިމަލް ވެލްފެއާ މިނިސްޓްރީން އަދި ވާހަކައެއް ނުދައްކާ" # sample Dhivehi text
16TEXT = f"{TEXT}, The Animal Welfare Ministry has not yet commented on the report"
17EXAGGERATION = 0.4
18TEMPERATURE = 0.3
19CFG_WEIGHT = 0.7
20SEED = 42
21SAMPLE_RATE = 24000
22OUT_PATH = "out.wav"
23
24# Extend Dhivehi support from local file
25chatterbox_dhivehi.extend_dhivehi()
26
27# Seed for reproducibility
28torch.manual_seed(SEED)
29if torch.cuda.is_available():
30 torch.cuda.manual_seed(SEED)
31 torch.cuda.manual_seed_all(SEED)
32random.seed(SEED)
33np.random.seed(SEED)
34
35# Load model
36device = "cuda" if torch.cuda.is_available() else "cpu"
37print(f"Loading ChatterboxTTS from: {CKPT_DIR} on {device}")
38model = ChatterboxTTS.from_dhivehi(ckpt_dir=Path(CKPT_DIR), device=device)
39print("Model loaded.")
40
41# Generate (reference audio optional)
42print(f"Generating audio... ref={'yes' if REF_WAV else 'no'}")
43gen_kwargs = dict(
44 text=TEXT,
45 exaggeration=EXAGGERATION,
46 temperature=TEMPERATURE,
47 cfg_weight=CFG_WEIGHT,
48)
49
50try:
51 if REF_WAV:
52 gen_kwargs["audio_prompt_path"] = REF_WAV
53 audio = model.generate(**gen_kwargs)
54 else:
55 # Try without reference first; if backend requires audio_prompt_path, fall back to ""
56 try:
57 audio = model.generate(**gen_kwargs)
58 except TypeError:
59 gen_kwargs["audio_prompt_path"] = ""
60 audio = model.generate(**gen_kwargs)
61except Exception as e:
62 raise RuntimeError(f"Generation failed: {e}")
63
64# Save
65torchaudio.save(OUT_PATH, audio, SAMPLE_RATE)
66dur = audio.shape[1] / SAMPLE_RATE
67print(f"Saved {OUT_PATH} ({dur:.2f}s)")Note: English prompts also work with this finetune; quality improves with a clean, representative reference clip.
exaggeration=0.5, cfg_weight=0.5.cfg_weight to ~0.3 for calmer pacing. ([Chatterbox TTS API][2])cfg_weight (~0.3) and higher exaggeration (≥0.7). Higher exaggeration tends to speed up delivery; reducing CFG compensates for pacing. ([Chatterbox TTS API][2])cfg_weight=0. ([Chatterbox TTS API][2])