Spaces:
Runtime error
Runtime error
Update app_lora.py
Browse files- app_lora.py +85 -126
app_lora.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import spaces
|
| 2 |
import torch
|
| 3 |
from diffusers import AutoencoderKLWan, WanImageToVideoPipeline, UniPCMultistepScheduler
|
| 4 |
from diffusers.utils import export_to_video
|
|
@@ -17,41 +16,48 @@ import warnings
|
|
| 17 |
warnings.filterwarnings("ignore", message=".*Attempting to use legacy OpenCV backend.*")
|
| 18 |
warnings.filterwarnings("ignore", message=".*num_frames - 1.*")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
MODEL_ID = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
|
| 21 |
|
| 22 |
LORA_REPO_ID = "vrgamedevgirl84/Wan14BT2VFusioniX"
|
| 23 |
LORA_FILENAME = "FusionX_LoRa/Wan2.1_I2V_14B_FusionX_LoRA.safetensors"
|
| 24 |
|
| 25 |
# Initialize models with proper dtype handling
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
pipe
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
pipe.
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
MOD_VALUE = 32
|
| 52 |
DEFAULT_H_SLIDER_VALUE = 640
|
| 53 |
DEFAULT_W_SLIDER_VALUE = 1024
|
| 54 |
-
NEW_FORMULA_MAX_AREA = 640.0 * 1024.0
|
| 55 |
|
| 56 |
SLIDER_MIN_H, SLIDER_MAX_H = 128, 1024
|
| 57 |
SLIDER_MIN_W, SLIDER_MAX_W = 128, 1024
|
|
@@ -74,16 +80,16 @@ def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
|
|
| 74 |
return default_h, default_w
|
| 75 |
|
| 76 |
aspect_ratio = orig_h / orig_w
|
| 77 |
-
|
| 78 |
calc_h = round(np.sqrt(calculation_max_area * aspect_ratio))
|
| 79 |
calc_w = round(np.sqrt(calculation_max_area / aspect_ratio))
|
| 80 |
|
| 81 |
calc_h = max(mod_val, (calc_h // mod_val) * mod_val)
|
| 82 |
calc_w = max(mod_val, (calc_w // mod_val) * mod_val)
|
| 83 |
-
|
| 84 |
new_h = int(np.clip(calc_h, min_slider_h, (max_slider_h // mod_val) * mod_val))
|
| 85 |
new_w = int(np.clip(calc_w, min_slider_w, (max_slider_w // mod_val) * mod_val))
|
| 86 |
-
|
| 87 |
return new_h, new_w
|
| 88 |
|
| 89 |
def handle_image_upload_for_dims_wan(uploaded_pil_image, current_h_val, current_w_val):
|
|
@@ -100,89 +106,62 @@ def handle_image_upload_for_dims_wan(uploaded_pil_image, current_h_val, current_
|
|
| 100 |
gr.Warning("Error attempting to calculate new dimensions")
|
| 101 |
return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
|
| 102 |
|
| 103 |
-
def get_duration(input_image, prompt, height, width,
|
| 104 |
-
negative_prompt, duration_seconds,
|
| 105 |
-
guidance_scale, steps,
|
| 106 |
-
seed, randomize_seed,
|
| 107 |
-
progress):
|
| 108 |
-
# Adjust timeout based on video length and complexity
|
| 109 |
-
if duration_seconds > 7:
|
| 110 |
-
return 180 # 3 minutes for very long videos
|
| 111 |
-
elif duration_seconds > 5:
|
| 112 |
-
return 120 # 2 minutes for long videos
|
| 113 |
-
elif duration_seconds > 3:
|
| 114 |
-
return 90 # 1.5 minutes for medium videos
|
| 115 |
-
else:
|
| 116 |
-
return 60 # 1 minute for short videos
|
| 117 |
|
| 118 |
def export_video_with_ffmpeg(frames, output_path, fps=24):
|
| 119 |
"""Export video using imageio if available, otherwise fall back to OpenCV"""
|
| 120 |
try:
|
| 121 |
import imageio
|
| 122 |
-
|
| 123 |
-
writer = imageio.get_writer(output_path, fps=fps, codec='libx264',
|
| 124 |
pixelformat='yuv420p', quality=8)
|
| 125 |
for frame in frames:
|
| 126 |
writer.append_data(np.array(frame))
|
| 127 |
writer.close()
|
| 128 |
return True
|
| 129 |
except ImportError:
|
| 130 |
-
# Fall back to OpenCV
|
| 131 |
export_to_video(frames, output_path, fps=fps)
|
| 132 |
return False
|
| 133 |
|
| 134 |
-
|
| 135 |
-
def generate_video(input_image, prompt, height, width,
|
| 136 |
negative_prompt=default_negative_prompt, duration_seconds=2,
|
| 137 |
guidance_scale=1, steps=4,
|
| 138 |
-
seed=42, randomize_seed=False,
|
| 139 |
progress=gr.Progress(track_tqdm=True)):
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
if input_image is None:
|
| 142 |
raise gr.Error("Please upload an input image.")
|
| 143 |
|
| 144 |
target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
|
| 145 |
target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
|
| 146 |
-
|
| 147 |
-
# Calculate frames with proper alignment
|
| 148 |
raw_frames = int(round(duration_seconds * FIXED_FPS))
|
| 149 |
-
# Ensure num_frames-1 is divisible by 4 as required by the model
|
| 150 |
num_frames = ((raw_frames - 1) // 4) * 4 + 1
|
| 151 |
num_frames = np.clip(num_frames, MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
| 152 |
-
|
| 153 |
-
# Additional check for very long videos
|
| 154 |
if num_frames > 120:
|
| 155 |
-
# For videos longer than 5 seconds, reduce resolution to manage memory
|
| 156 |
max_dim = max(target_h, target_w)
|
| 157 |
if max_dim > 768:
|
| 158 |
scale_factor = 768 / max_dim
|
| 159 |
target_h = max(MOD_VALUE, (int(target_h * scale_factor) // MOD_VALUE) * MOD_VALUE)
|
| 160 |
target_w = max(MOD_VALUE, (int(target_w * scale_factor) // MOD_VALUE) * MOD_VALUE)
|
| 161 |
gr.Info(f"Reduced resolution to {target_w}x{target_h} for long video generation")
|
| 162 |
-
|
| 163 |
print(f"Generating {num_frames} frames (requested {raw_frames}) at {target_w}x{target_h}")
|
| 164 |
-
|
| 165 |
-
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 166 |
|
|
|
|
| 167 |
resized_image = input_image.resize((target_w, target_h), Image.Resampling.LANCZOS)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
if torch.cuda.is_available():
|
| 171 |
-
torch.cuda.empty_cache()
|
| 172 |
|
| 173 |
try:
|
| 174 |
with torch.inference_mode():
|
| 175 |
-
# Generate video with autocast for memory efficiency
|
| 176 |
with torch.autocast("cuda", dtype=torch.float16):
|
| 177 |
output_frames_list = pipe(
|
| 178 |
-
image=resized_image,
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
height=target_h,
|
| 182 |
-
width=target_w,
|
| 183 |
-
num_frames=num_frames,
|
| 184 |
-
guidance_scale=float(guidance_scale),
|
| 185 |
-
num_inference_steps=int(steps),
|
| 186 |
generator=torch.Generator(device="cuda").manual_seed(current_seed),
|
| 187 |
return_dict=True
|
| 188 |
).frames[0]
|
|
@@ -193,47 +172,27 @@ def generate_video(input_image, prompt, height, width,
|
|
| 193 |
torch.cuda.empty_cache()
|
| 194 |
raise gr.Error(f"Generation failed: {str(e)}")
|
| 195 |
|
| 196 |
-
|
| 197 |
-
if torch.cuda.is_available():
|
| 198 |
-
torch.cuda.empty_cache()
|
| 199 |
|
| 200 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 201 |
video_path = tmpfile.name
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
used_imageio = export_video_with_ffmpeg(output_frames_list, video_path, fps=FIXED_FPS)
|
| 205 |
-
|
| 206 |
-
# Only try FFmpeg optimization if we have a valid video file
|
| 207 |
if os.path.exists(video_path) and os.path.getsize(video_path) > 0:
|
| 208 |
try:
|
| 209 |
-
# Check if ffmpeg is available
|
| 210 |
subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
|
| 211 |
-
|
| 212 |
optimized_path = video_path + "_opt.mp4"
|
| 213 |
cmd = [
|
| 214 |
-
'ffmpeg',
|
| 215 |
-
'-
|
| 216 |
-
'-
|
| 217 |
-
'-c:v', 'libx264', # Codec
|
| 218 |
-
'-pix_fmt', 'yuv420p', # Pixel format
|
| 219 |
-
'-profile:v', 'main', # Compatibility profile
|
| 220 |
-
'-level', '4.0', # Support for higher resolutions
|
| 221 |
-
'-movflags', '+faststart', # Streaming optimized
|
| 222 |
-
'-crf', '23', # Quality level
|
| 223 |
-
'-preset', 'medium', # Balance between speed and compression
|
| 224 |
-
'-maxrate', '10M', # Max bitrate for large videos
|
| 225 |
-
'-bufsize', '20M', # Buffer size
|
| 226 |
-
optimized_path
|
| 227 |
]
|
| 228 |
-
|
| 229 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
| 230 |
-
|
| 231 |
if result.returncode == 0 and os.path.exists(optimized_path) and os.path.getsize(optimized_path) > 0:
|
| 232 |
-
os.unlink(video_path)
|
| 233 |
video_path = optimized_path
|
| 234 |
else:
|
| 235 |
print(f"FFmpeg optimization failed: {result.stderr}")
|
| 236 |
-
|
| 237 |
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 238 |
print("FFmpeg not available or optimization failed, using original export")
|
| 239 |
|
|
@@ -243,19 +202,17 @@ def generate_video(input_image, prompt, height, width,
|
|
| 243 |
with gr.Blocks() as demo:
|
| 244 |
gr.Markdown("# Fast 4 steps Wan 2.1 I2V (14B) FusionX-LoRA")
|
| 245 |
gr.Markdown("Generate videos up to 10 seconds long! Longer videos may use reduced resolution for stability.")
|
| 246 |
-
|
| 247 |
with gr.Row():
|
| 248 |
with gr.Column():
|
| 249 |
input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)")
|
| 250 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 251 |
duration_seconds_input = gr.Slider(
|
| 252 |
-
minimum=round(MIN_FRAMES_MODEL/FIXED_FPS, 1),
|
| 253 |
-
maximum=round(MAX_FRAMES_MODEL/FIXED_FPS, 1),
|
| 254 |
-
step=0.1,
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
info=f"Video length: {MIN_FRAMES_MODEL/FIXED_FPS:.1f}-{MAX_FRAMES_MODEL/FIXED_FPS:.1f}s. Longer videos may take more time and use more memory."
|
| 258 |
-
)
|
| 259 |
with gr.Accordion("Advanced Settings", open=False):
|
| 260 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 261 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
|
@@ -263,46 +220,48 @@ with gr.Blocks() as demo:
|
|
| 263 |
with gr.Row():
|
| 264 |
height_input = gr.Slider(minimum=SLIDER_MIN_H, maximum=SLIDER_MAX_H, step=MOD_VALUE, value=DEFAULT_H_SLIDER_VALUE, label=f"Output Height (multiple of {MOD_VALUE})")
|
| 265 |
width_input = gr.Slider(minimum=SLIDER_MIN_W, maximum=SLIDER_MAX_W, step=MOD_VALUE, value=DEFAULT_W_SLIDER_VALUE, label=f"Output Width (multiple of {MOD_VALUE})")
|
| 266 |
-
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
|
| 267 |
guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
|
| 268 |
|
| 269 |
generate_button = gr.Button("Generate Video", variant="primary")
|
| 270 |
with gr.Column():
|
| 271 |
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 272 |
-
gr.Markdown("### Tips for best results
|
| 273 |
-
gr.Markdown("- For videos longer than 5 seconds, consider using lower resolutions (512-768px)")
|
| 274 |
-
gr.Markdown("- Clear, simple prompts often work better than complex descriptions")
|
| 275 |
-
gr.Markdown("- The model works best with 4-8 inference steps")
|
| 276 |
|
| 277 |
input_image_component.upload(
|
| 278 |
fn=handle_image_upload_for_dims_wan,
|
| 279 |
inputs=[input_image_component, height_input, width_input],
|
| 280 |
outputs=[height_input, width_input]
|
| 281 |
)
|
| 282 |
-
|
| 283 |
-
input_image_component.clear(
|
| 284 |
fn=handle_image_upload_for_dims_wan,
|
| 285 |
inputs=[input_image_component, height_input, width_input],
|
| 286 |
outputs=[height_input, width_input]
|
| 287 |
)
|
| 288 |
-
|
| 289 |
ui_inputs = [
|
| 290 |
input_image_component, prompt_input, height_input, width_input,
|
| 291 |
negative_prompt_input, duration_seconds_input,
|
| 292 |
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 293 |
]
|
| 294 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
|
|
|
|
|
|
| 306 |
|
| 307 |
if __name__ == "__main__":
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from diffusers import AutoencoderKLWan, WanImageToVideoPipeline, UniPCMultistepScheduler
|
| 3 |
from diffusers.utils import export_to_video
|
|
|
|
| 16 |
warnings.filterwarnings("ignore", message=".*Attempting to use legacy OpenCV backend.*")
|
| 17 |
warnings.filterwarnings("ignore", message=".*num_frames - 1.*")
|
| 18 |
|
| 19 |
+
# This decorator is specific to HuggingFace Spaces and will cause an error in other environments.
|
| 20 |
+
# import spaces
|
| 21 |
+
|
| 22 |
MODEL_ID = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
|
| 23 |
|
| 24 |
LORA_REPO_ID = "vrgamedevgirl84/Wan14BT2VFusioniX"
|
| 25 |
LORA_FILENAME = "FusionX_LoRa/Wan2.1_I2V_14B_FusionX_LoRA.safetensors"
|
| 26 |
|
| 27 |
# Initialize models with proper dtype handling
|
| 28 |
+
# This section requires a GPU and CUDA to be available
|
| 29 |
+
pipe = None
|
| 30 |
+
if torch.cuda.is_available():
|
| 31 |
+
image_encoder = CLIPVisionModel.from_pretrained(MODEL_ID, subfolder="image_encoder", torch_dtype=torch.float16)
|
| 32 |
+
vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float16)
|
| 33 |
+
pipe = WanImageToVideoPipeline.from_pretrained(
|
| 34 |
+
MODEL_ID, vae=vae, image_encoder=image_encoder, torch_dtype=torch.float16
|
| 35 |
+
)
|
| 36 |
+
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=8.0)
|
| 37 |
+
|
| 38 |
+
# Enable memory efficient attention and CPU offloading for large videos
|
| 39 |
+
pipe.enable_model_cpu_offload()
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
causvid_path = hf_hub_download(repo_id=LORA_REPO_ID, filename=LORA_FILENAME)
|
| 43 |
+
print("✅ LoRA downloaded to:", causvid_path)
|
| 44 |
+
|
| 45 |
+
pipe.load_lora_weights(causvid_path, adapter_name="causvid_lora")
|
| 46 |
+
pipe.set_adapters(["causvid_lora"], adapter_weights=[0.75])
|
| 47 |
+
pipe.fuse_lora()
|
| 48 |
+
|
| 49 |
+
except Exception as e:
|
| 50 |
+
import traceback
|
| 51 |
+
print("❌ Error during LoRA loading:")
|
| 52 |
+
traceback.print_exc()
|
| 53 |
+
else:
|
| 54 |
+
print("CUDA is not available. This script requires a GPU to run.")
|
| 55 |
+
|
| 56 |
|
| 57 |
MOD_VALUE = 32
|
| 58 |
DEFAULT_H_SLIDER_VALUE = 640
|
| 59 |
DEFAULT_W_SLIDER_VALUE = 1024
|
| 60 |
+
NEW_FORMULA_MAX_AREA = 640.0 * 1024.0
|
| 61 |
|
| 62 |
SLIDER_MIN_H, SLIDER_MAX_H = 128, 1024
|
| 63 |
SLIDER_MIN_W, SLIDER_MAX_W = 128, 1024
|
|
|
|
| 80 |
return default_h, default_w
|
| 81 |
|
| 82 |
aspect_ratio = orig_h / orig_w
|
| 83 |
+
|
| 84 |
calc_h = round(np.sqrt(calculation_max_area * aspect_ratio))
|
| 85 |
calc_w = round(np.sqrt(calculation_max_area / aspect_ratio))
|
| 86 |
|
| 87 |
calc_h = max(mod_val, (calc_h // mod_val) * mod_val)
|
| 88 |
calc_w = max(mod_val, (calc_w // mod_val) * mod_val)
|
| 89 |
+
|
| 90 |
new_h = int(np.clip(calc_h, min_slider_h, (max_slider_h // mod_val) * mod_val))
|
| 91 |
new_w = int(np.clip(calc_w, min_slider_w, (max_slider_w // mod_val) * mod_val))
|
| 92 |
+
|
| 93 |
return new_h, new_w
|
| 94 |
|
| 95 |
def handle_image_upload_for_dims_wan(uploaded_pil_image, current_h_val, current_w_val):
|
|
|
|
| 106 |
gr.Warning("Error attempting to calculate new dimensions")
|
| 107 |
return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
|
| 108 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
def export_video_with_ffmpeg(frames, output_path, fps=24):
|
| 111 |
"""Export video using imageio if available, otherwise fall back to OpenCV"""
|
| 112 |
try:
|
| 113 |
import imageio
|
| 114 |
+
writer = imageio.get_writer(output_path, fps=fps, codec='libx264',
|
|
|
|
| 115 |
pixelformat='yuv420p', quality=8)
|
| 116 |
for frame in frames:
|
| 117 |
writer.append_data(np.array(frame))
|
| 118 |
writer.close()
|
| 119 |
return True
|
| 120 |
except ImportError:
|
|
|
|
| 121 |
export_to_video(frames, output_path, fps=fps)
|
| 122 |
return False
|
| 123 |
|
| 124 |
+
def generate_video(input_image, prompt, height, width,
|
|
|
|
| 125 |
negative_prompt=default_negative_prompt, duration_seconds=2,
|
| 126 |
guidance_scale=1, steps=4,
|
| 127 |
+
seed=42, randomize_seed=False,
|
| 128 |
progress=gr.Progress(track_tqdm=True)):
|
| 129 |
+
|
| 130 |
+
if pipe is None or not torch.cuda.is_available():
|
| 131 |
+
raise gr.Error("Pipeline not initialized or CUDA not available. Please check the console for errors.")
|
| 132 |
+
|
| 133 |
if input_image is None:
|
| 134 |
raise gr.Error("Please upload an input image.")
|
| 135 |
|
| 136 |
target_h = max(MOD_VALUE, (int(height) // MOD_VALUE) * MOD_VALUE)
|
| 137 |
target_w = max(MOD_VALUE, (int(width) // MOD_VALUE) * MOD_VALUE)
|
| 138 |
+
|
|
|
|
| 139 |
raw_frames = int(round(duration_seconds * FIXED_FPS))
|
|
|
|
| 140 |
num_frames = ((raw_frames - 1) // 4) * 4 + 1
|
| 141 |
num_frames = np.clip(num_frames, MIN_FRAMES_MODEL, MAX_FRAMES_MODEL)
|
| 142 |
+
|
|
|
|
| 143 |
if num_frames > 120:
|
|
|
|
| 144 |
max_dim = max(target_h, target_w)
|
| 145 |
if max_dim > 768:
|
| 146 |
scale_factor = 768 / max_dim
|
| 147 |
target_h = max(MOD_VALUE, (int(target_h * scale_factor) // MOD_VALUE) * MOD_VALUE)
|
| 148 |
target_w = max(MOD_VALUE, (int(target_w * scale_factor) // MOD_VALUE) * MOD_VALUE)
|
| 149 |
gr.Info(f"Reduced resolution to {target_w}x{target_h} for long video generation")
|
| 150 |
+
|
| 151 |
print(f"Generating {num_frames} frames (requested {raw_frames}) at {target_w}x{target_h}")
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
current_seed = random.randint(0, MAX_SEED) if randomize_seed else int(seed)
|
| 154 |
resized_image = input_image.resize((target_w, target_h), Image.Resampling.LANCZOS)
|
| 155 |
|
| 156 |
+
torch.cuda.empty_cache()
|
|
|
|
|
|
|
| 157 |
|
| 158 |
try:
|
| 159 |
with torch.inference_mode():
|
|
|
|
| 160 |
with torch.autocast("cuda", dtype=torch.float16):
|
| 161 |
output_frames_list = pipe(
|
| 162 |
+
image=resized_image, prompt=prompt, negative_prompt=negative_prompt,
|
| 163 |
+
height=target_h, width=target_w, num_frames=num_frames,
|
| 164 |
+
guidance_scale=float(guidance_scale), num_inference_steps=int(steps),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
generator=torch.Generator(device="cuda").manual_seed(current_seed),
|
| 166 |
return_dict=True
|
| 167 |
).frames[0]
|
|
|
|
| 172 |
torch.cuda.empty_cache()
|
| 173 |
raise gr.Error(f"Generation failed: {str(e)}")
|
| 174 |
|
| 175 |
+
torch.cuda.empty_cache()
|
|
|
|
|
|
|
| 176 |
|
| 177 |
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
|
| 178 |
video_path = tmpfile.name
|
| 179 |
+
export_video_with_ffmpeg(output_frames_list, video_path, fps=FIXED_FPS)
|
| 180 |
+
|
|
|
|
|
|
|
|
|
|
| 181 |
if os.path.exists(video_path) and os.path.getsize(video_path) > 0:
|
| 182 |
try:
|
|
|
|
| 183 |
subprocess.run(['ffmpeg', '-version'], capture_output=True, check=True)
|
|
|
|
| 184 |
optimized_path = video_path + "_opt.mp4"
|
| 185 |
cmd = [
|
| 186 |
+
'ffmpeg', '-y', '-i', video_path, '-c:v', 'libx264', '-pix_fmt', 'yuv420p',
|
| 187 |
+
'-profile:v', 'main', '-level', '4.0', '-movflags', '+faststart', '-crf', '23',
|
| 188 |
+
'-preset', 'medium', '-maxrate', '10M', '-bufsize', '20M', optimized_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
]
|
|
|
|
| 190 |
result = subprocess.run(cmd, capture_output=True, text=True)
|
|
|
|
| 191 |
if result.returncode == 0 and os.path.exists(optimized_path) and os.path.getsize(optimized_path) > 0:
|
| 192 |
+
os.unlink(video_path)
|
| 193 |
video_path = optimized_path
|
| 194 |
else:
|
| 195 |
print(f"FFmpeg optimization failed: {result.stderr}")
|
|
|
|
| 196 |
except (subprocess.CalledProcessError, FileNotFoundError):
|
| 197 |
print("FFmpeg not available or optimization failed, using original export")
|
| 198 |
|
|
|
|
| 202 |
with gr.Blocks() as demo:
|
| 203 |
gr.Markdown("# Fast 4 steps Wan 2.1 I2V (14B) FusionX-LoRA")
|
| 204 |
gr.Markdown("Generate videos up to 10 seconds long! Longer videos may use reduced resolution for stability.")
|
| 205 |
+
|
| 206 |
with gr.Row():
|
| 207 |
with gr.Column():
|
| 208 |
input_image_component = gr.Image(type="pil", label="Input Image (auto-resized to target H/W)")
|
| 209 |
prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
|
| 210 |
duration_seconds_input = gr.Slider(
|
| 211 |
+
minimum=round(MIN_FRAMES_MODEL/FIXED_FPS, 1),
|
| 212 |
+
maximum=round(MAX_FRAMES_MODEL/FIXED_FPS, 1),
|
| 213 |
+
step=0.1, value=2, label="Duration (seconds)",
|
| 214 |
+
info=f"Video length: {MIN_FRAMES_MODEL/FIXED_FPS:.1f}-{MAX_FRAMES_MODEL/FIXED_FPS:.1f}s."
|
| 215 |
+
)
|
|
|
|
|
|
|
| 216 |
with gr.Accordion("Advanced Settings", open=False):
|
| 217 |
negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
|
| 218 |
seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
|
|
|
|
| 220 |
with gr.Row():
|
| 221 |
height_input = gr.Slider(minimum=SLIDER_MIN_H, maximum=SLIDER_MAX_H, step=MOD_VALUE, value=DEFAULT_H_SLIDER_VALUE, label=f"Output Height (multiple of {MOD_VALUE})")
|
| 222 |
width_input = gr.Slider(minimum=SLIDER_MIN_W, maximum=SLIDER_MAX_W, step=MOD_VALUE, value=DEFAULT_W_SLIDER_VALUE, label=f"Output Width (multiple of {MOD_VALUE})")
|
| 223 |
+
steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
|
| 224 |
guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
|
| 225 |
|
| 226 |
generate_button = gr.Button("Generate Video", variant="primary")
|
| 227 |
with gr.Column():
|
| 228 |
video_output = gr.Video(label="Generated Video", autoplay=True, interactive=False)
|
| 229 |
+
gr.Markdown("### Tips for best results:\n- For videos longer than 5 seconds, consider using lower resolutions (512-768px)\n- Clear, simple prompts often work better than complex descriptions\n- The model works best with 4-8 inference steps")
|
|
|
|
|
|
|
|
|
|
| 230 |
|
| 231 |
input_image_component.upload(
|
| 232 |
fn=handle_image_upload_for_dims_wan,
|
| 233 |
inputs=[input_image_component, height_input, width_input],
|
| 234 |
outputs=[height_input, width_input]
|
| 235 |
)
|
| 236 |
+
input_image_component.clear(
|
|
|
|
| 237 |
fn=handle_image_upload_for_dims_wan,
|
| 238 |
inputs=[input_image_component, height_input, width_input],
|
| 239 |
outputs=[height_input, width_input]
|
| 240 |
)
|
| 241 |
+
|
| 242 |
ui_inputs = [
|
| 243 |
input_image_component, prompt_input, height_input, width_input,
|
| 244 |
negative_prompt_input, duration_seconds_input,
|
| 245 |
guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
|
| 246 |
]
|
| 247 |
generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
|
| 248 |
+
|
| 249 |
+
# The example images 'peng.png' and 'forg.jpg' are not present in this environment,
|
| 250 |
+
# so the gr.Examples component is commented out to prevent errors.
|
| 251 |
+
# gr.Examples(
|
| 252 |
+
# examples=[
|
| 253 |
+
# ["path/to/your/peng.png", "a penguin playfully dancing in the snow, Antarctica", 896, 512],
|
| 254 |
+
# ["path/to/your/forg.jpg", "the frog jumps around", 448, 832],
|
| 255 |
+
# ],
|
| 256 |
+
# inputs=[input_image_component, prompt_input, height_input, width_input],
|
| 257 |
+
# outputs=[video_output, seed_input],
|
| 258 |
+
# fn=generate_video,
|
| 259 |
+
# cache_examples="lazy"
|
| 260 |
+
# )
|
| 261 |
|
| 262 |
if __name__ == "__main__":
|
| 263 |
+
if pipe is not None:
|
| 264 |
+
demo.queue(max_size=3).launch()
|
| 265 |
+
else:
|
| 266 |
+
gr.Blocks()._queue_closed = False # A hack to prevent Gradio from hanging
|
| 267 |
+
gr.Info("Application not started because a GPU (CUDA) is required but not found.")
|