Surn commited on
Commit
8f70be7
·
1 Parent(s): 1eb860a

Bug Fix in Sketch affecting other areas

Browse files
Files changed (2) hide show
  1. README.md +2 -2
  2. app.py +66 -43
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🌖
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
- python_version: 3.12.8
8
  sdk_version: 5.22.0
9
  app_file: app.py
10
  pinned: false
@@ -24,7 +24,7 @@ thumbnail: >-
24
 
25
  # Hex Game Maker
26
  ## Description
27
- Welcome to Hex Game Maker, the ultimate tool for transforming your images into mesmerizing hexagon grid masterpieces! Well, this was a test project for HexaGrid. It has some nice features that did not make it into the final version of the program.
28
  The intention was to do a full conversion, but the limitation on Negative Prompts is what killed this approach. It does not consistently render table top maps.. but it can do a lot!
29
 
30
  ### What Can You Do?
 
4
  colorFrom: yellow
5
  colorTo: purple
6
  sdk: gradio
7
+ python_version: 3.12.3
8
  sdk_version: 5.22.0
9
  app_file: app.py
10
  pinned: false
 
24
 
25
  # Hex Game Maker
26
  ## Description
27
+ Welcome to Hex Game Maker, the ultimate tool for transforming your images into mesmerizing hexagon grid masterpieces! **Well, this is a test project for HexaGrid.** It has some nice features that did not make it into the final version of the program.
28
  The intention was to do a full conversion, but the limitation on Negative Prompts is what killed this approach. It does not consistently render table top maps.. but it can do a lot!
29
 
30
  ### What Can You Do?
