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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# PyTorch 2.8 (temporary hack)
|
| 2 |
+
import os
|
| 3 |
+
os.system('pip install --upgrade --pre --extra-index-url https://download.pytorch.org/whl/nightly/cu126 "torch<2.9" spaces')
|
| 4 |
+
|
| 5 |
+
# Actual demo code
|
| 6 |
+
import spaces
|
| 7 |
+
import torch
|
| 8 |
+
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
|
| 9 |
+
from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
|
| 10 |
+
from diffusers.utils.export_utils import export_to_video
|
| 11 |
+
import gradio as gr
|
| 12 |
+
import tempfile
|
| 13 |
+
import numpy as np
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import random
|
| 16 |
+
|
| 17 |
+
from optimization import optimize_pipeline_
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
MODEL_ID = "Wan-AI/Wan2.2-T2V-A14B-Diffusers"
|
| 21 |
+
|
| 22 |
+
LANDSCAPE_WIDTH = 832
|
| 23 |
+
LANDSCAPE_HEIGHT = 480
|
| 24 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 25 |
+
|
| 26 |
+
FIXED_FPS = 24
|
| 27 |
+
MIN_FRAMES_MODEL = 8
|
| 28 |
+
MAX_FRAMES_MODEL = 81
|
| 29 |
+
|
| 30 |
+
MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
|
| 31 |
+
MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
|
| 35 |
+
transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
|
| 36 |
+
subfolder='transformer',
|
| 37 |
+
torch_dtype=torch.bfloat16,
|
| 38 |
+
device_map='cuda',
|
| 39 |
+
),
|
| 40 |
+
transformer_2=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
|
| 41 |
+
subfolder='transformer_2',
|
| 42 |
+
torch_dtype=torch.bfloat16,
|
| 43 |
+
device_map='cuda',
|
| 44 |
+
),
|
| 45 |
+
torch_dtype=torch.bfloat16,
|
| 46 |
+
).to('cuda')
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
optimize_pipeline_(pipe,
|
| 50 |
+
image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
|
| 51 |
+
prompt='prompt',
|
| 52 |
+
height=LANDSCAPE_HEIGHT,
|
| 53 |
+
width=LANDSCAPE_WIDTH,
|
| 54 |
+
num_frames=MAX_FRAMES_MODEL,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
|
| 59 |
+
default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def resize_image(image: Image.Image) -> Image.Image:
|
| 63 |
+
if image.height > image.width:
|
| 64 |
+
transposed = image.transpose(Image.Transpose.ROTATE_90)
|
| 65 |
+
resized = resize_image_landscape(transposed)
|
| 66 |
+
return resized.transpose(Image.Transpose.ROTATE_270)
|
| 67 |
+
return resize_image_landscape(image)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def resize_image_landscape(image: Image.Image) -> Image.Image:
|
| 71 |
+
target_aspect = LANDSCAPE_WIDTH / LANDSCAPE_HEIGHT
|
| 72 |
+
width, height = image.size
|
| 73 |
+
in_aspect = width / height
|
| 74 |
+
if in_aspect > target_aspect:
|
| 75 |
+
new_width = round(height * target_aspect)
|
| 76 |
+
left = (width - new_width) // 2
|
| 77 |
+
image = image.crop((left, 0, left + new_width, height))
|
| 78 |
+
else:
|
| 79 |
+
new_height = round(width / target_aspect)
|
| 80 |
+
top = (height - new_height) // 2
|
| 81 |
+
image = image.crop((0, top, width, top + new_height))
|
| 82 |
+
return image.resize((LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT), Image.LANCZOS)
|
| 83 |
+
|
| 84 |
+
def get_duration(
|
| 85 |
+
input_image,
|
| 86 |
+
prompt,
|
| 87 |
+
negative_prompt,
|
| 88 |
+
duration_seconds,
|
| 89 |
+
guidance_scale,
|
| 90 |
+
steps,
|
| 91 |
+
seed,
|
| 92 |
+
randomize_seed,
|
| 93 |
+
progress,
|
| 94 |
+
):
|
| 95 |
+
return steps * 15
|
| 96 |
+
|
| 97 |
+
@spaces.GPU(duration=get_duration)
|
| 98 |
+
def generate_video(
|
| 99 |
+
input_image,
|
| 100 |
+
prompt,
|
| 101 |
+
negative_prompt=default_negative_prompt,
|
| 102 |
+
duration_seconds = MAX_DURATION,
|
| 103 |
+
guidance_scale = 1,
|
| 104 |
+
steps = 4,
|
| 105 |
+
seed = 42,
|
| 106 |
+
randomize_seed = False,
|
| 107 |
+
progress=gr.Progress(track_tqdm=True),
|
| 108 |
+
):
|
| 109 |
+
|
| 110 |
+
if input_image is None:
|
| 111 |
+
raise gr.Error("Please upload an input image.")
|
| 112 |
+
|
| 113 |
+
num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
| 114 |
+
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 115 |
+
resized_image = resize_image(input_image)
|
| 116 |
+
|
| 117 |
+
output_frames_list = pipe(
|
| 118 |
+
image=None,
|
| 119 |
+
prompt=prompt,
|
| 120 |
+
negative_prompt=negative_prompt,
|
| 121 |
+
height=resized_image.height,
|
| 122 |
+
width=resized_image.width,
|
| 123 |
+
num_frames=num_frames,
|
| 124 |
+
guidance_scale=float(guidance_scale),
|
| 125 |
+
num_inference_steps=int(steps),
|
| 126 |
+
generator=torch.Generator(device="cuda").manual_seed(current_seed),
|
| 127 |
+
).frames[0]
|
| 128 |
+
|
| 129 |
+
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 130 |
+
video_path = tmpfile.name
|
| 131 |
+
|
| 132 |
+
export_to_video(output_frames_list, video_path, fps=FIXED_FPS)
|
| 133 |
+
|
| 134 |
+
return video_path, current_seed
|
| 135 |
+
|
| 136 |
+
with gr.Blocks() as demo:
|
| 137 |
+
gr.Markdown("# Wan2.2-T2V-A14B AND I2V Testing")
|
| 138 |
+
#gr.Markdown("[CausVid](https://github.com/tianweiy/CausVid) is a distilled version of Wan 2.1 to run faster in just 4-8 steps, [extracted as LoRA by Kijai](https://huggingface.co/Kijai/WanVideo_comfy/blob/main/Wan21_CausVid_14B_T2V_lora_rank32.safetensors) and is compatible with 🧨 diffusers")
|
| 139 |
+
with gr.Row():
|
| 140 |
+
with gr.Column():
|
| 141 |
+
input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)")
|
| 142 |
+
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 143 |
+
duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=MAX_DURATION, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
|
| 144 |
+
|
| 145 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 146 |
+
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 147 |
+
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 148 |
+
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 149 |
+
steps_slider = gr.Slider(minimum=1, maximum=40, step=1, value=35, label="Inference Steps")
|
| 150 |
+
guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
|
| 151 |
+
|
| 152 |
+
generate_button = gr.Button("Generate Video", variant="primary")
|
| 153 |
+
with gr.Column():
|
| 154 |
+
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 155 |
+
|
| 156 |
+
ui_inputs = [
|
| 157 |
+
input_image_component, prompt_input,
|
| 158 |
+
negative_prompt_input, duration_seconds_input,
|
| 159 |
+
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 160 |
+
]
|
| 161 |
+
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
| 162 |
+
|
| 163 |
+
gr.Examples(
|
| 164 |
+
examples=[
|
| 165 |
+
[
|
| 166 |
+
"wan_i2v_input.JPG",
|
| 167 |
+
"Summer beach vacation style, a white cat wearing sunglasses sits on a surfboard. The fluffy-furred feline gazes directly at the camera with a relaxed expression. Blurred beach scenery forms the background featuring crystal-clear waters, distant green hills, and a blue sky dotted with white clouds. The cat assumes a naturally relaxed posture, as if savoring the sea breeze and warm sunlight. A close-up shot highlights the feline's intricate details and the refreshing atmosphere of the seaside.",
|
| 168 |
+
],
|
| 169 |
+
],
|
| 170 |
+
inputs=[input_image_component, prompt_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
if __name__ == "__main__":
|
| 174 |
+
demo.queue().launch(mcp_server=True)
|