Spaces:
Build error
Build error
Julian Bilcke commited on
Commit ·
c0c993a
1
Parent(s): 3678ee4
up
Browse files309 unmodified lines310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364301 unmodified lines66666766866967067124 unmodified lines69669769869970070170270329 unmodified lines73373473573673773814 unmodified lines75375475575675775875910 unmodified lines7707717727737747751 unmodified line777778779780781782783784138 unmodified lines92392492592692792892993093193293351 unmodified lines985986987988989990309 unmodified lines# --- UI Constants and Helpers ---MAX_SEED = np.iinfo(np.int32).maxdef get_image_size_for_position(position_data, image_index, num_images):"""Determines optimal image size based on its position in the layout.Args:position_data: Layout position data [x, y, width, height] in relative unitsimage_index: Index of the current image (0-based)num_images: Total number of images in the layoutReturns:tuple: (width, height) optimized for the position's aspect ratio, max 1024 in any dimension"""if not position_data:return 1024, 1024 # Default squarex_rel, y_rel, w_rel, h_rel = position_dataaspect_ratio = w_rel / h_rel if h_rel > 0 else 1.0# Max dimension is 1024max_dim = 1024# Calculate dimensions maintaining aspect ratio with max of 1024if aspect_ratio >= 1: # Wider than tallwidth = max_dimheight = int(max_dim / aspect_ratio)# Ensure height is at least 256 for qualityif height < 256:height = 256width = int(256 * aspect_ratio)else: # Taller than wideheight = max_dimwidth = int(max_dim * aspect_ratio)# Ensure width is at least 256 for qualityif width < 256:width = 256height = int(256 / aspect_ratio)# Round to nearest 64 for better compatibilitywidth = (width // 64) * 64height = (height // 64) * 64# Ensure we don't exceed max_dim after roundingif width > max_dim:width = max_dimif height > max_dim:height = max_dim# Minimum size checkwidth = max(width, 256)height = max(height, 256)return width, height301 unmodified linescustom_style_text="",num_images=1,layout="default",session_state=None,progress=gr.Progress(track_tqdm=True),):24 unmodified linesif random.random() < 0.1: # 10% chance to cleanup on each requestsession_manager.cleanup_old_sessions()# Check page limitif session_manager.metadata["total_pages"] >= 128:return session_state, None, None, f"Page limit reached"generated_images = []29 unmodified linesdialogue=scene_dialogue, # Pass dialogue separatelystyle_preset=style_preset,custom_style_text=custom_style_text,)generated_images.append(image)14 unmodified lines# Next button labelnext_page_num = page_num + 1button_label = f"Generate page {next_page_num}" if next_page_num <= 128 else "Page limit reached"return session_state, pdf_path, pdf_path, button_label10 unmodified linesdialogue="", # New parameter for dialoguestyle_preset="no_style",custom_style_text="",):"""Generates an image with automatically determined aspect ratio based on layout position.1 unmodified lineif randomize_seed:seed = random.randint(0, MAX_SEED)# Automatically determine image size based on positionwidth, height = get_image_size_for_position(position_data, image_index, num_images)# Set up the generator for reproducibilitygenerator = torch.Generator(device="cuda").manual_seed(seed)138 unmodified linesvalue=8,)# Download linkpdf_output = gr.File(label="Download PDF", show_label=True, elem_id="pdf-download")gr.Markdown("""**Note:** Your images and PDF are saved for up to 24 hours.You can continue adding pages (up to 128) by clicking the generate button.""")# Examples section in the left columnwith gr.Accordion("Examples", open=True):51 unmodified linescustom_style_text,num_images_slider,layout_dropdown,session_state,],outputs=[session_state, pdf_output, pdf_preview, run_button],309 unmodified lines310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362301 unmodified lines66466566666766866967024 unmodified lines69569669769869970070170229 unmodified lines73273373473573673773814 unmodified lines75375475575675775875910 unmodified lines7707717727737747757761 unmodified line778779780781782783784785138 unmodified lines92492592692792892993093193293393493593693793893994094194294351 unmodified lines99599699799899910001001309 unmodified lines# --- UI Constants and Helpers ---MAX_SEED = np.iinfo(np.int32).maxdef get_image_size_for_position(position_data, image_index, num_images, max_resolution=1024):"""Determines optimal image size based on its position in the layout.Args:position_data: Layout position data [x, y, width, height] in relative unitsimage_index: Index of the current image (0-based)num_images: Total number of images in the layoutmax_resolution: Maximum resolution for any dimension (default 1024)Returns:tuple: (width, height) optimized for the position's aspect ratio"""if not position_data:return max_resolution, max_resolution # Default squarex_rel, y_rel, w_rel, h_rel = position_dataaspect_ratio = w_rel / h_rel if h_rel > 0 else 1.0# Calculate dimensions maintaining aspect ratioif aspect_ratio >= 1: # Wider than tallwidth = max_resolutionheight = int(max_resolution / aspect_ratio)# Ensure height is at least 384 for qualityif height < 384:height = 384width = int(384 * aspect_ratio)else: # Taller than wideheight = max_resolutionwidth = int(max_resolution * aspect_ratio)# Ensure width is at least 384 for qualityif width < 384:width = 384height = int(384 / aspect_ratio)# Round to nearest 64 for better compatibilitywidth = (width // 64) * 64height = (height // 64) * 64# Ensure we don't exceed max_resolution after roundingif width > max_resolution:width = max_resolutionif height > max_resolution:height = max_resolution# Minimum size check (increased from 256 to 384 for better quality)width = max(width, 384)height = max(height, 384)return width, height301 unmodified linescustom_style_text="",num_images=1,layout="default",max_resolution=1024,session_state=None,progress=gr.Progress(track_tqdm=True),):24 unmodified linesif random.random() < 0.1: # 10% chance to cleanup on each requestsession_manager.cleanup_old_sessions()# Check page limit (reduced to 24 for performance)if session_manager.metadata["total_pages"] >= 24:return session_state, None, None, f"Page limit reached"generated_images = []29 unmodified linesdialogue=scene_dialogue, # Pass dialogue separatelystyle_preset=style_preset,custom_style_text=custom_style_text,max_resolution=max_resolution,)generated_images.append(image)14 unmodified lines# Next button labelnext_page_num = page_num + 1button_label = f"Generate page {next_page_num}" if next_page_num <= 24 else "Page limit reached"return session_state, pdf_path, pdf_path, button_label10 unmodified linesdialogue="", # New parameter for dialoguestyle_preset="no_style",custom_style_text="",max_resolution=1024, # New parameter for max resolution):"""Generates an image with automatically determined aspect ratio based on layout position.1 unmodified lineif randomize_seed:seed = random.randint(0, MAX_SEED)# Automatically determine image size based on position with custom max resolutionwidth, height = get_image_size_for_position(position_data, image_index, num_images, max_resolution)# Set up the generator for reproducibilitygenerator = torch.Generator(device="cuda").manual_seed(seed)138 unmodified linesvalue=8,)max_resolution = gr.Slider(label="Max Resolution",minimum=768,maximum=1280,step=128,value=1024,info="Maximum dimension for generated images (higher = better quality but slower)")# Download linkpdf_output = gr.File(label="Download PDF", show_label=True, elem_id="pdf-download")gr.Markdown("""**Note:** Your images and PDF are saved for up to 24 hours.You can continue adding pages (up to 24) by clicking the generate button.""")# Examples section in the left columnwith gr.Accordion("Examples", open=True):51 unmodified linescustom_style_text,num_images_slider,layout_dropdown,max_resolution,session_state,],outputs=[session_state, pdf_output, pdf_preview, run_button],
app.py+44-33