File size: 9,099 Bytes
0e9a299
 
4bcda9c
0e9a299
afbf058
7da68af
0e9a299
 
 
 
 
 
 
 
 
 
 
 
 
afbf058
 
0e9a299
 
 
 
 
 
 
 
afbf058
 
 
 
0e9a299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afbf058
0e9a299
 
afbf058
0e9a299
afbf058
0e9a299
afbf058
0e9a299
afbf058
 
 
 
 
 
 
 
0e9a299
afbf058
0e9a299
afbf058
0e9a299
 
 
 
 
 
afbf058
0e9a299
 
 
 
 
 
 
 
 
 
 
 
afbf058
 
 
0e9a299
 
afbf058
 
 
 
 
 
 
 
 
 
 
 
 
 
0e9a299
 
 
 
 
afbf058
0e9a299
 
afbf058
0e9a299
 
 
 
 
afbf058
 
 
 
0e9a299
afbf058
 
 
0e9a299
afbf058
 
 
 
0e9a299
afbf058
 
 
0e9a299
 
afbf058
0e9a299
afbf058
 
 
 
0e9a299
 
 
 
 
 
afbf058
0e9a299
 
afbf058
 
 
 
0e9a299
 
 
afbf058
 
 
0e9a299
 
 
 
 
 
 
 
 
 
 
afbf058
 
 
 
 
 
0e9a299
 
 
 
afbf058
0e9a299
 
afbf058
 
 
 
 
0e9a299
 
afbf058
0e9a299
 
 
 
 
 
 
 
afbf058
 
 
 
0e9a299
 
 
 
 
 
 
afbf058
0e9a299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afbf058
0e9a299
afbf058
0e9a299
 
 
 
 
 
 
 
 
 
afbf058
0e9a299
 
 
 
 
 
afbf058
 
 
0e9a299
 
 
 
 
afbf058
0e9a299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afbf058
0e9a299
 
 
 
afbf058
0e9a299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afbf058
0e9a299
 
 
 
afbf058
0e9a299
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bcda9c
afbf058
 
 
 
4bcda9c
afbf058
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import os
import gc
import torch
import gradio as gr
import spaces
import ftfy
from diffusers import WanAnimatePipeline
from diffusers.utils import load_image, load_video, export_to_video


# ============================================================
# CONFIG
# ============================================================

MODEL_ID = "Wan-AI/Wan2.2-Animate-14B-Diffusers"

OUTPUT_DIR = "/tmp/outputs"
os.makedirs(OUTPUT_DIR, exist_ok=True)

pipe = None


# ============================================================
# PRESETS
# ============================================================

PRESETS = {
    "Realistic": {
        "prompt": (
            "A highly realistic video of the reference character "
            "performing the movements from the input motion video. "
            "Preserve identity, facial appearance, hairstyle and clothing. "
            "Natural body motion, realistic lighting and physics."
        ),
        "steps": 20,
        "guidance": 1.0,
    },

    "Cinematic": {
        "prompt": (
            "A cinematic photorealistic video of the character performing "
            "the exact movements and actions from the reference motion video. "
            "Natural facial expressions, realistic skin, detailed clothing, "
            "cinematic lighting, shallow depth of field, professional camera."
        ),
        "steps": 20,
        "guidance": 1.0,
    },

    "Portrait": {
        "prompt": (
            "A photorealistic portrait video of the reference character. "
            "Preserve the character's identity and facial features while "
            "accurately following the body and facial motion from the input video. "
            "Natural expression, realistic skin and cinematic portrait lighting."
        ),
        "steps": 20,
        "guidance": 1.0,
    },

    "Anime": {
        "prompt": (
            "An anime-style cinematic video featuring the reference character, "
            "accurately reproducing the movement and performance from the input "
            "motion video. Consistent character identity, expressive animation, "
            "detailed anime background."
        ),
        "steps": 20,
        "guidance": 1.0,
    },
}


# ============================================================
# MODEL LOADING
# ============================================================

def get_pipeline():

    global pipe

    if pipe is None:

        print("Loading Wan2.2 Animate...")

        pipe = WanAnimatePipeline.from_pretrained(
            MODEL_ID,
            torch_dtype=torch.bfloat16,
        )

        pipe.enable_model_cpu_offload()

        print("Wan2.2 Animate loaded.")

    return pipe


# ============================================================
# GENERATION
# ============================================================

