Spaces:
Running on Zero
Running on Zero
Match output video resolution to input image aspect ratio to avoid stretching
Browse files
app.py
CHANGED
|
@@ -29,6 +29,36 @@ def num_frames_for_duration(seconds, fps=24, base=8):
|
|
| 29 |
raw = int(seconds * fps)
|
| 30 |
return ((raw - 1) // base) * base + 1
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# βββ Model loading (module scope, on cuda for ZeroGPU) ββββββββββββββββββββββ
|
| 33 |
|
| 34 |
print("[LOAD] Loading LTX-2.3 Distilled pipeline (diffusers)...")
|
|
@@ -79,13 +109,12 @@ def generate(
|
|
| 79 |
|
| 80 |
num_frames = num_frames_for_duration(duration_seconds, fps=24)
|
| 81 |
|
| 82 |
-
#
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
height = 32
|
| 89 |
|
| 90 |
progress(0.05, desc="Encoding prompt...")
|
| 91 |
|
|
@@ -157,9 +186,10 @@ with gr.Blocks(title="LTX-2.3 Product Ad Style") as demo:
|
|
| 157 |
value=3.0,
|
| 158 |
step=0.5,
|
| 159 |
)
|
|
|
|
| 160 |
with gr.Row():
|
| 161 |
-
width_slider = gr.Slider(label="Width", minimum=256, maximum=1024, value=768, step=32)
|
| 162 |
-
height_slider = gr.Slider(label="Height", minimum=256, maximum=1024, value=512, step=32)
|
| 163 |
seed = gr.Number(label="Seed", value=42)
|
| 164 |
randomize = gr.Checkbox(label="Randomize seed", value=True)
|
| 165 |
lora_scale = gr.Slider(
|
|
|
|
| 29 |
raw = int(seconds * fps)
|
| 30 |
return ((raw - 1) // base) * base + 1
|
| 31 |
|
| 32 |
+
# Resolution helper: pick the closest supported output resolution that matches
|
| 33 |
+
# the input image's aspect ratio, so the video isn't stretched/distorted.
|
| 34 |
+
# LTX wants dimensions that are multiples of 32. The width/height sliders act as
|
| 35 |
+
# a bounding box; we fit the image's aspect ratio inside that box (matching its
|
| 36 |
+
# total pixel budget) and snap to multiples of 32.
|
| 37 |
+
def resolution_for_image(image, max_width, max_height, multiple=32, min_dim=256):
|
| 38 |
+
img_w, img_h = image.size
|
| 39 |
+
if img_w <= 0 or img_h <= 0:
|
| 40 |
+
# Fall back to the requested box if the image size is unusable.
|
| 41 |
+
return (
|
| 42 |
+
max(min_dim, (int(max_width) // multiple) * multiple),
|
| 43 |
+
max(min_dim, (int(max_height) // multiple) * multiple),
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
aspect = img_w / img_h
|
| 47 |
+
# Use the slider box as the pixel budget so quality is comparable to before.
|
| 48 |
+
pixel_budget = float(max_width) * float(max_height)
|
| 49 |
+
|
| 50 |
+
# Ideal continuous dimensions with the image's aspect ratio and same budget.
|
| 51 |
+
target_h = (pixel_budget / aspect) ** 0.5
|
| 52 |
+
target_w = target_h * aspect
|
| 53 |
+
|
| 54 |
+
# Snap to multiples of `multiple`, rounding to the nearest supported value.
|
| 55 |
+
def snap(x):
|
| 56 |
+
return int(round(x / multiple)) * multiple
|
| 57 |
+
|
| 58 |
+
width = max(min_dim, snap(target_w))
|
| 59 |
+
height = max(min_dim, snap(target_h))
|
| 60 |
+
return width, height
|
| 61 |
+
|
| 62 |
# βββ Model loading (module scope, on cuda for ZeroGPU) ββββββββββββββββββββββ
|
| 63 |
|
| 64 |
print("[LOAD] Loading LTX-2.3 Distilled pipeline (diffusers)...")
|
|
|
|
| 109 |
|
| 110 |
num_frames = num_frames_for_duration(duration_seconds, fps=24)
|
| 111 |
|
| 112 |
+
# Match the output resolution to the uploaded image's aspect ratio so the
|
| 113 |
+
# video isn't stretched. The width/height sliders act as a resolution
|
| 114 |
+
# budget; the closest supported (multiple-of-32) size with the image's
|
| 115 |
+
# aspect ratio is chosen.
|
| 116 |
+
width, height = resolution_for_image(image, width, height)
|
| 117 |
+
print(f"[GEN] Input image size {image.size} -> output resolution {width}x{height}")
|
|
|
|
| 118 |
|
| 119 |
progress(0.05, desc="Encoding prompt...")
|
| 120 |
|
|
|
|
| 186 |
value=3.0,
|
| 187 |
step=0.5,
|
| 188 |
)
|
| 189 |
+
gr.Markdown("Output resolution automatically matches the uploaded image's aspect ratio. The sliders below set the resolution budget (max size).")
|
| 190 |
with gr.Row():
|
| 191 |
+
width_slider = gr.Slider(label="Max Width", minimum=256, maximum=1024, value=768, step=32)
|
| 192 |
+
height_slider = gr.Slider(label="Max Height", minimum=256, maximum=1024, value=512, step=32)
|
| 193 |
seed = gr.Number(label="Seed", value=42)
|
| 194 |
randomize = gr.Checkbox(label="Randomize seed", value=True)
|
| 195 |
lora_scale = gr.Slider(
|