rahul7star commited on
Commit
89f6ece
·
verified ·
1 Parent(s): 1fcf9ff

Create app_I2V.py

Browse files
Files changed (1) hide show
  1. app_I2V.py +174 -0
app_I2V.py ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-I2V-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 = 13
28
+ MAX_FRAMES_MODEL = 121
29
+ NUM_FRAMES_DEFAULT = 81
30
+
31
+
32
+ pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
33
+ transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
34
+ subfolder='transformer',
35
+ torch_dtype=torch.bfloat16,
36
+ device_map='cuda',
37
+ ),
38
+ transformer_2=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
39
+ subfolder='transformer_2',
40
+ torch_dtype=torch.bfloat16,
41
+ device_map='cuda',
42
+ ),
43
+ torch_dtype=torch.bfloat16,
44
+ ).to('cuda')
45
+
46
+
47
+ optimize_pipeline_(pipe,
48
+ image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
49
+ prompt='prompt',
50
+ height=LANDSCAPE_HEIGHT,
51
+ width=LANDSCAPE_WIDTH,
52
+ num_frames=MAX_FRAMES_MODEL,
53
+ )
54
+
55
+
56
+ default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
57
+ default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
58
+
59
+
60
+ def resize_image(image: Image.Image) -> Image.Image:
61
+ if image.height > image.width:
62
+ transposed = image.transpose(Image.Transpose.ROTATE_90)
63
+ resized = resize_image_landscape(transposed)
64
+ return resized.transpose(Image.Transpose.ROTATE_270)
65
+ return resize_image_landscape(image)
66
+
67
+
68
+ def resize_image_landscape(image: Image.Image) -> Image.Image:
69
+ target_aspect = LANDSCAPE_WIDTH / LANDSCAPE_HEIGHT
70
+ width, height = image.size
71
+ in_aspect = width / height
72
+ if in_aspect > target_aspect:
73
+ new_width = round(height * target_aspect)
74
+ left = (width - new_width) // 2
75
+ image = image.crop((left, 0, left + new_width, height))
76
+ else:
77
+ new_height = round(width / target_aspect)
78
+ top = (height - new_height) // 2
79
+ image = image.crop((0, top, width, top + new_height))
80
+ return image.resize((LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT), Image.LANCZOS)
81
+
82
+ def get_duration(
83
+ input_image,
84
+ prompt,
85
+ negative_prompt,
86
+ num_frames,
87
+ guidance_scale,
88
+ steps,
89
+ seed,
90
+ randomize_seed,
91
+ progress,
92
+ ):
93
+ forward_duration_base = 8
94
+ forward_duration = forward_duration_base * (num_frames / NUM_FRAMES_DEFAULT)**1.5
95
+ forward_count = 2 if guidance_scale > 1 else 1
96
+ return 10 + steps * forward_count * forward_duration
97
+
98
+ @spaces.GPU(duration=get_duration)
99
+ def generate_video(
100
+ input_image,
101
+ prompt,
102
+ negative_prompt=default_negative_prompt,
103
+ num_frames = NUM_FRAMES_DEFAULT,
104
+ guidance_scale = 1,
105
+ steps = 28,
106
+ seed = 42,
107
+ randomize_seed = False,
108
+ progress=gr.Progress(track_tqdm=True),
109
+ ):
110
+
111
+ if input_image is None:
112
+ raise gr.Error("Please upload an input image.")
113
+
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=resized_image,
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-fp8da-81-frames")
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
+ num_frames_input = gr.Slider(minimum=MIN_FRAMES_MODEL, maximum=MAX_FRAMES_MODEL, step=1, value=NUM_FRAMES_DEFAULT, label="Frames")
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=28, label="Inference Steps")
150
+ guidance_scale_input = gr.Slider(minimum=1.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale")
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, num_frames_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)