Spaces:
Paused
Paused
File size: 11,659 Bytes
c8970fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
import gradio as gr
import subprocess
import os
import tempfile
import shutil
from pathlib import Path
import torch
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Constants
DEFAULT_CONFIG_PATH = "configs/inference.yaml"
DEFAULT_INPUT_FILE = "examples/infer_samples.txt"
OUTPUT_DIR = Path("demo_out/gradio_outputs")
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
def generate_avatar_video(
reference_image,
audio_file,
text_prompt,
seed=42,
num_steps=50,
guidance_scale=4.5,
audio_scale=None,
overlap_frames=13,
fps=25,
silence_duration=0.3,
resolution="720p",
progress=gr.Progress()
):
"""Generate an avatar video using OmniAvatar
Args:
reference_image: Path to reference avatar image
audio_file: Path to audio file for lip sync
text_prompt: Text description of the video to generate
seed: Random seed for generation
num_steps: Number of inference steps
guidance_scale: Classifier-free guidance scale
audio_scale: Audio guidance scale (uses guidance_scale if None)
overlap_frames: Number of overlapping frames between chunks
fps: Frames per second
silence_duration: Duration of silence to add before/after audio
resolution: Output resolution ("480p" or "720p")
progress: Gradio progress callback
Returns:
str: Path to generated video file
"""
try:
progress(0.1, desc="Preparing inputs")
# Create temporary directory for this generation
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = Path(temp_dir)
# Copy input files to temp directory
temp_image = temp_path / "input_image.jpeg"
temp_audio = temp_path / "input_audio.mp3"
shutil.copy(reference_image, temp_image)
shutil.copy(audio_file, temp_audio)
# Create input file for inference script
input_file = temp_path / "input.txt"
# Format: prompt@@image_path@@audio_path
with open(input_file, 'w') as f:
f.write(f"{text_prompt}@@{temp_image}@@{temp_audio}\n")
progress(0.2, desc="Configuring generation parameters")
# Determine max_hw based on resolution
max_hw = 720 if resolution == "480p" else 1280
# Build command to run inference script
cmd = [
"torchrun",
"--nproc_per_node=1",
"scripts/inference.py",
"--config", DEFAULT_CONFIG_PATH,
"--input_file", str(input_file),
"-hp", f"seed={seed},num_steps={num_steps},guidance_scale={guidance_scale},"
f"overlap_frame={overlap_frames},fps={fps},silence_duration_s={silence_duration},"
f"max_hw={max_hw},use_audio=True,i2v=True"
]
# Add audio scale if specified
if audio_scale is not None:
cmd[-1] += f",audio_scale={audio_scale}"
progress(0.3, desc="Running OmniAvatar generation")
logger.info(f"Running command: {' '.join(cmd)}")
# Run the inference script
env = os.environ.copy()
env['CUDA_VISIBLE_DEVICES'] = '0' # Use first GPU
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
env=env
)
# Monitor progress (simplified - in reality you'd parse the output)
stdout_lines = []
stderr_lines = []
while True:
output = process.stdout.readline()
if output:
stdout_lines.append(output.strip())
logger.info(output.strip())
# Update progress based on output
if "Starting video generation" in output:
progress(0.5, desc="Generating video frames")
elif "[1/" in output: # First chunk
progress(0.6, desc="Processing video chunks")
elif "Saving video" in output:
progress(0.9, desc="Finalizing video")
if process.poll() is not None:
break
# Get any remaining output
remaining_stdout, remaining_stderr = process.communicate()
if remaining_stdout:
stdout_lines.extend(remaining_stdout.strip().split('\n'))
if remaining_stderr:
stderr_lines.extend(remaining_stderr.strip().split('\n'))
if process.returncode != 0:
error_msg = '\n'.join(stderr_lines)
logger.error(f"Inference failed with return code {process.returncode}")
logger.error(f"Error output: {error_msg}")
raise gr.Error(f"Video generation failed: {error_msg}")
progress(0.95, desc="Retrieving generated video")
# Find the generated video file
# The inference script saves to demo_out/{exp_name}/res_{input_file_name}_...
# We need to find the most recent video file
generated_videos = list(Path("demo_out").rglob("result_000.mp4"))
if not generated_videos:
raise gr.Error("No video file was generated")
# Get the most recent video
latest_video = max(generated_videos, key=lambda p: p.stat().st_mtime)
# Copy to output directory with unique name
output_filename = f"avatar_video_{os.getpid()}_{torch.randint(1000, 9999, (1,)).item()}.mp4"
output_path = OUTPUT_DIR / output_filename
shutil.copy(latest_video, output_path)
progress(1.0, desc="Generation complete")
logger.info(f"Video saved to: {output_path}")
return str(output_path)
except Exception as e:
logger.error(f"Error generating video: {str(e)}")
raise gr.Error(f"Error generating video: {str(e)}")
# Create the Gradio interface
with gr.Blocks(title="OmniAvatar - Lipsynced Avatar Video Generation") as app:
gr.Markdown("""
# π OmniAvatar - Lipsynced Avatar Video Generation
Generate videos with lipsynced avatars using a reference image and audio file.
Based on Wan2.1 with OmniAvatar enhancements for audio-driven avatar animation.
""")
with gr.Row():
with gr.Column(scale=1):
# Input components
reference_image = gr.Image(
label="Reference Avatar Image",
type="filepath",
elem_id="reference_image"
)
audio_file = gr.Audio(
label="Speech Audio File",
type="filepath",
elem_id="audio_file"
)
text_prompt = gr.Textbox(
label="Video Description",
placeholder="Describe the video scene and actions...",
lines=3,
value="A person speaking naturally with subtle facial expressions"
)
with gr.Accordion("Advanced Settings", open=False):
with gr.Row():
seed = gr.Slider(
label="Seed",
minimum=0,
maximum=2147483647,
step=1,
value=42
)
resolution = gr.Radio(
label="Resolution",
choices=["480p", "720p"],
value="720p"
)
with gr.Row():
num_steps = gr.Slider(
label="Inference Steps",
minimum=10,
maximum=100,
step=5,
value=50
)
guidance_scale = gr.Slider(
label="Guidance Scale",
minimum=1.0,
maximum=10.0,
step=0.5,
value=4.5
)
with gr.Row():
audio_scale = gr.Slider(
label="Audio Scale (leave 0 to use guidance scale)",
minimum=0.0,
maximum=10.0,
step=0.5,
value=0.0
)
overlap_frames = gr.Slider(
label="Overlap Frames",
minimum=1,
maximum=25,
step=4,
value=13,
info="Must be 1 + 4*n"
)
with gr.Row():
fps = gr.Slider(
label="FPS",
minimum=10,
maximum=30,
step=1,
value=25
)
silence_duration = gr.Slider(
label="Silence Duration (s)",
minimum=0.0,
maximum=2.0,
step=0.1,
value=0.3
)
generate_btn = gr.Button(
"π¬ Generate Avatar Video",
variant="primary"
)
with gr.Column(scale=1):
# Output component
output_video = gr.Video(
label="Generated Avatar Video",
elem_id="output_video"
)
# Examples
gr.Examples(
examples=[
[
"examples/images/0000.jpeg",
"examples/audios/0000.MP3",
"A professional woman giving a presentation with confident gestures"
],
],
inputs=[reference_image, audio_file, text_prompt],
label="Example Inputs"
)
# Connect the generate button
generate_btn.click(
fn=generate_avatar_video,
inputs=[
reference_image,
audio_file,
text_prompt,
seed,
num_steps,
guidance_scale,
audio_scale,
overlap_frames,
fps,
silence_duration,
resolution
],
outputs=output_video
)
gr.Markdown("""
## π Notes
- The reference image should be a clear frontal view of the person
- Audio should be clear speech without background music
- Generation may take several minutes depending on video length
- For best results, use high-quality input images and audio
""")
# Launch the app
if __name__ == "__main__":
app.launch(share=True) |