Spaces:
Running
on
Zero
Running
on
Zero
File size: 12,518 Bytes
26e0cd3 |
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
import gradio as gr
import torch
import os
import time
import copy
from pathlib import Path
from typing import Optional, Tuple
import spaces
from vibevoice.modular.modeling_vibevoice_streaming_inference import (
VibeVoiceStreamingForConditionalGenerationInference,
)
from vibevoice.processor.vibevoice_streaming_processor import (
VibeVoiceStreamingProcessor,
)
class VoiceMapper:
"""Maps speaker names to voice file paths"""
def __init__(self):
self.setup_voice_presets()
# Change name according to our preset voice file
new_dict = {}
for name, path in self.voice_presets.items():
if "_" in name:
name = name.split("_")[0]
if "-" in name:
name = name.split("-")[-1]
new_dict[name] = path
self.voice_presets.update(new_dict)
def setup_voice_presets(self):
"""Setup voice presets by scanning the voices directory."""
voices_dir = os.path.join(os.path.dirname(__file__), "demo/voices/streaming_model")
# Check if voices directory exists
if not os.path.exists(voices_dir):
print(f"Warning: Voices directory not found at {voices_dir}")
self.voice_presets = {}
self.available_voices = {}
return
# Scan for all VOICE files in the voices directory
self.voice_presets = {}
# Get all .pt files in the voices directory
pt_files = [
f
for f in os.listdir(voices_dir)
if f.lower().endswith(".pt") and os.path.isfile(os.path.join(voices_dir, f))
]
# Create dictionary with filename (without extension) as key
for pt_file in pt_files:
# Remove .pt extension to get the name
name = os.path.splitext(pt_file)[0]
# Create full path
full_path = os.path.join(voices_dir, pt_file)
self.voice_presets[name] = full_path
# Sort the voice presets alphabetically by name for better UI
self.voice_presets = dict(sorted(self.voice_presets.items()))
# Filter out voices that don't exist (this is now redundant but kept for safety)
self.available_voices = {
name: path for name, path in self.voice_presets.items() if os.path.exists(path)
}
print(f"Found {len(self.available_voices)} voice files in {voices_dir}")
print(f"Available voices: {', '.join(self.available_voices.keys())}")
def get_voice_path(self, speaker_name: str) -> str:
"""Get voice file path for a given speaker name"""
# First try exact match
if speaker_name in self.voice_presets:
return self.voice_presets[speaker_name]
# Try partial matching (case insensitive)
speaker_lower = speaker_name.lower()
for preset_name, path in self.voice_presets.items():
if preset_name.lower() in speaker_lower or speaker_lower in preset_name.lower():
return path
# Default to first voice if no match found
default_voice = list(self.voice_presets.values())[0]
print(
f"Warning: No voice preset found for '{speaker_name}', using default voice: {default_voice}"
)
return default_voice
# Load model and processor directly
print("Loading VibeVoice-Realtime model...")
MODEL_PATH = "microsoft/VibeVoice-Realtime-0.5B"
# Load processor (CPU operation)
PROCESSOR = VibeVoiceStreamingProcessor.from_pretrained(MODEL_PATH)
# Load model on CPU initially (will be moved to GPU by @spaces.GPU decorator)
MODEL = VibeVoiceStreamingForConditionalGenerationInference.from_pretrained(
MODEL_PATH,
torch_dtype=torch.float16,
device_map="cpu",
attn_implementation="sdpa",
)
MODEL.eval()
MODEL.set_ddpm_inference_steps(num_steps=5)
# Initialize voice mapper
VOICE_MAPPER = VoiceMapper()
print("Model loaded successfully!")
@spaces.GPU(duration=60) # Request GPU for 60 seconds
def generate_speech(
text: str,
speaker_name: str,
cfg_scale: float = 1.5,
progress=gr.Progress(),
) -> Tuple[Optional[str], str]:
"""
Generate speech from text using VibeVoice-Realtime with ZeroGPU
Args:
text: Input text to convert to speech
speaker_name: Name of the speaker voice to use
cfg_scale: Classifier-Free Guidance scale (higher = more faithful to text)
progress: Gradio progress tracker
Returns:
Tuple of (audio_path, status_message)
"""
if not text or not text.strip():
return None, "β Error: Please enter some text to convert to speech."
try:
progress(0, desc="Loading voice preset...")
# Clean text
full_script = text.strip().replace("'", "'").replace('"', '"').replace('"', '"')
# Get voice sample
voice_sample = VOICE_MAPPER.get_voice_path(speaker_name)
# Load voice sample to GPU
all_prefilled_outputs = torch.load(
voice_sample, map_location="cuda", weights_only=False
)
progress(0.2, desc="Preparing inputs...")
# Prepare inputs
inputs = PROCESSOR.process_input_with_cached_prompt(
text=full_script,
cached_prompt=all_prefilled_outputs,
padding=True,
return_tensors="pt",
return_attention_mask=True,
)
# Move model and tensors to GPU
MODEL.to("cuda")
for k, v in inputs.items():
if torch.is_tensor(v):
inputs[k] = v.to("cuda")
progress(0.4, desc="Generating speech on GPU...")
# Generate audio
start_time = time.time()
with torch.cuda.amp.autocast(): # Enable automatic mixed precision
outputs = MODEL.generate(
**inputs,
max_new_tokens=None,
cfg_scale=cfg_scale,
tokenizer=PROCESSOR.tokenizer,
generation_config={"do_sample": False},
verbose=False,
all_prefilled_outputs=copy.deepcopy(all_prefilled_outputs)
if all_prefilled_outputs is not None
else None,
)
generation_time = time.time() - start_time
progress(0.8, desc="Saving audio...")
# Calculate metrics
if outputs.speech_outputs and outputs.speech_outputs[0] is not None:
sample_rate = 24000
audio_samples = (
outputs.speech_outputs[0].shape[-1]
if len(outputs.speech_outputs[0].shape) > 0
else len(outputs.speech_outputs[0])
)
audio_duration = audio_samples / sample_rate
rtf = generation_time / audio_duration if audio_duration > 0 else float("inf")
# Save output
output_dir = "./outputs"
os.makedirs(output_dir, exist_ok=True)
output_path = os.path.join(output_dir, f"generated_{int(time.time())}.wav")
PROCESSOR.save_audio(
outputs.speech_outputs[0].cpu(), # Move to CPU for saving
output_path=output_path,
)
progress(1.0, desc="Complete!")
# Create status message
status = f"""β
**Generation Complete!**
π **Metrics:**
- Audio Duration: {audio_duration:.2f}s
- Generation Time: {generation_time:.2f}s
- Real-Time Factor: {rtf:.2f}x
- Speaker: {speaker_name}
- CFG Scale: {cfg_scale}
- Device: ZeroGPU (CUDA)
"""
# Move model back to CPU to free GPU memory
MODEL.to("cpu")
torch.cuda.empty_cache()
return output_path, status
else:
MODEL.to("cpu")
torch.cuda.empty_cache()
return None, "β Error: No audio output generated."
except Exception as e:
import traceback
error_msg = f"β Error during generation:\n{str(e)}\n\n{traceback.format_exc()}"
print(error_msg)
# Clean up GPU memory on error
try:
MODEL.to("cpu")
torch.cuda.empty_cache()
except:
pass
return None, error_msg
# Create Gradio interface
with gr.Blocks(fill_height=True) as demo:
gr.Markdown(
"""
# ποΈ VibeVoice-Realtime Text-to-Speech
Convert text to natural-sounding speech using Microsoft's VibeVoice-Realtime model.
**π Powered by ZeroGPU** - Efficient GPU allocation for fast inference!
<div style="text-align: center; margin-top: 10px;">
<a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank" style="text-decoration: none; color: #4F46E5; font-weight: 600;">
Built with anycoder β¨
</a>
</div>
"""
)
with gr.Row():
with gr.Column(scale=2):
# Input section
text_input = gr.Textbox(
label="Text to Convert",
placeholder="Enter the text you want to convert to speech...",
lines=8,
max_lines=20,
)
with gr.Row():
speaker_dropdown = gr.Dropdown(
choices=list(VOICE_MAPPER.available_voices.keys()),
value=list(VOICE_MAPPER.available_voices.keys())[0]
if VOICE_MAPPER.available_voices
else None,
label="Speaker Voice",
info="Select the voice to use for speech generation",
)
cfg_slider = gr.Slider(
minimum=1.0,
maximum=3.0,
value=1.5,
step=0.1,
label="CFG Scale",
info="Higher values = more faithful to text (1.0-3.0)",
)
generate_btn = gr.Button("π΅ Generate Speech", variant="primary", size="lg")
with gr.Column(scale=1):
# Output section
audio_output = gr.Audio(
label="Generated Speech",
type="filepath",
interactive=False,
)
status_output = gr.Markdown(
"""
**Status:** Ready to generate speech
Enter text and click "Generate Speech" to start.
β‘ Using ZeroGPU for efficient processing
"""
)
# Example inputs
gr.Examples(
examples=[
[
"VibeVoice is a novel framework designed for generating expressive, long-form, multi-speaker conversational audio.",
list(VOICE_MAPPER.available_voices.keys())[0]
if VOICE_MAPPER.available_voices
else "Wayne",
1.5,
],
[
"The quick brown fox jumps over the lazy dog. This is a test of the text-to-speech system.",
list(VOICE_MAPPER.available_voices.keys())[0]
if VOICE_MAPPER.available_voices
else "Wayne",
1.5,
],
],
inputs=[text_input, speaker_dropdown, cfg_slider],
label="Example Inputs",
)
# Event handlers
generate_btn.click(
fn=generate_speech,
inputs=[text_input, speaker_dropdown, cfg_slider],
outputs=[audio_output, status_output],
api_name="generate",
)
# Footer
gr.Markdown(
"""
---
### π Notes:
- **Model**: Microsoft VibeVoice-Realtime-0.5B
- **Sample Rate**: 24kHz
- **Context Length**: 8K tokens
- **Generation Length**: ~10 minutes
- **Infrastructure**: ZeroGPU (Hugging Face Spaces)
### β οΈ Important:
- The model is designed for English text only
- Very short inputs (< 3 words) may produce unstable results
- Code, formulas, and special symbols are not supported
- Please use responsibly and disclose AI-generated content
- GPU is allocated dynamically - generation may take a few seconds to start
"""
)
# Launch the app with Gradio 6 syntax
if __name__ == "__main__":
demo.launch(
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="indigo",
neutral_hue="slate",
),
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
],
) |