Spaces:
Runtime error
Runtime error
Update app_t2v.py
Browse files- app_t2v.py +55 -160
app_t2v.py
CHANGED
|
@@ -1,174 +1,69 @@
|
|
| 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
|
| 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 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
height=LANDSCAPE_HEIGHT,
|
| 53 |
-
width=LANDSCAPE_WIDTH,
|
| 54 |
-
num_frames=MAX_FRAMES_MODEL,
|
| 55 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 141 |
-
|
| 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 |
-
|
| 153 |
-
|
| 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 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 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 |
-
|
| 174 |
-
demo.queue().launch(mcp_server=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from diffusers import WanPipeline, AutoencoderKLWan
|
| 4 |
+
from diffusers.utils import export_to_video
|
| 5 |
import tempfile
|
| 6 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Setup
|
| 9 |
+
dtype = torch.bfloat16
|
| 10 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
|
| 12 |
+
# Load model and VAE once
|
| 13 |
+
vae = AutoencoderKLWan.from_pretrained(
|
| 14 |
+
"Wan-AI/Wan2.2-T2V-A14B-Diffusers", subfolder="vae", torch_dtype=torch.float32
|
|
|
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
+
pipe = WanPipeline.from_pretrained(
|
| 17 |
+
"Wan-AI/Wan2.2-T2V-A14B-Diffusers", vae=vae, torch_dtype=dtype
|
| 18 |
+
)
|
| 19 |
+
pipe.to(device)
|
| 20 |
+
|
| 21 |
+
# Core inference function
|
| 22 |
+
def generate_video(prompt, negative_prompt, height, width, num_frames, guidance_scale, guidance_scale_2, num_steps):
|
| 23 |
+
with torch.autocast("cuda", dtype=dtype):
|
| 24 |
+
output = pipe(
|
| 25 |
+
prompt=prompt,
|
| 26 |
+
negative_prompt=negative_prompt,
|
| 27 |
+
height=height,
|
| 28 |
+
width=width,
|
| 29 |
+
num_frames=num_frames,
|
| 30 |
+
guidance_scale=guidance_scale,
|
| 31 |
+
guidance_scale_2=guidance_scale_2,
|
| 32 |
+
num_inference_steps=num_steps,
|
| 33 |
+
).frames[0]
|
| 34 |
+
|
| 35 |
+
temp_dir = tempfile.mkdtemp()
|
| 36 |
+
video_path = os.path.join(temp_dir, "output.mp4")
|
| 37 |
+
export_to_video(output, video_path, fps=16)
|
| 38 |
+
return video_path
|
| 39 |
+
|
| 40 |
+
# Gradio UI
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("## 🐾 Wan2.2 T2V Demo – Gradio Edition")
|
| 43 |
|
| 44 |
+
with gr.Row():
|
| 45 |
+
prompt = gr.Textbox(label="Prompt", value="Two anthropomorphic cats in comfy boxing gear fight intensely.")
|
| 46 |
+
negative_prompt = gr.Textbox(label="Negative Prompt", value="色调艳丽,过曝,静态,细节模糊不清,字幕,最差质量,丑陋的,多余的手指,畸形")
|
| 47 |
|
| 48 |
+
with gr.Row():
|
| 49 |
+
height = gr.Slider(360, 1024, value=720, step=16, label="Height")
|
| 50 |
+
width = gr.Slider(360, 1920, value=1280, step=16, label="Width")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
with gr.Row():
|
| 53 |
+
num_frames = gr.Slider(16, 100, value=81, step=1, label="Number of Frames")
|
| 54 |
+
num_steps = gr.Slider(10, 60, value=40, step=1, label="Inference Steps")
|
| 55 |
|
|
|
|
|
|
|
|
|
|
| 56 |
with gr.Row():
|
| 57 |
+
guidance_scale = gr.Slider(1.0, 10.0, value=4.0, step=0.5, label="Guidance Scale")
|
| 58 |
+
guidance_scale_2 = gr.Slider(1.0, 10.0, value=3.0, step=0.5, label="Guidance Scale 2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
generate_btn = gr.Button("Generate Video")
|
| 61 |
+
video_output = gr.Video(label="Generated Video")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
generate_btn.click(
|
| 64 |
+
fn=generate_video,
|
| 65 |
+
inputs=[prompt, negative_prompt, height, width, num_frames, guidance_scale, guidance_scale_2, num_steps],
|
| 66 |
+
outputs=video_output,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
)
|
| 68 |
|
| 69 |
+
demo.launch()
|
|
|