File size: 7,079 Bytes
e698a98 7661963 4c11795 e698a98 7661963 e698a98 bae7388 e698a98 7661963 e698a98 1c370d2 e698a98 1c370d2 e698a98 1c370d2 e698a98 1c370d2 e698a98 8a0d801 7661963 e698a98 8a0d801 e698a98 4c11795 8a0d801 4c11795 8a0d801 4c11795 8a0d801 4c11795 8a0d801 e698a98 5cd7466 e698a98 4c11795 1c370d2 4c11795 e698a98 fc6ebf6 e698a98 8a0d801 e698a98 8a0d801 52513e5 8a0d801 e698a98 8a0d801 e698a98 1c370d2 8a0d801 e698a98 375176e e698a98 6899c06 4c11795 6899c06 e698a98 f43d17b 8a0d801 e698a98 8a0d801 e698a98 4c11795 e698a98 5cd7466 1c370d2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | import random
import os
import time
import numpy as np
import torch
import gradio as gr
import spaces
from chatterbox.tts_turbo import ChatterboxTurboTTS
MODEL = ChatterboxTurboTTS.from_pretrained("cuda" )
EVENT_TAGS = [
"[clear throat]", "[sigh]", "[shush]", "[cough]", "[groan]",
"[sniff]", "[gasp]", "[chuckle]", "[laugh]"
]
CUSTOM_CSS = """
.tag-container {
display: flex !important;
flex-wrap: wrap !important;
gap: 8px !important;
margin-top: 5px !important;
margin-bottom: 10px !important;
border: none !important;
background: transparent !important;
}
.tag-btn {
min-width: fit-content !important;
width: auto !important;
height: 32px !important;
font-size: 13px !important;
background: #eef2ff !important;
border: 1px solid #c7d2fe !important;
color: #3730a3 !important;
border-radius: 6px !important;
padding: 0 10px !important;
margin: 0 !important;
box-shadow: none !important;
}
.tag-btn:hover {
background: #c7d2fe !important;
transform: translateY(-1px);
}
"""
INSERT_TAG_JS = """
(tag_val, current_text) => {
const textarea = document.querySelector('#main_textbox textarea');
if (!textarea) return current_text + " " + tag_val;
const start = textarea.selectionStart;
const end = textarea.selectionEnd;
let prefix = " ";
let suffix = " ";
if (start === 0) prefix = "";
else if (current_text[start - 1] === ' ') prefix = "";
if (end < current_text.length && current_text[end] === ' ') suffix = "";
return current_text.slice(0, start) + prefix + tag_val + suffix + current_text.slice(end);
}
"""
def set_seed(seed: int):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
random.seed(seed)
np.random.seed(seed)
def load_text_file(file_path):
"""Load text from uploaded file."""
if file_path is None:
return ""
try:
with open(file_path, 'r', encoding='utf-8') as f:
text = f.read()
return text
except Exception as e:
return f"Error reading file: {str(e)}"
@spaces.GPU
def generate(
input_mode,
file_input,
text_input,
audio_prompt_path,
temperature,
seed_num,
min_p,
top_p,
top_k,
repetition_penalty,
norm_loudness
):
# Start timing
start_time = time.time()
# Determine text source
if input_mode == "Upload File":
if file_input is None:
return None, "No file uploaded"
text = load_text_file(file_input)
if text.startswith("Error reading file:"):
return None, text
else:
text = text_input
if not text or not text.strip():
return None, "No text provided"
if seed_num != 0:
set_seed(int(seed_num))
wav = MODEL.generate(
text,
audio_prompt_path=audio_prompt_path,
temperature=temperature,
min_p=min_p,
top_p=top_p,
top_k=int(top_k),
repetition_penalty=repetition_penalty,
norm_loudness=norm_loudness,
)
# Calculate execution time
end_time = time.time()
execution_time = end_time - start_time
timing_text = f"⏱️ Generation completed in {execution_time:.2f} seconds"
return (MODEL.sr, wav.squeeze(0).cpu().numpy()), timing_text
with gr.Blocks(title="Chatterbox Turbo") as demo:
gr.Markdown("# ⚡ Chatterbox Turbo")
gr.Markdown("Automatic text chunking enabled for texts > 300 characters")
with gr.Row():
with gr.Column():
# Input mode selection
input_mode = gr.Radio(
["Paste Text", "Upload File"],
value="Paste Text",
label="Input Method"
)
# Text input (default visible)
text_input = gr.Textbox(
value="Oh, that's hilarious! [chuckle] Um anyway, we do have a new model in store. It's the SkyNet T-800 series and it's got basically everything. Including AI integration with ChatGPT and all that jazz. Would you like me to get some prices for you?",
label="Text to synthesize",
max_lines=5,
elem_id="main_textbox",
visible=True
)
# File input (initially hidden)
file_input = gr.File(
label="Upload .txt file",
file_types=[".txt"],
type="filepath",
visible=False
)
with gr.Row(elem_classes=["tag-container"]):
for tag in EVENT_TAGS:
btn = gr.Button(tag, elem_classes=["tag-btn"])
btn.click(
fn=None,
inputs=[btn, text_input],
outputs=text_input,
js=INSERT_TAG_JS
)
ref_wav = gr.Audio(
sources=["upload", "microphone"],
type="filepath",
label="Reference Audio File",
value="https://storage.googleapis.com/chatterbox-demo-samples/turbo/2.wav",
)
run_btn = gr.Button("Generate ⚡", variant="primary")
with gr.Column():
audio_output = gr.Audio(label="Output Audio")
timing_output = gr.Textbox(label="Execution Time", interactive=False, show_label=True)
with gr.Accordion("Advanced Options", open=False):
seed_num = gr.Number(value=0, label="Random seed (0 for random)")
temp = gr.Slider(0.05, 2.0, step=.05, label="Temperature", value=0.8)
top_p = gr.Slider(0.00, 1.00, step=0.01, label="Top P", value=0.95)
top_k = gr.Slider(0, 1000, step=10, label="Top K", value=1000)
repetition_penalty = gr.Slider(1.00, 2.00, step=0.05, label="Repetition Penalty", value=1.2)
min_p = gr.Slider(0.00, 1.00, step=0.01, label="Min P (Set to 0 to disable)", value=0.00)
norm_loudness = gr.Checkbox(value=True, label="Normalize Loudness (-27 LUFS)")
# Toggle visibility based on input mode
def update_input_visibility(mode):
return {
text_input: gr.update(visible=(mode == "Paste Text")),
file_input: gr.update(visible=(mode == "Upload File"))
}
input_mode.change(
fn=update_input_visibility,
inputs=[input_mode],
outputs=[text_input, file_input]
)
run_btn.click(
fn=generate,
inputs=[
input_mode,
file_input,
text_input,
ref_wav,
temp,
seed_num,
min_p,
top_p,
top_k,
repetition_penalty,
norm_loudness,
],
outputs=[audio_output, timing_output],
)
if __name__ == "__main__":
demo.queue().launch(
mcp_server=True,
css=CUSTOM_CSS,
ssr_mode=False
)
|