Spaces:
Running
on
Zero
Running
on
Zero
Julian Bilcke
commited on
Commit
·
c0c993a
1
Parent(s):
3678ee4
up
Browse files
app.py
CHANGED
|
@@ -310,55 +310,53 @@ pipe.fuse_lora()
|
|
| 310 |
# --- UI Constants and Helpers ---
|
| 311 |
MAX_SEED = np.iinfo(np.int32).max
|
| 312 |
|
| 313 |
-
def get_image_size_for_position(position_data, image_index, num_images):
|
| 314 |
"""Determines optimal image size based on its position in the layout.
|
| 315 |
|
| 316 |
Args:
|
| 317 |
position_data: Layout position data [x, y, width, height] in relative units
|
| 318 |
image_index: Index of the current image (0-based)
|
| 319 |
num_images: Total number of images in the layout
|
|
|
|
| 320 |
|
| 321 |
Returns:
|
| 322 |
-
tuple: (width, height) optimized for the position's aspect ratio
|
| 323 |
"""
|
| 324 |
if not position_data:
|
| 325 |
-
return
|
| 326 |
|
| 327 |
x_rel, y_rel, w_rel, h_rel = position_data
|
| 328 |
aspect_ratio = w_rel / h_rel if h_rel > 0 else 1.0
|
| 329 |
|
| 330 |
-
#
|
| 331 |
-
max_dim = 1024
|
| 332 |
-
|
| 333 |
-
# Calculate dimensions maintaining aspect ratio with max of 1024
|
| 334 |
if aspect_ratio >= 1: # Wider than tall
|
| 335 |
-
width =
|
| 336 |
-
height = int(
|
| 337 |
-
# Ensure height is at least
|
| 338 |
-
if height <
|
| 339 |
-
height =
|
| 340 |
-
width = int(
|
| 341 |
else: # Taller than wide
|
| 342 |
-
height =
|
| 343 |
-
width = int(
|
| 344 |
-
# Ensure width is at least
|
| 345 |
-
if width <
|
| 346 |
-
width =
|
| 347 |
-
height = int(
|
| 348 |
|
| 349 |
# Round to nearest 64 for better compatibility
|
| 350 |
width = (width // 64) * 64
|
| 351 |
height = (height // 64) * 64
|
| 352 |
|
| 353 |
-
# Ensure we don't exceed
|
| 354 |
-
if width >
|
| 355 |
-
width =
|
| 356 |
-
if height >
|
| 357 |
-
height =
|
| 358 |
|
| 359 |
-
# Minimum size check
|
| 360 |
-
width = max(width,
|
| 361 |
-
height = max(height,
|
| 362 |
|
| 363 |
return width, height
|
| 364 |
|
|
@@ -666,6 +664,7 @@ def infer_page(
|
|
| 666 |
custom_style_text="",
|
| 667 |
num_images=1,
|
| 668 |
layout="default",
|
|
|
|
| 669 |
session_state=None,
|
| 670 |
progress=gr.Progress(track_tqdm=True),
|
| 671 |
):
|
|
@@ -696,8 +695,8 @@ def infer_page(
|
|
| 696 |
if random.random() < 0.1: # 10% chance to cleanup on each request
|
| 697 |
session_manager.cleanup_old_sessions()
|
| 698 |
|
| 699 |
-
# Check page limit
|
| 700 |
-
if session_manager.metadata["total_pages"] >=
|
| 701 |
return session_state, None, None, f"Page limit reached"
|
| 702 |
|
| 703 |
generated_images = []
|
|
@@ -733,6 +732,7 @@ def infer_page(
|
|
| 733 |
dialogue=scene_dialogue, # Pass dialogue separately
|
| 734 |
style_preset=style_preset,
|
| 735 |
custom_style_text=custom_style_text,
|
|
|
|
| 736 |
)
|
| 737 |
|
| 738 |
generated_images.append(image)
|
|
@@ -753,7 +753,7 @@ def infer_page(
|
|
| 753 |
|
| 754 |
# Next button label
|
| 755 |
next_page_num = page_num + 1
|
| 756 |
-
button_label = f"Generate page {next_page_num}" if next_page_num <=
|
| 757 |
|
| 758 |
return session_state, pdf_path, pdf_path, button_label
|
| 759 |
|
|
@@ -770,6 +770,7 @@ def infer_single_auto(
|
|
| 770 |
dialogue="", # New parameter for dialogue
|
| 771 |
style_preset="no_style",
|
| 772 |
custom_style_text="",
|
|
|
|
| 773 |
):
|
| 774 |
"""
|
| 775 |
Generates an image with automatically determined aspect ratio based on layout position.
|
|
@@ -777,8 +778,8 @@ def infer_single_auto(
|
|
| 777 |
if randomize_seed:
|
| 778 |
seed = random.randint(0, MAX_SEED)
|
| 779 |
|
| 780 |
-
# Automatically determine image size based on position
|
| 781 |
-
width, height = get_image_size_for_position(position_data, image_index, num_images)
|
| 782 |
|
| 783 |
# Set up the generator for reproducibility
|
| 784 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
|
@@ -923,11 +924,20 @@ with gr.Blocks(css=css) as demo:
|
|
| 923 |
value=8,
|
| 924 |
)
|
| 925 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 926 |
# Download link
|
| 927 |
pdf_output = gr.File(label="Download PDF", show_label=True, elem_id="pdf-download")
|
| 928 |
|
| 929 |
gr.Markdown("""**Note:** Your images and PDF are saved for up to 24 hours.
|
| 930 |
-
You can continue adding pages (up to
|
| 931 |
|
| 932 |
# Examples section in the left column
|
| 933 |
with gr.Accordion("Examples", open=True):
|
|
@@ -985,6 +995,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 985 |
custom_style_text,
|
| 986 |
num_images_slider,
|
| 987 |
layout_dropdown,
|
|
|
|
| 988 |
session_state,
|
| 989 |
],
|
| 990 |
outputs=[session_state, pdf_output, pdf_preview, run_button],
|
|
|
|
| 310 |
# --- UI Constants and Helpers ---
|
| 311 |
MAX_SEED = np.iinfo(np.int32).max
|
| 312 |
|
| 313 |
+
def get_image_size_for_position(position_data, image_index, num_images, max_resolution=1024):
|
| 314 |
"""Determines optimal image size based on its position in the layout.
|
| 315 |
|
| 316 |
Args:
|
| 317 |
position_data: Layout position data [x, y, width, height] in relative units
|
| 318 |
image_index: Index of the current image (0-based)
|
| 319 |
num_images: Total number of images in the layout
|
| 320 |
+
max_resolution: Maximum resolution for any dimension (default 1024)
|
| 321 |
|
| 322 |
Returns:
|
| 323 |
+
tuple: (width, height) optimized for the position's aspect ratio
|
| 324 |
"""
|
| 325 |
if not position_data:
|
| 326 |
+
return max_resolution, max_resolution # Default square
|
| 327 |
|
| 328 |
x_rel, y_rel, w_rel, h_rel = position_data
|
| 329 |
aspect_ratio = w_rel / h_rel if h_rel > 0 else 1.0
|
| 330 |
|
| 331 |
+
# Calculate dimensions maintaining aspect ratio
|
|
|
|
|
|
|
|
|
|
| 332 |
if aspect_ratio >= 1: # Wider than tall
|
| 333 |
+
width = max_resolution
|
| 334 |
+
height = int(max_resolution / aspect_ratio)
|
| 335 |
+
# Ensure height is at least 384 for quality
|
| 336 |
+
if height < 384:
|
| 337 |
+
height = 384
|
| 338 |
+
width = int(384 * aspect_ratio)
|
| 339 |
else: # Taller than wide
|
| 340 |
+
height = max_resolution
|
| 341 |
+
width = int(max_resolution * aspect_ratio)
|
| 342 |
+
# Ensure width is at least 384 for quality
|
| 343 |
+
if width < 384:
|
| 344 |
+
width = 384
|
| 345 |
+
height = int(384 / aspect_ratio)
|
| 346 |
|
| 347 |
# Round to nearest 64 for better compatibility
|
| 348 |
width = (width // 64) * 64
|
| 349 |
height = (height // 64) * 64
|
| 350 |
|
| 351 |
+
# Ensure we don't exceed max_resolution after rounding
|
| 352 |
+
if width > max_resolution:
|
| 353 |
+
width = max_resolution
|
| 354 |
+
if height > max_resolution:
|
| 355 |
+
height = max_resolution
|
| 356 |
|
| 357 |
+
# Minimum size check (increased from 256 to 384 for better quality)
|
| 358 |
+
width = max(width, 384)
|
| 359 |
+
height = max(height, 384)
|
| 360 |
|
| 361 |
return width, height
|
| 362 |
|
|
|
|
| 664 |
custom_style_text="",
|
| 665 |
num_images=1,
|
| 666 |
layout="default",
|
| 667 |
+
max_resolution=1024,
|
| 668 |
session_state=None,
|
| 669 |
progress=gr.Progress(track_tqdm=True),
|
| 670 |
):
|
|
|
|
| 695 |
if random.random() < 0.1: # 10% chance to cleanup on each request
|
| 696 |
session_manager.cleanup_old_sessions()
|
| 697 |
|
| 698 |
+
# Check page limit (reduced to 24 for performance)
|
| 699 |
+
if session_manager.metadata["total_pages"] >= 24:
|
| 700 |
return session_state, None, None, f"Page limit reached"
|
| 701 |
|
| 702 |
generated_images = []
|
|
|
|
| 732 |
dialogue=scene_dialogue, # Pass dialogue separately
|
| 733 |
style_preset=style_preset,
|
| 734 |
custom_style_text=custom_style_text,
|
| 735 |
+
max_resolution=max_resolution,
|
| 736 |
)
|
| 737 |
|
| 738 |
generated_images.append(image)
|
|
|
|
| 753 |
|
| 754 |
# Next button label
|
| 755 |
next_page_num = page_num + 1
|
| 756 |
+
button_label = f"Generate page {next_page_num}" if next_page_num <= 24 else "Page limit reached"
|
| 757 |
|
| 758 |
return session_state, pdf_path, pdf_path, button_label
|
| 759 |
|
|
|
|
| 770 |
dialogue="", # New parameter for dialogue
|
| 771 |
style_preset="no_style",
|
| 772 |
custom_style_text="",
|
| 773 |
+
max_resolution=1024, # New parameter for max resolution
|
| 774 |
):
|
| 775 |
"""
|
| 776 |
Generates an image with automatically determined aspect ratio based on layout position.
|
|
|
|
| 778 |
if randomize_seed:
|
| 779 |
seed = random.randint(0, MAX_SEED)
|
| 780 |
|
| 781 |
+
# Automatically determine image size based on position with custom max resolution
|
| 782 |
+
width, height = get_image_size_for_position(position_data, image_index, num_images, max_resolution)
|
| 783 |
|
| 784 |
# Set up the generator for reproducibility
|
| 785 |
generator = torch.Generator(device="cuda").manual_seed(seed)
|
|
|
|
| 924 |
value=8,
|
| 925 |
)
|
| 926 |
|
| 927 |
+
max_resolution = gr.Slider(
|
| 928 |
+
label="Max Resolution",
|
| 929 |
+
minimum=768,
|
| 930 |
+
maximum=1280,
|
| 931 |
+
step=128,
|
| 932 |
+
value=1024,
|
| 933 |
+
info="Maximum dimension for generated images (higher = better quality but slower)"
|
| 934 |
+
)
|
| 935 |
+
|
| 936 |
# Download link
|
| 937 |
pdf_output = gr.File(label="Download PDF", show_label=True, elem_id="pdf-download")
|
| 938 |
|
| 939 |
gr.Markdown("""**Note:** Your images and PDF are saved for up to 24 hours.
|
| 940 |
+
You can continue adding pages (up to 24) by clicking the generate button.""")
|
| 941 |
|
| 942 |
# Examples section in the left column
|
| 943 |
with gr.Accordion("Examples", open=True):
|
|
|
|
| 995 |
custom_style_text,
|
| 996 |
num_images_slider,
|
| 997 |
layout_dropdown,
|
| 998 |
+
max_resolution,
|
| 999 |
session_state,
|
| 1000 |
],
|
| 1001 |
outputs=[session_state, pdf_output, pdf_preview, run_button],
|