Update app.py
Browse files
app.py
CHANGED
@@ -1,1174 +1,61 @@
|
|
1 |
-
"""
|
2 |
-
VibeVoice Gradio Demo - High-Quality Dialogue Generation Interface with Streaming Support
|
3 |
-
"""
|
4 |
-
|
5 |
-
import argparse
|
6 |
-
import json
|
7 |
import os
|
|
|
8 |
import sys
|
9 |
-
import tempfile
|
10 |
-
import time
|
11 |
-
from pathlib import Path
|
12 |
-
from typing import List, Dict, Any, Iterator
|
13 |
-
from datetime import datetime
|
14 |
-
import threading
|
15 |
-
import numpy as np
|
16 |
-
import gradio as gr
|
17 |
-
import librosa
|
18 |
-
import soundfile as sf
|
19 |
-
import torch
|
20 |
-
import os
|
21 |
-
import traceback
|
22 |
-
|
23 |
-
from vibevoice.modular.configuration_vibevoice import VibeVoiceConfig
|
24 |
-
from vibevoice.modular.modeling_vibevoice_inference import VibeVoiceForConditionalGenerationInference
|
25 |
-
from vibevoice.processor.vibevoice_processor import VibeVoiceProcessor
|
26 |
-
from vibevoice.modular.streamer import AudioStreamer
|
27 |
-
from transformers.utils import logging
|
28 |
-
from transformers import set_seed
|
29 |
-
|
30 |
-
logging.set_verbosity_info()
|
31 |
-
logger = logging.get_logger(__name__)
|
32 |
-
|
33 |
-
|
34 |
-
class VibeVoiceDemo:
|
35 |
-
def __init__(self, model_path: str, device: str = "cuda", inference_steps: int = 5):
|
36 |
-
"""Initialize the VibeVoice demo with model loading."""
|
37 |
-
self.model_path = model_path
|
38 |
-
self.device = device
|
39 |
-
self.inference_steps = inference_steps
|
40 |
-
self.is_generating = False # Track generation state
|
41 |
-
self.stop_generation = False # Flag to stop generation
|
42 |
-
self.current_streamer = None # Track current audio streamer
|
43 |
-
self.load_model()
|
44 |
-
self.setup_voice_presets()
|
45 |
-
self.load_example_scripts() # Load example scripts
|
46 |
-
|
47 |
-
def load_model(self):
|
48 |
-
"""Load the VibeVoice model and processor."""
|
49 |
-
print(f"Loading processor & model from {self.model_path}")
|
50 |
-
|
51 |
-
# Load processor
|
52 |
-
self.processor = VibeVoiceProcessor.from_pretrained(
|
53 |
-
self.model_path,
|
54 |
-
)
|
55 |
-
|
56 |
-
# Load model
|
57 |
-
self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
|
58 |
-
self.model_path,
|
59 |
-
torch_dtype=torch.bfloat16,
|
60 |
-
device_map='cuda',
|
61 |
-
attn_implementation="flash_attention_2",
|
62 |
-
)
|
63 |
-
self.model.eval()
|
64 |
-
|
65 |
-
# Use SDE solver by default
|
66 |
-
self.model.model.noise_scheduler = self.model.model.noise_scheduler.from_config(
|
67 |
-
self.model.model.noise_scheduler.config,
|
68 |
-
algorithm_type='sde-dpmsolver++',
|
69 |
-
beta_schedule='squaredcos_cap_v2'
|
70 |
-
)
|
71 |
-
self.model.set_ddpm_inference_steps(num_steps=self.inference_steps)
|
72 |
-
|
73 |
-
if hasattr(self.model.model, 'language_model'):
|
74 |
-
print(f"Language model attention: {self.model.model.language_model.config._attn_implementation}")
|
75 |
-
|
76 |
-
def setup_voice_presets(self):
|
77 |
-
"""Setup voice presets by scanning the voices directory."""
|
78 |
-
voices_dir = os.path.join(os.path.dirname(__file__), "voices")
|
79 |
-
|
80 |
-
# Check if voices directory exists
|
81 |
-
if not os.path.exists(voices_dir):
|
82 |
-
print(f"Warning: Voices directory not found at {voices_dir}")
|
83 |
-
self.voice_presets = {}
|
84 |
-
self.available_voices = {}
|
85 |
-
return
|
86 |
-
|
87 |
-
# Scan for all WAV files in the voices directory
|
88 |
-
self.voice_presets = {}
|
89 |
-
|
90 |
-
# Get all .wav files in the voices directory
|
91 |
-
wav_files = [f for f in os.listdir(voices_dir)
|
92 |
-
if f.lower().endswith(('.wav', '.mp3', '.flac', '.ogg', '.m4a', '.aac')) and os.path.isfile(os.path.join(voices_dir, f))]
|
93 |
-
|
94 |
-
# Create dictionary with filename (without extension) as key
|
95 |
-
for wav_file in wav_files:
|
96 |
-
# Remove .wav extension to get the name
|
97 |
-
name = os.path.splitext(wav_file)[0]
|
98 |
-
# Create full path
|
99 |
-
full_path = os.path.join(voices_dir, wav_file)
|
100 |
-
self.voice_presets[name] = full_path
|
101 |
-
|
102 |
-
# Sort the voice presets alphabetically by name for better UI
|
103 |
-
self.voice_presets = dict(sorted(self.voice_presets.items()))
|
104 |
-
|
105 |
-
# Filter out voices that don't exist (this is now redundant but kept for safety)
|
106 |
-
self.available_voices = {
|
107 |
-
name: path for name, path in self.voice_presets.items()
|
108 |
-
if os.path.exists(path)
|
109 |
-
}
|
110 |
-
|
111 |
-
if not self.available_voices:
|
112 |
-
raise gr.Error("No voice presets found. Please add .wav files to the demo/voices directory.")
|
113 |
-
|
114 |
-
print(f"Found {len(self.available_voices)} voice files in {voices_dir}")
|
115 |
-
print(f"Available voices: {', '.join(self.available_voices.keys())}")
|
116 |
-
|
117 |
-
def read_audio(self, audio_path: str, target_sr: int = 24000) -> np.ndarray:
|
118 |
-
"""Read and preprocess audio file."""
|
119 |
-
try:
|
120 |
-
wav, sr = sf.read(audio_path)
|
121 |
-
if len(wav.shape) > 1:
|
122 |
-
wav = np.mean(wav, axis=1)
|
123 |
-
if sr != target_sr:
|
124 |
-
wav = librosa.resample(wav, orig_sr=sr, target_sr=target_sr)
|
125 |
-
return wav
|
126 |
-
except Exception as e:
|
127 |
-
print(f"Error reading audio {audio_path}: {e}")
|
128 |
-
return np.array([])
|
129 |
-
|
130 |
-
def generate_podcast_streaming(self,
|
131 |
-
num_speakers: int,
|
132 |
-
script: str,
|
133 |
-
speaker_1: str = None,
|
134 |
-
speaker_2: str = None,
|
135 |
-
speaker_3: str = None,
|
136 |
-
speaker_4: str = None,
|
137 |
-
cfg_scale: float = 1.3) -> Iterator[tuple]:
|
138 |
-
try:
|
139 |
-
# Reset stop flag and set generating state
|
140 |
-
self.stop_generation = False
|
141 |
-
self.is_generating = True
|
142 |
-
|
143 |
-
# Validate inputs
|
144 |
-
if not script.strip():
|
145 |
-
self.is_generating = False
|
146 |
-
raise gr.Error("Error: Please provide a script.")
|
147 |
-
|
148 |
-
if num_speakers < 1 or num_speakers > 4:
|
149 |
-
self.is_generating = False
|
150 |
-
raise gr.Error("Error: Number of speakers must be between 1 and 4.")
|
151 |
-
|
152 |
-
# Collect selected speakers
|
153 |
-
selected_speakers = [speaker_1, speaker_2, speaker_3, speaker_4][:num_speakers]
|
154 |
-
|
155 |
-
# Validate speaker selections
|
156 |
-
for i, speaker in enumerate(selected_speakers):
|
157 |
-
if not speaker or speaker not in self.available_voices:
|
158 |
-
self.is_generating = False
|
159 |
-
raise gr.Error(f"Error: Please select a valid speaker for Speaker {i+1}.")
|
160 |
-
|
161 |
-
# Build initial log
|
162 |
-
log = f"🎙️ Generating podcast with {num_speakers} speakers\n"
|
163 |
-
log += f"📊 Parameters: CFG Scale={cfg_scale}, Inference Steps={self.inference_steps}\n"
|
164 |
-
log += f"🎭 Speakers: {', '.join(selected_speakers)}\n"
|
165 |
-
|
166 |
-
# Check for stop signal
|
167 |
-
if self.stop_generation:
|
168 |
-
self.is_generating = False
|
169 |
-
yield None, "🛑 Generation stopped by user", gr.update(visible=False)
|
170 |
-
return
|
171 |
-
|
172 |
-
# Load voice samples
|
173 |
-
voice_samples = []
|
174 |
-
for speaker_name in selected_speakers:
|
175 |
-
audio_path = self.available_voices[speaker_name]
|
176 |
-
audio_data = self.read_audio(audio_path)
|
177 |
-
if len(audio_data) == 0:
|
178 |
-
self.is_generating = False
|
179 |
-
raise gr.Error(f"Error: Failed to load audio for {speaker_name}")
|
180 |
-
voice_samples.append(audio_data)
|
181 |
-
|
182 |
-
# log += f"✅ Loaded {len(voice_samples)} voice samples\n"
|
183 |
-
|
184 |
-
# Check for stop signal
|
185 |
-
if self.stop_generation:
|
186 |
-
self.is_generating = False
|
187 |
-
yield None, "🛑 Generation stopped by user", gr.update(visible=False)
|
188 |
-
return
|
189 |
-
|
190 |
-
# Parse script to assign speaker ID's
|
191 |
-
lines = script.strip().split('\n')
|
192 |
-
formatted_script_lines = []
|
193 |
-
|
194 |
-
for line in lines:
|
195 |
-
line = line.strip()
|
196 |
-
if not line:
|
197 |
-
continue
|
198 |
-
|
199 |
-
# Check if line already has speaker format
|
200 |
-
if line.startswith('Speaker ') and ':' in line:
|
201 |
-
formatted_script_lines.append(line)
|
202 |
-
else:
|
203 |
-
# Auto-assign to speakers in rotation
|
204 |
-
speaker_id = len(formatted_script_lines) % num_speakers
|
205 |
-
formatted_script_lines.append(f"Speaker {speaker_id}: {line}")
|
206 |
-
|
207 |
-
formatted_script = '\n'.join(formatted_script_lines)
|
208 |
-
log += f"📝 Formatted script with {len(formatted_script_lines)} turns\n\n"
|
209 |
-
log += "🔄 Processing with VibeVoice (streaming mode)...\n"
|
210 |
-
|
211 |
-
# Check for stop signal before processing
|
212 |
-
if self.stop_generation:
|
213 |
-
self.is_generating = False
|
214 |
-
yield None, "🛑 Generation stopped by user", gr.update(visible=False)
|
215 |
-
return
|
216 |
-
|
217 |
-
start_time = time.time()
|
218 |
-
|
219 |
-
inputs = self.processor(
|
220 |
-
text=[formatted_script],
|
221 |
-
voice_samples=[voice_samples],
|
222 |
-
padding=True,
|
223 |
-
return_tensors="pt",
|
224 |
-
return_attention_mask=True,
|
225 |
-
)
|
226 |
-
|
227 |
-
# Create audio streamer
|
228 |
-
audio_streamer = AudioStreamer(
|
229 |
-
batch_size=1,
|
230 |
-
stop_signal=None,
|
231 |
-
timeout=None
|
232 |
-
)
|
233 |
-
|
234 |
-
# Store current streamer for potential stopping
|
235 |
-
self.current_streamer = audio_streamer
|
236 |
-
|
237 |
-
# Start generation in a separate thread
|
238 |
-
generation_thread = threading.Thread(
|
239 |
-
target=self._generate_with_streamer,
|
240 |
-
args=(inputs, cfg_scale, audio_streamer)
|
241 |
-
)
|
242 |
-
generation_thread.start()
|
243 |
-
|
244 |
-
# Wait for generation to actually start producing audio
|
245 |
-
time.sleep(1) # Reduced from 3 to 1 second
|
246 |
-
|
247 |
-
# Check for stop signal after thread start
|
248 |
-
if self.stop_generation:
|
249 |
-
audio_streamer.end()
|
250 |
-
generation_thread.join(timeout=5.0) # Wait up to 5 seconds for thread to finish
|
251 |
-
self.is_generating = False
|
252 |
-
yield None, "🛑 Generation stopped by user", gr.update(visible=False)
|
253 |
-
return
|
254 |
-
|
255 |
-
# Collect audio chunks as they arrive
|
256 |
-
sample_rate = 24000
|
257 |
-
all_audio_chunks = [] # For final statistics
|
258 |
-
pending_chunks = [] # Buffer for accumulating small chunks
|
259 |
-
chunk_count = 0
|
260 |
-
last_yield_time = time.time()
|
261 |
-
min_yield_interval = 15 # Yield every 15 seconds
|
262 |
-
min_chunk_size = sample_rate * 30 # At least 2 seconds of audio
|
263 |
-
|
264 |
-
# Get the stream for the first (and only) sample
|
265 |
-
audio_stream = audio_streamer.get_stream(0)
|
266 |
-
|
267 |
-
has_yielded_audio = False
|
268 |
-
has_received_chunks = False # Track if we received any chunks at all
|
269 |
-
|
270 |
-
for audio_chunk in audio_stream:
|
271 |
-
# Check for stop signal in the streaming loop
|
272 |
-
if self.stop_generation:
|
273 |
-
audio_streamer.end()
|
274 |
-
break
|
275 |
-
|
276 |
-
chunk_count += 1
|
277 |
-
has_received_chunks = True # Mark that we received at least one chunk
|
278 |
-
|
279 |
-
# Convert tensor to numpy
|
280 |
-
if torch.is_tensor(audio_chunk):
|
281 |
-
# Convert bfloat16 to float32 first, then to numpy
|
282 |
-
if audio_chunk.dtype == torch.bfloat16:
|
283 |
-
audio_chunk = audio_chunk.float()
|
284 |
-
audio_np = audio_chunk.cpu().numpy().astype(np.float32)
|
285 |
-
else:
|
286 |
-
audio_np = np.array(audio_chunk, dtype=np.float32)
|
287 |
-
|
288 |
-
# Ensure audio is 1D and properly normalized
|
289 |
-
if len(audio_np.shape) > 1:
|
290 |
-
audio_np = audio_np.squeeze()
|
291 |
-
|
292 |
-
# Convert to 16-bit for Gradio
|
293 |
-
audio_16bit = convert_to_16_bit_wav(audio_np)
|
294 |
-
|
295 |
-
# Store for final statistics
|
296 |
-
all_audio_chunks.append(audio_16bit)
|
297 |
-
|
298 |
-
# Add to pending chunks buffer
|
299 |
-
pending_chunks.append(audio_16bit)
|
300 |
-
|
301 |
-
# Calculate pending audio size
|
302 |
-
pending_audio_size = sum(len(chunk) for chunk in pending_chunks)
|
303 |
-
current_time = time.time()
|
304 |
-
time_since_last_yield = current_time - last_yield_time
|
305 |
-
|
306 |
-
# Decide whether to yield
|
307 |
-
should_yield = False
|
308 |
-
if not has_yielded_audio and pending_audio_size >= min_chunk_size:
|
309 |
-
# First yield: wait for minimum chunk size
|
310 |
-
should_yield = True
|
311 |
-
has_yielded_audio = True
|
312 |
-
elif has_yielded_audio and (pending_audio_size >= min_chunk_size or time_since_last_yield >= min_yield_interval):
|
313 |
-
# Subsequent yields: either enough audio or enough time has passed
|
314 |
-
should_yield = True
|
315 |
-
|
316 |
-
if should_yield and pending_chunks:
|
317 |
-
# Concatenate and yield only the new audio chunks
|
318 |
-
new_audio = np.concatenate(pending_chunks)
|
319 |
-
new_duration = len(new_audio) / sample_rate
|
320 |
-
total_duration = sum(len(chunk) for chunk in all_audio_chunks) / sample_rate
|
321 |
-
|
322 |
-
log_update = log + f"🎵 Streaming: {total_duration:.1f}s generated (chunk {chunk_count})\n"
|
323 |
-
|
324 |
-
# Yield streaming audio chunk and keep complete_audio as None during streaming
|
325 |
-
yield (sample_rate, new_audio), None, log_update, gr.update(visible=True)
|
326 |
-
|
327 |
-
# Clear pending chunks after yielding
|
328 |
-
pending_chunks = []
|
329 |
-
last_yield_time = current_time
|
330 |
-
|
331 |
-
# Yield any remaining chunks
|
332 |
-
if pending_chunks:
|
333 |
-
final_new_audio = np.concatenate(pending_chunks)
|
334 |
-
total_duration = sum(len(chunk) for chunk in all_audio_chunks) / sample_rate
|
335 |
-
log_update = log + f"🎵 Streaming final chunk: {total_duration:.1f}s total\n"
|
336 |
-
yield (sample_rate, final_new_audio), None, log_update, gr.update(visible=True)
|
337 |
-
has_yielded_audio = True # Mark that we yielded audio
|
338 |
-
|
339 |
-
# Wait for generation to complete (with timeout to prevent hanging)
|
340 |
-
generation_thread.join(timeout=5.0) # Increased timeout to 5 seconds
|
341 |
-
|
342 |
-
# If thread is still alive after timeout, force end
|
343 |
-
if generation_thread.is_alive():
|
344 |
-
print("Warning: Generation thread did not complete within timeout")
|
345 |
-
audio_streamer.end()
|
346 |
-
generation_thread.join(timeout=5.0)
|
347 |
-
|
348 |
-
# Clean up
|
349 |
-
self.current_streamer = None
|
350 |
-
self.is_generating = False
|
351 |
-
|
352 |
-
generation_time = time.time() - start_time
|
353 |
-
|
354 |
-
# Check if stopped by user
|
355 |
-
if self.stop_generation:
|
356 |
-
yield None, None, "🛑 Generation stopped by user", gr.update(visible=False)
|
357 |
-
return
|
358 |
-
|
359 |
-
# Debug logging
|
360 |
-
# print(f"Debug: has_received_chunks={has_received_chunks}, chunk_count={chunk_count}, all_audio_chunks length={len(all_audio_chunks)}")
|
361 |
-
|
362 |
-
# Check if we received any chunks but didn't yield audio
|
363 |
-
if has_received_chunks and not has_yielded_audio and all_audio_chunks:
|
364 |
-
# We have chunks but didn't meet the yield criteria, yield them now
|
365 |
-
complete_audio = np.concatenate(all_audio_chunks)
|
366 |
-
final_duration = len(complete_audio) / sample_rate
|
367 |
-
|
368 |
-
final_log = log + f"⏱️ Generation completed in {generation_time:.2f} seconds\n"
|
369 |
-
final_log += f"🎵 Final audio duration: {final_duration:.2f} seconds\n"
|
370 |
-
final_log += f"📊 Total chunks: {chunk_count}\n"
|
371 |
-
final_log += "✨ Generation successful! Complete audio is ready.\n"
|
372 |
-
final_log += "💡 Not satisfied? You can regenerate or adjust the CFG scale for different results."
|
373 |
-
|
374 |
-
# Yield the complete audio
|
375 |
-
yield None, (sample_rate, complete_audio), final_log, gr.update(visible=False)
|
376 |
-
return
|
377 |
-
|
378 |
-
if not has_received_chunks:
|
379 |
-
error_log = log + f"\n❌ Error: No audio chunks were received from the model. Generation time: {generation_time:.2f}s"
|
380 |
-
yield None, None, error_log, gr.update(visible=False)
|
381 |
-
return
|
382 |
-
|
383 |
-
if not has_yielded_audio:
|
384 |
-
error_log = log + f"\n❌ Error: Audio was generated but not streamed. Chunk count: {chunk_count}"
|
385 |
-
yield None, None, error_log, gr.update(visible=False)
|
386 |
-
return
|
387 |
-
|
388 |
-
# Prepare the complete audio
|
389 |
-
if all_audio_chunks:
|
390 |
-
complete_audio = np.concatenate(all_audio_chunks)
|
391 |
-
final_duration = len(complete_audio) / sample_rate
|
392 |
-
|
393 |
-
final_log = log + f"⏱️ Generation completed in {generation_time:.2f} seconds\n"
|
394 |
-
final_log += f"🎵 Final audio duration: {final_duration:.2f} seconds\n"
|
395 |
-
final_log += f"📊 Total chunks: {chunk_count}\n"
|
396 |
-
final_log += "✨ Generation successful! Complete audio is ready in the 'Complete Audio' tab.\n"
|
397 |
-
final_log += "💡 Not satisfied? You can regenerate or adjust the CFG scale for different results."
|
398 |
-
|
399 |
-
# Final yield: Clear streaming audio and provide complete audio
|
400 |
-
yield None, (sample_rate, complete_audio), final_log, gr.update(visible=False)
|
401 |
-
else:
|
402 |
-
final_log = log + "❌ No audio was generated."
|
403 |
-
yield None, None, final_log, gr.update(visible=False)
|
404 |
-
|
405 |
-
except gr.Error as e:
|
406 |
-
# Handle Gradio-specific errors (like input validation)
|
407 |
-
self.is_generating = False
|
408 |
-
self.current_streamer = None
|
409 |
-
error_msg = f"❌ Input Error: {str(e)}"
|
410 |
-
print(error_msg)
|
411 |
-
yield None, None, error_msg, gr.update(visible=False)
|
412 |
-
|
413 |
-
except Exception as e:
|
414 |
-
self.is_generating = False
|
415 |
-
self.current_streamer = None
|
416 |
-
error_msg = f"❌ An unexpected error occurred: {str(e)}"
|
417 |
-
print(error_msg)
|
418 |
-
import traceback
|
419 |
-
traceback.print_exc()
|
420 |
-
yield None, None, error_msg, gr.update(visible=False)
|
421 |
-
|
422 |
-
def _generate_with_streamer(self, inputs, cfg_scale, audio_streamer):
|
423 |
-
"""Helper method to run generation with streamer in a separate thread."""
|
424 |
-
try:
|
425 |
-
# Check for stop signal before starting generation
|
426 |
-
if self.stop_generation:
|
427 |
-
audio_streamer.end()
|
428 |
-
return
|
429 |
-
|
430 |
-
# Define a stop check function that can be called from generate
|
431 |
-
def check_stop_generation():
|
432 |
-
return self.stop_generation
|
433 |
-
|
434 |
-
outputs = self.model.generate(
|
435 |
-
**inputs,
|
436 |
-
max_new_tokens=None,
|
437 |
-
cfg_scale=cfg_scale,
|
438 |
-
tokenizer=self.processor.tokenizer,
|
439 |
-
generation_config={
|
440 |
-
'do_sample': False,
|
441 |
-
},
|
442 |
-
audio_streamer=audio_streamer,
|
443 |
-
stop_check_fn=check_stop_generation, # Pass the stop check function
|
444 |
-
verbose=False, # Disable verbose in streaming mode
|
445 |
-
refresh_negative=True,
|
446 |
-
)
|
447 |
-
|
448 |
-
except Exception as e:
|
449 |
-
print(f"Error in generation thread: {e}")
|
450 |
-
traceback.print_exc()
|
451 |
-
# Make sure to end the stream on error
|
452 |
-
audio_streamer.end()
|
453 |
-
|
454 |
-
def stop_audio_generation(self):
|
455 |
-
"""Stop the current audio generation process."""
|
456 |
-
self.stop_generation = True
|
457 |
-
if self.current_streamer is not None:
|
458 |
-
try:
|
459 |
-
self.current_streamer.end()
|
460 |
-
except Exception as e:
|
461 |
-
print(f"Error stopping streamer: {e}")
|
462 |
-
print("🛑 Audio generation stop requested")
|
463 |
-
|
464 |
-
def load_example_scripts(self):
|
465 |
-
"""Load example scripts from the text_examples directory."""
|
466 |
-
examples_dir = os.path.join(os.path.dirname(__file__), "text_examples")
|
467 |
-
self.example_scripts = []
|
468 |
-
|
469 |
-
# Check if text_examples directory exists
|
470 |
-
if not os.path.exists(examples_dir):
|
471 |
-
print(f"Warning: text_examples directory not found at {examples_dir}")
|
472 |
-
return
|
473 |
-
|
474 |
-
# Get all .txt files in the text_examples directory
|
475 |
-
txt_files = sorted([f for f in os.listdir(examples_dir)
|
476 |
-
if f.lower().endswith('.txt') and os.path.isfile(os.path.join(examples_dir, f))])
|
477 |
-
|
478 |
-
for txt_file in txt_files:
|
479 |
-
file_path = os.path.join(examples_dir, txt_file)
|
480 |
-
|
481 |
-
import re
|
482 |
-
# Check if filename contains a time pattern like "45min", "90min", etc.
|
483 |
-
time_pattern = re.search(r'(\d+)min', txt_file.lower())
|
484 |
-
if time_pattern:
|
485 |
-
minutes = int(time_pattern.group(1))
|
486 |
-
if minutes > 15:
|
487 |
-
print(f"Skipping {txt_file}: duration {minutes} minutes exceeds 15-minute limit")
|
488 |
-
continue
|
489 |
-
|
490 |
-
try:
|
491 |
-
with open(file_path, 'r', encoding='utf-8') as f:
|
492 |
-
script_content = f.read().strip()
|
493 |
-
|
494 |
-
# Remove empty lines and lines with only whitespace
|
495 |
-
script_content = '\n'.join(line for line in script_content.split('\n') if line.strip())
|
496 |
-
|
497 |
-
if not script_content:
|
498 |
-
continue
|
499 |
-
|
500 |
-
# Parse the script to determine number of speakers
|
501 |
-
num_speakers = self._get_num_speakers_from_script(script_content)
|
502 |
-
|
503 |
-
# Add to examples list as [num_speakers, script_content]
|
504 |
-
self.example_scripts.append([num_speakers, script_content])
|
505 |
-
print(f"Loaded example: {txt_file} with {num_speakers} speakers")
|
506 |
-
|
507 |
-
except Exception as e:
|
508 |
-
print(f"Error loading example script {txt_file}: {e}")
|
509 |
-
|
510 |
-
if self.example_scripts:
|
511 |
-
print(f"Successfully loaded {len(self.example_scripts)} example scripts")
|
512 |
-
else:
|
513 |
-
print("No example scripts were loaded")
|
514 |
-
|
515 |
-
def _get_num_speakers_from_script(self, script: str) -> int:
|
516 |
-
"""Determine the number of unique speakers in a script."""
|
517 |
-
import re
|
518 |
-
speakers = set()
|
519 |
-
|
520 |
-
lines = script.strip().split('\n')
|
521 |
-
for line in lines:
|
522 |
-
# Use regex to find speaker patterns
|
523 |
-
match = re.match(r'^Speaker\s+(\d+)\s*:', line.strip(), re.IGNORECASE)
|
524 |
-
if match:
|
525 |
-
speaker_id = int(match.group(1))
|
526 |
-
speakers.add(speaker_id)
|
527 |
-
|
528 |
-
# If no speakers found, default to 1
|
529 |
-
if not speakers:
|
530 |
-
return 1
|
531 |
-
|
532 |
-
# Return the maximum speaker ID + 1 (assuming 0-based indexing)
|
533 |
-
# or the count of unique speakers if they're 1-based
|
534 |
-
max_speaker = max(speakers)
|
535 |
-
min_speaker = min(speakers)
|
536 |
-
|
537 |
-
if min_speaker == 0:
|
538 |
-
return max_speaker + 1
|
539 |
-
else:
|
540 |
-
# Assume 1-based indexing, return the count
|
541 |
-
return len(speakers)
|
542 |
-
|
543 |
-
|
544 |
-
def create_demo_interface(demo_instance: VibeVoiceDemo):
|
545 |
-
"""Create the Gradio interface with streaming support."""
|
546 |
-
|
547 |
-
# Custom CSS for high-end aesthetics with lighter theme
|
548 |
-
custom_css = """
|
549 |
-
/* Modern light theme with gradients */
|
550 |
-
.gradio-container {
|
551 |
-
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
|
552 |
-
font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, sans-serif;
|
553 |
-
}
|
554 |
-
|
555 |
-
/* Header styling */
|
556 |
-
.main-header {
|
557 |
-
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
|
558 |
-
padding: 2rem;
|
559 |
-
border-radius: 20px;
|
560 |
-
margin-bottom: 2rem;
|
561 |
-
text-align: center;
|
562 |
-
box-shadow: 0 10px 40px rgba(102, 126, 234, 0.3);
|
563 |
-
}
|
564 |
-
|
565 |
-
.main-header h1 {
|
566 |
-
color: white;
|
567 |
-
font-size: 2.5rem;
|
568 |
-
font-weight: 700;
|
569 |
-
margin: 0;
|
570 |
-
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
|
571 |
-
}
|
572 |
-
|
573 |
-
.main-header p {
|
574 |
-
color: rgba(255,255,255,0.9);
|
575 |
-
font-size: 1.1rem;
|
576 |
-
margin: 0.5rem 0 0 0;
|
577 |
-
}
|
578 |
-
|
579 |
-
/* Card styling */
|
580 |
-
.settings-card, .generation-card {
|
581 |
-
background: rgba(255, 255, 255, 0.8);
|
582 |
-
backdrop-filter: blur(10px);
|
583 |
-
border: 1px solid rgba(226, 232, 240, 0.8);
|
584 |
-
border-radius: 16px;
|
585 |
-
padding: 1.5rem;
|
586 |
-
margin-bottom: 1rem;
|
587 |
-
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
588 |
-
}
|
589 |
-
|
590 |
-
/* Speaker selection styling */
|
591 |
-
.speaker-grid {
|
592 |
-
display: grid;
|
593 |
-
gap: 1rem;
|
594 |
-
margin-bottom: 1rem;
|
595 |
-
}
|
596 |
-
|
597 |
-
.speaker-item {
|
598 |
-
background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e1 100%);
|
599 |
-
border: 1px solid rgba(148, 163, 184, 0.4);
|
600 |
-
border-radius: 12px;
|
601 |
-
padding: 1rem;
|
602 |
-
color: #374151;
|
603 |
-
font-weight: 500;
|
604 |
-
}
|
605 |
-
|
606 |
-
/* Streaming indicator */
|
607 |
-
.streaming-indicator {
|
608 |
-
display: inline-block;
|
609 |
-
width: 10px;
|
610 |
-
height: 10px;
|
611 |
-
background: #22c55e;
|
612 |
-
border-radius: 50%;
|
613 |
-
margin-right: 8px;
|
614 |
-
animation: pulse 1.5s infinite;
|
615 |
-
}
|
616 |
-
|
617 |
-
@keyframes pulse {
|
618 |
-
0% { opacity: 1; transform: scale(1); }
|
619 |
-
50% { opacity: 0.5; transform: scale(1.1); }
|
620 |
-
100% { opacity: 1; transform: scale(1); }
|
621 |
-
}
|
622 |
-
|
623 |
-
/* Queue status styling */
|
624 |
-
.queue-status {
|
625 |
-
background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
|
626 |
-
border: 1px solid rgba(14, 165, 233, 0.3);
|
627 |
-
border-radius: 8px;
|
628 |
-
padding: 0.75rem;
|
629 |
-
margin: 0.5rem 0;
|
630 |
-
text-align: center;
|
631 |
-
font-size: 0.9rem;
|
632 |
-
color: #0369a1;
|
633 |
-
}
|
634 |
-
|
635 |
-
.generate-btn {
|
636 |
-
background: linear-gradient(135deg, #059669 0%, #0d9488 100%);
|
637 |
-
border: none;
|
638 |
-
border-radius: 12px;
|
639 |
-
padding: 1rem 2rem;
|
640 |
-
color: white;
|
641 |
-
font-weight: 600;
|
642 |
-
font-size: 1.1rem;
|
643 |
-
box-shadow: 0 4px 20px rgba(5, 150, 105, 0.4);
|
644 |
-
transition: all 0.3s ease;
|
645 |
-
}
|
646 |
-
|
647 |
-
.generate-btn:hover {
|
648 |
-
transform: translateY(-2px);
|
649 |
-
box-shadow: 0 6px 25px rgba(5, 150, 105, 0.6);
|
650 |
-
}
|
651 |
-
|
652 |
-
.stop-btn {
|
653 |
-
background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
|
654 |
-
border: none;
|
655 |
-
border-radius: 12px;
|
656 |
-
padding: 1rem 2rem;
|
657 |
-
color: white;
|
658 |
-
font-weight: 600;
|
659 |
-
font-size: 1.1rem;
|
660 |
-
box-shadow: 0 4px 20px rgba(239, 68, 68, 0.4);
|
661 |
-
transition: all 0.3s ease;
|
662 |
-
}
|
663 |
-
|
664 |
-
.stop-btn:hover {
|
665 |
-
transform: translateY(-2px);
|
666 |
-
box-shadow: 0 6px 25px rgba(239, 68, 68, 0.6);
|
667 |
-
}
|
668 |
-
|
669 |
-
/* Audio player styling */
|
670 |
-
.audio-output {
|
671 |
-
background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
|
672 |
-
border-radius: 16px;
|
673 |
-
padding: 1.5rem;
|
674 |
-
border: 1px solid rgba(148, 163, 184, 0.3);
|
675 |
-
}
|
676 |
-
|
677 |
-
.complete-audio-section {
|
678 |
-
margin-top: 1rem;
|
679 |
-
padding: 1rem;
|
680 |
-
background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
|
681 |
-
border: 1px solid rgba(34, 197, 94, 0.3);
|
682 |
-
border-radius: 12px;
|
683 |
-
}
|
684 |
-
|
685 |
-
/* Text areas */
|
686 |
-
.script-input, .log-output {
|
687 |
-
background: rgba(255, 255, 255, 0.9) !important;
|
688 |
-
border: 1px solid rgba(148, 163, 184, 0.4) !important;
|
689 |
-
border-radius: 12px !important;
|
690 |
-
color: #1e293b !important;
|
691 |
-
font-family: 'JetBrains Mono', monospace !important;
|
692 |
-
}
|
693 |
-
|
694 |
-
.script-input::placeholder {
|
695 |
-
color: #64748b !important;
|
696 |
-
}
|
697 |
-
|
698 |
-
/* Sliders */
|
699 |
-
.slider-container {
|
700 |
-
background: rgba(248, 250, 252, 0.8);
|
701 |
-
border: 1px solid rgba(226, 232, 240, 0.6);
|
702 |
-
border-radius: 8px;
|
703 |
-
padding: 1rem;
|
704 |
-
margin: 0.5rem 0;
|
705 |
-
}
|
706 |
-
|
707 |
-
/* Labels and text */
|
708 |
-
.gradio-container label {
|
709 |
-
color: #374151 !important;
|
710 |
-
font-weight: 600 !important;
|
711 |
-
}
|
712 |
-
|
713 |
-
.gradio-container .markdown {
|
714 |
-
color: #1f2937 !important;
|
715 |
-
}
|
716 |
-
|
717 |
-
/* Responsive design */
|
718 |
-
@media (max-width: 768px) {
|
719 |
-
.main-header h1 { font-size: 2rem; }
|
720 |
-
.settings-card, .generation-card { padding: 1rem; }
|
721 |
-
}
|
722 |
-
|
723 |
-
/* Random example button styling - more subtle professional color */
|
724 |
-
.random-btn {
|
725 |
-
background: linear-gradient(135deg, #64748b 0%, #475569 100%);
|
726 |
-
border: none;
|
727 |
-
border-radius: 12px;
|
728 |
-
padding: 1rem 1.5rem;
|
729 |
-
color: white;
|
730 |
-
font-weight: 600;
|
731 |
-
font-size: 1rem;
|
732 |
-
box-shadow: 0 4px 20px rgba(100, 116, 139, 0.3);
|
733 |
-
transition: all 0.3s ease;
|
734 |
-
display: inline-flex;
|
735 |
-
align-items: center;
|
736 |
-
gap: 0.5rem;
|
737 |
-
}
|
738 |
-
|
739 |
-
.random-btn:hover {
|
740 |
-
transform: translateY(-2px);
|
741 |
-
box-shadow: 0 6px 25px rgba(100, 116, 139, 0.4);
|
742 |
-
background: linear-gradient(135deg, #475569 0%, #334155 100%);
|
743 |
-
}
|
744 |
-
"""
|
745 |
-
|
746 |
-
with gr.Blocks(
|
747 |
-
title="VibeVoice - AI Podcast Generator",
|
748 |
-
css=custom_css,
|
749 |
-
theme=gr.themes.Soft(
|
750 |
-
primary_hue="blue",
|
751 |
-
secondary_hue="purple",
|
752 |
-
neutral_hue="slate",
|
753 |
-
)
|
754 |
-
) as interface:
|
755 |
-
|
756 |
-
# Header
|
757 |
-
gr.HTML("""
|
758 |
-
<div class="main-header">
|
759 |
-
<h1>🎙️ Vibe Podcasting </h1>
|
760 |
-
<p>Generating Long-form Multi-speaker AI Podcast with VibeVoice</p>
|
761 |
-
</div>
|
762 |
-
""")
|
763 |
-
|
764 |
-
with gr.Row():
|
765 |
-
# Left column - Settings
|
766 |
-
with gr.Column(scale=1, elem_classes="settings-card"):
|
767 |
-
gr.Markdown("### 🎛️ **Podcast Settings**")
|
768 |
-
|
769 |
-
# Number of speakers
|
770 |
-
num_speakers = gr.Slider(
|
771 |
-
minimum=1,
|
772 |
-
maximum=4,
|
773 |
-
value=2,
|
774 |
-
step=1,
|
775 |
-
label="Number of Speakers",
|
776 |
-
elem_classes="slider-container"
|
777 |
-
)
|
778 |
-
|
779 |
-
# Speaker selection
|
780 |
-
gr.Markdown("### 🎭 **Speaker Selection**")
|
781 |
-
|
782 |
-
available_speaker_names = list(demo_instance.available_voices.keys())
|
783 |
-
# default_speakers = available_speaker_names[:4] if len(available_speaker_names) >= 4 else available_speaker_names
|
784 |
-
default_speakers = ['en-Alice_woman', 'en-Carter_man', 'en-Frank_man', 'en-Maya_woman']
|
785 |
-
|
786 |
-
speaker_selections = []
|
787 |
-
for i in range(4):
|
788 |
-
default_value = default_speakers[i] if i < len(default_speakers) else None
|
789 |
-
speaker = gr.Dropdown(
|
790 |
-
choices=available_speaker_names,
|
791 |
-
value=default_value,
|
792 |
-
label=f"Speaker {i+1}",
|
793 |
-
visible=(i < 2), # Initially show only first 2 speakers
|
794 |
-
elem_classes="speaker-item"
|
795 |
-
)
|
796 |
-
speaker_selections.append(speaker)
|
797 |
-
|
798 |
-
# Advanced settings
|
799 |
-
gr.Markdown("### ⚙️ **Advanced Settings**")
|
800 |
-
|
801 |
-
# Sampling parameters (contains all generation settings)
|
802 |
-
with gr.Accordion("Generation Parameters", open=False):
|
803 |
-
cfg_scale = gr.Slider(
|
804 |
-
minimum=1.0,
|
805 |
-
maximum=2.0,
|
806 |
-
value=1.3,
|
807 |
-
step=0.05,
|
808 |
-
label="CFG Scale (Guidance Strength)",
|
809 |
-
# info="Higher values increase adherence to text",
|
810 |
-
elem_classes="slider-container"
|
811 |
-
)
|
812 |
-
|
813 |
-
# Right column - Generation
|
814 |
-
with gr.Column(scale=2, elem_classes="generation-card"):
|
815 |
-
gr.Markdown("### 📝 **Script Input**")
|
816 |
-
|
817 |
-
script_input = gr.Textbox(
|
818 |
-
label="Conversation Script",
|
819 |
-
placeholder="""Enter your podcast script here. You can format it as:
|
820 |
-
|
821 |
-
Speaker 0: Welcome to our podcast today!
|
822 |
-
Speaker 1: Thanks for having me. I'm excited to discuss...
|
823 |
-
|
824 |
-
Or paste text directly and it will auto-assign speakers.""",
|
825 |
-
lines=12,
|
826 |
-
max_lines=20,
|
827 |
-
elem_classes="script-input"
|
828 |
-
)
|
829 |
-
|
830 |
-
# Button row with Random Example on the left and Generate on the right
|
831 |
-
with gr.Row():
|
832 |
-
# Random example button (now on the left)
|
833 |
-
random_example_btn = gr.Button(
|
834 |
-
"🎲 Random Example",
|
835 |
-
size="lg",
|
836 |
-
variant="secondary",
|
837 |
-
elem_classes="random-btn",
|
838 |
-
scale=1 # Smaller width
|
839 |
-
)
|
840 |
-
|
841 |
-
# Generate button (now on the right)
|
842 |
-
generate_btn = gr.Button(
|
843 |
-
"🚀 Generate Podcast",
|
844 |
-
size="lg",
|
845 |
-
variant="primary",
|
846 |
-
elem_classes="generate-btn",
|
847 |
-
scale=2 # Wider than random button
|
848 |
-
)
|
849 |
-
|
850 |
-
# Stop button
|
851 |
-
stop_btn = gr.Button(
|
852 |
-
"🛑 Stop Generation",
|
853 |
-
size="lg",
|
854 |
-
variant="stop",
|
855 |
-
elem_classes="stop-btn",
|
856 |
-
visible=False
|
857 |
-
)
|
858 |
-
|
859 |
-
# Streaming status indicator
|
860 |
-
streaming_status = gr.HTML(
|
861 |
-
value="""
|
862 |
-
<div style="background: linear-gradient(135deg, #dcfce7 0%, #bbf7d0 100%);
|
863 |
-
border: 1px solid rgba(34, 197, 94, 0.3);
|
864 |
-
border-radius: 8px;
|
865 |
-
padding: 0.75rem;
|
866 |
-
margin: 0.5rem 0;
|
867 |
-
text-align: center;
|
868 |
-
font-size: 0.9rem;
|
869 |
-
color: #166534;">
|
870 |
-
<span class="streaming-indicator"></span>
|
871 |
-
<strong>LIVE STREAMING</strong> - Audio is being generated in real-time
|
872 |
-
</div>
|
873 |
-
""",
|
874 |
-
visible=False,
|
875 |
-
elem_id="streaming-status"
|
876 |
-
)
|
877 |
-
|
878 |
-
# Output section
|
879 |
-
gr.Markdown("### 🎵 **Generated Podcast**")
|
880 |
-
|
881 |
-
# Streaming audio output (outside of tabs for simpler handling)
|
882 |
-
audio_output = gr.Audio(
|
883 |
-
label="Streaming Audio (Real-time)",
|
884 |
-
type="numpy",
|
885 |
-
elem_classes="audio-output",
|
886 |
-
streaming=True, # Enable streaming mode
|
887 |
-
autoplay=True,
|
888 |
-
show_download_button=False, # Explicitly show download button
|
889 |
-
visible=True
|
890 |
-
)
|
891 |
-
|
892 |
-
# Complete audio output (non-streaming)
|
893 |
-
complete_audio_output = gr.Audio(
|
894 |
-
label="Complete Podcast (Download after generation)",
|
895 |
-
type="numpy",
|
896 |
-
elem_classes="audio-output complete-audio-section",
|
897 |
-
streaming=False, # Non-streaming mode
|
898 |
-
autoplay=False,
|
899 |
-
show_download_button=True, # Explicitly show download button
|
900 |
-
visible=False # Initially hidden, shown when audio is ready
|
901 |
-
)
|
902 |
-
|
903 |
-
gr.Markdown("""
|
904 |
-
*💡 **Streaming**: Audio plays as it's being generated (may have slight pauses)
|
905 |
-
*💡 **Complete Audio**: Will appear below after generation finishes*
|
906 |
-
""")
|
907 |
-
|
908 |
-
# Generation log
|
909 |
-
log_output = gr.Textbox(
|
910 |
-
label="Generation Log",
|
911 |
-
lines=8,
|
912 |
-
max_lines=15,
|
913 |
-
interactive=False,
|
914 |
-
elem_classes="log-output"
|
915 |
-
)
|
916 |
-
|
917 |
-
def update_speaker_visibility(num_speakers):
|
918 |
-
updates = []
|
919 |
-
for i in range(4):
|
920 |
-
updates.append(gr.update(visible=(i < num_speakers)))
|
921 |
-
return updates
|
922 |
-
|
923 |
-
num_speakers.change(
|
924 |
-
fn=update_speaker_visibility,
|
925 |
-
inputs=[num_speakers],
|
926 |
-
outputs=speaker_selections
|
927 |
-
)
|
928 |
-
|
929 |
-
# Main generation function with streaming
|
930 |
-
def generate_podcast_wrapper(num_speakers, script, *speakers_and_params):
|
931 |
-
"""Wrapper function to handle the streaming generation call."""
|
932 |
-
try:
|
933 |
-
# Extract speakers and parameters
|
934 |
-
speakers = speakers_and_params[:4] # First 4 are speaker selections
|
935 |
-
cfg_scale = speakers_and_params[4] # CFG scale
|
936 |
-
|
937 |
-
# Clear outputs and reset visibility at start
|
938 |
-
yield None, gr.update(value=None, visible=False), "🎙️ Starting generation...", gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
|
939 |
-
|
940 |
-
# The generator will yield multiple times
|
941 |
-
final_log = "Starting generation..."
|
942 |
-
|
943 |
-
for streaming_audio, complete_audio, log, streaming_visible in demo_instance.generate_podcast_streaming(
|
944 |
-
num_speakers=int(num_speakers),
|
945 |
-
script=script,
|
946 |
-
speaker_1=speakers[0],
|
947 |
-
speaker_2=speakers[1],
|
948 |
-
speaker_3=speakers[2],
|
949 |
-
speaker_4=speakers[3],
|
950 |
-
cfg_scale=cfg_scale
|
951 |
-
):
|
952 |
-
final_log = log
|
953 |
-
|
954 |
-
# Check if we have complete audio (final yield)
|
955 |
-
if complete_audio is not None:
|
956 |
-
# Final state: clear streaming, show complete audio
|
957 |
-
yield None, gr.update(value=complete_audio, visible=True), log, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
958 |
-
else:
|
959 |
-
# Streaming state: update streaming audio only
|
960 |
-
if streaming_audio is not None:
|
961 |
-
yield streaming_audio, gr.update(visible=False), log, streaming_visible, gr.update(visible=False), gr.update(visible=True)
|
962 |
-
else:
|
963 |
-
# No new audio, just update status
|
964 |
-
yield None, gr.update(visible=False), log, streaming_visible, gr.update(visible=False), gr.update(visible=True)
|
965 |
-
|
966 |
-
except Exception as e:
|
967 |
-
error_msg = f"❌ A critical error occurred in the wrapper: {str(e)}"
|
968 |
-
print(error_msg)
|
969 |
-
import traceback
|
970 |
-
traceback.print_exc()
|
971 |
-
# Reset button states on error
|
972 |
-
yield None, gr.update(value=None, visible=False), error_msg, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
973 |
-
|
974 |
-
def stop_generation_handler():
|
975 |
-
"""Handle stopping generation."""
|
976 |
-
demo_instance.stop_audio_generation()
|
977 |
-
# Return values for: log_output, streaming_status, generate_btn, stop_btn
|
978 |
-
return "🛑 Generation stopped.", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
979 |
-
|
980 |
-
# Add a clear audio function
|
981 |
-
def clear_audio_outputs():
|
982 |
-
"""Clear both audio outputs before starting new generation."""
|
983 |
-
return None, gr.update(value=None, visible=False)
|
984 |
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
-
|
990 |
-
queue=False
|
991 |
-
).then(
|
992 |
-
fn=generate_podcast_wrapper,
|
993 |
-
inputs=[num_speakers, script_input] + speaker_selections + [cfg_scale],
|
994 |
-
outputs=[audio_output, complete_audio_output, log_output, streaming_status, generate_btn, stop_btn],
|
995 |
-
queue=True # Enable Gradio's built-in queue
|
996 |
-
)
|
997 |
-
|
998 |
-
# Connect stop button
|
999 |
-
stop_btn.click(
|
1000 |
-
fn=stop_generation_handler,
|
1001 |
-
inputs=[],
|
1002 |
-
outputs=[log_output, streaming_status, generate_btn, stop_btn],
|
1003 |
-
queue=False # Don't queue stop requests
|
1004 |
-
).then(
|
1005 |
-
# Clear both audio outputs after stopping
|
1006 |
-
fn=lambda: (None, None),
|
1007 |
-
inputs=[],
|
1008 |
-
outputs=[audio_output, complete_audio_output],
|
1009 |
-
queue=False
|
1010 |
-
)
|
1011 |
-
|
1012 |
-
# Function to randomly select an example
|
1013 |
-
def load_random_example():
|
1014 |
-
"""Randomly select and load an example script."""
|
1015 |
-
import random
|
1016 |
-
|
1017 |
-
# Get available examples
|
1018 |
-
if hasattr(demo_instance, 'example_scripts') and demo_instance.example_scripts:
|
1019 |
-
example_scripts = demo_instance.example_scripts
|
1020 |
-
else:
|
1021 |
-
# Fallback to default
|
1022 |
-
example_scripts = [
|
1023 |
-
[2, "Speaker 0: Welcome to our AI podcast demonstration!\nSpeaker 1: Thanks for having me. This is exciting!"]
|
1024 |
-
]
|
1025 |
-
|
1026 |
-
# Randomly select one
|
1027 |
-
if example_scripts:
|
1028 |
-
selected = random.choice(example_scripts)
|
1029 |
-
num_speakers_value = selected[0]
|
1030 |
-
script_value = selected[1]
|
1031 |
-
|
1032 |
-
# Return the values to update the UI
|
1033 |
-
return num_speakers_value, script_value
|
1034 |
-
|
1035 |
-
# Default values if no examples
|
1036 |
-
return 2, ""
|
1037 |
-
|
1038 |
-
# Connect random example button
|
1039 |
-
random_example_btn.click(
|
1040 |
-
fn=load_random_example,
|
1041 |
-
inputs=[],
|
1042 |
-
outputs=[num_speakers, script_input],
|
1043 |
-
queue=False # Don't queue this simple operation
|
1044 |
-
)
|
1045 |
-
|
1046 |
-
# Add usage tips
|
1047 |
-
gr.Markdown("""
|
1048 |
-
### 💡 **Usage Tips**
|
1049 |
-
|
1050 |
-
- Click **🚀 Generate Podcast** to start audio generation
|
1051 |
-
- **Live Streaming** tab shows audio as it's generated (may have slight pauses)
|
1052 |
-
- **Complete Audio** tab provides the full, uninterrupted podcast after generation
|
1053 |
-
- During generation, you can click **🛑 Stop Generation** to interrupt the process
|
1054 |
-
- The streaming indicator shows real-time generation progress
|
1055 |
-
""")
|
1056 |
-
|
1057 |
-
# Add example scripts
|
1058 |
-
gr.Markdown("### 📚 **Example Scripts**")
|
1059 |
-
|
1060 |
-
# Use dynamically loaded examples if available, otherwise provide a default
|
1061 |
-
if hasattr(demo_instance, 'example_scripts') and demo_instance.example_scripts:
|
1062 |
-
example_scripts = demo_instance.example_scripts
|
1063 |
-
else:
|
1064 |
-
# Fallback to a simple default example if no scripts loaded
|
1065 |
-
example_scripts = [
|
1066 |
-
[1, "Speaker 1: Welcome to our AI podcast demonstration! This is a sample script showing how VibeVoice can generate natural-sounding speech."]
|
1067 |
-
]
|
1068 |
-
|
1069 |
-
gr.Examples(
|
1070 |
-
examples=example_scripts,
|
1071 |
-
inputs=[num_speakers, script_input],
|
1072 |
-
label="Try these example scripts:"
|
1073 |
-
)
|
1074 |
-
|
1075 |
-
return interface
|
1076 |
-
|
1077 |
-
|
1078 |
-
def convert_to_16_bit_wav(data):
|
1079 |
-
# Check if data is a tensor and move to cpu
|
1080 |
-
if torch.is_tensor(data):
|
1081 |
-
data = data.detach().cpu().numpy()
|
1082 |
-
|
1083 |
-
# Ensure data is numpy array
|
1084 |
-
data = np.array(data)
|
1085 |
-
|
1086 |
-
# Normalize to range [-1, 1] if it's not already
|
1087 |
-
if np.max(np.abs(data)) > 1.0:
|
1088 |
-
data = data / np.max(np.abs(data))
|
1089 |
-
|
1090 |
-
# Scale to 16-bit integer range
|
1091 |
-
data = (data * 32767).astype(np.int16)
|
1092 |
-
return data
|
1093 |
-
|
1094 |
-
|
1095 |
-
def parse_args():
|
1096 |
-
parser = argparse.ArgumentParser(description="VibeVoice Gradio Demo")
|
1097 |
-
parser.add_argument(
|
1098 |
-
"--model_path",
|
1099 |
-
type=str,
|
1100 |
-
default="/tmp/vibevoice-model",
|
1101 |
-
help="Path to the VibeVoice model directory",
|
1102 |
-
)
|
1103 |
-
parser.add_argument(
|
1104 |
-
"--device",
|
1105 |
-
type=str,
|
1106 |
-
default="cuda" if torch.cuda.is_available() else "cpu",
|
1107 |
-
help="Device for inference",
|
1108 |
-
)
|
1109 |
-
parser.add_argument(
|
1110 |
-
"--inference_steps",
|
1111 |
-
type=int,
|
1112 |
-
default=10,
|
1113 |
-
help="Number of inference steps for DDPM (not exposed to users)",
|
1114 |
-
)
|
1115 |
-
parser.add_argument(
|
1116 |
-
"--share",
|
1117 |
-
action="store_true",
|
1118 |
-
help="Share the demo publicly via Gradio",
|
1119 |
-
)
|
1120 |
-
parser.add_argument(
|
1121 |
-
"--port",
|
1122 |
-
type=int,
|
1123 |
-
default=7860,
|
1124 |
-
help="Port to run the demo on",
|
1125 |
-
)
|
1126 |
-
|
1127 |
-
return parser.parse_args()
|
1128 |
-
|
1129 |
-
|
1130 |
-
def main():
|
1131 |
-
"""Main function to run the demo."""
|
1132 |
-
args = parse_args()
|
1133 |
-
|
1134 |
-
set_seed(42) # Set a fixed seed for reproducibility
|
1135 |
-
|
1136 |
-
print("🎙️ Initializing VibeVoice Demo with Streaming Support...")
|
1137 |
-
|
1138 |
-
# Initialize demo instance
|
1139 |
-
demo_instance = VibeVoiceDemo(
|
1140 |
-
model_path=args.model_path,
|
1141 |
-
device=args.device,
|
1142 |
-
inference_steps=args.inference_steps
|
1143 |
-
)
|
1144 |
-
|
1145 |
-
# Create interface
|
1146 |
-
interface = create_demo_interface(demo_instance)
|
1147 |
-
|
1148 |
-
print(f"🚀 Launching demo on port {args.port}")
|
1149 |
-
print(f"📁 Model path: {args.model_path}")
|
1150 |
-
print(f"🎭 Available voices: {len(demo_instance.available_voices)}")
|
1151 |
-
print(f"🔴 Streaming mode: ENABLED")
|
1152 |
-
print(f"🔒 Session isolation: ENABLED")
|
1153 |
-
|
1154 |
-
# Launch the interface
|
1155 |
try:
|
1156 |
-
|
1157 |
-
|
1158 |
-
|
1159 |
-
|
1160 |
-
|
1161 |
-
|
1162 |
-
|
1163 |
-
|
1164 |
-
|
1165 |
-
)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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# --- 1. Clone the VibeVoice Repository ---
|
6 |
+
# Check if the repository directory already exists
|
7 |
+
repo_dir = "VibeVoice"
|
8 |
+
if not os.path.exists(repo_dir):
|
9 |
+
print("Cloning the VibeVoice repository...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
try:
|
11 |
+
subprocess.run(
|
12 |
+
["git", "clone", "https://github.com/microsoft/VibeVoice.git"],
|
13 |
+
check=True,
|
14 |
+
capture_output=True,
|
15 |
+
text=True
|
16 |
+
)
|
17 |
+
print("Repository cloned successfully.")
|
18 |
+
except subprocess.CalledProcessError as e:
|
19 |
+
print(f"Error cloning repository: {e.stderr}")
|
20 |
+
sys.exit(1)
|
21 |
+
else:
|
22 |
+
print("Repository already exists. Skipping clone.")
|
23 |
+
|
24 |
+
# --- 2. Install the Package ---
|
25 |
+
# Navigate into the repository directory
|
26 |
+
os.chdir(repo_dir)
|
27 |
+
print(f"Changed directory to: {os.getcwd()}")
|
28 |
+
|
29 |
+
print("Installing the VibeVoice package...")
|
30 |
+
try:
|
31 |
+
# Use pip to install the package in editable mode
|
32 |
+
subprocess.run(
|
33 |
+
[sys.executable, "-m", "pip", "install", "-e", "."],
|
34 |
+
check=True,
|
35 |
+
capture_output=True,
|
36 |
+
text=True
|
37 |
+
)
|
38 |
+
print("Package installed successfully.")
|
39 |
+
except subprocess.CalledProcessError as e:
|
40 |
+
print(f"Error installing package: {e.stderr}")
|
41 |
+
sys.exit(1)
|
42 |
+
|
43 |
+
# --- 3. Launch the Gradio Demo ---
|
44 |
+
# Define the path to the demo script and the model to use
|
45 |
+
demo_script_path = "demo/gradio_demo.py"
|
46 |
+
model_id = "microsoft/VibeVoice-1.5B"
|
47 |
+
|
48 |
+
# Construct the command to run the demo
|
49 |
+
# The --share flag is necessary to make the Gradio app accessible within the Hugging Face Space environment
|
50 |
+
command = [
|
51 |
+
"python",
|
52 |
+
demo_script_path,
|
53 |
+
"--model_path",
|
54 |
+
model_id,
|
55 |
+
"--share"
|
56 |
+
]
|
57 |
+
|
58 |
+
print(f"Launching Gradio demo with command: {' '.join(command)}")
|
59 |
+
# Run the command. This will start the Gradio server and launch the demo.
|
60 |
+
# The process will remain active, serving the web interface.
|
61 |
+
subprocess.run(command)
|