Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,15 @@
|
|
| 1 |
import spaces
|
| 2 |
import torch
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
T5EncoderModel,
|
| 7 |
-
)
|
| 8 |
-
|
| 9 |
-
from diffusers import (
|
| 10 |
-
WanImageToVideoPipeline,
|
| 11 |
-
WanTransformer3DModel,
|
| 12 |
-
AutoencoderKL,
|
| 13 |
-
EulerDiscreteScheduler,
|
| 14 |
-
)
|
| 15 |
import gradio as gr
|
| 16 |
import tempfile
|
| 17 |
import numpy as np
|
| 18 |
from PIL import Image
|
| 19 |
import random
|
| 20 |
import gc
|
| 21 |
-
|
| 22 |
-
from diffusers.utils.export_utils import export_to_video
|
| 23 |
-
from huggingface_hub import hf_hub_download
|
| 24 |
-
from safetensors.torch import load_file as safetensors_load
|
| 25 |
|
| 26 |
from torchao.quantization import quantize_
|
| 27 |
from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
|
|
@@ -44,7 +32,7 @@ MAX_SEED = np.iinfo(np.int32).max
|
|
| 44 |
|
| 45 |
FIXED_FPS = 16
|
| 46 |
MIN_FRAMES_MODEL = 8
|
| 47 |
-
MAX_FRAMES_MODEL = 80
|
| 48 |
|
| 49 |
MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
|
| 50 |
MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
|
|
@@ -64,6 +52,7 @@ pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
|
|
| 64 |
torch_dtype=torch.bfloat16,
|
| 65 |
).to('cuda')
|
| 66 |
|
|
|
|
| 67 |
pipe.load_lora_weights(
|
| 68 |
"Kijai/WanVideo_comfy",
|
| 69 |
weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
|
|
@@ -105,8 +94,8 @@ quantize_(pipe.text_encoder, Int8WeightOnlyConfig())
|
|
| 105 |
quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig())
|
| 106 |
quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig())
|
| 107 |
|
| 108 |
-
aoti.aoti_blocks_load(pipe.transformer, '
|
| 109 |
-
aoti.aoti_blocks_load(pipe.transformer_2, '
|
| 110 |
|
| 111 |
|
| 112 |
default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
|
|
@@ -123,12 +112,12 @@ def resize_image(image: Image.Image) -> Image.Image:
|
|
| 123 |
return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)
|
| 124 |
|
| 125 |
aspect_ratio = width / height
|
| 126 |
-
|
| 127 |
-
MAX_ASPECT_RATIO = MAX_DIM / MIN_DIM
|
| 128 |
-
MIN_ASPECT_RATIO = MIN_DIM / MAX_DIM
|
| 129 |
|
| 130 |
image_to_resize = image
|
| 131 |
-
|
| 132 |
if aspect_ratio > MAX_ASPECT_RATIO:
|
| 133 |
# Very wide image -> crop width to fit 832x480 aspect ratio
|
| 134 |
target_w, target_h = MAX_DIM, MIN_DIM
|
|
@@ -154,10 +143,62 @@ def resize_image(image: Image.Image) -> Image.Image:
|
|
| 154 |
|
| 155 |
final_w = max(MIN_DIM, min(MAX_DIM, final_w))
|
| 156 |
final_h = max(MIN_DIM, min(MAX_DIM, final_h))
|
| 157 |
-
|
| 158 |
return image_to_resize.resize((final_w, final_h), Image.LANCZOS)
|
| 159 |
|
| 160 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
def get_num_frames(duration_seconds: float):
|
| 162 |
return 1 + int(np.clip(
|
| 163 |
int(round(duration_seconds * FIXED_FPS)),
|
|
@@ -166,28 +207,42 @@ def get_num_frames(duration_seconds: float):
|
|
| 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 |
@spaces.GPU(duration=120)
|
| 193 |
def generate_video(
|
|
@@ -197,24 +252,22 @@ def generate_video(
|
|
| 197 |
negative_prompt=default_negative_prompt,
|
| 198 |
duration_seconds = MAX_DURATION,
|
| 199 |
guidance_scale = 1,
|
| 200 |
-
guidance_scale_2 = 1,
|
| 201 |
seed = 42,
|
| 202 |
randomize_seed = False,
|
| 203 |
progress=gr.Progress(track_tqdm=True),
|
| 204 |
):
|
| 205 |
"""
|
| 206 |
Generate a video from an input image using the Wan 2.2 14B I2V model with Lightning LoRA.
|
| 207 |
-
|
| 208 |
This function takes an input image and generates a video animation based on the provided
|
| 209 |
prompt and parameters. It uses an FP8 qunatized Wan 2.2 14B Image-to-Video model in with Lightning LoRA
|
| 210 |
for fast generation in 4-8 steps.
|
| 211 |
-
|
| 212 |
Args:
|
| 213 |
input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
|
| 214 |
prompt (str): Text prompt describing the desired animation or motion.
|
| 215 |
steps (int, optional): Number of inference steps. More steps = higher quality but slower.
|
| 216 |
Defaults to 4. Range: 1-30.
|
| 217 |
-
negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
|
| 218 |
Defaults to default_negative_prompt (contains unwanted visual artifacts).
|
| 219 |
duration_seconds (float, optional): Duration of the generated video in seconds.
|
| 220 |
Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
|
|
@@ -227,15 +280,12 @@ def generate_video(
|
|
| 227 |
randomize_seed (bool, optional): Whether to use a random seed instead of the provided seed.
|
| 228 |
Defaults to False.
|
| 229 |
progress (gr.Progress, optional): Gradio progress tracker. Defaults to gr.Progress(track_tqdm=True).
|
| 230 |
-
|
| 231 |
Returns:
|
| 232 |
tuple: A tuple containing:
|
| 233 |
- video_path (str): Path to the generated video file (.mp4)
|
| 234 |
- current_seed (int): The seed used for generation (useful when randomize_seed=True)
|
| 235 |
-
|
| 236 |
Raises:
|
| 237 |
gr.Error: If input_image is None (no image uploaded).
|
| 238 |
-
|
| 239 |
Note:
|
| 240 |
- Frame count is calculated as duration_seconds * FIXED_FPS (24)
|
| 241 |
- Output dimensions are adjusted to be multiples of MOD_VALUE (32)
|
|
@@ -244,10 +294,15 @@ def generate_video(
|
|
| 244 |
"""
|
| 245 |
if input_image is None:
|
| 246 |
raise gr.Error("Please upload an input image.")
|
| 247 |
-
|
| 248 |
num_frames = get_num_frames(duration_seconds)
|
| 249 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 250 |
resized_image = resize_image(input_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
output_frames_list = pipe(
|
| 253 |
image=resized_image,
|
|
@@ -296,9 +351,9 @@ def generate_video(
|
|
| 296 |
# Команда ffmpeg для создания гауссова размытия
|
| 297 |
cmd_blur = [
|
| 298 |
'ffmpeg',
|
| 299 |
-
'-i', video_with_audio_path,
|
| 300 |
-
'-vf', 'gblur=sigma=25',
|
| 301 |
-
'-c:a', 'copy',
|
| 302 |
'-y',
|
| 303 |
blurred_video_path
|
| 304 |
]
|
|
@@ -319,21 +374,20 @@ def generate_video(
|
|
| 319 |
print("FFmpeg not available, returning video without audio")
|
| 320 |
return video_path, video_path, current_seed
|
| 321 |
|
| 322 |
-
|
| 323 |
with gr.Blocks() as demo:
|
| 324 |
-
gr.Markdown("#
|
| 325 |
-
gr.Markdown("run Wan 2.2 in just 4-8 steps, with [Lightning LoRA](https://huggingface.co/Kijai/WanVideo_comfy/tree/main/Wan22-Lightning), fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU⚡️")
|
| 326 |
with gr.Row():
|
| 327 |
with gr.Column():
|
| 328 |
input_image_component = gr.Image(type="pil", label="Input Image")
|
| 329 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 330 |
duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
|
| 331 |
-
|
| 332 |
with gr.Accordion("Advanced Settings", open=False):
|
| 333 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 334 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 335 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 336 |
-
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
|
| 337 |
guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage")
|
| 338 |
guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale 2 - low noise stage")
|
| 339 |
|
|
@@ -341,34 +395,15 @@ with gr.Blocks() as demo:
|
|
| 341 |
with gr.Column():
|
| 342 |
video_output_1 = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 343 |
video_output_2 = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 344 |
-
|
|
|
|
| 345 |
ui_inputs = [
|
| 346 |
input_image_component, prompt_input, steps_slider,
|
| 347 |
negative_prompt_input, duration_seconds_input,
|
| 348 |
guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox
|
| 349 |
]
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
gr.Examples(
|
| 353 |
-
examples=[
|
| 354 |
-
[
|
| 355 |
-
"wan_i2v_input.JPG",
|
| 356 |
-
"POV selfie video, white cat with sunglasses standing on surfboard, relaxed smile, tropical beach behind (clear water, green hills, blue sky with clouds). Surfboard tips, cat falls into ocean, camera plunges underwater with bubbles and sunlight beams. Brief underwater view of cat’s face, then cat resurfaces, still filming selfie, playful summer vacation mood.",
|
| 357 |
-
4,
|
| 358 |
-
],
|
| 359 |
-
[
|
| 360 |
-
"wan22_input_2.jpg",
|
| 361 |
-
"A sleek lunar vehicle glides into view from left to right, kicking up moon dust as astronauts in white spacesuits hop aboard with characteristic lunar bouncing movements. In the distant background, a VTOL craft descends straight down and lands silently on the surface. Throughout the entire scene, ethereal aurora borealis ribbons dance across the star-filled sky, casting shimmering curtains of green, blue, and purple light that bathe the lunar landscape in an otherworldly, magical glow.",
|
| 362 |
-
4,
|
| 363 |
-
],
|
| 364 |
-
[
|
| 365 |
-
"kill_bill.jpeg",
|
| 366 |
-
"Uma Thurman's character, Beatrix Kiddo, holds her razor-sharp katana blade steady in the cinematic lighting. Suddenly, the polished steel begins to soften and distort, like heated metal starting to lose its structural integrity. The blade's perfect edge slowly warps and droops, molten steel beginning to flow downward in silvery rivulets while maintaining its metallic sheen. The transformation starts subtly at first - a slight bend in the blade - then accelerates as the metal becomes increasingly fluid. The camera holds steady on her face as her piercing eyes gradually narrow, not with lethal focus, but with confusion and growing alarm as she watches her weapon dissolve before her eyes. Her breathing quickens slightly as she witnesses this impossible transformation. The melting intensifies, the katana's perfect form becoming increasingly abstract, dripping like liquid mercury from her grip. Molten droplets fall to the ground with soft metallic impacts. Her expression shifts from calm readiness to bewilderment and concern as her legendary instrument of vengeance literally liquefies in her hands, leaving her defenseless and disoriented",
|
| 367 |
-
6,
|
| 368 |
-
],
|
| 369 |
-
],
|
| 370 |
-
inputs=[input_image_component, prompt_input, steps_slider], outputs=[video_output_1, video_output_2, seed_input], fn=generate_video, cache_examples="lazy"
|
| 371 |
-
)
|
| 372 |
|
| 373 |
def check_ffmpeg():
|
| 374 |
try:
|
|
@@ -378,5 +413,4 @@ def check_ffmpeg():
|
|
| 378 |
return False
|
| 379 |
|
| 380 |
if __name__ == "__main__":
|
| 381 |
-
demo.queue().launch(ssl_verify=False)
|
| 382 |
-
|
|
|
|
| 1 |
import spaces
|
| 2 |
import torch
|
| 3 |
+
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
|
| 4 |
+
from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
|
| 5 |
+
from diffusers.utils.export_utils import export_to_video
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import gradio as gr
|
| 7 |
import tempfile
|
| 8 |
import numpy as np
|
| 9 |
from PIL import Image
|
| 10 |
import random
|
| 11 |
import gc
|
| 12 |
+
import os
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
from torchao.quantization import quantize_
|
| 15 |
from torchao.quantization import Float8DynamicActivationFloat8WeightConfig
|
|
|
|
| 32 |
|
| 33 |
FIXED_FPS = 16
|
| 34 |
MIN_FRAMES_MODEL = 8
|
| 35 |
+
MAX_FRAMES_MODEL = 176#80
|
| 36 |
|
| 37 |
MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
|
| 38 |
MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
|
|
|
|
| 52 |
torch_dtype=torch.bfloat16,
|
| 53 |
).to('cuda')
|
| 54 |
|
| 55 |
+
|
| 56 |
pipe.load_lora_weights(
|
| 57 |
"Kijai/WanVideo_comfy",
|
| 58 |
weight_name="Lightx2v/lightx2v_I2V_14B_480p_cfg_step_distill_rank128_bf16.safetensors",
|
|
|
|
| 94 |
quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig())
|
| 95 |
quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig())
|
| 96 |
|
| 97 |
+
aoti.aoti_blocks_load(pipe.transformer, 'rahul7star/WanAot', variant='fp8da')
|
| 98 |
+
aoti.aoti_blocks_load(pipe.transformer_2, 'rahul7star/WanAot', variant='fp8da')
|
| 99 |
|
| 100 |
|
| 101 |
default_prompt_i2v = "make this image come alive, cinematic motion, smooth animation"
|
|
|
|
| 112 |
return image.resize((SQUARE_DIM, SQUARE_DIM), Image.LANCZOS)
|
| 113 |
|
| 114 |
aspect_ratio = width / height
|
| 115 |
+
|
| 116 |
+
MAX_ASPECT_RATIO = MAX_DIM / MIN_DIM
|
| 117 |
+
MIN_ASPECT_RATIO = MIN_DIM / MAX_DIM
|
| 118 |
|
| 119 |
image_to_resize = image
|
| 120 |
+
|
| 121 |
if aspect_ratio > MAX_ASPECT_RATIO:
|
| 122 |
# Very wide image -> crop width to fit 832x480 aspect ratio
|
| 123 |
target_w, target_h = MAX_DIM, MIN_DIM
|
|
|
|
| 143 |
|
| 144 |
final_w = max(MIN_DIM, min(MAX_DIM, final_w))
|
| 145 |
final_h = max(MIN_DIM, min(MAX_DIM, final_h))
|
| 146 |
+
|
| 147 |
return image_to_resize.resize((final_w, final_h), Image.LANCZOS)
|
| 148 |
|
| 149 |
|
| 150 |
+
HF_MODEL = os.environ.get("HF_UPLOAD_REPO", "rahul7star/wan22-aot-image-2025-dec")
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
# --- CPU-only upload function ---
|
| 154 |
+
def upload_image_and_prompt_cpu(input_image, prompt_text) -> str:
|
| 155 |
+
from datetime import datetime
|
| 156 |
+
import tempfile, os, uuid, shutil
|
| 157 |
+
from huggingface_hub import HfApi
|
| 158 |
+
|
| 159 |
+
# Instantiate the HfApi class
|
| 160 |
+
api = HfApi()
|
| 161 |
+
|
| 162 |
+
today_str = datetime.now().strftime("%Y-%m-%d")
|
| 163 |
+
unique_subfolder = f"Upload-Image-{uuid.uuid4().hex[:8]}"
|
| 164 |
+
hf_folder = f"{today_str}/{unique_subfolder}"
|
| 165 |
+
|
| 166 |
+
# Save image temporarily
|
| 167 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_img:
|
| 168 |
+
if isinstance(input_image, str):
|
| 169 |
+
shutil.copy(input_image, tmp_img.name)
|
| 170 |
+
else:
|
| 171 |
+
input_image.save(tmp_img.name, format="PNG")
|
| 172 |
+
tmp_img_path = tmp_img.name
|
| 173 |
+
|
| 174 |
+
# Upload image using HfApi instance
|
| 175 |
+
api.upload_file(
|
| 176 |
+
path_or_fileobj=tmp_img_path,
|
| 177 |
+
path_in_repo=f"{hf_folder}/input_image.png",
|
| 178 |
+
repo_id=HF_MODEL,
|
| 179 |
+
repo_type="model",
|
| 180 |
+
token=os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 181 |
+
)
|
| 182 |
+
|
| 183 |
+
# Save prompt as summary.txt
|
| 184 |
+
summary_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt").name
|
| 185 |
+
with open(summary_file, "w", encoding="utf-8") as f:
|
| 186 |
+
f.write(prompt_text)
|
| 187 |
+
|
| 188 |
+
api.upload_file(
|
| 189 |
+
path_or_fileobj=summary_file,
|
| 190 |
+
path_in_repo=f"{hf_folder}/summary.txt",
|
| 191 |
+
repo_id=HF_MODEL,
|
| 192 |
+
repo_type="model",
|
| 193 |
+
token=os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 194 |
+
)
|
| 195 |
+
|
| 196 |
+
# Cleanup
|
| 197 |
+
os.remove(tmp_img_path)
|
| 198 |
+
os.remove(summary_file)
|
| 199 |
+
|
| 200 |
+
return hf_folder
|
| 201 |
+
|
| 202 |
def get_num_frames(duration_seconds: float):
|
| 203 |
return 1 + int(np.clip(
|
| 204 |
int(round(duration_seconds * FIXED_FPS)),
|
|
|
|
| 207 |
))
|
| 208 |
|
| 209 |
|
| 210 |
+
# --- Wrapper to upload image/prompt on CPU before GPU generation ---
|
| 211 |
+
def generate_video_with_upload(input_image, prompt, steps=4, negative_prompt=default_negative_prompt,
|
| 212 |
+
duration_seconds=2, guidance_scale=1, guidance_scale_2=1,
|
| 213 |
+
seed=44, randomize_seed=False):
|
| 214 |
+
# Upload on CPU (hidden, no UI)
|
| 215 |
+
try:
|
| 216 |
+
upload_image_and_prompt_cpu(input_image, prompt)
|
| 217 |
+
except Exception as e:
|
| 218 |
+
print("Upload failed:", e)
|
| 219 |
+
|
| 220 |
+
# Proceed with GPU video generation
|
| 221 |
+
return generate_video(input_image, prompt, steps,
|
| 222 |
+
negative_prompt, duration_seconds,
|
| 223 |
+
guidance_scale, guidance_scale_2, seed, randomize_seed)
|
| 224 |
+
|
| 225 |
+
# def get_duration(
|
| 226 |
+
# input_image,
|
| 227 |
+
# prompt,
|
| 228 |
+
# steps,
|
| 229 |
+
# negative_prompt,
|
| 230 |
+
# duration_seconds,
|
| 231 |
+
# guidance_scale,
|
| 232 |
+
# guidance_scale_2,
|
| 233 |
+
# seed,
|
| 234 |
+
# randomize_seed,
|
| 235 |
+
# progress,
|
| 236 |
+
# ):
|
| 237 |
+
# BASE_FRAMES_HEIGHT_WIDTH = 81 * 832 * 624
|
| 238 |
+
# BASE_STEP_DURATION = 15
|
| 239 |
+
# width, height = resize_image(input_image).size
|
| 240 |
+
# frames = get_num_frames(duration_seconds)
|
| 241 |
+
# factor = frames * width * height / BASE_FRAMES_HEIGHT_WIDTH
|
| 242 |
+
# step_duration = BASE_STEP_DURATION * factor ** 1.5
|
| 243 |
+
# return 10 + int(steps) * step_duration
|
| 244 |
+
|
| 245 |
+
|
| 246 |
|
| 247 |
@spaces.GPU(duration=120)
|
| 248 |
def generate_video(
|
|
|
|
| 252 |
negative_prompt=default_negative_prompt,
|
| 253 |
duration_seconds = MAX_DURATION,
|
| 254 |
guidance_scale = 1,
|
| 255 |
+
guidance_scale_2 = 1,
|
| 256 |
seed = 42,
|
| 257 |
randomize_seed = False,
|
| 258 |
progress=gr.Progress(track_tqdm=True),
|
| 259 |
):
|
| 260 |
"""
|
| 261 |
Generate a video from an input image using the Wan 2.2 14B I2V model with Lightning LoRA.
|
|
|
|
| 262 |
This function takes an input image and generates a video animation based on the provided
|
| 263 |
prompt and parameters. It uses an FP8 qunatized Wan 2.2 14B Image-to-Video model in with Lightning LoRA
|
| 264 |
for fast generation in 4-8 steps.
|
|
|
|
| 265 |
Args:
|
| 266 |
input_image (PIL.Image): The input image to animate. Will be resized to target dimensions.
|
| 267 |
prompt (str): Text prompt describing the desired animation or motion.
|
| 268 |
steps (int, optional): Number of inference steps. More steps = higher quality but slower.
|
| 269 |
Defaults to 4. Range: 1-30.
|
| 270 |
+
negative_prompt (str, optional): Negative prompt to avoid unwanted elements.
|
| 271 |
Defaults to default_negative_prompt (contains unwanted visual artifacts).
|
| 272 |
duration_seconds (float, optional): Duration of the generated video in seconds.
|
| 273 |
Defaults to 2. Clamped between MIN_FRAMES_MODEL/FIXED_FPS and MAX_FRAMES_MODEL/FIXED_FPS.
|
|
|
|
| 280 |
randomize_seed (bool, optional): Whether to use a random seed instead of the provided seed.
|
| 281 |
Defaults to False.
|
| 282 |
progress (gr.Progress, optional): Gradio progress tracker. Defaults to gr.Progress(track_tqdm=True).
|
|
|
|
| 283 |
Returns:
|
| 284 |
tuple: A tuple containing:
|
| 285 |
- video_path (str): Path to the generated video file (.mp4)
|
| 286 |
- current_seed (int): The seed used for generation (useful when randomize_seed=True)
|
|
|
|
| 287 |
Raises:
|
| 288 |
gr.Error: If input_image is None (no image uploaded).
|
|
|
|
| 289 |
Note:
|
| 290 |
- Frame count is calculated as duration_seconds * FIXED_FPS (24)
|
| 291 |
- Output dimensions are adjusted to be multiples of MOD_VALUE (32)
|
|
|
|
| 294 |
"""
|
| 295 |
if input_image is None:
|
| 296 |
raise gr.Error("Please upload an input image.")
|
| 297 |
+
|
| 298 |
num_frames = get_num_frames(duration_seconds)
|
| 299 |
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 300 |
resized_image = resize_image(input_image)
|
| 301 |
+
print("pompt is")
|
| 302 |
+
print(prompt)
|
| 303 |
+
if "child" in prompt.lower():
|
| 304 |
+
print("Found 'child' in prompt. Exiting loop.")
|
| 305 |
+
return
|
| 306 |
|
| 307 |
output_frames_list = pipe(
|
| 308 |
image=resized_image,
|
|
|
|
| 351 |
# Команда ffmpeg для создания гауссова размытия
|
| 352 |
cmd_blur = [
|
| 353 |
'ffmpeg',
|
| 354 |
+
'-i', video_with_audio_path,
|
| 355 |
+
'-vf', 'gblur=sigma=25',
|
| 356 |
+
'-c:a', 'copy',
|
| 357 |
'-y',
|
| 358 |
blurred_video_path
|
| 359 |
]
|
|
|
|
| 374 |
print("FFmpeg not available, returning video without audio")
|
| 375 |
return video_path, video_path, current_seed
|
| 376 |
|
|
|
|
| 377 |
with gr.Blocks() as demo:
|
| 378 |
+
gr.Markdown("# Wan22 AOT")
|
| 379 |
+
#gr.Markdown("run Wan 2.2 in just 4-8 steps, with [Lightning LoRA](https://huggingface.co/Kijai/WanVideo_comfy/tree/main/Wan22-Lightning), fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU⚡️")
|
| 380 |
with gr.Row():
|
| 381 |
with gr.Column():
|
| 382 |
input_image_component = gr.Image(type="pil", label="Input Image")
|
| 383 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 384 |
duration_seconds_input = gr.Slider(minimum=MIN_DURATION, maximum=MAX_DURATION, step=0.1, value=3.5, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
|
| 385 |
+
|
| 386 |
with gr.Accordion("Advanced Settings", open=False):
|
| 387 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 388 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
| 389 |
randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
|
| 390 |
+
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=6, label="Inference Steps")
|
| 391 |
guidance_scale_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale - high noise stage")
|
| 392 |
guidance_scale_2_input = gr.Slider(minimum=0.0, maximum=10.0, step=0.5, value=1, label="Guidance Scale 2 - low noise stage")
|
| 393 |
|
|
|
|
| 395 |
with gr.Column():
|
| 396 |
video_output_1 = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 397 |
video_output_2 = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 398 |
+
|
| 399 |
+
#upload_image_and_prompt(input_image_component, prompt_input)
|
| 400 |
ui_inputs = [
|
| 401 |
input_image_component, prompt_input, steps_slider,
|
| 402 |
negative_prompt_input, duration_seconds_input,
|
| 403 |
guidance_scale_input, guidance_scale_2_input, seed_input, randomize_seed_checkbox
|
| 404 |
]
|
| 405 |
+
|
| 406 |
+
generate_button.click(fn=generate_video_with_upload, inputs=ui_inputs, outputs=[video_output_1, video_output_2, seed_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
|
| 408 |
def check_ffmpeg():
|
| 409 |
try:
|
|
|
|
| 413 |
return False
|
| 414 |
|
| 415 |
if __name__ == "__main__":
|
| 416 |
+
demo.queue().launch(ssl_verify=False)
|
|
|