adptbyt commited on
Commit
518c038
·
verified ·
1 Parent(s): 3d619a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -24
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # app.py
2
-
3
  import torch
4
  from diffusers import AutoencoderKLWan, WanImageToVideoPipeline, UniPCMultistepScheduler
5
  from diffusers.utils import export_to_video
@@ -50,8 +48,6 @@ def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
50
  min_slider_h, max_slider_h,
51
  min_slider_w, max_slider_w,
52
  default_h, default_w):
53
- if not isinstance(pil_image, Image.Image):
54
- return default_h, default_w
55
  orig_w, orig_h = pil_image.size
56
  if orig_w <= 0 or orig_h <= 0:
57
  return default_h, default_w
@@ -69,22 +65,37 @@ def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
69
 
70
  return new_h, new_w
71
 
72
- def handle_image_upload_for_dims_wan(uploaded_pil_image):
73
- new_h, new_w = _calculate_new_dimensions_wan(
74
- uploaded_pil_image, MOD_VALUE, NEW_FORMULA_MAX_AREA,
75
- SLIDER_MIN_H, SLIDER_MAX_H, SLIDER_MIN_W, SLIDER_MAX_W,
76
- DEFAULT_H_SLIDER_VALUE, DEFAULT_W_SLIDER_VALUE
77
- )
78
- return gr.update(value=new_h), gr.update(value=new_w)
79
-
80
-
81
- # NOTE: The @spaces.GPU decorator is ignored outside of the Hugging Face platform
82
- @spaces.GPU
83
- def generate_video(input_image, prompt, height, width,
 
 
 
84
  negative_prompt, duration_seconds,
85
  guidance_scale, steps,
86
  seed, randomize_seed,
87
- # *** KEY CHANGE 1: Remove the default value for progress ***
 
 
 
 
 
 
 
 
 
 
 
 
88
  progress=gr.Progress(track_tqdm=True)):
89
 
90
  if input_image is None:
@@ -104,8 +115,7 @@ def generate_video(input_image, prompt, height, width,
104
  image=resized_image, prompt=prompt, negative_prompt=negative_prompt,
105
  height=target_h, width=target_w, num_frames=num_frames,
106
  guidance_scale=float(guidance_scale), num_inference_steps=int(steps),
107
- generator=torch.Generator(device="cuda").manual_seed(current_seed),
108
- callback_on_step_end=lambda p, s, l: progress(s / steps) # Use the progress object
109
  ).frames[0]
110
 
111
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
@@ -130,7 +140,7 @@ with gr.Blocks() as demo:
130
  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})")
131
  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})")
132
  steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
133
- guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale") # Set visible=True for debugging if needed
134
 
135
  generate_button = gr.Button("Generate Video", variant="primary")
136
  with gr.Column():
@@ -138,16 +148,21 @@ with gr.Blocks() as demo:
138
 
139
  input_image_component.upload(
140
  fn=handle_image_upload_for_dims_wan,
141
- inputs=[input_image_component],
 
 
 
 
 
 
142
  outputs=[height_input, width_input]
143
  )
144
 
145
- # Bundle all the UI components that the generate_video function needs
146
  ui_inputs = [
147
  input_image_component, prompt_input, height_input, width_input,
148
  negative_prompt_input, duration_seconds_input,
149
  guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
150
  ]
 
151
 
152
- # *** KEY CHANGE 2: Add gr.Progress() to the list of inputs for the click event ***
153
- generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
 
 
 
1
  import torch
2
  from diffusers import AutoencoderKLWan, WanImageToVideoPipeline, UniPCMultistepScheduler
3
  from diffusers.utils import export_to_video
 
48
  min_slider_h, max_slider_h,
49
  min_slider_w, max_slider_w,
50
  default_h, default_w):
 
 
51
  orig_w, orig_h = pil_image.size
52
  if orig_w <= 0 or orig_h <= 0:
53
  return default_h, default_w
 
65
 
66
  return new_h, new_w
67
 
68
+ def handle_image_upload_for_dims_wan(uploaded_pil_image, current_h_val, current_w_val):
69
+ if uploaded_pil_image is None:
70
+ return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
71
+ try:
72
+ new_h, new_w = _calculate_new_dimensions_wan(
73
+ uploaded_pil_image, MOD_VALUE, NEW_FORMULA_MAX_AREA,
74
+ SLIDER_MIN_H, SLIDER_MAX_H, SLIDER_MIN_W, SLIDER_MAX_W,
75
+ DEFAULT_H_SLIDER_VALUE, DEFAULT_W_SLIDER_VALUE
76
+ )
77
+ return gr.update(value=new_h), gr.update(value=new_w)
78
+ except Exception as e:
79
+ gr.Warning("Error attempting to calculate new dimensions")
80
+ return gr.update(value=DEFAULT_H_SLIDER_VALUE), gr.update(value=DEFAULT_W_SLIDER_VALUE)
81
+
82
+ def get_duration(input_image, prompt, height, width,
83
  negative_prompt, duration_seconds,
84
  guidance_scale, steps,
85
  seed, randomize_seed,
86
+ progress):
87
+ if steps > 4 and duration_seconds > 2:
88
+ return 90
89
+ elif steps > 4 or duration_seconds > 2:
90
+ return 75
91
+ else:
92
+ return 60
93
+
94
+ @spaces.GPU(duration=get_duration)
95
+ def generate_video(input_image, prompt, height, width,
96
+ negative_prompt=default_negative_prompt, duration_seconds = 2,
97
+ guidance_scale = 1, steps = 4,
98
+ seed = 42, randomize_seed = False,
99
  progress=gr.Progress(track_tqdm=True)):
100
 
101
  if input_image is None:
 
115
  image=resized_image, prompt=prompt, negative_prompt=negative_prompt,
116
  height=target_h, width=target_w, num_frames=num_frames,
117
  guidance_scale=float(guidance_scale), num_inference_steps=int(steps),
118
+ generator=torch.Generator(device="cuda").manual_seed(current_seed)
 
119
  ).frames[0]
120
 
121
  with tempfile.NamedTemporaryFile(suffix=".mp4", delete=False) as tmpfile:
 
140
  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})")
141
  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})")
142
  steps_slider = gr.Slider(minimum=1, maximum=30, step=1, value=4, label="Inference Steps")
143
+ guidance_scale_input = gr.Slider(minimum=0.0, maximum=20.0, step=0.5, value=1.0, label="Guidance Scale", visible=False)
144
 
145
  generate_button = gr.Button("Generate Video", variant="primary")
146
  with gr.Column():
 
148
 
149
  input_image_component.upload(
150
  fn=handle_image_upload_for_dims_wan,
151
+ inputs=[input_image_component, height_input, width_input],
152
+ outputs=[height_input, width_input]
153
+ )
154
+
155
+ input_image_component.clear(
156
+ fn=handle_image_upload_for_dims_wan,
157
+ inputs=[input_image_component, height_input, width_input],
158
  outputs=[height_input, width_input]
159
  )
160
 
 
161
  ui_inputs = [
162
  input_image_component, prompt_input, height_input, width_input,
163
  negative_prompt_input, duration_seconds_input,
164
  guidance_scale_input, steps_slider, seed_input, randomize_seed_checkbox
165
  ]
166
+ generate_button.click(fn=generate_video, inputs=ui_inputs, outputs=[video_output, seed_input])
167
 
168
+ demo.launch(share=True)