Spaces:
Running on Zero
Running on Zero
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -4,30 +4,150 @@ os.environ.setdefault("NUMBA_DISABLE_CUDA", "1")
|
|
| 4 |
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
import subprocess, sys
|
| 10 |
subprocess.run(
|
| 11 |
[sys.executable, "-m", "pip", "install", "--no-deps",
|
| 12 |
"chatterbox-tts==0.1.7", "chatterbox-flash==0.1.0"],
|
| 13 |
check=True,
|
| 14 |
)
|
| 15 |
-
print(f"After pip install: cuda_available={torch.cuda.is_available()}")
|
| 16 |
|
| 17 |
from chatterbox_flash import ChatterboxFlashTTS
|
| 18 |
-
print(f"After chatterbox import: cuda_available={torch.cuda.is_available()}")
|
| 19 |
|
| 20 |
MODEL_ID = "ResembleAI/chatterbox-flash"
|
| 21 |
-
print(f"Loading model from {MODEL_ID}...")
|
| 22 |
-
tts = ChatterboxFlashTTS.from_pretrained(MODEL_ID, device="cuda", dtype=torch.bfloat16)
|
| 23 |
-
print(f"After model load: cuda_available={torch.cuda.is_available()}")
|
| 24 |
-
print("Model loaded successfully.")
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
demo
|
| 33 |
-
demo.launch(mcp_server=True)
|
|
|
|
| 4 |
|
| 5 |
import spaces
|
| 6 |
import torch
|
| 7 |
+
import numpy as np
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
import subprocess
|
| 11 |
+
import sys
|
| 12 |
|
|
|
|
| 13 |
subprocess.run(
|
| 14 |
[sys.executable, "-m", "pip", "install", "--no-deps",
|
| 15 |
"chatterbox-tts==0.1.7", "chatterbox-flash==0.1.0"],
|
| 16 |
check=True,
|
| 17 |
)
|
|
|
|
| 18 |
|
| 19 |
from chatterbox_flash import ChatterboxFlashTTS
|
|
|
|
| 20 |
|
| 21 |
MODEL_ID = "ResembleAI/chatterbox-flash"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
print(f"Loading Chatterbox-Flash from {MODEL_ID} on CPU...")
|
| 24 |
+
# Load on CPU to avoid ZeroGPU tensors packing issue
|
| 25 |
+
# (the packing step corrupts CUDA state for the worker fork)
|
| 26 |
+
tts = ChatterboxFlashTTS.from_pretrained(
|
| 27 |
+
MODEL_ID, device="cpu", dtype=torch.bfloat16,
|
| 28 |
+
)
|
| 29 |
+
print("Model loaded on CPU successfully.")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@spaces.GPU(duration=120)
|
| 33 |
+
def generate_tts(
|
| 34 |
+
text_input: str,
|
| 35 |
+
audio_prompt_path: str | None = None,
|
| 36 |
+
exaggeration: float = 0.5,
|
| 37 |
+
temperature: float = 0.6,
|
| 38 |
+
cfg_scale: float = 1.0,
|
| 39 |
+
num_steps: int = 10,
|
| 40 |
+
seed_num: int = 0,
|
| 41 |
+
):
|
| 42 |
+
"""Generate speech from text using Chatterbox-Flash block-diffusion TTS."""
|
| 43 |
+
# Move model to GPU inside the @spaces.GPU function
|
| 44 |
+
# (avoids ZeroGPU tensors packing corrupting CUDA state)
|
| 45 |
+
tts.t3.to(device="cuda", dtype=torch.bfloat16)
|
| 46 |
+
tts.s3gen.to("cuda")
|
| 47 |
+
tts.ve.to("cuda")
|
| 48 |
+
tts.device = torch.device("cuda")
|
| 49 |
+
|
| 50 |
+
if seed_num != 0:
|
| 51 |
+
torch.manual_seed(int(seed_num))
|
| 52 |
+
torch.cuda.manual_seed(int(seed_num))
|
| 53 |
+
np.random.seed(int(seed_num))
|
| 54 |
+
|
| 55 |
+
generate_kwargs = {
|
| 56 |
+
"exaggeration": exaggeration,
|
| 57 |
+
"temperature": temperature,
|
| 58 |
+
"cfg_scale": cfg_scale,
|
| 59 |
+
"num_steps": num_steps,
|
| 60 |
+
"backend": "torch",
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
if audio_prompt_path:
|
| 64 |
+
generate_kwargs["audio_prompt_path"] = audio_prompt_path
|
| 65 |
+
|
| 66 |
+
wav = tts.generate(text_input[:300], **generate_kwargs)
|
| 67 |
+
# Move output to CPU before returning
|
| 68 |
+
return (tts.sr, wav.squeeze(0).cpu().numpy())
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
CSS = """
|
| 72 |
+
#col-container { max-width: 1100px; margin: 0 auto; }
|
| 73 |
+
.dark .gradio-container { color: var(--body-text-color); }
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
with gr.Blocks() as demo:
|
| 77 |
+
gr.Markdown(
|
| 78 |
+
"""
|
| 79 |
+
# Chatterbox-Flash TTS
|
| 80 |
+
Prior-calibrated block-diffusion zero-shot TTS by Resemble AI.
|
| 81 |
+
Provide a reference audio clip to clone a voice, or generate with the default voice.
|
| 82 |
+
|
| 83 |
+
[Paper](https://huggingface.co/papers/2605.30748) · [Model](https://huggingface.co/ResembleAI/chatterbox-flash) · [GitHub](https://github.com/resemble-ai/chatterbox-flash)
|
| 84 |
+
"""
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with gr.Row(elem_id="col-container"):
|
| 88 |
+
with gr.Column(scale=3):
|
| 89 |
+
text = gr.Textbox(
|
| 90 |
+
value="Sometimes it's better to just let things slide, you know?",
|
| 91 |
+
label="Text to synthesize (max 300 chars)",
|
| 92 |
+
max_lines=5,
|
| 93 |
+
)
|
| 94 |
+
ref_wav = gr.Audio(
|
| 95 |
+
sources=["upload", "microphone"],
|
| 96 |
+
type="filepath",
|
| 97 |
+
label="Reference Audio (for voice cloning)",
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
with gr.Accordion("Advanced settings", open=False):
|
| 101 |
+
exaggeration = gr.Slider(
|
| 102 |
+
0.25, 2.0, step=0.05,
|
| 103 |
+
label="Exaggeration (0.5=neutral, higher=more expressive)",
|
| 104 |
+
value=0.5,
|
| 105 |
+
)
|
| 106 |
+
temperature = gr.Slider(
|
| 107 |
+
0.05, 2.0, step=0.05,
|
| 108 |
+
label="Temperature",
|
| 109 |
+
value=0.6,
|
| 110 |
+
)
|
| 111 |
+
cfg_scale = gr.Slider(
|
| 112 |
+
0.2, 1.0, step=0.05,
|
| 113 |
+
label="CFG Scale",
|
| 114 |
+
value=1.0,
|
| 115 |
+
)
|
| 116 |
+
num_steps = gr.Slider(
|
| 117 |
+
1, 30, step=1,
|
| 118 |
+
label="Denoising Steps",
|
| 119 |
+
value=10,
|
| 120 |
+
)
|
| 121 |
+
seed_num = gr.Number(
|
| 122 |
+
value=0, label="Seed (0=random)", precision=0,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
run_btn = gr.Button("Generate", variant="primary")
|
| 126 |
+
|
| 127 |
+
with gr.Column(scale=2):
|
| 128 |
+
audio_output = gr.Audio(label="Output Audio")
|
| 129 |
+
|
| 130 |
+
gr.Examples(
|
| 131 |
+
examples=[
|
| 132 |
+
["Sometimes it's better to just let things slide, you know?"],
|
| 133 |
+
["The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs."],
|
| 134 |
+
["In the depths of winter, I finally learned that within me lay an invincible summer."],
|
| 135 |
+
],
|
| 136 |
+
inputs=[text],
|
| 137 |
+
outputs=[audio_output],
|
| 138 |
+
fn=generate_tts,
|
| 139 |
+
cache_examples=True,
|
| 140 |
+
cache_mode="lazy",
|
| 141 |
+
)
|
| 142 |
|
| 143 |
+
run_btn.click(
|
| 144 |
+
fn=generate_tts,
|
| 145 |
+
inputs=[
|
| 146 |
+
text, ref_wav, exaggeration,
|
| 147 |
+
temperature, cfg_scale, num_steps, seed_num,
|
| 148 |
+
],
|
| 149 |
+
outputs=[audio_output],
|
| 150 |
+
api_name="generate",
|
| 151 |
+
)
|
| 152 |
|
| 153 |
+
demo.launch(mcp_server=True, theme=gr.themes.Citrus(), css=CSS)
|
|
|