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