multimodalart HF Staff commited on
Commit
769a6e3
·
verified ·
1 Parent(s): af05b73

Upload app.py with huggingface_hub

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