"""
VibeVoice Gradio Demo - High-Quality Dialogue Generation Interface with Streaming Support
"""
import argparse
import torch
import gradio as gr
from transformers.utils import logging
from transformers import set_seed
from model import VibeVoiceDemo
logging.set_verbosity_info()
logger = logging.get_logger(__name__)
def create_demo_interface(demo_instance: VibeVoiceDemo):
"""Create the Gradio interface with streaming support."""
# Custom CSS for high-end aesthetics with lighter theme
custom_css = """
/* Modern light theme with gradients */
.gradio-container {
background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, sans-serif;
}
/* Header styling */
.main-header {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
padding: 2rem;
border-radius: 20px;
margin-bottom: 2rem;
text-align: center;
box-shadow: 0 10px 40px rgba(102, 126, 234, 0.3);
}
.main-header h1 {
color: white;
font-size: 2.5rem;
font-weight: 700;
margin: 0;
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}
.main-header p {
color: rgba(255,255,255,0.9);
font-size: 1.1rem;
margin: 0.5rem 0 0 0;
}
/* Card styling */
.settings-card, .generation-card {
background: rgba(255, 255, 255, 0.8);
backdrop-filter: blur(10px);
border: 1px solid rgba(226, 232, 240, 0.8);
border-radius: 16px;
padding: 1.5rem;
margin-bottom: 1rem;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}
/* Speaker selection styling */
.speaker-grid {
display: grid;
gap: 1rem;
margin-bottom: 1rem;
}
.speaker-item {
background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e1 100%);
border: 1px solid rgba(148, 163, 184, 0.4);
border-radius: 12px;
padding: 1rem;
color: #374151;
font-weight: 500;
}
/* Streaming indicator */
.streaming-indicator {
display: inline-block;
width: 10px;
height: 10px;
background: #22c55e;
border-radius: 50%;
margin-right: 8px;
animation: pulse 1.5s infinite;
}
@keyframes pulse {
0% { opacity: 1; transform: scale(1); }
50% { opacity: 0.5; transform: scale(1.1); }
100% { opacity: 1; transform: scale(1); }
}
/* Queue status styling */
.queue-status {
background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
border: 1px solid rgba(14, 165, 233, 0.3);
border-radius: 8px;
padding: 0.75rem;
margin: 0.5rem 0;
text-align: center;
font-size: 0.9rem;
color: #0369a1;
}
.generate-btn {
background: linear-gradient(135deg, #059669 0%, #0d9488 100%);
border: none;
border-radius: 12px;
padding: 1rem 2rem;
color: white;
font-weight: 600;
font-size: 1.1rem;
box-shadow: 0 4px 20px rgba(5, 150, 105, 0.4);
transition: all 0.3s ease;
}
.generate-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 25px rgba(5, 150, 105, 0.6);
}
.stop-btn {
background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
border: none;
border-radius: 12px;
padding: 1rem 2rem;
color: white;
font-weight: 600;
font-size: 1.1rem;
box-shadow: 0 4px 20px rgba(239, 68, 68, 0.4);
transition: all 0.3s ease;
}
.stop-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 25px rgba(239, 68, 68, 0.6);
}
/* Audio player styling */
.audio-output {
background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
border-radius: 16px;
padding: 1.5rem;
border: 1px solid rgba(148, 163, 184, 0.3);
}
.complete-audio-section {
margin-top: 1rem;
padding: 1rem;
background: linear-gradient(135deg, #f0fdf4 0%, #dcfce7 100%);
border: 1px solid rgba(34, 197, 94, 0.3);
border-radius: 12px;
}
/* Text areas */
.script-input, .log-output {
background: rgba(255, 255, 255, 0.9) !important;
border: 1px solid rgba(148, 163, 184, 0.4) !important;
border-radius: 12px !important;
color: #1e293b !important;
font-family: 'JetBrains Mono', monospace !important;
}
.script-input::placeholder {
color: #64748b !important;
}
/* Sliders */
.slider-container {
background: rgba(248, 250, 252, 0.8);
border: 1px solid rgba(226, 232, 240, 0.6);
border-radius: 8px;
padding: 1rem;
margin: 0.5rem 0;
}
/* Labels and text */
.gradio-container label {
color: #374151 !important;
font-weight: 600 !important;
}
.gradio-container .markdown {
color: #1f2937 !important;
}
/* Responsive design */
@media (max-width: 768px) {
.main-header h1 { font-size: 2rem; }
.settings-card, .generation-card { padding: 1rem; }
}
/* Random example button styling - more subtle professional color */
.random-btn {
background: linear-gradient(135deg, #64748b 0%, #475569 100%);
border: none;
border-radius: 12px;
padding: 1rem 1.5rem;
color: white;
font-weight: 600;
font-size: 1rem;
box-shadow: 0 4px 20px rgba(100, 116, 139, 0.3);
transition: all 0.3s ease;
display: inline-flex;
align-items: center;
gap: 0.5rem;
}
.random-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 25px rgba(100, 116, 139, 0.4);
background: linear-gradient(135deg, #475569 0%, #334155 100%);
}
"""
with gr.Blocks(
title="VibeVoice - AI Podcast Generator",
css=custom_css,
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="purple",
neutral_hue="slate",
)
) as interface:
# Header
gr.HTML("""
šļø Vibe Podcasting
Generating Long-form Multi-speaker AI Podcast with VibeVoice
""")
with gr.Row():
# Left column - Settings
with gr.Column(scale=1, elem_classes="settings-card"):
gr.Markdown("### šļø **Podcast Settings**")
# Number of speakers
num_speakers = gr.Slider(
minimum=1,
maximum=4,
value=2,
step=1,
label="Number of Speakers",
elem_classes="slider-container"
)
# Speaker selection
gr.Markdown("### š **Speaker Selection**")
available_speaker_names = list(demo_instance.available_voices.keys())
# default_speakers = available_speaker_names[:4] if len(available_speaker_names) >= 4 else available_speaker_names
default_speakers = ['en-Alice_woman', 'en-Carter_man', 'en-Frank_man', 'en-Maya_woman']
speaker_selections = []
for i in range(4):
default_value = default_speakers[i] if i < len(default_speakers) else None
speaker = gr.Dropdown(
choices=available_speaker_names,
value=default_value,
label=f"Speaker {i+1}",
visible=(i < 2), # Initially show only first 2 speakers
elem_classes="speaker-item"
)
speaker_selections.append(speaker)
# Advanced settings
gr.Markdown("### āļø **Advanced Settings**")
# Sampling parameters (contains all generation settings)
with gr.Accordion("Generation Parameters", open=False):
cfg_scale = gr.Slider(
minimum=1.0,
maximum=2.0,
value=1.3,
step=0.05,
label="CFG Scale (Guidance Strength)",
# info="Higher values increase adherence to text",
elem_classes="slider-container"
)
disable_voice_cloning = gr.Checkbox(
value=False,
label="Disable voice cloning (skip conditioning voice prompts)",
info="When enabled, sets is_prefill=False so the model ignores provided speaker audio."
)
# Right column - Generation
with gr.Column(scale=2, elem_classes="generation-card"):
gr.Markdown("### š **Script Input**")
script_input = gr.Textbox(
label="Conversation Script",
placeholder="""Enter your podcast script here. You can format it as:
Speaker 1: Welcome to our podcast today!
Speaker 2: Thanks for having me. I'm excited to discuss...
Or paste text directly and it will auto-assign speakers.""",
lines=12,
max_lines=20,
elem_classes="script-input"
)
# Button row with Random Example on the left and Generate on the right
with gr.Row():
# Random example button (now on the left)
random_example_btn = gr.Button(
"š² Random Example",
size="lg",
variant="secondary",
elem_classes="random-btn",
scale=1 # Smaller width
)
# Generate button (now on the right)
generate_btn = gr.Button(
"š Generate Podcast",
size="lg",
variant="primary",
elem_classes="generate-btn",
scale=2 # Wider than random button
)
# Stop button
stop_btn = gr.Button(
"š Stop Generation",
size="lg",
variant="stop",
elem_classes="stop-btn",
visible=False
)
# Streaming status indicator
streaming_status = gr.HTML(
value="""
LIVE STREAMING - Audio is being generated in real-time
""",
visible=False,
elem_id="streaming-status"
)
# Output section
gr.Markdown("### šµ **Generated Podcast**")
# Streaming audio output (outside of tabs for simpler handling)
audio_output = gr.Audio(
label="Streaming Audio (Real-time)",
type="numpy",
elem_classes="audio-output",
streaming=True, # Enable streaming mode
autoplay=True,
show_download_button=False, # Explicitly show download button
visible=True
)
# Complete audio output (non-streaming)
complete_audio_output = gr.Audio(
label="Complete Podcast (Download after generation)",
type="numpy",
elem_classes="audio-output complete-audio-section",
streaming=False, # Non-streaming mode
autoplay=False,
show_download_button=True, # Explicitly show download button
visible=False # Initially hidden, shown when audio is ready
)
gr.Markdown("""
*š” **Streaming**: Audio plays as it's being generated (may have slight pauses)
*š” **Complete Audio**: Will appear below after generation finishes*
""")
# Generation log
log_output = gr.Textbox(
label="Generation Log",
lines=8,
max_lines=15,
interactive=False,
elem_classes="log-output"
)
def update_speaker_visibility(num_speakers):
updates = []
for i in range(4):
updates.append(gr.update(visible=(i < num_speakers)))
return updates
num_speakers.change(
fn=update_speaker_visibility,
inputs=[num_speakers],
outputs=speaker_selections
)
# Main generation function with streaming
def generate_podcast_wrapper(num_speakers, script, speaker_1, speaker_2, speaker_3, speaker_4, cfg_scale, disable_voice_cloning):
"""Wrapper function to handle the streaming generation call."""
try:
speakers = [speaker_1, speaker_2, speaker_3, speaker_4]
# Clear outputs and reset visibility at start
yield None, gr.update(value=None, visible=False), "šļø Starting generation...", gr.update(visible=True), gr.update(visible=False), gr.update(visible=True)
# The generator will yield multiple times
final_log = "Starting generation..."
for streaming_audio, complete_audio, log, streaming_visible in demo_instance.generate_podcast_streaming(
num_speakers=int(num_speakers),
script=script,
speaker_1=speakers[0],
speaker_2=speakers[1],
speaker_3=speakers[2],
speaker_4=speakers[3],
cfg_scale=cfg_scale,
disable_voice_cloning=disable_voice_cloning
):
final_log = log
# Check if we have complete audio (final yield)
if complete_audio is not None:
# Final state: clear streaming, show complete audio
yield None, gr.update(value=complete_audio, visible=True), log, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
else:
# Streaming state: update streaming audio only
if streaming_audio is not None:
yield streaming_audio, gr.update(visible=False), log, streaming_visible, gr.update(visible=False), gr.update(visible=True)
else:
# No new audio, just update status
yield None, gr.update(visible=False), log, streaming_visible, gr.update(visible=False), gr.update(visible=True)
except Exception as e:
error_msg = f"ā A critical error occurred in the wrapper: {str(e)}"
print(error_msg)
import traceback
traceback.print_exc()
# Reset button states on error
yield None, gr.update(value=None, visible=False), error_msg, gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
def stop_generation_handler():
"""Handle stopping generation."""
demo_instance.stop_audio_generation()
# Return values for: log_output, streaming_status, generate_btn, stop_btn
return "š Generation stopped.", gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
# Add a clear audio function
def clear_audio_outputs():
"""Clear both audio outputs before starting new generation."""
return None, gr.update(value=None, visible=False)
# Connect generation button with streaming outputs
generate_btn.click(
fn=clear_audio_outputs,
inputs=[],
outputs=[audio_output, complete_audio_output],
queue=False
).then( # Immediate UI update to hide Generate, show Stop (non-queued)
fn=lambda: (gr.update(visible=False), gr.update(visible=True)),
inputs=[],
outputs=[generate_btn, stop_btn],
queue=False
).then(
fn=generate_podcast_wrapper,
inputs=[num_speakers, script_input] + speaker_selections + [cfg_scale, disable_voice_cloning],
outputs=[audio_output, complete_audio_output, log_output, streaming_status, generate_btn, stop_btn],
queue=True # Enable Gradio's built-in queue
)
# Connect stop button
stop_btn.click(
fn=stop_generation_handler,
inputs=[],
outputs=[log_output, streaming_status, generate_btn, stop_btn],
queue=False # Don't queue stop requests
).then(
# Clear both audio outputs after stopping
fn=lambda: (None, None),
inputs=[],
outputs=[audio_output, complete_audio_output],
queue=False
)
# Function to randomly select an example
def load_random_example():
"""Randomly select and load an example script."""
import random
# Get available examples
if hasattr(demo_instance, 'example_scripts') and demo_instance.example_scripts:
example_scripts = demo_instance.example_scripts
else:
# Fallback to default
example_scripts = [
[2, "Speaker 0: Welcome to our AI podcast demonstration!\nSpeaker 1: Thanks for having me. This is exciting!"]
]
# Randomly select one
if example_scripts:
selected = random.choice(example_scripts)
num_speakers_value = selected[0]
script_value = selected[1]
# Return the values to update the UI
return num_speakers_value, script_value
# Default values if no examples
return 2, ""
# Connect random example button
random_example_btn.click(
fn=load_random_example,
inputs=[],
outputs=[num_speakers, script_input],
queue=False # Don't queue this simple operation
)
# Add usage tips
gr.Markdown("""
### š” **Usage Tips**
- Click **š Generate Podcast** to start audio generation
- **Live Streaming** tab shows audio as it's generated (may have slight pauses)
- **Complete Audio** tab provides the full, uninterrupted podcast after generation
- During generation, you can click **š Stop Generation** to interrupt the process
- The streaming indicator shows real-time generation progress
""")
# Add example scripts
gr.Markdown("### š **Example Scripts**")
# Use dynamically loaded examples if available, otherwise provide a default
if hasattr(demo_instance, 'example_scripts') and demo_instance.example_scripts:
example_scripts = demo_instance.example_scripts
else:
# Fallback to a simple default example if no scripts loaded
example_scripts = [
[1, "Speaker 1: Welcome to our AI podcast demonstration! This is a sample script showing how VibeVoice can generate natural-sounding speech."]
]
gr.Examples(
examples=example_scripts,
inputs=[num_speakers, script_input],
label="Try these example scripts:"
)
# --- Risks & limitations (footer) ---
gr.Markdown(
"""
## Risks and limitations
While efforts have been made to optimize it through various techniques, it may still produce outputs that are unexpected, biased, or inaccurate. VibeVoice inherits any biases, errors, or omissions produced by its base model (specifically, Qwen2.5 1.5b in this release).
Potential for Deepfakes and Disinformation: High-quality synthetic speech can be misused to create convincing fake audio content for impersonation, fraud, or spreading disinformation. Users must ensure transcripts are reliable, check content accuracy, and avoid using generated content in misleading ways. Users are expected to use the generated content and to deploy the models in a lawful manner, in full compliance with all applicable laws and regulations in the relevant jurisdictions. It is best practice to disclose the use of AI when sharing AI-generated content.
""",
elem_classes="generation-card", # åÆéļ¼å¤ēØå”ēę ·å¼
)
return interface
def parse_args():
parser = argparse.ArgumentParser(description="VibeVoice Gradio Demo")
parser.add_argument(
"--model_path",
type=str,
default="microsoft/VibeVoice-1.5B",
help="Path to the VibeVoice model directory",
)
parser.add_argument(
"--device",
type=str,
default=("cuda" if torch.cuda.is_available() else ("mps" if torch.backends.mps.is_available() else "cpu")),
help="Device for inference: cuda | mps | cpu",
)
parser.add_argument(
"--inference_steps",
type=int,
default=10,
help="Number of inference steps for DDPM (not exposed to users)",
)
parser.add_argument(
"--share",
action="store_true",
help="Share the demo publicly via Gradio",
)
parser.add_argument(
"--port",
type=int,
default=7860,
help="Port to run the demo on",
)
parser.add_argument(
"--checkpoint_path",
type=str,
default=None,
help="Path to a fine-tuned checkpoint directory containing LoRA adapters (optional)",
)
return parser.parse_args()
def main():
"""Main function to run the demo."""
args = parse_args()
set_seed(42) # Set a fixed seed for reproducibility
print("šļø Initializing VibeVoice Demo with Streaming Support...")
# Initialize demo instance
demo_instance = VibeVoiceDemo(
model_path=args.model_path,
device=args.device,
inference_steps=args.inference_steps,
adapter_path=args.checkpoint_path,
)
# Create interface
interface = create_demo_interface(demo_instance)
print(f"š Launching demo on port {args.port}")
print(f"š Model path: {args.model_path}")
print(f"š Available voices: {len(demo_instance.available_voices)}")
print(f"š“ Streaming mode: ENABLED")
print(f"š Session isolation: ENABLED")
# Launch the interface
try:
interface.queue(
max_size=20, # Maximum queue size
default_concurrency_limit=1 # Process one request at a time
).launch(
share=args.share,
# server_port=args.port,
server_name="0.0.0.0" if args.share else "127.0.0.1",
show_error=True,
show_api=False # Hide API docs for cleaner interface
)
except KeyboardInterrupt:
print("\nš Shutting down gracefully...")
except Exception as e:
print(f"ā Server error: {e}")
raise
if __name__ == "__main__":
main()