@spaces.GPU
def generate_video(
    prompt,
    reference_image,
    motion_video,
    preset,
    seed,
    inference_steps,
    guidance_scale,
    progress=gr.Progress(),
):

    if reference_image is None:
        raise gr.Error(
            "Please upload a reference image."
        )

    if motion_video is None:
        raise gr.Error(
            "Please upload a motion video."
        )

    # --------------------------------------------------------
    # LOAD MODEL AFTER ZERO GPU ALLOCATION
    # --------------------------------------------------------

    progress(
        0.05,
        desc="Loading Wan2.2 Animate..."
    )

    pipe = get_pipeline()

    # --------------------------------------------------------
    # PRESET
    # --------------------------------------------------------

    config = PRESETS[preset]

    if not prompt or not prompt.strip():
        prompt = config["prompt"]

    # --------------------------------------------------------
    # LOAD INPUTS
    # --------------------------------------------------------

    progress(
        0.15,
        desc="Loading reference image..."
    )

    image = load_image(
        reference_image
    )

    progress(
        0.25,
        desc="Loading motion video..."
    )

    motion = load_video(
        motion_video
    )

    # --------------------------------------------------------
    # TEMPORARY CONDITIONING
    #
    # NOTE:
    # For the real Wan2.2 Animate workflow, the motion video
    # should be processed into dedicated pose/face inputs.
    # This is the basic prototype.
    # --------------------------------------------------------

    pose_video = motion
    face_video = motion

    # --------------------------------------------------------
    # GENERATE
    # --------------------------------------------------------

    progress(
        0.35,
        desc="Generating video..."
    )

    generator = torch.Generator(
        device="cuda"
    ).manual_seed(
        int(seed)
    )

    with torch.inference_mode():

        result = pipe(
            image=image,
            pose_video=pose_video,
            face_video=face_video,
            prompt=prompt,
            mode="animate",
            segment_frame_length=77,
            prev_segment_conditioning_frames=1,
            guidance_scale=float(
                guidance_scale
            ),
            num_inference_steps=int(
                inference_steps
            ),
            generator=generator,
        ).frames[0]

    # --------------------------------------------------------
    # EXPORT
    # --------------------------------------------------------

    progress(
        0.9,
        desc="Encoding video..."
    )

    output_path = os.path.join(
        OUTPUT_DIR,
        f"output_{int(seed)}.mp4"
    )

    export_to_video(
        result,
        output_path,
        fps=30,
    )

    progress(
        1.0,
        desc="Done"
    )

    gc.collect()

    return output_path


# ============================================================
# PRESET UPDATE
# ============================================================

def update_preset(preset):

    config = PRESETS[preset]

    return (
        config["prompt"],
        config["steps"],
        config["guidance"],
    )


# ============================================================
# GRADIO UI
# ============================================================

with gr.Blocks(
    title="Wan2.2 Character Animation"
) as demo:

    gr.Markdown(
        """
        # 🎬 Wan2.2 Character Animation

        **Reference Photo + Motion Video + Prompt → Video**

        The reference photo provides the character identity.
        The motion video provides the movement.
        """
    )

    with gr.Row():

        with gr.Column():

            prompt = gr.Textbox(
                label="Prompt",
                lines=5,
                placeholder=(
                    "Describe the generated video..."
                ),
            )

            preset = gr.Dropdown(
                choices=list(
                    PRESETS.keys()
                ),
                value="Realistic",
                label="Style Preset",
            )

            reference_image = gr.Image(
                label="Reference Photo",
                type="filepath",
            )

            motion_video = gr.Video(
                label="Motion Video",
                sources=["upload"],
            )

        with gr.Column():

            output_video = gr.Video(
                label="Generated Video",
                autoplay=True,
            )

            generate_button = gr.Button(
                "🎬 Generate",
                variant="primary",
            )

    # ========================================================
    # ADVANCED
    # ========================================================

    with gr.Accordion(
        "Advanced Settings",
        open=False,
    ):

        seed = gr.Number(
            label="Seed",
            value=42,
            precision=0,
        )

        inference_steps = gr.Slider(
            minimum=5,
            maximum=50,
            value=20,
            step=1,
            label="Inference Steps",
        )

        guidance_scale = gr.Slider(
            minimum=0.5,
            maximum=5.0,
            value=1.0,
            step=0.1,
            label="Guidance Scale",
        )

    # ========================================================
    # EVENTS
    # ========================================================

    preset.change(
        fn=update_preset,
        inputs=preset,
        outputs=[
            prompt,
            inference_steps,
            guidance_scale,
        ],
    )

    generate_button.click(
        fn=generate_video,
        inputs=[
            prompt,
            reference_image,
            motion_video,
            preset,
            seed,
            inference_steps,
            guidance_scale,
        ],
        outputs=output_video,
    )


# ============================================================
# LAUNCH
# ============================================================

demo.queue(
    max_size=10,
    default_concurrency_limit=1,
)

demo.launch()