thankfulcarp commited on
Commit
1ab10dc
·
1 Parent(s): d4c2a58

Fixed dimension bug

Browse files
Files changed (1) hide show
  1. app.py +36 -29
app.py CHANGED
@@ -90,18 +90,18 @@ available_i2v_presets = get_available_presets(I2V_LORA_REPO_ID, I2V_LORA_SUBFOLD
90
 
91
 
92
  # --- Constants and Configuration ---
93
- MOD_VALUE = 8
94
- DEFAULT_H_SLIDER_VALUE = 512
95
- DEFAULT_W_SLIDER_VALUE = 768
96
- NEW_FORMULA_MAX_AREA = 768.0 * 512.0
97
- LORA_MAX_AREA = 640.0 * 480.0 # Max pixel area when a LoRA is active
98
-
99
- SLIDER_MIN_H, SLIDER_MAX_H = 128, 896
100
- SLIDER_MIN_W, SLIDER_MAX_W = 128, 896
 
101
  MAX_SEED = np.iinfo(np.int32).max
102
 
103
  FIXED_FPS = 16
104
- T2V_FIXED_FPS = 16
105
  MIN_FRAMES_MODEL = 8
106
  MAX_FRAMES_MODEL = 81
107
 
@@ -164,11 +164,13 @@ def handle_lora_selection_change(preset_name, current_prompt, current_h, current
164
  gr.Info(f"Resolution too high for LoRA. Scaling down to a 640x480 equivalent area.")
165
  # aspect_ratio is W/H
166
  if aspect_ratio > 0:
167
- calc_w = round(np.sqrt(LORA_MAX_AREA * aspect_ratio))
168
- calc_h = round(np.sqrt(LORA_MAX_AREA / aspect_ratio))
 
169
 
170
- new_h = max(MOD_VALUE, (calc_h // MOD_VALUE) * MOD_VALUE)
171
- new_w = max(MOD_VALUE, (calc_w // MOD_VALUE) * MOD_VALUE)
 
172
 
173
  h_update = gr.update(value=new_h)
174
  w_update = gr.update(value=new_w)
@@ -201,7 +203,7 @@ def update_linked_dimension(driving_value, other_value, aspect_ratio, mod_val, m
201
  new_other_value = driving_value / aspect_ratio
202
 
203
  # Round to the nearest multiple of mod_val
204
- new_other_value = max(mod_val, (round(new_other_value / mod_val)) * mod_val)
205
 
206
  # Return an update only if the value has changed to prevent infinite loops
207
  return gr.update(value=new_other_value) if int(new_other_value) != int(other_value) else gr.update()
@@ -214,10 +216,15 @@ def _calculate_new_dimensions_wan(pil_image, mod_val, calculation_max_area,
214
  if orig_w <= 0 or orig_h <= 0:
215
  return default_h, default_w
216
  aspect_ratio = orig_h / orig_w
217
- calc_h = round(np.sqrt(calculation_max_area * aspect_ratio))
218
- calc_w = round(np.sqrt(calculation_max_area / aspect_ratio))
219
- calc_h = max(mod_val, (calc_h // mod_val) * mod_val)
220
- calc_w = max(mod_val, (calc_w // mod_val) * mod_val)
 
 
 
 
 
221
  new_h = int(np.clip(calc_h, min_slider_h, (max_slider_h // mod_val) * mod_val))
222
  new_w = int(np.clip(calc_w, min_slider_w, (max_slider_w // mod_val) * mod_val))
223
  return new_h, new_w
@@ -260,10 +267,10 @@ def get_t2v_duration(steps, duration_seconds):
260
 
261
  @spaces.GPU(duration_from_args=get_i2v_duration)
262
  def generate_i2v_video(input_image, prompt, height, width,
263
- negative_prompt, duration_seconds,
264
- guidance_scale, steps, seed, randomize_seed,
265
- preset_name, lora_weight,
266
- progress=gr.Progress(track_tqdm=True)):
267
  """Generates a video from an initial image and a prompt."""
268
  if input_image is None:
269
  raise gr.Error("Please upload an input image for Image-to-Video generation.")
@@ -279,13 +286,13 @@ def generate_i2v_video(input_image, prompt, height, width,
279
  print(f"⚠️ Warning: Resolution {target_w}x{target_h} is too high for LoRA. Rescaling to fit max area.")
280
  aspect_ratio = target_w / target_h if target_h > 0 else 1.0
281
 
282
- # Re-calculate w and h based on max area
283
- target_w = round(np.sqrt(LORA_MAX_AREA * aspect_ratio))
284
- target_h = round(np.sqrt(LORA_MAX_AREA / aspect_ratio))
285
 
286
- # Snap to MOD_VALUE
287
- target_h = max(MOD_VALUE, (target_h // MOD_VALUE) * MOD_VALUE)
288
- target_w = max(MOD_VALUE, (target_w // MOD_VALUE) * MOD_VALUE)
289
  print(f" - Rescaled to: {target_w}x{target_h}")
290
 
291
  # Calculate and adjust num_frames to be compatible with video codecs
@@ -454,4 +461,4 @@ with gr.Blocks() as demo:
454
 
455
 
456
  if __name__ == "__main__":
457
- demo.queue().launch()
 
90
 
91
 
92
  # --- Constants and Configuration ---
93
+ MOD_VALUE = 16 # Changed to 16 for model compatibility
94
+ DEFAULT_H_SLIDER_VALUE = 480 # Default to 480p height
95
+ DEFAULT_W_SLIDER_VALUE = 640 # Default to 640p width
96
+ NEW_FORMULA_MAX_AREA = 640.0 * 480.0 # Default area for new images
97
+
98
+ LORA_MAX_AREA = 640.0 * 480.0 # Max area when using a LoRA
99
+
100
+ SLIDER_MIN_H, SLIDER_MAX_H = 128, 1024
101
+ SLIDER_MIN_W, SLIDER_MAX_W = 128, 1024
102
  MAX_SEED = np.iinfo(np.int32).max
103
 
104
  FIXED_FPS = 16
 
105
  MIN_FRAMES_MODEL = 8
106
  MAX_FRAMES_MODEL = 81
107
 
 
164
  gr.Info(f"Resolution too high for LoRA. Scaling down to a 640x480 equivalent area.")
165
  # aspect_ratio is W/H
166
  if aspect_ratio > 0:
167
+ # Calculate ideal dimensions based on area, without premature rounding
168
+ calc_w = np.sqrt(LORA_MAX_AREA * aspect_ratio)
169
+ calc_h = np.sqrt(LORA_MAX_AREA / aspect_ratio)
170
 
171
+ # Round to the nearest multiple of MOD_VALUE
172
+ new_h = max(MOD_VALUE, round(calc_h / MOD_VALUE) * MOD_VALUE)
173
+ new_w = max(MOD_VALUE, round(calc_w / MOD_VALUE) * MOD_VALUE)
174
 
175
  h_update = gr.update(value=new_h)
176
  w_update = gr.update(value=new_w)
 
203
  new_other_value = driving_value / aspect_ratio
204
 
205
  # Round to the nearest multiple of mod_val
206
+ new_other_value = max(mod_val, round(new_other_value / mod_val) * mod_val)
207
 
208
  # Return an update only if the value has changed to prevent infinite loops
209
  return gr.update(value=new_other_value) if int(new_other_value) != int(other_value) else gr.update()
 
216
  if orig_w <= 0 or orig_h <= 0:
217
  return default_h, default_w
218
  aspect_ratio = orig_h / orig_w
219
+
220
+ # Calculate ideal dimensions based on area, without premature rounding
221
+ calc_h = np.sqrt(calculation_max_area * aspect_ratio)
222
+ calc_w = np.sqrt(calculation_max_area / aspect_ratio)
223
+
224
+ # Round to the nearest multiple of mod_val
225
+ calc_h = max(mod_val, round(calc_h / mod_val) * mod_val)
226
+ calc_w = max(mod_val, round(calc_w / mod_val) * mod_val)
227
+
228
  new_h = int(np.clip(calc_h, min_slider_h, (max_slider_h // mod_val) * mod_val))
229
  new_w = int(np.clip(calc_w, min_slider_w, (max_slider_w // mod_val) * mod_val))
230
  return new_h, new_w
 
267
 
268
  @spaces.GPU(duration_from_args=get_i2v_duration)
269
  def generate_i2v_video(input_image, prompt, height, width,
270
+ negative_prompt, duration_seconds,
271
+ guidance_scale, steps, seed, randomize_seed,
272
+ preset_name, lora_weight,
273
+ progress=gr.Progress(track_tqdm=True)):
274
  """Generates a video from an initial image and a prompt."""
275
  if input_image is None:
276
  raise gr.Error("Please upload an input image for Image-to-Video generation.")
 
286
  print(f"⚠️ Warning: Resolution {target_w}x{target_h} is too high for LoRA. Rescaling to fit max area.")
287
  aspect_ratio = target_w / target_h if target_h > 0 else 1.0
288
 
289
+ # Re-calculate w and h based on max area, without premature rounding
290
+ calc_w = np.sqrt(LORA_MAX_AREA * aspect_ratio)
291
+ calc_h = np.sqrt(LORA_MAX_AREA / aspect_ratio)
292
 
293
+ # Snap to MOD_VALUE by rounding to the nearest multiple
294
+ target_h = max(MOD_VALUE, round(calc_h / MOD_VALUE) * MOD_VALUE)
295
+ target_w = max(MOD_VALUE, round(calc_w / MOD_VALUE) * MOD_VALUE)
296
  print(f" - Rescaled to: {target_w}x{target_h}")
297
 
298
  # Calculate and adjust num_frames to be compatible with video codecs
 
461
 
462
 
463
  if __name__ == "__main__":
464
+ demo.queue().launch()