NeuralFalcon commited on
Commit
a1e47d6
·
verified ·
1 Parent(s): 7560e56

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +640 -0
app.py ADDED
@@ -0,0 +1,640 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # %%writefile /content/VibeVoice/demo/colab.py
2
+ # Original Code: https://github.com/microsoft/VibeVoice/blob/main/demo/gradio_demo.py
3
+ """
4
+ VibeVoice Gradio Demo
5
+ """
6
+
7
+ import json
8
+ import os
9
+ import sys
10
+ import tempfile
11
+ import time
12
+ from pathlib import Path
13
+ from typing import List, Dict, Any, Iterator
14
+ from datetime import datetime
15
+ import threading
16
+ import numpy as np
17
+ import gradio as gr
18
+ import librosa
19
+ import soundfile as sf
20
+ import torch
21
+ import os
22
+ import traceback
23
+ import shutil
24
+ import re # Added for timestamp feature
25
+ import uuid # Added for timestamp feature
26
+
27
+ from vibevoice.modular.configuration_vibevoice import VibeVoiceConfig
28
+ from vibevoice.modular.modeling_vibevoice_inference import VibeVoiceForConditionalGenerationInference, VibeVoiceGenerationOutput
29
+ from vibevoice.processor.vibevoice_processor import VibeVoiceProcessor
30
+ from vibevoice.modular.streamer import AudioStreamer
31
+ from transformers import set_seed
32
+
33
+ from pydub import AudioSegment
34
+ from pydub.silence import split_on_silence
35
+
36
+ def drive_save(file_copy):
37
+ drive_path = "/content/gdrive/MyDrive"
38
+ save_folder = os.path.join(drive_path, "VibeVoice_Podcast")
39
+
40
+ if os.path.exists(drive_path):
41
+ print("Running on Google Colab and auto-saving to Google Drive...")
42
+ os.makedirs(save_folder, exist_ok=True)
43
+ dest_path = os.path.join(save_folder, os.path.basename(file_copy))
44
+ shutil.copy2(file_copy, dest_path) # preserves metadata
45
+ print(f"File saved to: {dest_path}")
46
+ return dest_path
47
+ else:
48
+ print("Not running on Google Colab (or Google Drive not mounted). Skipping auto-save.")
49
+ return None
50
+
51
+ import os, requests, urllib.request, urllib.error
52
+ from tqdm.auto import tqdm
53
+
54
+ def download_file(url, download_file_path, redownload=False):
55
+ """Download a single file with urllib + tqdm progress bar."""
56
+
57
+ base_path = os.path.dirname(download_file_path)
58
+ os.makedirs(base_path, exist_ok=True)
59
+
60
+ # skip logic
61
+ if os.path.exists(download_file_path):
62
+ if redownload:
63
+ os.remove(download_file_path)
64
+ tqdm.write(f"♻️ Redownloading: {os.path.basename(download_file_path)}")
65
+ elif os.path.getsize(download_file_path) > 0:
66
+ tqdm.write(f"✔️ Skipped (already exists): {os.path.basename(download_file_path)}")
67
+ return True
68
+
69
+ try:
70
+ request = urllib.request.urlopen(url)
71
+ total = int(request.headers.get('Content-Length', 0))
72
+ except urllib.error.URLError as e:
73
+ print(f"❌ Error: Unable to open URL: {url}")
74
+ print(f"Reason: {e.reason}")
75
+ return False
76
+
77
+ with tqdm(total=total, desc=os.path.basename(download_file_path), unit='B', unit_scale=True, unit_divisor=1024) as progress:
78
+ try:
79
+ urllib.request.urlretrieve(
80
+ url,
81
+ download_file_path,
82
+ reporthook=lambda count, block_size, total_size: progress.update(block_size)
83
+ )
84
+ except urllib.error.URLError as e:
85
+ print(f"❌ Error: Failed to download {url}")
86
+ print(f"Reason: {e.reason}")
87
+ return False
88
+
89
+ tqdm.write(f"⬇️ Downloaded: {os.path.basename(download_file_path)}")
90
+ return True
91
+
92
+
93
+ def download_model(repo_id, download_folder="./", redownload=False):
94
+ # normalize empty string as current dir
95
+ if not download_folder.strip():
96
+ download_folder = "."
97
+ url = f"https://huggingface.co/api/models/{repo_id}"
98
+ download_dir = os.path.abspath(f"{download_folder.rstrip('/')}/{repo_id.split('/')[-1]}")
99
+ os.makedirs(download_dir, exist_ok=True)
100
+
101
+ print(f"📂 Download directory: {download_dir}")
102
+
103
+ response = requests.get(url)
104
+ if response.status_code != 200:
105
+ print("❌ Error:", response.status_code, response.text)
106
+ return None
107
+
108
+ data = response.json()
109
+ siblings = data.get("siblings", [])
110
+ files = [f["rfilename"] for f in siblings]
111
+
112
+ print(f"📦 Found {len(files)} files in repo '{repo_id}'. Checking cache ...")
113
+
114
+ for file in tqdm(files, desc="Processing files", unit="file"):
115
+ file_url = f"https://huggingface.co/{repo_id}/resolve/main/{file}"
116
+ file_path = os.path.join(download_dir, file)
117
+ download_file(file_url, file_path, redownload=redownload)
118
+
119
+ return download_dir
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+ # NEW FEATURE: Function to generate unique filenames for output
128
+ def generate_file_name(text):
129
+ """Generates a unique, clean filename based on the script's first line."""
130
+ output_dir = "./podcast_audio"
131
+ os.makedirs(output_dir, exist_ok=True)
132
+ # Clean the text to get a base for the filename
133
+ cleaned = re.sub(r"^\s*speaker\s*\d+\s*:\s*", "", text, flags=re.IGNORECASE)
134
+ short = cleaned[:30].strip()
135
+ short = re.sub(r'[^a-zA-Z0-9\s]', '', short)
136
+ short = short.lower().strip().replace(" ", "_")
137
+ if not short:
138
+ short = "podcast_output"
139
+ # Add a unique identifier
140
+ unique_name = f"{short}_{uuid.uuid4().hex[:6]}"
141
+
142
+ return os.path.join(output_dir, unique_name)
143
+
144
+
145
+ class VibeVoiceDemo:
146
+ def __init__(self, model_path: str, device: str = "cuda", inference_steps: int = 5):
147
+ """Initialize the VibeVoice demo with model loading."""
148
+ self.model_path = model_path
149
+ self.device = device
150
+ self.inference_steps = inference_steps
151
+ self.is_generating = False # Track generation state
152
+ self.stop_generation = False # Flag to stop generation
153
+ self.load_model()
154
+ self.setup_voice_presets()
155
+ self.load_example_scripts() # Load example scripts
156
+
157
+ def load_model(self):
158
+ """Load the VibeVoice model and processor."""
159
+ print(f"Loading processor & model from {self.model_path}")
160
+ self.processor = VibeVoiceProcessor.from_pretrained(self.model_path)
161
+ self.model = VibeVoiceForConditionalGenerationInference.from_pretrained(
162
+ self.model_path,
163
+ torch_dtype=torch.bfloat16,
164
+ device_map='cuda',
165
+ )
166
+ self.model.eval()
167
+ self.model.model.noise_scheduler = self.model.model.noise_scheduler.from_config(
168
+ self.model.model.noise_scheduler.config,
169
+ algorithm_type='sde-dpmsolver++',
170
+ beta_schedule='squaredcos_cap_v2'
171
+ )
172
+ self.model.set_ddpm_inference_steps(num_steps=self.inference_steps)
173
+ if hasattr(self.model.model, 'language_model'):
174
+ print(f"Language model attention: {self.model.model.language_model.config._attn_implementation}")
175
+
176
+ def setup_voice_presets(self):
177
+ """Setup voice presets by scanning the voices directory."""
178
+ voices_dir = os.path.join(os.path.dirname(__file__), "voices")
179
+ if not os.path.exists(voices_dir):
180
+ print(f"Warning: Voices directory not found at {voices_dir}, creating it.")
181
+ os.makedirs(voices_dir, exist_ok=True)
182
+ self.voice_presets = {}
183
+ audio_files = [f for f in os.listdir(voices_dir)
184
+ if f.lower().endswith(('.wav', '.mp3', '.flac', '.ogg', '.m4a', '.aac')) and os.path.isfile(os.path.join(voices_dir, f))]
185
+ for audio_file in audio_files:
186
+ name = os.path.splitext(audio_file)[0]
187
+ full_path = os.path.join(voices_dir, audio_file)
188
+ self.voice_presets[name] = full_path
189
+ self.voice_presets = dict(sorted(self.voice_presets.items()))
190
+ self.available_voices = {name: path for name, path in self.voice_presets.items() if os.path.exists(path)}
191
+ if not self.available_voices:
192
+ print("Warning: No voice presets found.")
193
+ print(f"Found {len(self.available_voices)} voice files in {voices_dir}")
194
+
195
+ def read_audio(self, audio_path: str, target_sr: int = 24000) -> np.ndarray:
196
+ """Read and preprocess audio file."""
197
+ try:
198
+ wav, sr = sf.read(audio_path)
199
+ if len(wav.shape) > 1:
200
+ wav = np.mean(wav, axis=1)
201
+ if sr != target_sr:
202
+ wav = librosa.resample(wav, orig_sr=sr, target_sr=target_sr)
203
+ return wav
204
+ except Exception as e:
205
+ print(f"Error reading audio {audio_path}: {e}")
206
+ return np.array([])
207
+
208
+ def trim_silence_from_numpy(self, audio_np: np.ndarray, sample_rate: int, silence_thresh: int = -45, min_silence_len: int = 100, keep_silence: int = 50) -> np.ndarray:
209
+ """Removes silence from a NumPy audio array using pydub."""
210
+ audio_int16 = (audio_np * 32767).astype(np.int16)
211
+ sound = AudioSegment(
212
+ data=audio_int16.tobytes(),
213
+ sample_width=audio_int16.dtype.itemsize,
214
+ frame_rate=sample_rate,
215
+ channels=1
216
+ )
217
+ audio_chunks = split_on_silence(
218
+ sound, min_silence_len=min_silence_len, silence_thresh=silence_thresh, keep_silence=keep_silence
219
+ )
220
+ if not audio_chunks:
221
+ return np.array([0.0], dtype=np.float32)
222
+
223
+ combined = sum(audio_chunks)
224
+ samples = np.array(combined.get_array_of_samples())
225
+ trimmed_audio_np = samples.astype(np.float32) / 32767.0
226
+ return trimmed_audio_np
227
+
228
+ def generate_podcast_with_timestamps(self,
229
+ num_speakers: int,
230
+ script: str,
231
+ speaker_1: str = None,
232
+ speaker_2: str = None,
233
+ speaker_3: str = None,
234
+ speaker_4: str = None,
235
+ cfg_scale: float = 1.3,
236
+ remove_silence: bool = False,
237
+ progress=gr.Progress()):
238
+ try:
239
+ self.stop_generation = False
240
+ self.is_generating = True
241
+
242
+ # --- Input Validation and Setup ---
243
+ if not script.strip(): raise gr.Error("Error: Please provide a script.")
244
+ script = script.replace("’", "'")
245
+ if not 1 <= num_speakers <= 4: raise gr.Error("Error: Number of speakers must be between 1 and 4.")
246
+
247
+ selected_speakers = [speaker_1, speaker_2, speaker_3, speaker_4][:num_speakers]
248
+ for i, speaker in enumerate(selected_speakers):
249
+ if not speaker or speaker not in self.available_voices:
250
+ raise gr.Error(f"Error: Please select a valid speaker for Speaker {i+1}.")
251
+
252
+ voice_samples = [self.read_audio(self.available_voices[name]) for name in selected_speakers]
253
+ if any(len(vs) == 0 for vs in voice_samples): raise gr.Error("Error: Failed to load one or more audio files.")
254
+
255
+ lines = script.strip().split('\n')
256
+ formatted_script_lines = []
257
+ for line in lines:
258
+ line = line.strip()
259
+ if not line: continue
260
+ if re.match(r'Speaker\s*\d+:', line, re.IGNORECASE):
261
+ formatted_script_lines.append(line)
262
+ else:
263
+ speaker_id = len(formatted_script_lines) % num_speakers
264
+ formatted_script_lines.append(f"Speaker {speaker_id}: {line}")
265
+
266
+ if not formatted_script_lines: raise gr.Error("Error: Script is empty after formatting.")
267
+
268
+ # --- Prepare for Generation ---
269
+ timestamps = {}
270
+ current_time = 0.0
271
+ sample_rate = 24000
272
+ total_lines = len(formatted_script_lines)
273
+
274
+ base_filename = generate_file_name(formatted_script_lines[0])
275
+ final_audio_path = base_filename + ".wav"
276
+ final_json_path = base_filename + ".json"
277
+
278
+ # --- Open file and write chunks sequentially (MEMORY EFFICIENT) ---
279
+ with sf.SoundFile(final_audio_path, 'w', samplerate=sample_rate, channels=1, subtype='PCM_16') as audio_file:
280
+ for i, line in enumerate(formatted_script_lines):
281
+ if self.stop_generation:
282
+ break
283
+
284
+ progress(i / total_lines, desc=f"Generating line {i+1}/{total_lines}")
285
+
286
+ match = re.match(r'Speaker\s*(\d+):\s*(.*)', line, re.IGNORECASE)
287
+ if not match: continue
288
+
289
+ speaker_idx = int(match.group(1)) - 1
290
+ text_content = match.group(2).strip()
291
+
292
+ if speaker_idx < 0 or speaker_idx >= len(voice_samples):
293
+ continue
294
+
295
+ inputs = self.processor(
296
+ text=[line], voice_samples=[voice_samples], padding=True, return_tensors="pt"
297
+ )
298
+
299
+ output_waveform: VibeVoiceGenerationOutput = self.model.generate(
300
+ **inputs, max_new_tokens=None, cfg_scale=cfg_scale, tokenizer=self.processor.tokenizer,
301
+ generation_config={'do_sample': False}, verbose=False, refresh_negative=True
302
+ )
303
+
304
+ audio_np = output_waveform.speech_outputs[0].cpu().float().numpy().squeeze()
305
+
306
+ # NEW FEATURE: Remove silence if enabled
307
+ if remove_silence:
308
+ audio_np = self.trim_silence_from_numpy(audio_np, sample_rate)
309
+
310
+ duration = len(audio_np) / sample_rate
311
+ audio_int16 = (audio_np * 32767).astype(np.int16)
312
+ audio_file.write(audio_int16)
313
+
314
+ timestamps[str(i + 1)] = {
315
+ "text": text_content, "speaker_id": speaker_idx,
316
+ "start": current_time, "end": current_time + duration
317
+ }
318
+ current_time += duration
319
+
320
+ # --- Finalize and Save JSON ---
321
+ progress(1.0, desc="Saving timestamp file...")
322
+ with open(final_json_path, "w") as f:
323
+ json.dump(timestamps, f, indent=2)
324
+ try:
325
+ drive_save(final_audio_path)
326
+ drive_save(final_json_path)
327
+ except Exception as e:
328
+ print(f"Error saving files to Google Drive: {e}")
329
+
330
+ print(f"\n✨ Generation successful!\n🎵 Audio: {final_audio_path}\n📄 Timestamps: {final_json_path}\n")
331
+ self.is_generating = False
332
+
333
+ return final_audio_path, final_audio_path, final_json_path, gr.update(visible=True), gr.update(visible=False)
334
+
335
+ except Exception as e:
336
+ self.is_generating = False
337
+ print(f"❌ An unexpected error occurred: {str(e)}")
338
+ traceback.print_exc()
339
+ return None, None, None, gr.update(visible=True), gr.update(visible=False)
340
+
341
+ def stop_audio_generation(self):
342
+ if self.is_generating:
343
+ self.stop_generation = True
344
+ print("🛑 Audio generation stop requested")
345
+
346
+ def load_example_scripts(self):
347
+ examples_dir = os.path.join(os.path.dirname(__file__), "text_examples")
348
+ self.example_scripts = []
349
+ if not os.path.exists(examples_dir): return
350
+ txt_files = sorted([f for f in os.listdir(examples_dir) if f.lower().endswith('.txt')])
351
+ for txt_file in txt_files:
352
+ try:
353
+ with open(os.path.join(examples_dir, txt_file), 'r', encoding='utf-8') as f:
354
+ script = f.read().strip()
355
+ if script: self.example_scripts.append([self._get_num_speakers_from_script(script), script])
356
+ except Exception as e:
357
+ print(f"Error loading example {txt_file}: {e}")
358
+
359
+ def _get_num_speakers_from_script(self, script: str) -> int:
360
+ speakers = set(re.findall(r'^Speaker\s+(\d+)\s*:', script, re.MULTILINE | re.IGNORECASE))
361
+ return max(int(s) for s in speakers) if speakers else 1
362
+
363
+ def create_demo_interface(demo_instance: VibeVoiceDemo):
364
+ with gr.Blocks(
365
+ title="VibeVoice AI Podcast Generator"
366
+ ) as interface:
367
+
368
+ gr.HTML("""
369
+ <div style="text-align: center; margin: 20px auto; max-width: 800px;">
370
+ <h1 style="font-size: 2.5em; margin-bottom: 5px;">🎙️ Vibe Podcasting</h1>
371
+ <p style="font-size: 1.2em; color: #555;">Generating Long-form Multi-speaker AI Podcast with VibeVoice</p>
372
+ </div>
373
+ """)
374
+
375
+ with gr.Row():
376
+ # Left column - Settings
377
+ with gr.Column(scale=1):
378
+ with gr.Group():
379
+ gr.Markdown("### 🎛️ Podcast Settings")
380
+ num_speakers = gr.Slider(minimum=1, maximum=4, value=2, step=1, label="Number of Speakers")
381
+
382
+ gr.Markdown("### 🎭 Speaker Selection")
383
+ speaker_selections = []
384
+ available_voices = list(demo_instance.available_voices.keys())
385
+ defaults = ['en-Alice_woman', 'en-Carter_man', 'en-Frank_man', 'en-Maya_woman']
386
+ for i in range(4):
387
+ val = defaults[i] if i < len(defaults) and defaults[i] in available_voices else None
388
+ speaker = gr.Dropdown(choices=available_voices, value=val, label=f"Speaker {i+1}", visible=(i < 2))
389
+ speaker_selections.append(speaker)
390
+
391
+ with gr.Accordion("🎤 Upload Custom Voices", open=False):
392
+ upload_audio = gr.File(label="Upload Voice Samples", file_count="multiple", file_types=["audio"])
393
+ process_upload_btn = gr.Button("Add Uploaded Voices to Speaker Selection")
394
+
395
+ with gr.Accordion("⚙️ Advanced Settings", open=False):
396
+ cfg_scale = gr.Slider(minimum=1.0, maximum=2.0, value=1.3, step=0.05, label="CFG Scale")
397
+ # NEW FEATURE: Silence removal checkbox
398
+ remove_silence_checkbox = gr.Checkbox(label="Trim Silence from Podcast", value=False,)
399
+
400
+ # Right column - Generation
401
+ with gr.Column(scale=2):
402
+ with gr.Group():
403
+ gr.Markdown("### 📝 Script Input")
404
+ script_input = gr.Textbox(label="Conversation Script", placeholder="Enter script here...", lines=10)
405
+
406
+ with gr.Row():
407
+ random_example_btn = gr.Button("🎲 Random Example", scale=1)
408
+ generate_btn = gr.Button("🚀 Generate Podcast", variant="primary", scale=2)
409
+
410
+ stop_btn = gr.Button("🛑 Stop Generation", variant="stop", visible=False)
411
+
412
+ gr.Markdown("### 🎵 **Generated Output**")
413
+ audio_output = gr.Audio(label="Play Generated Podcast")
414
+ with gr.Accordion("📦 Download Files", open=False):
415
+ download_file = gr.File(label="Download Audio File (.wav)")
416
+ json_file_output = gr.File(label="Download Timestamps (.json)")
417
+
418
+ with gr.Accordion("💡 Usage Tips & Examples", open=True):
419
+ gr.Markdown("""
420
+ - **Upload Your Own Voices:** Create your own podcast with custom voice samples.
421
+ - **Timestamps:** Useful if you want to generate a video using Wan2.2 or other tools. The timestamps let you automatically separate each speaker (splitting the long podcast into smaller chunks), pass the audio clips to your video generation model, and then merge the generated video clips into a full podcast video (e.g., using FFmpeg + any video generation model such as image+audio → video).
422
+ """)
423
+ gr.Examples(examples=demo_instance.example_scripts, inputs=[num_speakers, script_input], label="Try these example scripts:")
424
+
425
+ # --- Backend Functions ---
426
+ def process_and_refresh_voices(uploaded_files):
427
+ if not uploaded_files: return [gr.update() for _ in speaker_selections] + [None]
428
+ voices_dir = os.path.join(os.path.dirname(__file__), "voices")
429
+ for f in uploaded_files: shutil.copy(f.name, os.path.join(voices_dir, os.path.basename(f.name)))
430
+ demo_instance.setup_voice_presets()
431
+ new_choices = list(demo_instance.available_voices.keys())
432
+ return [gr.update(choices=new_choices) for _ in speaker_selections] + [None]
433
+
434
+ def update_speaker_visibility(num):
435
+ return [gr.update(visible=(i < num)) for i in range(4)]
436
+
437
+ def handle_generate_click():
438
+ return gr.update(visible=False), gr.update(visible=True)
439
+
440
+ num_speakers.change(fn=update_speaker_visibility, inputs=num_speakers, outputs=speaker_selections)
441
+ process_upload_btn.click(fn=process_and_refresh_voices, inputs=upload_audio, outputs=speaker_selections + [upload_audio])
442
+
443
+ gen_event = generate_btn.click(
444
+ fn=handle_generate_click,
445
+ outputs=[generate_btn, stop_btn]
446
+ ).then(
447
+ fn=demo_instance.generate_podcast_with_timestamps,
448
+ inputs=[num_speakers, script_input] + speaker_selections + [cfg_scale, remove_silence_checkbox],
449
+ outputs=[audio_output, download_file, json_file_output, generate_btn, stop_btn],
450
+ )
451
+
452
+ stop_btn.click(fn=demo_instance.stop_audio_generation, cancels=[gen_event])
453
+
454
+ def load_random_example():
455
+ import random
456
+ return random.choice(demo_instance.example_scripts) if demo_instance.example_scripts else (2, "Speaker 0: No examples loaded.")
457
+
458
+ random_example_btn.click(fn=load_random_example, outputs=[num_speakers, script_input])
459
+
460
+ return interface
461
+
462
+
463
+
464
+ import gradio as gr
465
+
466
+ def build_conversation_prompt(topic, *speaker_names):
467
+ """
468
+ Generates the final prompt. It takes the topic and a variable number of speaker names.
469
+ """
470
+ names = [name for name in speaker_names if name and name.strip()]
471
+
472
+ # Error checking
473
+ if not topic or not topic.strip():
474
+ return "Error: Please provide a topic."
475
+ if not names:
476
+ return "Error: Please provide at least one speaker name."
477
+
478
+ num_speakers = len(names)
479
+ speaker_mapping_str = "Speaker mapping (for context only, DO NOT use these names as labels):\n"
480
+ for i, name in enumerate(names):
481
+ speaker_mapping_str += f"- Speaker {i+1} = {name}\n"
482
+
483
+ speaker_labels = [f"\"Speaker {i+1}:\"" for i in range(num_speakers)]
484
+
485
+ introductions_str = ""
486
+ for i, name in enumerate(names):
487
+ introductions_str += f" - Speaker {i+1} introduces themselves by saying: \"I’m {name}...\"\n"
488
+
489
+ example_str = "STRICT Example (follow this format exactly):\n"
490
+ example_str += f"Speaker 1: Hi everyone, I’m {names[0]}, and I’m excited to be here today.\n"
491
+ if num_speakers > 1:
492
+ for i in range(1, num_speakers):
493
+ example_str += f"Speaker {i+1}: And I’m {names[i]}. Thanks for joining us.\n"
494
+ example_str += "Speaker 1: So, let’s dive into our topic...\n"
495
+
496
+ prompt = f"""
497
+ You are a professional podcast scriptwriter.
498
+ Write a natural, engaging conversation between {num_speakers} speakers on the topic: "{topic}".
499
+
500
+ {speaker_mapping_str}
501
+ Formatting Rules:
502
+ - You MUST always format dialogue with {', '.join(speaker_labels)} ONLY.
503
+ - Never replace the labels with real names. The labels stay exactly as they are.
504
+ - At the beginning:
505
+ {introductions_str}
506
+ - During the conversation, they may occasionally mention each other's names ({', '.join(names)}) naturally in the dialogue, but the labels must remain unchanged.
507
+ - Do not add narration, descriptions, or any extra formatting.
508
+
509
+ {example_str}
510
+ """
511
+ return prompt
512
+
513
+ def update_speaker_name_visibility(num_speakers):
514
+ """
515
+ Shows or hides the speaker name textboxes based on the slider value.
516
+ """
517
+ num = int(num_speakers)
518
+ updates = []
519
+ for i in range(4):
520
+ if i < num:
521
+ updates.append(gr.update(visible=True))
522
+ else:
523
+ updates.append(gr.update(visible=False, value=""))
524
+
525
+ return tuple(updates)
526
+
527
+ def ui2():
528
+
529
+ with gr.Blocks(title="Prompt Builder") as demo:
530
+ gr.HTML("""
531
+ <div style="text-align: center; margin: 20px auto; max-width: 800px;">
532
+ <h1 style="font-size: 2.5em; margin-bottom: 5px;">🎙️ Sample Podcast Prompt Generator</h1>
533
+ <p style="font-size: 1.2em; color: #555;">Paste the prompt into any LLM, and customize the propmt if you want.</p>
534
+ </div>""")
535
+
536
+ with gr.Row():
537
+ with gr.Column(scale=1):
538
+ topic = gr.Textbox(label="Topic", placeholder="e.g., The Future of Artificial Intelligence")
539
+
540
+ num_speakers = gr.Slider(
541
+ minimum=1,
542
+ maximum=4,
543
+ value=2,
544
+ step=1,
545
+ label="Number of Speakers"
546
+ )
547
+
548
+ with gr.Group():
549
+ speaker_textboxes = [
550
+ gr.Textbox(label=f"Speaker {i+1} Name", visible=(i < 2), placeholder=f"e.g., Speaker {i+1}")
551
+ for i in range(4)
552
+ ]
553
+
554
+ gen_btn = gr.Button("Generate Prompt", variant="primary")
555
+
556
+
557
+ gr.Examples(
558
+ examples=[
559
+ ["The Ethics of Gene Editing", 2, "Dr. Evelyn Reed", "Dr. Ben Carter", "", ""],
560
+ ["Exploring the Deep Sea", 3, "Maria", "Leo", "Samira", ""],
561
+ ["The Future of Space Tourism", 4, "Alex", "Zara", "Kenji", "Isla"]
562
+ ],
563
+ # The inputs list must match the order of items in the examples list
564
+ inputs=[topic, num_speakers] + speaker_textboxes,
565
+ label="Quick Examples"
566
+ )
567
+
568
+ with gr.Column(scale=2):
569
+ output_prompt = gr.Textbox(label="Generated Prompt", lines=25, interactive=False, show_copy_button=True)
570
+
571
+
572
+ num_speakers.change(
573
+ fn=update_speaker_name_visibility,
574
+ inputs=num_speakers,
575
+ outputs=speaker_textboxes
576
+ )
577
+
578
+ gen_btn.click(
579
+ fn=build_conversation_prompt,
580
+ inputs=[topic] + speaker_textboxes,
581
+ outputs=[output_prompt]
582
+ )
583
+
584
+ return demo
585
+
586
+
587
+ import click
588
+ @click.command()
589
+ @click.option(
590
+ "--model_path",
591
+ default="microsoft/VibeVoice-1.5B",
592
+ help="Hugging Face Model Repo ID."
593
+ )
594
+ @click.option(
595
+ "--inference_steps",
596
+ default=10,
597
+ show_default=True,
598
+ type=int,
599
+ help="Number of inference steps for generation."
600
+ )
601
+ @click.option(
602
+ "--debug",
603
+ is_flag=True,
604
+ default=False,
605
+ help="Enable debug mode."
606
+ )
607
+ @click.option(
608
+ "--share",
609
+ is_flag=True,
610
+ default=False,
611
+ help="Enable sharing of the interface."
612
+ )
613
+ def main(model_path, inference_steps, debug, share):
614
+ # model_path = "microsoft/VibeVoice-1.5B"
615
+ # model_folder = download_model(model_path, download_folder="./", redownload=False)
616
+ model_folder=model_path
617
+ device = "cuda" if torch.cuda.is_available() else "cpu"
618
+ set_seed(42)
619
+ print("🎙️ Initializing VibeVoice Demo with Timestamp Support...")
620
+ demo_instance = VibeVoiceDemo(
621
+ model_path=model_folder,
622
+ device=device,
623
+ inference_steps=inference_steps
624
+ )
625
+
626
+ custom_css = """
627
+ .gradio-container {
628
+ font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, sans-serif;
629
+ }"""
630
+ demo1 = create_demo_interface(demo_instance)
631
+ demo2=ui2()
632
+ demo = gr.TabbedInterface([demo1, demo2],["Vibe Podcasting","Generate Sample Podcast Script"],title="",theme=gr.themes.Soft(),css=custom_css)
633
+
634
+ print("🚀 Launching Gradio Demo...")
635
+ demo.queue().launch(debug=debug, share=share)
636
+
637
+ if __name__ == "__main__":
638
+ main()
639
+
640
+ # !python /content/VibeVoice/demo/colab.py --model_path microsoft/VibeVoice-1.5B --inference_steps 10 --debug --share