app.py CHANGED
@@ -54,6 +54,7 @@ from modules.constants import (
54
  default_lut_example_img,
55
  lut_files,
56
  MAX_SEED,
 
57
  # lut_folder,cards,
58
  # cards_alternating,
59
  # card_colors,
@@ -348,9 +349,14 @@ def generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scal
348
  flash_attention_enabled = torch.backends.cuda.flash_sdp_enabled()
349
  if flash_attention_enabled:
350
  pipe.attn_implementation="flash_attention_2"
351
- # Compile UNet
352
- #pipe.transformer = torch.compile(pipe.transformer, mode="reduce-overhead")
353
- pipe.vae.enable_tiling() # For larger resolutions if needed
 
 
 
 
 
354
 
355
  # Disable unnecessary features
356
  pipe.safety_checker = None
@@ -384,12 +390,18 @@ def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps
384
  flash_attention_enabled = torch.backends.cuda.flash_sdp_enabled()
385
  if flash_attention_enabled:
386
  pipe_i2i.attn_implementation="flash_attention_2"
387
- # Compile UNet
388
- #pipe.transformer = torch.compile(pipe.transformer, mode="reduce-overhead")
389
- pipe.vae.enable_tiling() # For larger resolutions if needed
 
 
 
 
 
 
390
 
391
  # Disable unnecessary features
392
- pipe.safety_checker = None
393
  image_input = open_image(image_input_path)
394
  print(f"\nGenerating image with prompt: {prompt_mash} and {image_input_path}\n")
395
  approx_tokens= approximate_token_count(prompt_mash)
@@ -399,22 +411,24 @@ def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps
399
  else:
400
  prompt = prompt_mash
401
  prompt2 = None
402
- final_image = pipe_i2i(
403
- prompt=prompt,
404
- prompt_2=prompt2,
405
- image=image_input,
406
- strength=image_strength,
407
- num_inference_steps=steps,
408
- guidance_scale=cfg_scale,
409
- width=width,
410
- height=height,
411
- generator=generator,
412
- joint_attention_kwargs={"scale": lora_scale},
413
- output_type="pil",
414
- ).images[0]
 
 
415
  return final_image
416
 
417
- @spaces.GPU(duration=140)
418
  def run_lora(prompt, map_option, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge, use_conditioned_image=False, progress=gr.Progress(track_tqdm=True)):
419
  if selected_index is None:
420
  raise gr.Error("You must select a LoRA before proceeding.🧨")
@@ -422,6 +436,8 @@ def run_lora(prompt, map_option, image_input, image_strength, cfg_scale, steps,
422
  # handle selecting a conditioned image from the gallery
423
  global current_prerendered_image
424
  conditioned_image=None
 
 
425
  if use_conditioned_image:
426
  print(f"Conditioned path: {current_prerendered_image.value}.. converting to RGB\n")
427
  # ensure the conditioned image is an image and not a string, cannot use RGBA
@@ -468,15 +484,18 @@ def run_lora(prompt, map_option, image_input, image_strength, cfg_scale, steps,
468
 
469
  if(image_input is not None):
470
  print(f"\nGenerating image to image with seed: {seed}\n")
471
- final_image = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed, progress)
 
472
  if enlarge:
473
- upscaled_image = upscale_image(final_image, max(1.0,min((TARGET_SIZE[0]/width),(TARGET_SIZE[1]/height))))
474
- # Save the upscaled image to a temporary file
475
- with NamedTemporaryFile(delete=False, suffix=".png") as tmp_upscaled:
476
- upscaled_image.save(tmp_upscaled.name, format="PNG")
477
- temp_files.append(tmp_upscaled.name)
478
- print(f"Upscaled image saved to {tmp_upscaled.name}")
479
- final_image = tmp_upscaled.name
 
 
480
  yield final_image, seed, gr.update(visible=False)
481
  else:
482
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress)
@@ -485,18 +504,20 @@ def run_lora(prompt, map_option, image_input, image_strength, cfg_scale, steps,
485
  step_counter = 0
486
  for image in image_generator:
487
  step_counter+=1
488
- final_image = image
489
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
490
  yield image, seed, gr.update(value=progress_bar, visible=True)
491
 
492
  if enlarge:
493
- upscaled_image = upscale_image(final_image, max(1.0,min((TARGET_SIZE[0]/width),(TARGET_SIZE[1]/height))))
494
- # Save the upscaled image to a temporary file
495
- with NamedTemporaryFile(delete=False, suffix=".png") as tmp_upscaled:
496
- upscaled_image.save(tmp_upscaled.name, format="PNG")
497
- temp_files.append(tmp_upscaled.name)
498
- print(f"Upscaled image saved to {tmp_upscaled.name}")
499
- final_image = tmp_upscaled.name
 
 
500
  yield final_image, seed, gr.update(value=progress_bar, visible=False)
501
 
502
  def get_huggingface_safetensors(link):
@@ -631,9 +652,11 @@ def update_sketch_dimensions(input_image, sketch_image):
631
  sk_img_path, _ = get_image_from_dict(sketch_image)
632
  sk_img = open_image(sk_img_path)
633
  # Resize sketch image if dimensions don't match input image.
634
- if in_img.size != sk_img.size:
635
  sk_img = sk_img.resize(in_img.size, Image.LANCZOS)
636
- return sk_img
 
 
637
 
638
  @spaces.GPU()
639
  def getVersions():
@@ -722,7 +745,7 @@ with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',
722
  lut_intensity = gr.Slider(label="Filter Intensity", minimum=-200, maximum=200, value=100, info="0 none, negative inverts the filter", interactive=True)
723
  apply_lut_button = gr.Button("Apply Filter (LUT)", elem_classes="solid", elem_id="apply_lut_button")
724
  with gr.Row():
725
- lut_example_image = gr.Image(type="pil", label="Filter (LUT) Example Image", value=default_lut_example_img)
726
  with gr.Row():
727
  with gr.Column():
728
  gr.Markdown("""
@@ -933,7 +956,7 @@ with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',
933
  ).then(
934
  fn=update_sketch_dimensions,
935
  inputs=[input_image, sketch_image],
936
- outputs=[sketch_image]
937
  )
938
  prerendered_image_gallery.select(
939
  fn=on_prerendered_gallery_selection,
@@ -960,7 +983,7 @@ with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',
960
  ).then(
961
  fn=update_sketch_dimensions,
962
  inputs=[input_image, sketch_image],
963
- outputs=[sketch_image]
964
  )
965
  lora_gallery.select(
966
  update_selection,
@@ -984,7 +1007,7 @@ with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',
984
  ).then(
985
  fn=update_sketch_dimensions,
986
  inputs=[input_image, sketch_image],
987
- outputs=[sketch_image]
988
  )
989
 
990
  load_env_vars(dotenv_path)
 
54
  default_lut_example_img,
55
  lut_files,
56
  MAX_SEED,
57
+ IS_SHARED_SPACE,
58
  # lut_folder,cards,
59
  # cards_alternating,
60
  # card_colors,
 
349
  flash_attention_enabled = torch.backends.cuda.flash_sdp_enabled()
350
  if flash_attention_enabled:
351
  pipe.attn_implementation="flash_attention_2"
352
+
353
+ if IS_SHARED_SPACE:
354
+ pipe.vae.enable_tiling() # For larger resolutions if needed
355
+ else:
356
+ # Compile UNet
357
+ #pipe.transformer = torch.compile(pipe.transformer, mode="reduce-overhead")
358
+ #pipe.enable_model_cpu_offload() #for smaller GPUs
359
+ pipe.vae.enable_slicing()
360
 
361
  # Disable unnecessary features
362
  pipe.safety_checker = None
 
390
  flash_attention_enabled = torch.backends.cuda.flash_sdp_enabled()
391
  if flash_attention_enabled:
392
  pipe_i2i.attn_implementation="flash_attention_2"
393
+
394
+ if IS_SHARED_SPACE:
395
+ pipe_i2i.vae.enable_tiling() # For larger resolutions if needed
396
+ else:
397
+ # Compile UNet
398
+ # pipe.transformer = torch.compile(pipe.transformer, mode="reduce-overhead") # uses the other pipe's transformer
399
+ # pipe_i2i.enable_model_cpu_offload() #for smaller GPUs
400
+ pipe_i2i.vae.enable_slicing()
401
+
402
 
403
  # Disable unnecessary features
404
+ pipe_i2i.safety_checker = None
405
  image_input = open_image(image_input_path)
406
  print(f"\nGenerating image with prompt: {prompt_mash} and {image_input_path}\n")
407
  approx_tokens= approximate_token_count(prompt_mash)
 
411
  else:
412
  prompt = prompt_mash
413
  prompt2 = None
414
+ with calculateDuration("Generating image"):
415
+ # Generate image
416
+ final_image = pipe_i2i(
417
+ prompt=prompt,
418
+ prompt_2=prompt2,
419
+ image=image_input,
420
+ strength=image_strength,
421
+ num_inference_steps=steps,
422
+ guidance_scale=cfg_scale,
423
+ width=width,
424
+ height=height,
425
+ generator=generator,
426
+ joint_attention_kwargs={"scale": lora_scale},
427
+ output_type="pil",
428
+ ).images[0]
429
  return final_image
430
 
431
+ @spaces.GPU(duration=140,progress=gr.Progress(track_tqdm=True))
432
  def run_lora(prompt, map_option, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge, use_conditioned_image=False, progress=gr.Progress(track_tqdm=True)):
433
  if selected_index is None:
434
  raise gr.Error("You must select a LoRA before proceeding.🧨")
 
436
  # handle selecting a conditioned image from the gallery
437
  global current_prerendered_image
438
  conditioned_image=None
439
+ formatted_map_option = map_option.lower().replace(' ', '_')
440
+
441
  if use_conditioned_image:
442
  print(f"Conditioned path: {current_prerendered_image.value}.. converting to RGB\n")
443
  # ensure the conditioned image is an image and not a string, cannot use RGBA
 
484
 
485
  if(image_input is not None):
486
  print(f"\nGenerating image to image with seed: {seed}\n")
487
+ generated_image = generate_image_to_image(prompt_mash, image_input, image_strength, steps, cfg_scale, width, height, lora_scale, seed, progress)
488
+
489
  if enlarge:
490
+ upscaled_image = upscale_image(generated_image, max(1.0,min((TARGET_SIZE[0]/width),(TARGET_SIZE[1]/height))))
491
+ else:
492
+ upscaled_image = generated_image
493
+ # Save the upscaled image to a temporary file
494
+ with NamedTemporaryFile(delete=False, suffix=".png", prefix=f"{formatted_map_option}_") as tmp_upscaled:
495
+ upscaled_image.save(tmp_upscaled.name, format="PNG")
496
+ temp_files.append(tmp_upscaled.name)
497
+ print(f"Upscaled image saved to {tmp_upscaled.name}")
498
+ final_image = tmp_upscaled.name
499
  yield final_image, seed, gr.update(visible=False)
500
  else:
501
  image_generator = generate_image(prompt_mash, steps, seed, cfg_scale, width, height, lora_scale, progress)
 
504
  step_counter = 0
505
  for image in image_generator:
506
  step_counter+=1
507
+ generated_image = image
508
  progress_bar = f'<div class="progress-container"><div class="progress-bar" style="--current: {step_counter}; --total: {steps};"></div></div>'
509
  yield image, seed, gr.update(value=progress_bar, visible=True)
510
 
511
  if enlarge:
512
+ upscaled_image = upscale_image(generated_image, max(1.0,min((TARGET_SIZE[0]/width),(TARGET_SIZE[1]/height))))
513
+ else:
514
+ upscaled_image = generated_image
515
+ # Save the upscaled image to a temporary file
516
+ with NamedTemporaryFile(delete=False, suffix=".png", prefix=f"{formatted_map_option}_") as tmp_upscaled:
517
+ upscaled_image.save(tmp_upscaled.name, format="PNG")
518
+ temp_files.append(tmp_upscaled.name)
519
+ print(f"Upscaled image saved to {tmp_upscaled.name}")
520
+ final_image = tmp_upscaled.name
521
  yield final_image, seed, gr.update(value=progress_bar, visible=False)
522
 
523
  def get_huggingface_safetensors(link):
 
652
  sk_img_path, _ = get_image_from_dict(sketch_image)
653
  sk_img = open_image(sk_img_path)
654
  # Resize sketch image if dimensions don't match input image.
655
+ if (in_img) and (in_img.size != sk_img.size):
656
  sk_img = sk_img.resize(in_img.size, Image.LANCZOS)
657
+ return [sk_img, gr.update(width=in_img.width, height=in_img.height)]
658
+ else:
659
+ return [sk_img, gr.update()]
660
 
661
  @spaces.GPU()
662
  def getVersions():
 
745
  lut_intensity = gr.Slider(label="Filter Intensity", minimum=-200, maximum=200, value=100, info="0 none, negative inverts the filter", interactive=True)
746
  apply_lut_button = gr.Button("Apply Filter (LUT)", elem_classes="solid", elem_id="apply_lut_button")
747
  with gr.Row():
748
+ lut_example_image = gr.Image(type="pil", label="Filter (LUT) Example Image", value=default_lut_example_img, format="png")
749
  with gr.Row():
750
  with gr.Column():
751
  gr.Markdown("""
 
956
  ).then(
957
  fn=update_sketch_dimensions,
958
  inputs=[input_image, sketch_image],
959
+ outputs=[sketch_image, sketch_image]
960
  )
961
  prerendered_image_gallery.select(
962
  fn=on_prerendered_gallery_selection,
 
983
  ).then(
984
  fn=update_sketch_dimensions,
985
  inputs=[input_image, sketch_image],
986
+ outputs=[sketch_image, sketch_image]
987
  )
988
  lora_gallery.select(
989
  update_selection,
 
1007
  ).then(
1008
  fn=update_sketch_dimensions,
1009
  inputs=[input_image, sketch_image],
1010
+ outputs=[sketch_image, sketch_image]
1011
  )
1012
 
1013
  load_env_vars(dotenv_path)