ovi054's picture
Update app2.py
8f73625 verified
import torch
from diffusers import AutoModel, UniPCMultistepScheduler, FlowMatchEulerDiscreteScheduler, DDIMScheduler, DPMSolverMultistepScheduler
from diffusers import WanPipeline, AutoencoderKLWan # Use Wan-specific VAE
# from diffusers.hooks import apply_first_block_cache, FirstBlockCacheConfig
from para_attn.first_block_cache.diffusers_adapters import apply_cache_on_pipe
from diffusers.models import UNetSpatioTemporalConditionModel
from transformers import T5EncoderModel, T5Tokenizer
from huggingface_hub import hf_hub_download
from PIL import Image
import numpy as np
import gradio as gr
device = "cuda" if torch.cuda.is_available() else "cpu"
model_id = "Wan-AI/Wan2.2-T2V-A14B-Diffusers"
# model_id_21 = "Wan-AI/Wan2.1-T2V-14B-Diffusers"
vae = AutoencoderKLWan.from_pretrained(model_id, subfolder="vae", torch_dtype=torch.float32)
transformer = AutoModel.from_pretrained(model_id, subfolder="transformer_2", torch_dtype=torch.bfloat16)
pipe = WanPipeline.from_pretrained(model_id, vae=vae, transformer=transformer, torch_dtype=torch.bfloat16)
flow_shift = 8.0 #5.0 1.0 for image, 5.0 for 720P, 3.0 for 480P
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=flow_shift)
pipe.load_lora_weights(
"lightx2v/Wan2.2-Lightning",
weight_name="Wan2.2-T2V-A14B-4steps-lora-250928/low_noise_model.safetensors"
)
pipe.fuse_lora()
pipe.to(device)
# print("Initialization complete. Gradio is starting...")
def generate(prompt, negative_prompt, width=1024, height=1024, num_inference_steps=30, lora_id=None, progress=gr.Progress(track_tqdm=True)):
if lora_id and lora_id.strip() != "":
pipe.unload_lora_weights()
pipe.load_lora_weights(lora_id.strip())
#pipe.to("cuda")
# apply_first_block_cache(pipe.transformer, FirstBlockCacheConfig(threshold=0.2))
apply_cache_on_pipe(
pipe,
# residual_diff_threshold=0.2,
)
try:
output = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
height=height,
width=width,
num_frames=1,
num_inference_steps=num_inference_steps,
guidance_scale=1.0, #5.0
)
image = output.frames[0][0]
image = (image * 255).astype(np.uint8)
return Image.fromarray(image)
finally:
pipe.unload_lora_weights()
# pass
iface = gr.Interface(
fn=generate,
inputs=[
gr.Textbox(label="Input prompt"),
],
additional_inputs = [
gr.Textbox(label="Negative prompt", value = "Bright tones, overexposed, static, blurred details, subtitles, style, works, paintings, images, static, overall gray, worst quality, low quality, JPEG compression residue, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn faces, deformed, disfigured, misshapen limbs, fused fingers, still picture, messy background, three legs, many people in the background, walking backwards"),
gr.Slider(label="Width", minimum=480, maximum=2048, step=16, value=1024),
gr.Slider(label="Height", minimum=480, maximum=2048, step=16, value=1024),
gr.Slider(minimum=1, maximum=80, step=1, label="Inference Steps", value=8),
gr.Textbox(label="LoRA ID"),
],
outputs=gr.Image(label="output"),
)
iface.launch(debug=True, share=True)