jiuface commited on
Commit
eec59d0
·
verified ·
1 Parent(s): d1b9b5b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -33
app.py CHANGED
@@ -1,18 +1,14 @@
1
  import spaces
2
  import torch
3
- from diffusers import AutoencoderKLWan, UniPCMultistepScheduler
4
  from diffusers.pipelines.wan.pipeline_wan_i2v import WanImageToVideoPipeline
5
  from diffusers.models.transformers.transformer_wan import WanTransformer3DModel
6
  from diffusers.utils import export_to_video
7
- from transformers import CLIPVisionModel
8
  import gradio as gr
9
  import tempfile
10
- from huggingface_hub import hf_hub_download
11
  import numpy as np
12
  from PIL import Image
13
  import random
14
  from datetime import datetime
15
- from huggingface_hub import login
16
  import os
17
  import time
18
  from PIL import Image
@@ -46,18 +42,6 @@ MAX_FRAMES_MODEL = 80
46
  MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
47
  MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
48
 
49
- MOD_VALUE = 32
50
- DEFAULT_H_SLIDER_VALUE = 512
51
- DEFAULT_W_SLIDER_VALUE = 896
52
- NEW_FORMULA_MAX_AREA = 480.0 * 832.0
53
-
54
- SLIDER_MIN_H, SLIDER_MAX_H = 128, 896
55
- SLIDER_MIN_W, SLIDER_MAX_W = 128, 896
56
- MAX_SEED = np.iinfo(np.int32).max
57
-
58
- #MIN_FRAMES_MODEL = 8
59
- #MAX_FRAMES_MODEL = 81
60
-
61
 
62
  pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
63
  transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
@@ -197,20 +181,32 @@ def upload_video_to_r2(video_file, account_id, access_key, secret_key, bucket_na
197
 
198
  return video_remote_path
199
 
200
- def handle_image_upload_for_dims_wan(uploaded_pil_image, current_h_val, current_w_val):
201
- if uploaded_pil_image is None:
202
- return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
203
- try:
204
- new_h, new_w = _calculate_new_dimensions_wan(
205
- uploaded_pil_image, MOD_VALUE, NEW_FORMULA_MAX_AREA,
206
- SLIDER_MIN_H, SLIDER_MAX_H, SLIDER_MIN_W, SLIDER_MAX_W,
207
- DEFAULT_H_SLIDER_VALUE, DEFAULT_W_SLIDER_VALUE
208
- )
209
- return gr.update(value=new_h), gr.update(value=new_w)
210
- except Exception as e:
211
- gr.Warning("Error attempting to calculate new dimensions")
212
- return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
213
-
 
 
 
 
 
 
 
 
 
 
 
 
214
 
215
 
216
  @spaces.GPU(duration=120)
@@ -251,6 +247,7 @@ def generate_video(image_url,
251
  width=resized_image.width,
252
  num_frames=num_frames,
253
  guidance_scale=float(guidance_scale),
 
254
  num_inference_steps=int(steps),
255
  generator=torch.Generator(device="cuda").manual_seed(current_seed)
256
  ).frames[0]
@@ -279,15 +276,15 @@ with gr.Blocks() as demo:
279
  container=False
280
  )
281
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
282
- duration_seconds_input = gr.Slider(minimum=round(MIN_FRAMES_MODEL/FIXED_FPS,1), maximum=round(MAX_FRAMES_MODEL/FIXED_FPS,1), step=0.1, value=2, label="Duration (seconds)", info=f"Clamped to model's {MIN_FRAMES_MODEL}-{MAX_FRAMES_MODEL} frames at {FIXED_FPS}fps.")
283
 
284
  with gr.Accordion("Advanced Settings", open=False):
285
  negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
286
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
287
  randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
288
  with gr.Row():
289
- 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})")
290
- 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})")
291
  steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
292
  guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=True)
293
 
 
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 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
  from datetime import datetime
 
12
  import os
13
  import time
14
  from PIL import Image
 
42
  MIN_DURATION = round(MIN_FRAMES_MODEL/FIXED_FPS,1)
43
  MAX_DURATION = round(MAX_FRAMES_MODEL/FIXED_FPS,1)
44
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  pipe = WanImageToVideoPipeline.from_pretrained(MODEL_ID,
47
  transformer=WanTransformer3DModel.from_pretrained('cbensimon/Wan2.2-I2V-A14B-bf16-Diffusers',
 
181
 
182
  return video_remote_path
183
 
184
+ def get_duration(
185
+ image_url,
186
+ prompt,
187
+ height,
188
+ width,
189
+ negative_prompt,
190
+ duration_seconds,
191
+ guidance_scale,
192
+ steps,
193
+ seed,
194
+ randomize_seed,
195
+ upload_to_r2,
196
+ account_id,
197
+ access_key,
198
+ secret_key,
199
+ bucket,
200
+ progress
201
+ ):
202
+ BASE_FRAMES_HEIGHT_WIDTH = 81 * 832 * 624
203
+ BASE_STEP_DURATION = 15
204
+ input_image = load_image(image_url)
205
+ width, height = resize_image(input_image).size
206
+ frames = get_num_frames(duration_seconds)
207
+ factor = frames * width * height / BASE_FRAMES_HEIGHT_WIDTH
208
+ step_duration = BASE_STEP_DURATION * factor ** 1.5
209
+ return 10 + int(steps) * step_duration
210
 
211
 
212
  @spaces.GPU(duration=120)
 
247
  width=resized_image.width,
248
  num_frames=num_frames,
249
  guidance_scale=float(guidance_scale),
250
+ guidance_scale_2=float(guidance_scale),
251
  num_inference_steps=int(steps),
252
  generator=torch.Generator(device="cuda").manual_seed(current_seed)
253
  ).frames[0]
 
276
  container=False
277
  )
278
  prompt_input = gr.Textbox(label="Prompt", value=default_prompt_i2v)
279
+ 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.")
280
 
281
  with gr.Accordion("Advanced Settings", open=False):
282
  negative_prompt_input = gr.Textbox(label="Negative Prompt", value=default_negative_prompt, lines=3)
283
  seed_input = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42, interactive=True)
284
  randomize_seed_checkbox = gr.Checkbox(label="Randomize seed", value=True, interactive=True)
285
  with gr.Row():
286
+ height_input = gr.Slider(minimum=512, maximum=1024, step=1, value=640, label=f"Output Height")
287
+ width_input = gr.Slider(minimum=512, maximum=1024, step=1, value=540, label=f"Output Width")
288
  steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
289
  guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=True)
290