Julian Bilcke commited on
Commit
4cf61fa
·
1 Parent(s): a0da14a

using pdf preview

Browse files
Files changed (2) hide show
  1. app.py +11 -34
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
  import numpy as np
3
  import random
4
  import torch
@@ -668,8 +669,6 @@ def create_multi_page_pdf(session_manager: SessionManager) -> str:
668
  @spaces.GPU(duration=180) # Increased duration for up to 6 images
669
  def infer_page(
670
  prompt,
671
- seed=42,
672
- randomize_seed=False,
673
  guidance_scale=1.0,
674
  num_inference_steps=8,
675
  style_preset="no_style",
@@ -684,8 +683,6 @@ def infer_page(
684
 
685
  Args:
686
  prompt (str): The text prompt to generate images from.
687
- seed (int): The seed for the random number generator for reproducibility.
688
- randomize_seed (bool): If True, a random seed is used for each image.
689
  guidance_scale (float): Corresponds to `true_cfg_scale`.
690
  num_inference_steps (int): The number of denoising steps.
691
  style_preset (str): The key of the style preset to apply.
@@ -696,7 +693,7 @@ def infer_page(
696
  progress (gr.Progress): A Gradio Progress object to track generation.
697
 
698
  Returns:
699
- tuple: Updated session state, PDF path, preview image, page info, and updated button label.
700
  """
701
  # Initialize or retrieve session
702
  if session_state is None or "session_id" not in session_state:
@@ -710,7 +707,7 @@ def infer_page(
710
 
711
  # Check page limit
712
  if session_manager.metadata["total_pages"] >= 128:
713
- return session_state, None, None, "Maximum page limit (128) reached!", f"Page limit reached"
714
 
715
  generated_images = []
716
  used_seeds = []
@@ -723,7 +720,7 @@ def infer_page(
723
  for i in range(int(num_images)):
724
  progress((i + 0.5) / num_images, f"Generating image {i+1} of {num_images} for page {session_manager.metadata['total_pages'] + 1}")
725
 
726
- current_seed = seed + i if not randomize_seed else random.randint(0, MAX_SEED)
727
 
728
  # Get optimal aspect ratio based on position in layout
729
  position_data = get_layout_position_for_image(layout, int(num_images), i)
@@ -763,15 +760,11 @@ def infer_page(
763
  # Update session state
764
  session_state["page_count"] = page_num
765
 
766
- # Prepare page info
767
- seeds_str = ", ".join(str(s) for s in used_seeds)
768
- page_info = f"Page {page_num} added\nSeeds: {seeds_str}\nTotal pages: {page_num}"
769
-
770
  # Next button label
771
  next_page_num = page_num + 1
772
  button_label = f"Generate page {next_page_num}" if next_page_num <= 128 else "Page limit reached"
773
 
774
- return session_state, pdf_path, generated_images[0] if generated_images else None, page_info, button_label
775
 
776
  # New inference function with automatic aspect ratio
777
  def infer_single_auto(
@@ -921,27 +914,13 @@ with gr.Blocks(css=css) as demo:
921
  )
922
 
923
  with gr.Row():
924
- with gr.Column(scale=1):
925
- result_preview = gr.Image(label="Preview", show_label=True, type="pil")
926
- with gr.Column(scale=1):
927
- pdf_output = gr.File(label="Download PDF", show_label=True)
928
- page_info = gr.Textbox(label="Page Info", show_label=True, interactive=False, lines=3)
929
  gr.Markdown("""**Note:** Your images and PDF are saved for up to 24 hours.
930
  You can continue adding pages (up to 128) by clicking the generate button.""")
931
 
932
  with gr.Accordion("Advanced Settings", open=False):
933
- seed = gr.Slider(
934
- label="Seed",
935
- minimum=0,
936
- maximum=MAX_SEED,
937
- step=1,
938
- value=0,
939
- )
940
-
941
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
942
-
943
- # Removed prompt_enhance checkbox - story generation is now always enabled
944
-
945
  with gr.Row():
946
  guidance_scale = gr.Slider(
947
  label="Guidance scale (True CFG Scale)",
@@ -1004,8 +983,6 @@ with gr.Blocks(css=css) as demo:
1004
  fn=infer_page,
1005
  inputs=[
1006
  prompt,
1007
- seed,
1008
- randomize_seed,
1009
  guidance_scale,
1010
  num_inference_steps,
1011
  style_preset,
@@ -1014,19 +991,19 @@ with gr.Blocks(css=css) as demo:
1014
  layout_dropdown,
1015
  session_state,
1016
  ],
1017
- outputs=[session_state, pdf_output, result_preview, page_info, run_button],
1018
  )
1019
 
1020
  # Reset button functionality
1021
  def reset_session():
1022
  new_state = {"session_id": str(uuid.uuid4()), "page_count": 0}
1023
- return new_state, None, None, "", "Generate page 1"
1024
 
1025
  # Connect the reset button
1026
  reset_button.click(
1027
  fn=reset_session,
1028
  inputs=[],
1029
- outputs=[session_state, pdf_output, result_preview, page_info, run_button]
1030
  )
1031
 
1032
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from gradio_pdf import PDF
3
  import numpy as np
4
  import random
5
  import torch
 
669
  @spaces.GPU(duration=180) # Increased duration for up to 6 images
670
  def infer_page(
671
  prompt,
 
 
672
  guidance_scale=1.0,
673
  num_inference_steps=8,
674
  style_preset="no_style",
 
683
 
684
  Args:
685
  prompt (str): The text prompt to generate images from.
 
 
686
  guidance_scale (float): Corresponds to `true_cfg_scale`.
687
  num_inference_steps (int): The number of denoising steps.
688
  style_preset (str): The key of the style preset to apply.
 
693
  progress (gr.Progress): A Gradio Progress object to track generation.
694
 
695
  Returns:
696
+ tuple: Updated session state, PDF path, and updated button label.
697
  """
698
  # Initialize or retrieve session
699
  if session_state is None or "session_id" not in session_state:
 
707
 
708
  # Check page limit
709
  if session_manager.metadata["total_pages"] >= 128:
710
+ return session_state, None, None, f"Page limit reached"
711
 
712
  generated_images = []
713
  used_seeds = []
 
720
  for i in range(int(num_images)):
721
  progress((i + 0.5) / num_images, f"Generating image {i+1} of {num_images} for page {session_manager.metadata['total_pages'] + 1}")
722
 
723
+ current_seed = random.randint(0, MAX_SEED) # Always randomize seed
724
 
725
  # Get optimal aspect ratio based on position in layout
726
  position_data = get_layout_position_for_image(layout, int(num_images), i)
 
760
  # Update session state
761
  session_state["page_count"] = page_num
762
 
 
 
 
 
763
  # Next button label
764
  next_page_num = page_num + 1
765
  button_label = f"Generate page {next_page_num}" if next_page_num <= 128 else "Page limit reached"
766
 
767
+ return session_state, pdf_path, pdf_path, button_label
768
 
769
  # New inference function with automatic aspect ratio
770
  def infer_single_auto(
 
914
  )
915
 
916
  with gr.Row():
917
+ with gr.Column():
918
+ pdf_preview = PDF(label="PDF Preview", show_label=True, height=600, elem_id="pdf-preview")
919
+ pdf_output = gr.File(label="Download PDF", show_label=True, elem_id="pdf-download")
 
 
920
  gr.Markdown("""**Note:** Your images and PDF are saved for up to 24 hours.
921
  You can continue adding pages (up to 128) by clicking the generate button.""")
922
 
923
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
 
 
 
 
 
 
924
  with gr.Row():
925
  guidance_scale = gr.Slider(
926
  label="Guidance scale (True CFG Scale)",
 
983
  fn=infer_page,
984
  inputs=[
985
  prompt,
 
 
986
  guidance_scale,
987
  num_inference_steps,
988
  style_preset,
 
991
  layout_dropdown,
992
  session_state,
993
  ],
994
+ outputs=[session_state, pdf_output, pdf_preview, run_button],
995
  )
996
 
997
  # Reset button functionality
998
  def reset_session():
999
  new_state = {"session_id": str(uuid.uuid4()), "page_count": 0}
1000
+ return new_state, None, None, "Generate page 1"
1001
 
1002
  # Connect the reset button
1003
  reset_button.click(
1004
  fn=reset_session,
1005
  inputs=[],
1006
+ outputs=[session_state, pdf_output, pdf_preview, run_button]
1007
  )
1008
 
1009
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -7,4 +7,5 @@ sentencepiece
7
  dashscope
8
  pyyaml
9
  reportlab
10
- pypdf2
 
 
7
  dashscope
8
  pyyaml
9
  reportlab
10
+ pypdf2
11
+ gradio_pdf==0.0.22