Update app.py (#2)
Browse files- Update app.py (cea01700bf2260c5aacdd47f80e0e5810abb0cde)
- Update optimization.py (201ea86637fa9ad7fb65d35aa467abe396108f3c)
- app.py +82 -37
- optimization.py +17 -0
app.py
CHANGED
|
@@ -13,19 +13,22 @@ 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-
|
| 21 |
|
| 22 |
LANDSCAPE_WIDTH = 832
|
| 23 |
LANDSCAPE_HEIGHT = 480
|
| 24 |
MAX_SEED = np.iinfo(np.int32).max
|
| 25 |
|
| 26 |
-
FIXED_FPS =
|
| 27 |
MIN_FRAMES_MODEL = 8
|
| 28 |
-
MAX_FRAMES_MODEL =
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
|
|
@@ -42,6 +45,39 @@ pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
|
|
| 42 |
torch_dtype=torch.bfloat16,
|
| 43 |
).to('cuda')
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
optimize_pipeline_(pipe,
|
| 47 |
image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
|
|
@@ -53,7 +89,7 @@ optimize_pipeline_(pipe,
|
|
| 53 |
|
| 54 |
|
| 55 |
default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
|
| 56 |
-
default_negative_prompt = "
|
| 57 |
|
| 58 |
|
| 59 |
def resize_image(image: Image.Image) -> Image.Image:
|
|
@@ -82,8 +118,9 @@ def get_duration(
|
|
| 82 |
input_image,
|
| 83 |
prompt,
|
| 84 |
negative_prompt,
|
| 85 |
-
|
| 86 |
guidance_scale,
|
|
|
|
| 87 |
steps,
|
| 88 |
seed,
|
| 89 |
randomize_seed,
|
|
@@ -96,29 +133,32 @@ def generate_video(
|
|
| 96 |
input_image,
|
| 97 |
prompt,
|
| 98 |
negative_prompt=default_negative_prompt,
|
| 99 |
-
|
| 100 |
-
guidance_scale =
|
| 101 |
-
|
|
|
|
| 102 |
seed = 42,
|
| 103 |
randomize_seed = False,
|
| 104 |
progress=gr.Progress(track_tqdm=True),
|
| 105 |
):
|
| 106 |
"""
|
| 107 |
-
Generate a video from an input image using the Wan 2.
|
| 108 |
|
| 109 |
This function takes an input image and generates a video animation based on the provided
|
| 110 |
-
prompt and parameters. It uses
|
| 111 |
-
for fast generation in
|
| 112 |
|
| 113 |
Args:
|
| 114 |
input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
|
| 115 |
prompt (str): Text prompt describing the desired animation or motion.
|
| 116 |
negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
|
| 117 |
Defaults to default_negative_prompt (contains unwanted visual artifacts).
|
| 118 |
-
|
| 119 |
-
Defaults to MAX_FRAMES_MODEL
|
| 120 |
guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
|
| 121 |
Defaults to 1.0. Range: 0.0-20.0.
|
|
|
|
|
|
|
| 122 |
steps (int, optional): Number of inference steps. More steps = higher quality but slower.
|
| 123 |
Defaults to 4. Range: 1-30.
|
| 124 |
seed (int, optional): Random seed for reproducible results. Defaults to 42.
|
|
@@ -137,23 +177,27 @@ def generate_video(
|
|
| 137 |
|
| 138 |
Note:
|
| 139 |
- The function automatically resizes the input image to the target dimensions
|
|
|
|
| 140 |
- Output dimensions are adjusted to be multiples of MOD_VALUE (32)
|
| 141 |
- The function uses GPU acceleration via the @spaces.GPU decorator
|
|
|
|
| 142 |
"""
|
| 143 |
-
if input_image is None:
|
| 144 |
-
|
| 145 |
|
|
|
|
| 146 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 147 |
-
resized_image = resize_image(input_image)
|
| 148 |
|
| 149 |
output_frames_list = pipe(
|
| 150 |
-
image=resized_image,
|
| 151 |
prompt=prompt,
|
| 152 |
negative_prompt=negative_prompt,
|
| 153 |
-
height=
|
| 154 |
-
width=
|
| 155 |
num_frames=num_frames,
|
| 156 |
guidance_scale=float(guidance_scale),
|
|
|
|
| 157 |
num_inference_steps=int(steps),
|
| 158 |
generator=torch.Generator(device="cuda").manual_seed(current_seed),
|
| 159 |
).frames[0]
|
|
@@ -166,20 +210,21 @@ def generate_video(
|
|
| 166 |
return video_path, current_seed
|
| 167 |
|
| 168 |
with gr.Blocks() as demo:
|
| 169 |
-
gr.Markdown("# Fast
|
| 170 |
-
gr.Markdown("
|
| 171 |
with gr.Row():
|
| 172 |
with gr.Column():
|
| 173 |
-
input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)")
|
| 174 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 175 |
-
|
| 176 |
|
| 177 |
with gr.Accordion("Advanced Settings", open=False):
|
| 178 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 179 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 180 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 181 |
-
steps_slider = gr.Slider(minimum=1, maximum=
|
| 182 |
-
guidance_scale_input = gr.Slider(minimum=0.0, maximum=
|
|
|
|
| 183 |
|
| 184 |
generate_button = gr.Button("Generate Video", variant="primary")
|
| 185 |
with gr.Column():
|
|
@@ -187,20 +232,20 @@ with gr.Blocks() as demo:
|
|
| 187 |
|
| 188 |
ui_inputs = [
|
| 189 |
input_image_component, prompt_input,
|
| 190 |
-
negative_prompt_input,
|
| 191 |
-
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 192 |
]
|
| 193 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
| 194 |
|
| 195 |
-
gr.Examples(
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
)
|
| 204 |
|
| 205 |
if __name__ == "__main__":
|
| 206 |
demo.queue().launch(mcp_server=True)
|
|
|
|
| 13 |
import numpy as np
|
| 14 |
from PIL import Image
|
| 15 |
import random
|
| 16 |
+
import gc
|
| 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 = 16
|
| 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,
|
|
|
|
| 45 |
torch_dtype=torch.bfloat16,
|
| 46 |
).to('cuda')
|
| 47 |
|
| 48 |
+
# load, fuse, unload before compilation
|
| 49 |
+
# pipe.load_lora_weights(
|
| 50 |
+
# "vrgamedevgirl84/Wan14BT2VFusioniX",
|
| 51 |
+
# weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
|
| 52 |
+
# adapter_name="phantom"
|
| 53 |
+
# )
|
| 54 |
+
|
| 55 |
+
# pipe.set_adapters(["phantom"], adapter_weights=[0.95])
|
| 56 |
+
# pipe.fuse_lora(adapter_names=["phantom"], lora_scale=1.0)
|
| 57 |
+
# pipe.unload_lora_weights()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# pipe.load_lora_weights(
|
| 61 |
+
# "vrgamedevgirl84/Wan14BT2VFusioniX",
|
| 62 |
+
# weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
|
| 63 |
+
# adapter_name="phantom"
|
| 64 |
+
# )
|
| 65 |
+
# kwargs = {}
|
| 66 |
+
# kwargs["load_into_transformer_2"] = True
|
| 67 |
+
# pipe.load_lora_weights(
|
| 68 |
+
# "vrgamedevgirl84/Wan14BT2VFusioniX",
|
| 69 |
+
# weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
|
| 70 |
+
# adapter_name="phantom_2", **kwargs
|
| 71 |
+
# )
|
| 72 |
+
# pipe.set_adapters(["phantom", "phantom_2"], adapter_weights=[1., 1.])
|
| 73 |
+
# pipe.fuse_lora(adapter_names=["phantom"], lora_scale=3., components=["transformer"])
|
| 74 |
+
# pipe.fuse_lora(adapter_names=["phantom_2"], lora_scale=1., components=["transformer_2"])
|
| 75 |
+
# pipe.unload_lora_weights()
|
| 76 |
+
|
| 77 |
+
for i in range(3):
|
| 78 |
+
gc.collect()
|
| 79 |
+
torch.cuda.synchronize()
|
| 80 |
+
torch.cuda.empty_cache()
|
| 81 |
|
| 82 |
optimize_pipeline_(pipe,
|
| 83 |
image=Image.new('RGB', (LANDSCAPE_WIDTH, LANDSCAPE_HEIGHT)),
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
|
| 92 |
+
default_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"
|
| 93 |
|
| 94 |
|
| 95 |
def resize_image(image: Image.Image) -> Image.Image:
|
|
|
|
| 118 |
input_image,
|
| 119 |
prompt,
|
| 120 |
negative_prompt,
|
| 121 |
+
duration_seconds,
|
| 122 |
guidance_scale,
|
| 123 |
+
guidance_scale_2,
|
| 124 |
steps,
|
| 125 |
seed,
|
| 126 |
randomize_seed,
|
|
|
|
| 133 |
input_image,
|
| 134 |
prompt,
|
| 135 |
negative_prompt=default_negative_prompt,
|
| 136 |
+
duration_seconds = MAX_DURATION,
|
| 137 |
+
guidance_scale = 1,
|
| 138 |
+
guidance_scale_2 = 3,
|
| 139 |
+
steps = 6,
|
| 140 |
seed = 42,
|
| 141 |
randomize_seed = False,
|
| 142 |
progress=gr.Progress(track_tqdm=True),
|
| 143 |
):
|
| 144 |
"""
|
| 145 |
+
Generate a video from an input image using the Wan 2.2 14B I2V model with Phantom LoRA.
|
| 146 |
|
| 147 |
This function takes an input image and generates a video animation based on the provided
|
| 148 |
+
prompt and parameters. It uses an FP8 qunatized Wan 2.2 14B Image-to-Video model in with Phantom LoRA
|
| 149 |
+
for fast generation in 6-8 steps.
|
| 150 |
|
| 151 |
Args:
|
| 152 |
input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
|
| 153 |
prompt (str): Text prompt describing the desired animation or motion.
|
| 154 |
negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
|
| 155 |
Defaults to default_negative_prompt (contains unwanted visual artifacts).
|
| 156 |
+
duration_seconds (float, optional): Duration of the generated video in seconds.
|
| 157 |
+
Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
|
| 158 |
guidance_scale (float, optional): Controls adherence to the prompt. Higher values = more adherence.
|
| 159 |
Defaults to 1.0. Range: 0.0-20.0.
|
| 160 |
+
guidance_scale_2 (float, optional): Controls adherence to the prompt. Higher values = more adherence.
|
| 161 |
+
Defaults to 1.0. Range: 0.0-20.0.
|
| 162 |
steps (int, optional): Number of inference steps. More steps = higher quality but slower.
|
| 163 |
Defaults to 4. Range: 1-30.
|
| 164 |
seed (int, optional): Random seed for reproducible results. Defaults to 42.
|
|
|
|
| 177 |
|
| 178 |
Note:
|
| 179 |
- The function automatically resizes the input image to the target dimensions
|
| 180 |
+
- Frame count is calculated as duration_seconds * FIXED_FPS (24)
|
| 181 |
- Output dimensions are adjusted to be multiples of MOD_VALUE (32)
|
| 182 |
- The function uses GPU acceleration via the @spaces.GPU decorator
|
| 183 |
+
- Generation time varies based on steps and duration (see get_duration function)
|
| 184 |
"""
|
| 185 |
+
# if input_image is None:
|
| 186 |
+
# raise gr.Error("Please upload an input image.")
|
| 187 |
|
| 188 |
+
num_frames = np.clip(int(round(duration_seconds * FIXED_FPS)), MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
| 189 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 190 |
+
# resized_image = resize_image(input_image)
|
| 191 |
|
| 192 |
output_frames_list = pipe(
|
| 193 |
+
#image=resized_image,
|
| 194 |
prompt=prompt,
|
| 195 |
negative_prompt=negative_prompt,
|
| 196 |
+
height=480,
|
| 197 |
+
width=832,
|
| 198 |
num_frames=num_frames,
|
| 199 |
guidance_scale=float(guidance_scale),
|
| 200 |
+
guidance_scale_2=float(guidance_scale_2),
|
| 201 |
num_inference_steps=int(steps),
|
| 202 |
generator=torch.Generator(device="cuda").manual_seed(current_seed),
|
| 203 |
).frames[0]
|
|
|
|
| 210 |
return video_path, current_seed
|
| 211 |
|
| 212 |
with gr.Blocks() as demo:
|
| 213 |
+
gr.Markdown("# Fast 6 steps Wan 2.2 I2V (14B) with Phantom LoRA")
|
| 214 |
+
gr.Markdown("run Wan 2.2 in just 6-8 steps, with [FusionX Phantom LoRA by DeeJayT](https://huggingface.co/vrgamedevgirl84/Wan14BT2VFusioniX/tree/main/FusionX_LoRa), compatible with 🧨 diffusers")
|
| 215 |
with gr.Row():
|
| 216 |
with gr.Column():
|
| 217 |
+
input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)", visible=False)
|
| 218 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 219 |
+
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.")
|
| 220 |
|
| 221 |
with gr.Accordion("Advanced Settings", open=False):
|
| 222 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 223 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 224 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 225 |
+
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
|
| 226 |
+
guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage")
|
| 227 |
+
guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=3, label="Guidance Scale 2 - low noise stage")
|
| 228 |
|
| 229 |
generate_button = gr.Button("Generate Video", variant="primary")
|
| 230 |
with gr.Column():
|
|
|
|
| 232 |
|
| 233 |
ui_inputs = [
|
| 234 |
input_image_component, prompt_input,
|
| 235 |
+
negative_prompt_input, duration_seconds_input,
|
| 236 |
+
guidance_scale_input, guidance_scale_2_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 237 |
]
|
| 238 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
| 239 |
|
| 240 |
+
# gr.Examples(
|
| 241 |
+
# examples=[
|
| 242 |
+
# [
|
| 243 |
+
# "wan_i2v_input.JPG",
|
| 244 |
+
# "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.",
|
| 245 |
+
# ],
|
| 246 |
+
# ],
|
| 247 |
+
# inputs=[input_image_component, prompt_input], outputs=[video_output, seed_input], fn=generate_video, cache_examples="lazy"
|
| 248 |
+
# )
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
| 251 |
demo.queue().launch(mcp_server=True)
|
optimization.py
CHANGED
|
@@ -36,6 +36,23 @@ def optimize_pipeline_(pipeline: Callable[P, Any], *args: P.args, **kwargs: P.kw
|
|
| 36 |
|
| 37 |
@spaces.GPU(duration=1500)
|
| 38 |
def compile_transformer():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
with capture_component_call(pipeline, 'transformer') as call:
|
| 41 |
pipeline(*args, **kwargs)
|
|
|
|
| 36 |
|
| 37 |
@spaces.GPU(duration=1500)
|
| 38 |
def compile_transformer():
|
| 39 |
+
|
| 40 |
+
pipeline.load_lora_weights(
|
| 41 |
+
"vrgamedevgirl84/Wan14BT2VFusioniX",
|
| 42 |
+
weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
|
| 43 |
+
adapter_name="phantom"
|
| 44 |
+
)
|
| 45 |
+
kwargs_lora = {}
|
| 46 |
+
kwargs_lora["load_into_transformer_2"] = True
|
| 47 |
+
pipeline.load_lora_weights(
|
| 48 |
+
"vrgamedevgirl84/Wan14BT2VFusioniX",
|
| 49 |
+
weight_name="FusionX_LoRa/Phantom_Wan_14B_FusionX_LoRA.safetensors",
|
| 50 |
+
adapter_name="phantom_2", **kwargs_lora
|
| 51 |
+
)
|
| 52 |
+
pipeline.set_adapters(["phantom", "phantom_2"], adapter_weights=[1., 1.])
|
| 53 |
+
pipeline.fuse_lora(adapter_names=["phantom"], lora_scale=3., components=["transformer"])
|
| 54 |
+
pipeline.fuse_lora(adapter_names=["phantom_2"], lora_scale=1., components=["transformer_2"])
|
| 55 |
+
pipeline.unload_lora_weights()
|
| 56 |
|
| 57 |
with capture_component_call(pipeline, 'transformer') as call:
|
| 58 |
pipeline(*args, **kwargs)
|