Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,55 +14,50 @@ pipe = LucyEditPipeline.from_pretrained(model_id, vae=vae, torch_dtype=torch.bfl
|
|
| 14 |
pipe.to("cuda")
|
| 15 |
|
| 16 |
def calculate_resolution(input_width, input_height, min_dimension=480, max_dimension=832):
|
| 17 |
-
"""Calculate optimal resolution preserving aspect ratio"""
|
| 18 |
# Ensure dimensions are multiples of 16
|
| 19 |
def round_to_16(x):
|
| 20 |
-
return int(round(x / 16.0) * 16)
|
| 21 |
|
| 22 |
# Get aspect ratio
|
| 23 |
aspect_ratio = input_width / input_height
|
| 24 |
|
| 25 |
-
# Square videos
|
| 26 |
-
if 0.
|
| 27 |
return 640, 640
|
| 28 |
|
| 29 |
# Landscape videos (width > height)
|
| 30 |
elif aspect_ratio > 1:
|
| 31 |
-
#
|
| 32 |
new_width = max_dimension
|
| 33 |
-
new_height =
|
| 34 |
|
| 35 |
-
# If height
|
| 36 |
if new_height < min_dimension:
|
| 37 |
new_height = min_dimension
|
| 38 |
-
new_width =
|
| 39 |
-
|
| 40 |
-
# Clamp width if needed
|
| 41 |
if new_width > max_dimension:
|
| 42 |
new_width = max_dimension
|
| 43 |
-
|
|
|
|
| 44 |
|
| 45 |
-
# Portrait videos (height > width)
|
| 46 |
else:
|
| 47 |
-
#
|
| 48 |
new_height = max_dimension
|
| 49 |
-
new_width =
|
| 50 |
|
| 51 |
-
# If width
|
| 52 |
if new_width < min_dimension:
|
| 53 |
new_width = min_dimension
|
| 54 |
-
new_height =
|
| 55 |
-
|
| 56 |
-
# Clamp height if needed
|
| 57 |
if new_height > max_dimension:
|
| 58 |
new_height = max_dimension
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
final_width = round_to_16(max(min_dimension, min(max_dimension, new_width)))
|
| 63 |
-
final_height = round_to_16(max(min_dimension, min(max_dimension, new_height)))
|
| 64 |
-
|
| 65 |
-
return final_width, final_height
|
| 66 |
|
| 67 |
@spaces.GPU(duration=90)
|
| 68 |
def process_video(
|
|
|
|
| 14 |
pipe.to("cuda")
|
| 15 |
|
| 16 |
def calculate_resolution(input_width, input_height, min_dimension=480, max_dimension=832):
|
| 17 |
+
"""Calculate optimal resolution preserving aspect ratio within bounds"""
|
| 18 |
# Ensure dimensions are multiples of 16
|
| 19 |
def round_to_16(x):
|
| 20 |
+
return max(min_dimension, min(max_dimension, int(round(x / 16.0) * 16)))
|
| 21 |
|
| 22 |
# Get aspect ratio
|
| 23 |
aspect_ratio = input_width / input_height
|
| 24 |
|
| 25 |
+
# Square videos (aspect ratio close to 1:1)
|
| 26 |
+
if 0.98 <= aspect_ratio <= 1.02:
|
| 27 |
return 640, 640
|
| 28 |
|
| 29 |
# Landscape videos (width > height)
|
| 30 |
elif aspect_ratio > 1:
|
| 31 |
+
# Try to use max width
|
| 32 |
new_width = max_dimension
|
| 33 |
+
new_height = new_width / aspect_ratio
|
| 34 |
|
| 35 |
+
# If height would be too small, use min height
|
| 36 |
if new_height < min_dimension:
|
| 37 |
new_height = min_dimension
|
| 38 |
+
new_width = new_height * aspect_ratio
|
| 39 |
+
# If width exceeds max, clamp it
|
|
|
|
| 40 |
if new_width > max_dimension:
|
| 41 |
new_width = max_dimension
|
| 42 |
+
|
| 43 |
+
return round_to_16(new_width), round_to_16(new_height)
|
| 44 |
|
| 45 |
+
# Portrait videos (height > width)
|
| 46 |
else:
|
| 47 |
+
# Try to use max height
|
| 48 |
new_height = max_dimension
|
| 49 |
+
new_width = new_height * aspect_ratio
|
| 50 |
|
| 51 |
+
# If width would be too small, use min width
|
| 52 |
if new_width < min_dimension:
|
| 53 |
new_width = min_dimension
|
| 54 |
+
new_height = new_width / aspect_ratio
|
| 55 |
+
# If height exceeds max, clamp it
|
|
|
|
| 56 |
if new_height > max_dimension:
|
| 57 |
new_height = max_dimension
|
| 58 |
+
|
| 59 |
+
return round_to_16(new_width), round_to_16(new_height)
|
| 60 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
@spaces.GPU(duration=90)
|
| 63 |
def process_video(
|