Instructions to use Folx/chatterbox-ONNX-polish with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Chatterbox
How to use Folx/chatterbox-ONNX-polish with Chatterbox:
# pip install chatterbox-tts import torchaudio as ta from chatterbox.tts import ChatterboxTTS model = ChatterboxTTS.from_pretrained(device="cuda") text = "Ezreal and Jinx teamed up with Ahri, Yasuo, and Teemo to take down the enemy's Nexus in an epic late-game pentakill." wav = model.generate(text) ta.save("test-1.wav", wav, model.sr) # If you want to synthesize with a different voice, specify the audio prompt AUDIO_PROMPT_PATH="YOUR_FILE.wav" wav = model.generate(text, audio_prompt_path=AUDIO_PROMPT_PATH) ta.save("test-2.wav", wav, model.sr) - Notebooks
- Google Colab
- Kaggle
Upload folder using huggingface_hub
Browse files80 unmodified lines818283848586878810 unmodified lines9910010110210310410510610724 unmodified lines1321331341351361371381391401411421431441451461471481491501511521531541 unmodified line1561571581591601611621631641651661671681691701711721731741751761771781791801811821831843 unmodified lines18818919019119219319419519619719819920018 unmodified lines21922022122222322422522622722822923023123223323423523623723820 unmodified lines25926026126226326426512 unmodified lines27827928028128228328480 unmodified lines# Generation parameters — tuned for ONNX multilingual qualitymax_new_tokens=500,exaggeration=0.5, # 0.0-1.0: emotion/intensity control (unique to Chatterbox)cfg_weight=0.0, # CFG not yet supported in ONNX, keep at 0temperature=0.3, # low temp avoids language drift without CFGtop_p=1.0, # nucleus sampling threshold (1.0 = disabled)min_p=0.05, # min probability filter (relative to top token)repetition_penalty=2.0, # higher penalty prevents repetition loops10 unmodified linestarget_voice_path = os.path.join(model_dir, "default_voice.wav")print(f"Text: {text}")print(f"Settings: exaggeration={exaggeration}, cfg_weight={cfg_weight}, "f"temperature={temperature}, top_p={top_p}, min_p={min_p}, "f"repetition_penalty={repetition_penalty}, seed={seed}")print(f"Voice: {os.path.basename(target_voice_path)}")speech_encoder_session = onnxruntime.InferenceSession(os.path.join(onnx_dir, "speech_encoder.onnx"))24 unmodified linesnum_key_value_heads = 16head_dim = 64use_cfg = cfg_weight > 0.0# Batch size: 2 for CFG (conditional + unconditional), 1 withoutB = 2 if use_cfg else 1# Load text position embeddings for CFG uncond branch# PyTorch zeros text content but KEEPS position embeddings in the uncond branchtext_pos_emb_weight = Noneif use_cfg:import onnxfrom onnx import numpy_helperonnx_model = onnx.load(os.path.join(onnx_dir, "embed_tokens.onnx"), load_external_data=True)for init in onnx_model.graph.initializer:if init.name == "text_pos_emb.weight":text_pos_emb_weight = numpy_helper.to_array(init) # (2050, 1024)breakdel onnx_modelgenerate_tokens = np.array([[START_SPEECH_TOKEN]])# --- Initial step: build full context embeddings ---1 unmodified linecond_emb, prompt_token, ref_x_vector, prompt_feat = speech_encoder_session.run(None, {"audio_values": audio_values})cond_inputs = np.concatenate((cond_emb, inputs_embeds), axis=1)if use_cfg:# CFG unconditional: replace text embeddings with ONLY position embeddings# (matching PyTorch: text_emb[1].zero_() then += text_pos_emb)uncond_embeds = inputs_embeds.copy()text_mask = input_ids[0] < START_SPEECH_TOKEN # which tokens are textfor j in range(len(text_mask)):if text_mask[j]:pos = int(position_ids[0, j])uncond_embeds[0, j, :] = text_pos_emb_weight[pos] # position only, no contentuncond_inputs = np.concatenate((cond_emb, uncond_embeds), axis=1)current_embeds = np.concatenate([cond_inputs, uncond_inputs], axis=0) # (2, seq, dim)else:current_embeds = cond_inputs # (1, seq, dim)_, seq_len, _ = current_embeds.shapepast_key_values = {f"past_key_values.{layer}.{kv}": np.zeros([B, num_key_value_heads, 0, head_dim], dtype=np.float32)for layer in range(num_hidden_layers)for kv in ("key", "value")}attention_mask = np.ones((B, seq_len), dtype=np.int64)# --- Generation loop ---for i in tqdm(range(max_new_tokens), desc="Generating", dynamic_ncols=True):3 unmodified lines**past_key_values,))logits = logits[:, -1, :] # (B, vocab)if use_cfg:# CFG: logits = cond + cfg_weight * (cond - uncond)cond_logits = logits[0:1, :]uncond_logits = logits[1:2, :]logits = cond_logits + cfg_weight * (cond_logits - uncond_logits)# Apply repetition penaltylogits = repetition_penalty_processor(generate_tokens, logits)18 unmodified lines# Embed next speech token for next stepnext_pos = np.full((1, 1), i + 1, dtype=np.int64)next_embeds = embed_tokens_session.run(None, {"input_ids": next_token,"position_ids": next_pos,"exaggeration": np.array([exaggeration], dtype=np.float32),})[0] # (1, 1, dim)# For CFG: duplicate token embedding for both branchesif use_cfg:current_embeds = np.concatenate([next_embeds, next_embeds], axis=0)else:current_embeds = next_embedsattention_mask = np.concatenate([attention_mask, np.ones((B, 1), dtype=np.int64)], axis=1)# Map present.X.key/value outputs -> past_key_values.X.key/value inputsfor layer in range(num_hidden_layers):for ki, kv in enumerate(("key", "value")):out_idx = layer * 2 + ki20 unmodified linesparser.add_argument("--voice", type=str, default=os.path.join(MODEL_DIR, "polish-female-common-voice.wav"))parser.add_argument("--output", "-o", type=str, default=os.path.join(MODEL_DIR, "example_output_polish.wav"))parser.add_argument("--exaggeration", type=float, default=0.5, help="Emotion intensity 0.0-1.0 (default: 0.5)")parser.add_argument("--cfg-weight", type=float, default=0.0, help="Classifier-free guidance weight (default: 0.0, not yet supported)")parser.add_argument("--temperature", type=float, default=0.3, help="Sampling temperature, 0=greedy (default: 0.3)")parser.add_argument("--top-p", type=float, default=1.0, help="Nucleus sampling threshold (default: 1.0)")parser.add_argument("--min-p", type=float, default=0.05, help="Min probability filter (default: 0.05)")12 unmodified linestarget_voice_path=args.voice,output_file_name=args.output,exaggeration=args.exaggeration,cfg_weight=args.cfg_weight,temperature=args.temperature,top_p=args.top_p,min_p=args.min_p,80 unmodified lines8182838485868710 unmodified lines989910010110210310410524 unmodified lines1301311321331341351 unmodified line1371381391401411421431441451461471481491501513 unmodified lines15515615715815916016118 unmodified lines18018118218318418518618718818919019119220 unmodified lines21321421521621721812 unmodified lines23123223323423523680 unmodified lines# Generation parameters — tuned for ONNX multilingual qualitymax_new_tokens=500,exaggeration=0.5, # 0.0-1.0: emotion/intensity control (unique to Chatterbox)temperature=0.3, # low temp avoids language drift (CFG not yet available for ONNX)top_p=1.0, # nucleus sampling threshold (1.0 = disabled)min_p=0.05, # min probability filter (relative to top token)repetition_penalty=2.0, # higher penalty prevents repetition loops10 unmodified linestarget_voice_path = os.path.join(model_dir, "default_voice.wav")print(f"Text: {text}")print(f"Settings: exaggeration={exaggeration}, temperature={temperature}, "f"top_p={top_p}, min_p={min_p}, repetition_penalty={repetition_penalty}, seed={seed}")print(f"Voice: {os.path.basename(target_voice_path)}")speech_encoder_session = onnxruntime.InferenceSession(os.path.join(onnx_dir, "speech_encoder.onnx"))24 unmodified linesnum_key_value_heads = 16head_dim = 64generate_tokens = np.array([[START_SPEECH_TOKEN]])# --- Initial step: build full context embeddings ---1 unmodified linecond_emb, prompt_token, ref_x_vector, prompt_feat = speech_encoder_session.run(None, {"audio_values": audio_values})current_embeds = np.concatenate((cond_emb, inputs_embeds), axis=1)_, seq_len, _ = current_embeds.shapepast_key_values = {f"past_key_values.{layer}.{kv}": np.zeros([1, num_key_value_heads, 0, head_dim], dtype=np.float32)for layer in range(num_hidden_layers)for kv in ("key", "value")}attention_mask = np.ones((1, seq_len), dtype=np.int64)# --- Generation loop ---for i in tqdm(range(max_new_tokens), desc="Generating", dynamic_ncols=True):3 unmodified lines**past_key_values,))logits = logits[:, -1, :]# Apply repetition penaltylogits = repetition_penalty_processor(generate_tokens, logits)18 unmodified lines# Embed next speech token for next stepnext_pos = np.full((1, 1), i + 1, dtype=np.int64)current_embeds = embed_tokens_session.run(None, {"input_ids": next_token,"position_ids": next_pos,"exaggeration": np.array([exaggeration], dtype=np.float32),})[0]attention_mask = np.concatenate([attention_mask, np.ones((1, 1), dtype=np.int64)], axis=1)for layer in range(num_hidden_layers):for ki, kv in enumerate(("key", "value")):out_idx = layer * 2 + ki20 unmodified linesparser.add_argument("--voice", type=str, default=os.path.join(MODEL_DIR, "polish-female-common-voice.wav"))parser.add_argument("--output", "-o", type=str, default=os.path.join(MODEL_DIR, "example_output_polish.wav"))parser.add_argument("--exaggeration", type=float, default=0.5, help="Emotion intensity 0.0-1.0 (default: 0.5)")parser.add_argument("--temperature", type=float, default=0.3, help="Sampling temperature, 0=greedy (default: 0.3)")parser.add_argument("--top-p", type=float, default=1.0, help="Nucleus sampling threshold (default: 1.0)")parser.add_argument("--min-p", type=float, default=0.05, help="Min probability filter (default: 0.05)")12 unmodified linestarget_voice_path=args.voice,output_file_name=args.output,exaggeration=args.exaggeration,temperature=args.temperature,top_p=args.top_p,min_p=args.min_p,
test_polish.py+10-58