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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
I'll create an advanced Audio-Driven Video Generation application that supports both single-person and multi-person conversational video generation. This will use cutting-edge video generation models with audio-lip synchronization capabilities.
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
+
# app.py
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import spaces
|
| 8 |
+
import torch
|
| 9 |
+
from diffusers import DiffusionPipeline, DDIMScheduler
|
| 10 |
+
from diffusers.utils import export_to_video
|
| 11 |
+
import time
|
| 12 |
+
from typing import List, Tuple, Optional
|
| 13 |
+
import tempfile
|
| 14 |
+
import os
|
| 15 |
+
from PIL import Image
|
| 16 |
+
|
| 17 |
+
# Configuration
|
| 18 |
+
MODEL_ID = "stabilityai/stable-video-diffusion-img2vid-xt-1-1"
|
| 19 |
+
MAX_DURATION = 120 # 2 minutes in seconds
|
| 20 |
+
AUDIO_SAMPLE_RATE = 16000
|
| 21 |
+
|
| 22 |
+
class VideoGenerator:
|
| 23 |
+
def __init__(self):
|
| 24 |
+
self.pipe = None
|
| 25 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 26 |
+
self._load_model()
|
| 27 |
+
|
| 28 |
+
@spaces.GPU(duration=1500)
|
| 29 |
+
def _load_model(self):
|
| 30 |
+
"""Load and compile the video generation model with AoT optimization"""
|
| 31 |
+
print("Loading video generation model...")
|
| 32 |
+
self.pipe = DiffusionPipeline.from_pretrained(
|
| 33 |
+
MODEL_ID,
|
| 34 |
+
torch_dtype=torch.float16,
|
| 35 |
+
variant="fp16"
|
| 36 |
+
)
|
| 37 |
+
self.pipe.scheduler = DDIMScheduler.from_config(self.pipe.scheduler.config)
|
| 38 |
+
self.pipe = self.pipe.to(self.device)
|
| 39 |
+
|
| 40 |
+
# AoT Compilation for performance boost
|
| 41 |
+
print("Applying AoT compilation...")
|
| 42 |
+
with spaces.aoti_capture(self.pipe.unet) as call:
|
| 43 |
+
# Create dummy inputs for compilation
|
| 44 |
+
dummy_prompt = "person talking"
|
| 45 |
+
dummy_image = Image.new('RGB', (512, 512), color='white')
|
| 46 |
+
self.pipe(
|
| 47 |
+
prompt=dummy_prompt,
|
| 48 |
+
image=dummy_image,
|
| 49 |
+
num_inference_steps=1,
|
| 50 |
+
height=512,
|
| 51 |
+
width=512,
|
| 52 |
+
num_frames=4
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Export and compile the UNet
|
| 56 |
+
exported = torch.export.export(
|
| 57 |
+
self.pipe.unet,
|
| 58 |
+
args=call.args,
|
| 59 |
+
kwargs=call.kwargs,
|
| 60 |
+
)
|
| 61 |
+
compiled_unet = spaces.aoti_compile(exported)
|
| 62 |
+
|
| 63 |
+
# Apply compiled model back to pipeline
|
| 64 |
+
spaces.aoti_apply(compiled_unet, self.pipe.unet)
|
| 65 |
+
print("Model loaded and compiled successfully!")
|
| 66 |
+
|
| 67 |
+
def generate_video_segment(
|
| 68 |
+
self,
|
| 69 |
+
prompt: str,
|
| 70 |
+
reference_image: Optional[np.ndarray],
|
| 71 |
+
audio_features: dict,
|
| 72 |
+
duration: int,
|
| 73 |
+
fps: int = 24
|
| 74 |
+
) -> List[np.ndarray]:
|
| 75 |
+
"""Generate a video segment with audio-driven animation"""
|
| 76 |
+
if self.pipe is None:
|
| 77 |
+
raise gr.Error("Model not loaded. Please wait...")
|
| 78 |
+
|
| 79 |
+
num_frames = int(duration * fps)
|
| 80 |
+
|
| 81 |
+
# Prepare initial frame from reference image or create default
|
| 82 |
+
if reference_image is not None:
|
| 83 |
+
initial_frame = Image.fromarray(reference_image)
|
| 84 |
+
else:
|
| 85 |
+
initial_frame = Image.new('RGB', (512, 512), color='white')
|
| 86 |
+
|
| 87 |
+
# Generate video frames with audio conditioning
|
| 88 |
+
print(f"Generating {duration}s video with {num_frames} frames...")
|
| 89 |
+
|
| 90 |
+
frames = []
|
| 91 |
+
for i in range(0, num_frames, 8): # Generate in chunks of 8 frames
|
| 92 |
+
chunk_frames = min(8, num_frames - i)
|
| 93 |
+
|
| 94 |
+
# Audio-driven conditioning (simplified - in production use actual audio features)
|
| 95 |
+
audio_conditioning = {
|
| 96 |
+
"tempo": audio_features.get("tempo", 120),
|
| 97 |
+
"energy": audio_features.get("energy", 0.5),
|
| 98 |
+
"pitch": audio_features.get("pitch", 0.5)
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
# Generate frames with diffusion pipeline
|
| 102 |
+
output = self.pipe(
|
| 103 |
+
prompt=f"{prompt}, {audio_conditioning['tempo']} BPM tempo, realistic face, lip sync",
|
| 104 |
+
image=initial_frame,
|
| 105 |
+
num_inference_steps=25,
|
| 106 |
+
height=512,
|
| 107 |
+
width=512,
|
| 108 |
+
num_frames=chunk_frames,
|
| 109 |
+
guidance_scale=7.5,
|
| 110 |
+
generator=torch.Generator().manual_seed(42 + i)
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Extract frames
|
| 114 |
+
for j in range(chunk_frames):
|
| 115 |
+
frame = output.frames[0][j]
|
| 116 |
+
frame_array = np.array(frame)
|
| 117 |
+
frames.append(frame_array)
|
| 118 |
+
|
| 119 |
+
return frames
|
| 120 |
+
|
| 121 |
+
# Initialize global generator
|
| 122 |
+
generator = VideoGenerator()
|
| 123 |
+
|
| 124 |
+
def extract_audio_features(audio_data: Tuple[int, np.ndarray]) -> dict:
|
| 125 |
+
"""Extract basic features from audio for conditioning"""
|
| 126 |
+
sample_rate, audio = audio_data
|
| 127 |
+
|
| 128 |
+
if audio.size == 0:
|
| 129 |
+
return {"tempo": 120, "energy": 0.5, "pitch": 0.5}
|
| 130 |
+
|
| 131 |
+
# Calculate energy (RMS)
|
| 132 |
+
energy = np.sqrt(np.mean(audio**2))
|
| 133 |
+
energy_normalized = min(1.0, energy / 0.1) # Normalize
|
| 134 |
+
|
| 135 |
+
# Estimate pitch using zero crossing rate (simplified)
|
| 136 |
+
zero_crossings = np.where(np.diff(np.sign(audio)))[0]
|
| 137 |
+
estimated_freq = len(zero_crossings) / (len(audio) / sample_rate) * 60 # BPM
|
| 138 |
+
tempo = np.clip(estimated_freq, 60, 200)
|
| 139 |
+
|
| 140 |
+
# Simple spectral centroid for pitch estimation
|
| 141 |
+
fft = np.fft.fft(audio)
|
| 142 |
+
magnitude = np.abs(fft[:len(fft)//2])
|
| 143 |
+
freqs = np.fft.fftfreq(len(fft), 1/sample_rate)[:len(fft)//2]
|
| 144 |
+
spectral_centroid = np.sum(freqs * magnitude) / (np.sum(magnitude) + 1e-10)
|
| 145 |
+
pitch_normalized = min(1.0, spectral_centroid / 2000)
|
| 146 |
+
|
| 147 |
+
return {
|
| 148 |
+
"tempo": tempo,
|
| 149 |
+
"energy": energy_normalized,
|
| 150 |
+
"pitch": pitch_normalized
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
@spaces.GPU(duration=180)
|
| 154 |
+
def generate_conversational_video(
|
| 155 |
+
audio_1: Tuple[int, np.ndarray],
|
| 156 |
+
prompt_1: str,
|
| 157 |
+
audio_2: Optional[Tuple[int, np.ndarray]] = None,
|
| 158 |
+
prompt_2: Optional[str] = None,
|
| 159 |
+
reference_image_1: Optional[np.ndarray] = None,
|
| 160 |
+
reference_image_2: Optional[np.ndarray] = None,
|
| 161 |
+
duration: int = 30,
|
| 162 |
+
mode: str = "single",
|
| 163 |
+
fps: int = 24,
|
| 164 |
+
progress=gr.Progress()
|
| 165 |
+
) -> str:
|
| 166 |
+
"""Generate conversational video from audio inputs"""
|
| 167 |
+
|
| 168 |
+
try:
|
| 169 |
+
progress(0.1, desc="Processing audio inputs...")
|
| 170 |
+
|
| 171 |
+
# Extract features from audio(s)
|
| 172 |
+
audio_features_1 = extract_audio_features(audio_1)
|
| 173 |
+
if audio_2 is not None:
|
| 174 |
+
audio_features_2 = extract_audio_features(audio_2)
|
| 175 |
+
|
| 176 |
+
progress(0.2, desc="Initializing video generation...")
|
| 177 |
+
|
| 178 |
+
# Generate video segments based on mode
|
| 179 |
+
if mode == "single":
|
| 180 |
+
progress(0.3, desc="Generating single-person video...")
|
| 181 |
+
frames = generator.generate_video_segment(
|
| 182 |
+
prompt=prompt_1,
|
| 183 |
+
reference_image=reference_image_1,
|
| 184 |
+
audio_features=audio_features_1,
|
| 185 |
+
duration=duration,
|
| 186 |
+
fps=fps
|
| 187 |
+
)
|
| 188 |
+
else: # multi-person conversation
|
| 189 |
+
progress(0.25, desc="Generating person 1 video...")
|
| 190 |
+
frames_1 = generator.generate_video_segment(
|
| 191 |
+
prompt=f"Person 1: {prompt_1}",
|
| 192 |
+
reference_image=reference_image_1,
|
| 193 |
+
audio_features=audio_features_1,
|
| 194 |
+
duration=duration//2,
|
| 195 |
+
fps=fps
|
| 196 |
+
)
|
| 197 |
+
|
| 198 |
+
progress(0.5, desc="Generating person 2 video...")
|
| 199 |
+
frames_2 = generator.generate_video_segment(
|
| 200 |
+
prompt=f"Person 2: {prompt_2 or 'Responding'}",
|
| 201 |
+
reference_image=reference_image_2,
|
| 202 |
+
audio_features=audio_features_2 or {"tempo": 120, "energy": 0.5, "pitch": 0.5},
|
| 203 |
+
duration=duration//2,
|
| 204 |
+
fps=fps
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
progress(0.7, desc="Combining conversation...")
|
| 208 |
+
# Interleave frames for conversation effect
|
| 209 |
+
frames = []
|
| 210 |
+
for i in range(min(len(frames_1), len(frames_2))):
|
| 211 |
+
frames.extend([frames_1[i], frames_2[i]])
|
| 212 |
+
|
| 213 |
+
progress(0.9, desc="Rendering video...")
|
| 214 |
+
|
| 215 |
+
# Create temporary file for video
|
| 216 |
+
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp_file:
|
| 217 |
+
video_path = tmp_file.name
|
| 218 |
+
|
| 219 |
+
# Export frames to video
|
| 220 |
+
export_to_video(frames, video_path, fps=fps)
|
| 221 |
+
|
| 222 |
+
progress(1.0, desc="Video generation complete!")
|
| 223 |
+
return video_path
|
| 224 |
+
|
| 225 |
+
except Exception as e:
|
| 226 |
+
raise gr.Error(f"Video generation failed: {str(e)}")
|
| 227 |
+
|
| 228 |
+
def create_reference_image_from_prompt(prompt: str, seed: int = 42) -> np.ndarray:
|
| 229 |
+
"""Create a reference image from text prompt"""
|
| 230 |
+
@spaces.GPU(duration=30)
|
| 231 |
+
def generate_image():
|
| 232 |
+
# Use a simple image generation for reference
|
| 233 |
+
from diffusers import StableDiffusionPipeline
|
| 234 |
+
|
| 235 |
+
img_pipe = StableDiffusionPipeline.from_pretrained(
|
| 236 |
+
"runwayml/stable-diffusion-v1-5",
|
| 237 |
+
torch_dtype=torch.float16
|
| 238 |
+
).to("cuda")
|
| 239 |
+
|
| 240 |
+
image = img_pipe(
|
| 241 |
+
prompt=f"portrait of {prompt}, photorealistic, neutral expression",
|
| 242 |
+
num_inference_steps=20,
|
| 243 |
+
guidance_scale=7.5,
|
| 244 |
+
generator=torch.Generator().manual_seed(seed)
|
| 245 |
+
).images[0]
|
| 246 |
+
|
| 247 |
+
return np.array(image)
|
| 248 |
+
|
| 249 |
+
return generate_image()
|
| 250 |
+
|
| 251 |
+
# Gradio Interface
|
| 252 |
+
with gr.Blocks(
|
| 253 |
+
title="Audio-Driven Conversational Video Generator",
|
| 254 |
+
description="Generate realistic conversational videos from audio inputs with up to 2 minutes duration",
|
| 255 |
+
theme=gr.themes.Soft(),
|
| 256 |
+
css="""
|
| 257 |
+
.header { text-align: center; margin-bottom: 2rem; }
|
| 258 |
+
.mode-toggle { margin: 1rem 0; }
|
| 259 |
+
.person-section { border: 1px solid #e0e0e0; border-radius: 8px; padding: 1rem; margin: 1rem 0; }
|
| 260 |
+
.warning { background-color: #fff3cd; border: 1px solid #ffeaa7; border-radius: 4px; padding: 0.75rem; margin: 0.5rem 0; }
|
| 261 |
+
.success { background-color: #d4edda; border: 1px solid #c3e6cb; border-radius: 4px; padding: 0.75rem; margin: 0.5rem 0; }
|
| 262 |
+
"""
|
| 263 |
+
) as demo:
|
| 264 |
+
|
| 265 |
+
gr.HTML("""
|
| 266 |
+
<div class="header">
|
| 267 |
+
<h1>π¬ Audio-Driven Conversational Video Generator</h1>
|
| 268 |
+
<p>Generate realistic talking videos from audio with support for single and multi-person conversations</p>
|
| 269 |
+
<p><strong>Built with anycoder</strong> - <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">Advanced AI Video Generation</a></p>
|
| 270 |
+
</div>
|
| 271 |
+
""")
|
| 272 |
+
|
| 273 |
+
with gr.Row():
|
| 274 |
+
mode = gr.Radio(
|
| 275 |
+
choices=["single", "multi-person"],
|
| 276 |
+
value="single",
|
| 277 |
+
label="Generation Mode",
|
| 278 |
+
info="Choose between single person or conversational video"
|
| 279 |
+
)
|
| 280 |
+
|
| 281 |
+
duration = gr.Slider(
|
| 282 |
+
minimum=5,
|
| 283 |
+
maximum=MAX_DURATION,
|
| 284 |
+
value=30,
|
| 285 |
+
step=5,
|
| 286 |
+
label="Duration (seconds)",
|
| 287 |
+
info="Video length up to 2 minutes"
|
| 288 |
+
)
|
| 289 |
+
|
| 290 |
+
fps = gr.Slider(
|
| 291 |
+
minimum=12,
|
| 292 |
+
maximum=30,
|
| 293 |
+
value=24,
|
| 294 |
+
step=1,
|
| 295 |
+
label="FPS",
|
| 296 |
+
info="Frames per second for output video"
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
# Person 1 inputs
|
| 300 |
+
with gr.Group(elem_classes="person-section"):
|
| 301 |
+
gr.Markdown("### π€ Person 1")
|
| 302 |
+
|
| 303 |
+
with gr.Row():
|
| 304 |
+
audio_1 = gr.Audio(
|
| 305 |
+
sources=["upload", "microphone"],
|
| 306 |
+
type="numpy",
|
| 307 |
+
label="Audio Input 1",
|
| 308 |
+
info="Upload audio file or record directly"
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
+
ref_img_1 = gr.Image(
|
| 312 |
+
sources=["upload"],
|
| 313 |
+
type="numpy",
|
| 314 |
+
label="Reference Image 1 (Optional)",
|
| 315 |
+
info="Upload a reference image for the first person"
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
prompt_1 = gr.Textbox(
|
| 319 |
+
label="Prompt for Person 1",
|
| 320 |
+
placeholder="Describe the first person (e.g., 'young woman, professional attire')",
|
| 321 |
+
value="friendly person speaking naturally"
|
| 322 |
+
)
|
| 323 |
+
|
| 324 |
+
with gr.Row():
|
| 325 |
+
generate_ref_1 = gr.Button("Generate Reference Image 1", size="sm")
|
| 326 |
+
use_placeholder_1 = gr.Button("Use Default Avatar 1", size="sm")
|
| 327 |
+
|
| 328 |
+
# Person 2 inputs (for multi-person mode)
|
| 329 |
+
with gr.Group(elem_classes="person-section", visible=False) as person_2_section:
|
| 330 |
+
gr.Markdown("### π₯ Person 2")
|
| 331 |
+
|
| 332 |
+
with gr.Row():
|
| 333 |
+
audio_2 = gr.Audio(
|
| 334 |
+
sources=["upload", "microphone"],
|
| 335 |
+
type="numpy",
|
| 336 |
+
label="Audio Input 2",
|
| 337 |
+
info="Upload or record second person's audio"
|
| 338 |
+
)
|
| 339 |
+
|
| 340 |
+
ref_img_2 = gr.Image(
|
| 341 |
+
sources=["upload"],
|
| 342 |
+
type="numpy",
|
| 343 |
+
label="Reference Image 2 (Optional)",
|
| 344 |
+
info="Upload a reference image for the second person"
|
| 345 |
+
)
|
| 346 |
+
|
| 347 |
+
prompt_2 = gr.Textbox(
|
| 348 |
+
label="Prompt for Person 2",
|
| 349 |
+
placeholder="Describe the second person",
|
| 350 |
+
value="friendly person responding"
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
with gr.Row():
|
| 354 |
+
generate_ref_2 = gr.Button("Generate Reference Image 2", size="sm")
|
| 355 |
+
use_placeholder_2 = gr.Button("Use Default Avatar 2", size="sm")
|
| 356 |
+
|
| 357 |
+
# Generation controls
|
| 358 |
+
with gr.Row():
|
| 359 |
+
generate_btn = gr.Button(
|
| 360 |
+
"π₯ Generate Video",
|
| 361 |
+
variant="primary",
|
| 362 |
+
size="lg"
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
stop_btn = gr.Button("βΉ Stop Generation", variant="stop", size="lg", visible=False)
|
| 366 |
+
|
| 367 |
+
# Output
|
| 368 |
+
video_output = gr.Video(
|
| 369 |
+
label="Generated Conversational Video",
|
| 370 |
+
autoplay=True,
|
| 371 |
+
show_label=True,
|
| 372 |
+
show_share_button=True,
|
| 373 |
+
show_download_button=True
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
# Status and info
|
| 377 |
+
status_info = gr.HTML(
|
| 378 |
+
value='<div class="info">π§ Model loading... This may take a few minutes for initial setup.</div>',
|
| 379 |
+
label="Status"
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
# Example gallery
|
| 383 |
+
gr.Examples(
|
| 384 |
+
examples=[
|
| 385 |
+
[
|
| 386 |
+
"single",
|
| 387 |
+
30,
|
| 388 |
+
24,
|
| 389 |
+
None, # Will use default audio
|
| 390 |
+
"professional presenter in business attire",
|
| 391 |
+
None,
|
| 392 |
+
None
|
| 393 |
+
],
|
| 394 |
+
[
|
| 395 |
+
"multi-person",
|
| 396 |
+
60,
|
| 397 |
+
24,
|
| 398 |
+
None,
|
| 399 |
+
"casual young woman",
|
| 400 |
+
None,
|
| 401 |
+
"casual young man"
|
| 402 |
+
]
|
| 403 |
+
],
|
| 404 |
+
inputs=[mode, duration, fps, audio_1, prompt_1, audio_2, prompt_2],
|
| 405 |
+
cache_examples=False
|
| 406 |
+
)
|
| 407 |
+
|
| 408 |
+
# Event handlers
|
| 409 |
+
def toggle_mode(selected_mode):
|
| 410 |
+
"""Show/hide person 2 section based on mode"""
|
| 411 |
+
if selected_mode == "multi-person":
|
| 412 |
+
return gr.update(visible=True), gr.update(value="π₯ Generate Conversation")
|
| 413 |
+
else:
|
| 414 |
+
return gr.update(visible=False), gr.update(value="π₯ Generate Video")
|
| 415 |
+
|
| 416 |
+
mode.change(
|
| 417 |
+
toggle_mode,
|
| 418 |
+
inputs=[mode],
|
| 419 |
+
outputs=[person_2_section, generate_btn]
|
| 420 |
+
)
|
| 421 |
+
|
| 422 |
+
# Generate reference images
|
| 423 |
+
generate_ref_1.click(
|
| 424 |
+
create_reference_image_from_prompt,
|
| 425 |
+
inputs=[prompt_1],
|
| 426 |
+
outputs=[ref_img_1]
|
| 427 |
+
).then(
|
| 428 |
+
lambda: gr.update(value='<div class="success">β
Reference image generated for Person 1</div>'),
|
| 429 |
+
outputs=[status_info]
|
| 430 |
+
)
|
| 431 |
+
|
| 432 |
+
generate_ref_2.click(
|
| 433 |
+
create_reference_image_from_prompt,
|
| 434 |
+
inputs=[prompt_2],
|
| 435 |
+
outputs=[ref_img_2]
|
| 436 |
+
).then(
|
| 437 |
+
lambda: gr.update(value='<div class="success">β
Reference image generated for Person 2</div>'),
|
| 438 |
+
outputs=[status_info]
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
# Use default avatars
|
| 442 |
+
def create_default_avatar(person_id: int):
|
| 443 |
+
"""Create a simple default avatar"""
|
| 444 |
+
color_map = {1: "#FFE4E1", 2: "#E1F4FF"}
|
| 445 |
+
avatar = Image.new('RGB', (256, 256), color=color_map.get(person_id, "#FFFFFF"))
|
| 446 |
+
|
| 447 |
+
# Add simple face features
|
| 448 |
+
from PIL import ImageDraw
|
| 449 |
+
draw = ImageDraw.Draw(avatar)
|
| 450 |
+
|
| 451 |
+
# Simple face outline
|
| 452 |
+
draw.ellipse([50, 50, 206, 206], outline="#000000", width=3)
|
| 453 |
+
# Eyes
|
| 454 |
+
draw.ellipse([80, 90, 110, 120], fill="#000000")
|
| 455 |
+
draw.ellipse([146, 90, 176, 120], fill="#000000")
|
| 456 |
+
# Smile
|
| 457 |
+
draw.arc([100, 130, 156, 160], 0, 180, fill="#000000", width=2)
|
| 458 |
+
|
| 459 |
+
return np.array(avatar)
|
| 460 |
+
|
| 461 |
+
use_placeholder_1.click(
|
| 462 |
+
lambda: create_default_avatar(1),
|
| 463 |
+
outputs=[ref_img_1]
|
| 464 |
+
)
|
| 465 |
+
|
| 466 |
+
use_placeholder_2.click(
|
| 467 |
+
lambda: create_default_avatar(2),
|
| 468 |
+
outputs=[ref_img_2]
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
# Main generation function
|
| 472 |
+
def start_generation(*args):
|
| 473 |
+
"""Start video generation with loading indicator"""
|
| 474 |
+
return (
|
| 475 |
+
gr.update(visible=False), # Hide generate button
|
| 476 |
+
gr.update(visible=True), # Show stop button
|
| 477 |
+
gr.update(value='<div class="warning">βοΈ Generating video... This may take several minutes depending on duration.</div>'),
|
| 478 |
+
None # Clear previous video
|
| 479 |
+
)
|
| 480 |
+
|
| 481 |
+
def stop_generation():
|
| 482 |
+
"""Handle stop generation"""
|
| 483 |
+
return (
|
| 484 |
+
gr.update(visible=True), # Show generate button
|
| 485 |
+
gr.update(visible=False), # Hide stop button
|
| 486 |
+
gr.update(value='<div class="info">π§ Generation stopped. Ready for new video.</div>')
|
| 487 |
+
)
|
| 488 |
+
|
| 489 |
+
generate_btn.click(
|
| 490 |
+
start_generation,
|
| 491 |
+
outputs=[generate_btn, stop_btn, status_info, video_output]
|
| 492 |
+
).then(
|
| 493 |
+
generate_conversational_video,
|
| 494 |
+
inputs=[
|
| 495 |
+
audio_1, prompt_1, audio_2, prompt_2,
|
| 496 |
+
ref_img_1, ref_img_2, duration, mode, fps
|
| 497 |
+
],
|
| 498 |
+
outputs=[video_output]
|
| 499 |
+
).then(
|
| 500 |
+
lambda: gr.update(value='<div class="success">β
Video generation complete! You can now download your video.</div>'),
|
| 501 |
+
outputs=[status_info]
|
| 502 |
+
).then(
|
| 503 |
+
lambda: [gr.update(visible=True), gr.update(visible=False)],
|
| 504 |
+
outputs=[generate_btn, stop_btn]
|
| 505 |
+
)
|
| 506 |
+
|
| 507 |
+
stop_btn.click(
|
| 508 |
+
stop_generation,
|
| 509 |
+
outputs=[generate_btn, stop_btn, status_info]
|
| 510 |
+
)
|
| 511 |
+
|
| 512 |
+
# Update status on model load
|
| 513 |
+
demo.load(
|
| 514 |
+
lambda: gr.update(value='<div class="success">β
Model loaded successfully! Ready to generate videos.</div>'),
|
| 515 |
+
outputs=[status_info]
|
| 516 |
+
)
|
| 517 |
+
|
| 518 |
+
if __name__ == "__main__":
|
| 519 |
+
demo.queue(max_size=10, default_concurrency_limit=1)
|
| 520 |
+
demo.launch(share=True)
|
| 521 |
+
```
|
| 522 |
+
|
| 523 |
+
```python
|
| 524 |
+
# requirements.txt
|
| 525 |
+
gradio
|
| 526 |
+
torch
|
| 527 |
+
diffusers
|
| 528 |
+
transformers
|
| 529 |
+
accelerate
|
| 530 |
+
numpy
|
| 531 |
+
pillow
|
| 532 |
+
opencv-python
|
| 533 |
+
spaces
|
| 534 |
+
torchvision
|
| 535 |
+
```
|