Update app.py
Browse files14 unmodified lines151617181920210 unmodified lines23123223323423523623723823918 unmodified lines2582592602612622632642652662672681 unmodified line2702712722732742752762775 unmodified lines28328428528628728870 unmodified lines3593603613623633643651 unmodified line367368369370371372373374375376185 unmodified lines56256356456556656756825 unmodified lines59459559659759859960060114 unmodified linesimport cv2import numpy as npimport torchfrom torch.nn import functional as Ffrom PIL import Image210 unmodified lines# WANMODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"CACHE_DIR = os.path.expanduser("~/.cache/huggingface/")MAX_DIM = 832MIN_DIM = 480SQUARE_DIM = 64018 unmodified lines}pipe = WanImageToVideoPipeline.from_pretrained("TestOrganizationPleaseIgnore/WAMU_v1_WAN2.2_I2V_LIGHTNING",torch_dtype=torch.bfloat16,).to('cuda')original_scheduler = copy.deepcopy(pipe.scheduler)if os.path.exists(CACHE_DIR):shutil.rmtree(CACHE_DIR)print("Deleted Hugging Face cache.")1 unmodified lineprint("No hub cache found.")quantize_(pipe.text_encoder, Int8WeightOnlyConfig())quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig())quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig())aoti.aoti_blocks_load(pipe.transformer, 'zerogpu-aoti/Wan2', variant='fp8da')aoti.aoti_blocks_load(pipe.transformer_2, 'zerogpu-aoti/Wan2', variant='fp8da')5 unmodified linesdefault_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"def resize_image(image: Image.Image) -> Image.Image:width, height = image.sizeif width == height:70 unmodified linesfactor = num_frames * width * height / BASE_FRAMES_HEIGHT_WIDTHstep_duration = BASE_STEP_DURATION * factor ** 1.5gen_time = int(steps) * step_durationprint(gen_time)if guidance_scale > 1:gen_time = gen_time * 1.81 unmodified lineif frame_factor > 1:total_out_frames = (num_frames * frame_factor) - num_framesinter_time = (total_out_frames * 0.02)print(inter_time)gen_time += inter_timeprint("Time GPU", gen_time + 10)return 10 + gen_time185 unmodified lineswith gr.Blocks(delete_cache=(3600, 10800)) as demo:gr.Markdown("## WAMU - Wan 2.2 I2V (14B) 🐢")gr.Markdown("#### ℹ️ **A Note on Performance:** This version prioritizes a straightforward setup over maximum speed, so performance may vary.")gr.Markdown("Run Wan 2.2 in just 4-8 steps, fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU")25 unmodified lines)flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift")play_result_video = gr.Checkbox(label="Display result", value=True, interactive=True)org_name = "TestOrganizationPleaseIgnore"gr.Markdown(f"[ZeroGPU help, tips and troubleshooting](https://huggingface.co/datasets/{org_name}/help/blob/main/gpu_help.md)")generate_button = gr.Button("Generate Video", variant="primary")14 unmodified lines1516171819202122210 unmodified lines23323423523623723823924024124224324424524624724824925025125225325425525625725825926026126218 unmodified lines2812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223231 unmodified line3253263273283293303313323333343355 unmodified lines34134234334434534634734834935035135270 unmodified lines4234244254264274284291 unmodified line431432433434435436437438185 unmodified lines62462562662762862963025 unmodified lines65665765865966066166266366466566614 unmodified linesimport cv2import numpy as npimport torchimport torch._dynamofrom huggingface_hub import list_modelsfrom torch.nn import functional as Ffrom PIL import Image210 unmodified lines# WANORG_NAME = "TestOrganizationPleaseIgnore"# MODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"MODEL_ID = os.getenv("REPO_ID") or random.choice(list(list_models(author=ORG_NAME, filter='diffusers:WanImageToVideoPipeline'))).modelIdCACHE_DIR = os.path.expanduser("~/.cache/huggingface/")LORA_MODELS = [# {# "repo_id": "exampleuser/example_lora_1",# "high_tr": "example_lora_1_high.safetensors",# "low_tr": "example_lora_1_low.safetensors",# "high_scale": 0.5,# "low_scale": 0.5# },# {# "repo_id": "exampleuser/example_lora_2",# "high_tr": "subfolder/example_lora_2_high.safetensors",# "low_tr": "subfolder/example_lora_2_low.safetensors",# "high_scale": 0.4,# "low_scale": 0.4# },]MAX_DIM = 832MIN_DIM = 480SQUARE_DIM = 64018 unmodified lines}pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,torch_dtype=torch.bfloat16,).to('cuda')original_scheduler = copy.deepcopy(pipe.scheduler)for i, lora in enumerate(LORA_MODELS):name_high_tr = lora["high_tr"].split(".")[0].split("/")[-1] + "Hh"name_low_tr = lora["low_tr"].split(".")[0].split("/")[-1] + "Ll"try:pipe.load_lora_weights(lora["repo_id"],weight_name=lora["high_tr"],adapter_name=name_high_tr)kwargs_lora = {"load_into_transformer_2": True}pipe.load_lora_weights(lora["repo_id"],weight_name=lora["low_tr"],adapter_name=name_low_tr,**kwargs_lora)pipe.set_adapters([name_high_tr, name_low_tr], adapter_weights=[1.0, 1.0])pipe.fuse_lora(adapter_names=[name_high_tr], lora_scale=lora["high_scale"], components=["transformer"])pipe.fuse_lora(adapter_names=[name_low_tr], lora_scale=lora["low_scale"], components=["transformer_2"])pipe.unload_lora_weights()print(f"Applied: {lora['high_tr']}, hs={lora['high_scale']}/ls={lora['low_scale']}, {i+1}/{len(LORA_MODELS)}")except Exception as e:print("Error:", str(e))print("Failed LoRA:", name_high_tr)pipe.unload_lora_weights()if os.path.exists(CACHE_DIR):shutil.rmtree(CACHE_DIR)print("Deleted Hugging Face cache.")1 unmodified lineprint("No hub cache found.")quantize_(pipe.text_encoder, Int8WeightOnlyConfig())torch._dynamo.reset()quantize_(pipe.transformer, Float8DynamicActivationFloat8WeightConfig())torch._dynamo.reset()quantize_(pipe.transformer_2, Float8DynamicActivationFloat8WeightConfig())torch._dynamo.reset()aoti.aoti_blocks_load(pipe.transformer, 'zerogpu-aoti/Wan2', variant='fp8da')aoti.aoti_blocks_load(pipe.transformer_2, 'zerogpu-aoti/Wan2', variant='fp8da')5 unmodified linesdefault_negative_prompt = "色调艳丽, 过曝, 静态, 细节模糊不清, 字幕, 风格, 作品, 画作, 画面, 静止, 整体发灰, 最差质量, 低质量, JPEG压缩残留, 丑陋的, 残缺的, 多余的手指, 画得不好的手部, 画得不好的脸部, 畸形的, 毁容的, 形态畸形的肢体, 手指融合, 静止不动的画面, 杂乱的背景, 三条腿, 背景人很多, 倒着走"def model_title():repo_name = MODEL_ID.split('/')[-1].replace("_", " ")url = f"https://huggingface.co/{MODEL_ID}"return f"## This space is currently running [{repo_name}]({url}) 🐢"def resize_image(image: Image.Image) -> Image.Image:width, height = image.sizeif width == height:70 unmodified linesfactor = num_frames * width * height / BASE_FRAMES_HEIGHT_WIDTHstep_duration = BASE_STEP_DURATION * factor ** 1.5gen_time = int(steps) * step_durationif guidance_scale > 1:gen_time = gen_time * 1.81 unmodified lineif frame_factor > 1:total_out_frames = (num_frames * frame_factor) - num_framesinter_time = (total_out_frames * 0.02)gen_time += inter_timereturn 10 + gen_time185 unmodified lineswith gr.Blocks(delete_cache=(3600, 10800)) as demo:gr.Markdown(model_title())gr.Markdown("#### ℹ️ **A Note on Performance:** This version prioritizes a straightforward setup over maximum speed, so performance may vary.")gr.Markdown("Run Wan 2.2 in just 4-8 steps, fp8 quantization & AoT compilation - compatible with 🧨 diffusers and ZeroGPU")25 unmodified lines)flow_shift_slider = gr.Slider(minimum=0.5, maximum=15.0, step=0.1, value=3.0, label="Flow Shift")play_result_video = gr.Checkbox(label="Display result", value=True, interactive=True)gr.Markdown(f"[ZeroGPU help, tips and troubleshooting](https://huggingface.co/datasets/{ORG_NAME}/help/blob/main/gpu_help.md)")gr.Markdown( # TestOrganizationPleaseIgnore/wamu-tools"To use a different model, **duplicate this Space** first, then change the `REPO_ID` environment variable. ""[See compatible models here](https://huggingface.co/models?other=diffusers:WanImageToVideoPipeline&sort=trending&search=WAN2.2_I2V_LIGHTNING).")generate_button = gr.Button("Generate Video", variant="primary")
app.py+73-8