Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
|
|
| 1 |
import torch
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from diffusers.utils import export_to_video
|
| 4 |
from transformers import CLIPVisionModel
|
| 5 |
import gradio as gr
|
|
@@ -19,24 +24,22 @@ import boto3
|
|
| 19 |
from io import BytesIO
|
| 20 |
from diffusers.utils import load_image
|
| 21 |
|
|
|
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
MODEL_ID = "Wan-AI/Wan2.1-I2V-14B-480P-Diffusers"
|
| 25 |
-
LORA_REPO_ID = "Kijai/WanVideo_comfy"
|
| 26 |
-
LORA_FILENAME = "Wan21_CausVid_14B_T2V_lora_rank32.safetensors"
|
| 27 |
-
|
| 28 |
-
image_encoder = CLIPVisionModel.from_pretrained(MODEL_ID, subfolder="image_encoder", torch_dtype=torch.float32)
|
| 29 |
-
vae = AutoencoderKLWan.from_pretrained(MODEL_ID, subfolder="vae", torch_dtype=torch.float32)
|
| 30 |
-
pipe = WanImageToVideoPipeline.from_pretrained(
|
| 31 |
-
MODEL_ID, vae=vae, image_encoder=image_encoder, torch_dtype=torch.bfloat16
|
| 32 |
-
)
|
| 33 |
-
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config, flow_shift=8.0)
|
| 34 |
-
pipe.to("cuda")
|
| 35 |
-
|
| 36 |
-
causvid_path = hf_hub_download(repo_id=LORA_REPO_ID, filename=LORA_FILENAME)
|
| 37 |
-
pipe.load_lora_weights(causvid_path, adapter_name="causvid_lora")
|
| 38 |
-
pipe.set_adapters(["causvid_lora"], adapter_weights=[0.95])
|
| 39 |
-
pipe.fuse_lora()
|
| 40 |
|
| 41 |
MOD_VALUE = 32
|
| 42 |
DEFAULT_H_SLIDER_VALUE = 512
|
|
@@ -117,46 +120,6 @@ def upload_video_to_r2(video_file, account_id, access_key, secret_key, bucket_na
|
|
| 117 |
|
| 118 |
return video_remote_path
|
| 119 |
|
| 120 |
-
def handle_image_upload_for_dims_wan(uploaded_pil_image, current_h_val, current_w_val):
|
| 121 |
-
if uploaded_pil_image is None:
|
| 122 |
-
return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
|
| 123 |
-
try:
|
| 124 |
-
new_h, new_w = _calculate_new_dimensions_wan(
|
| 125 |
-
uploaded_pil_image, MOD_VALUE, NEW_FORMULA_MAX_AREA,
|
| 126 |
-
SLIDER_MIN_H, SLIDER_MAX_H, SLIDER_MIN_W, SLIDER_MAX_W,
|
| 127 |
-
DEFAULT_H_SLIDER_VALUE, DEFAULT_W_SLIDER_VALUE
|
| 128 |
-
)
|
| 129 |
-
return gr.update(value=new_h), gr.update(value=new_w)
|
| 130 |
-
except Exception as e:
|
| 131 |
-
gr.Warning("Error attempting to calculate new dimensions")
|
| 132 |
-
return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
|
| 133 |
-
|
| 134 |
-
def get_duration(
|
| 135 |
-
image_url,
|
| 136 |
-
prompt,
|
| 137 |
-
height,
|
| 138 |
-
width,
|
| 139 |
-
negative_prompt,
|
| 140 |
-
duration_seconds,
|
| 141 |
-
guidance_scale,
|
| 142 |
-
steps,
|
| 143 |
-
seed,
|
| 144 |
-
randomize_seed,
|
| 145 |
-
upload_to_r2,
|
| 146 |
-
account_id,
|
| 147 |
-
access_key,
|
| 148 |
-
secret_key,
|
| 149 |
-
bucket,
|
| 150 |
-
progress=gr.Progress(track_tqdm=True)
|
| 151 |
-
):
|
| 152 |
-
# 保持逻辑不变
|
| 153 |
-
if steps > 4 and duration_seconds > 2:
|
| 154 |
-
return 90
|
| 155 |
-
elif steps > 4 or duration_seconds > 2:
|
| 156 |
-
return 75
|
| 157 |
-
else:
|
| 158 |
-
return 60
|
| 159 |
-
|
| 160 |
|
| 161 |
@spaces.GPU(duration=120)
|
| 162 |
def generate_video(image_url,
|
|
@@ -191,9 +154,14 @@ def generate_video(image_url,
|
|
| 191 |
|
| 192 |
with torch.inference_mode():
|
| 193 |
output_frames_list = pipe(
|
| 194 |
-
image=resized_image,
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
generator=torch.Generator(device="cuda").manual_seed(current_seed)
|
| 198 |
).frames[0]
|
| 199 |
|
|
@@ -209,8 +177,7 @@ def generate_video(image_url,
|
|
| 209 |
|
| 210 |
|
| 211 |
with gr.Blocks() as demo:
|
| 212 |
-
gr.Markdown("#
|
| 213 |
-
gr.Markdown("[CausVid](https://github.com/tianweiy/CausVid) is a distilled version of Wan 2.1 to run faster in just 4-8 steps, [extracted as LoRA by Kijai](https://huggingface.co/Kijai/WanVideo_comfy/blob/main/Wan21_CausVid_14B_T2V_lora_rank32.safetensors) and is compatible with 🧨 diffusers")
|
| 214 |
with gr.Row():
|
| 215 |
with gr.Column():
|
| 216 |
image_url_input = gr.Textbox(
|
|
|
|
| 1 |
+
import os
|
| 2 |
import torch
|
| 3 |
+
|
| 4 |
+
from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
|
| 5 |
+
from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
|
| 6 |
+
from diffusers.utils.export_utils import export_to_video
|
| 7 |
+
|
| 8 |
from diffusers.utils import export_to_video
|
| 9 |
from transformers import CLIPVisionModel
|
| 10 |
import gradio as gr
|
|
|
|
| 24 |
from io import BytesIO
|
| 25 |
from diffusers.utils import load_image
|
| 26 |
|
| 27 |
+
MODEL_ID = "Wan-AI/Wan2.2-I2V-A14B-Diffusers"
|
| 28 |
|
| 29 |
+
pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
|
| 30 |
+
transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
|
| 31 |
+
subfolder='transformer',
|
| 32 |
+
torch_dtype=torch.bfloat16,
|
| 33 |
+
device_map='cuda',
|
| 34 |
+
),
|
| 35 |
+
transformer_2=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
|
| 36 |
+
subfolder='transformer_2',
|
| 37 |
+
torch_dtype=torch.bfloat16,
|
| 38 |
+
device_map='cuda',
|
| 39 |
+
),
|
| 40 |
+
torch_dtype=torch.bfloat16,
|
| 41 |
+
).to('cuda')
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
MOD_VALUE = 32
|
| 45 |
DEFAULT_H_SLIDER_VALUE = 512
|
|
|
|
| 120 |
|
| 121 |
return video_remote_path
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
@spaces.GPU(duration=120)
|
| 125 |
def generate_video(image_url,
|
|
|
|
| 154 |
|
| 155 |
with torch.inference_mode():
|
| 156 |
output_frames_list = pipe(
|
| 157 |
+
image=resized_image,
|
| 158 |
+
prompt=prompt,
|
| 159 |
+
negative_prompt=negative_prompt,
|
| 160 |
+
height=target_h,
|
| 161 |
+
width=target_w,
|
| 162 |
+
num_frames=num_frames,
|
| 163 |
+
guidance_scale=float(guidance_scale),
|
| 164 |
+
num_inference_steps=int(steps),
|
| 165 |
generator=torch.Generator(device="cuda").manual_seed(current_seed)
|
| 166 |
).frames[0]
|
| 167 |
|
|
|
|
| 177 |
|
| 178 |
|
| 179 |
with gr.Blocks() as demo:
|
| 180 |
+
gr.Markdown("# Wan2.2-I2V-A14B-Diffusers")
|
|
|
|
| 181 |
with gr.Row():
|
| 182 |
with gr.Column():
|
| 183 |
image_url_input = gr.Textbox(
|