Oysiyl commited on
Commit
515964d
·
1 Parent(s): 6587064

add more params in advanced settings

Browse files
Files changed (1) hide show
  1. app.py +90 -23
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import random
3
  import sys
4
  from typing import Sequence, Mapping, Any, Union
5
- from functools import partial
6
  import torch
7
  import gradio as gr
8
  from PIL import Image
@@ -177,7 +176,7 @@ valid_models = [
177
  model_management.load_models_gpu(valid_models)
178
 
179
  @spaces.GPU(duration=30)
180
- def generate_qr_code_unified(prompt: str, text_input: str, input_type: str = "URL", image_size: int = 512, border_size: int = 4, error_correction: str = "Medium (15%)", module_size: int = 12, module_drawer: str = "Square", use_custom_seed: bool = False, seed: int = 0, pipeline: str = "standard"):
181
  # Only manipulate the text if it's a URL input type
182
  qr_text = text_input
183
  if input_type == "URL":
@@ -193,7 +192,15 @@ def generate_qr_code_unified(prompt: str, text_input: str, input_type: str = "UR
193
  if pipeline == "standard":
194
  yield from _pipeline_standard(prompt, qr_text, input_type, image_size, border_size, error_correction, module_size, module_drawer, actual_seed)
195
  else: # artistic
196
- yield from _pipeline_artistic(prompt, qr_text, input_type, image_size, border_size, error_correction, module_size, module_drawer, actual_seed)
 
 
 
 
 
 
 
 
197
 
198
  def add_noise_to_border_only(image_tensor, seed: int, border_size: int, image_size: int, module_size: int = 12):
199
  """
@@ -433,7 +440,7 @@ def _pipeline_standard(prompt: str, qr_text: str, input_type: str, image_size: i
433
  pil_image = Image.fromarray(image_np)
434
  yield pil_image, "No errors, all good! Final QR art generated."
435
 
436
- def _pipeline_artistic(prompt: str, qr_text: str, input_type: str, image_size: int, border_size: int, error_correction: str, module_size: int, module_drawer: str, seed: int):
437
  # Generate QR code
438
  qr_protocol = "None" if input_type == "Plain Text" else "Https"
439
 
@@ -549,21 +556,22 @@ def _pipeline_artistic(prompt: str, qr_text: str, input_type: str, image_size: i
549
  freeu = FreeU_V2()
550
  freeu_model = freeu.patch(
551
  model=base_model,
552
- b1=1.3, # Backbone feature enhancement - improves fine details (reduced for more blending)
553
- b2=1.4, # Backbone feature enhancement (layer 2) - improves textures (reduced)
554
- s1=0.9, # Skip connection dampening - reduces QR structure visibility (increased)
555
- s2=0.2 # Skip connection dampening (layer 2) - hides more cubics (increased)
556
  )[0]
557
 
558
- # Apply SEG (Self-Attention Guidance) for improved structural coherence
559
- # DISABLED: SEG blurs attention maps which rounds position marker corners, affecting scannability
560
- # smoothed_energy = NODE_CLASS_MAPPINGS["SelfAttentionGuidance"]()
561
- # enhanced_model = smoothed_energy.patch(
562
- # model=freeu_model,
563
- # scale=1.5,
564
- # blur_sigma=0.5,
565
- # )[0]
566
- enhanced_model = freeu_model # Use only FreeU, skip SEG
 
567
 
568
  # First sampling pass
569
  samples = ksampler.sample(
@@ -616,7 +624,7 @@ def _pipeline_artistic(prompt: str, qr_text: str, input_type: str, image_size: i
616
  step_msg = "Image upscaled (step 4/5)... final refinement pass" if border_size > 0 else "Image upscaled (step 3/4)... final refinement pass"
617
  yield upscaled_pil, step_msg
618
 
619
- # Final ControlNet pass
620
  controlnet_apply_final = controlnetapplyadvanced.apply_controlnet(
621
  strength=0.7,
622
  start_percent=0,
@@ -819,7 +827,7 @@ if __name__ == "__main__":
819
 
820
  # When clicking the button, it will trigger the main function
821
  generate_btn.click(
822
- fn=partial(generate_qr_code_unified, pipeline="standard"),
823
  inputs=[prompt_input, text_input, input_type, image_size, border_size, error_correction, module_size, module_drawer, use_custom_seed, seed],
824
  outputs=[output_image, error_message]
825
  )
@@ -941,7 +949,7 @@ if __name__ == "__main__":
941
  module_drawer
942
  ],
943
  outputs=[output_image, error_message],
944
- fn=partial(generate_qr_code_unified, pipeline="standard"),
945
  cache_examples=False
946
  )
947
 
@@ -1033,6 +1041,65 @@ if __name__ == "__main__":
1033
  info="Seed value for reproducibility. Same seed with same settings will produce the same result."
1034
  )
1035
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1036
  # Add style examples with labels
1037
  gr.Markdown("### Style Examples:")
1038
 
@@ -1074,8 +1141,8 @@ if __name__ == "__main__":
1074
 
1075
  # When clicking the button, it will trigger the artistic function
1076
  artistic_generate_btn.click(
1077
- fn=partial(generate_qr_code_unified, pipeline="artistic"),
1078
- inputs=[artistic_prompt_input, artistic_text_input, artistic_input_type, artistic_image_size, artistic_border_size, artistic_error_correction, artistic_module_size, artistic_module_drawer, artistic_use_custom_seed, artistic_seed],
1079
  outputs=[artistic_output_image, artistic_error_message]
1080
  )
1081
 
@@ -1196,7 +1263,7 @@ if __name__ == "__main__":
1196
  artistic_module_drawer
1197
  ],
1198
  outputs=[artistic_output_image, artistic_error_message],
1199
- fn=partial(generate_qr_code_unified, pipeline="artistic"),
1200
  cache_examples=False
1201
  )
1202
 
 
2
  import random
3
  import sys
4
  from typing import Sequence, Mapping, Any, Union
 
5
  import torch
6
  import gradio as gr
7
  from PIL import Image
 
176
  model_management.load_models_gpu(valid_models)
177
 
178
  @spaces.GPU(duration=30)
179
+ def generate_qr_code_unified(prompt: str, text_input: str, input_type: str = "URL", image_size: int = 512, border_size: int = 4, error_correction: str = "Medium (15%)", module_size: int = 12, module_drawer: str = "Square", use_custom_seed: bool = False, seed: int = 0, pipeline: str = "standard", freeu_b1: float = 1.2, freeu_b2: float = 1.3, freeu_s1: float = 1.0, freeu_s2: float = 0.6, enable_sag: bool = True, sag_scale: float = 1.5, sag_blur_sigma: float = 1.5):
180
  # Only manipulate the text if it's a URL input type
181
  qr_text = text_input
182
  if input_type == "URL":
 
192
  if pipeline == "standard":
193
  yield from _pipeline_standard(prompt, qr_text, input_type, image_size, border_size, error_correction, module_size, module_drawer, actual_seed)
194
  else: # artistic
195
+ yield from _pipeline_artistic(prompt, qr_text, input_type, image_size, border_size, error_correction, module_size, module_drawer, actual_seed, freeu_b1, freeu_b2, freeu_s1, freeu_s2, enable_sag, sag_scale, sag_blur_sigma)
196
+
197
+ def generate_standard_qr(prompt: str, text_input: str, input_type: str = "URL", image_size: int = 512, border_size: int = 4, error_correction: str = "Medium (15%)", module_size: int = 12, module_drawer: str = "Square", use_custom_seed: bool = False, seed: int = 0):
198
+ """Wrapper function for standard QR generation"""
199
+ yield from generate_qr_code_unified(prompt, text_input, input_type, image_size, border_size, error_correction, module_size, module_drawer, use_custom_seed, seed, pipeline="standard")
200
+
201
+ def generate_artistic_qr(prompt: str, text_input: str, input_type: str = "URL", image_size: int = 512, border_size: int = 4, error_correction: str = "Medium (15%)", module_size: int = 12, module_drawer: str = "Square", use_custom_seed: bool = False, seed: int = 0, freeu_b1: float = 1.2, freeu_b2: float = 1.3, freeu_s1: float = 1.0, freeu_s2: float = 0.6, enable_sag: bool = True, sag_scale: float = 1.5, sag_blur_sigma: float = 1.5):
202
+ """Wrapper function for artistic QR generation with FreeU and SAG parameters"""
203
+ yield from generate_qr_code_unified(prompt, text_input, input_type, image_size, border_size, error_correction, module_size, module_drawer, use_custom_seed, seed, pipeline="artistic", freeu_b1=freeu_b1, freeu_b2=freeu_b2, freeu_s1=freeu_s1, freeu_s2=freeu_s2, enable_sag=enable_sag, sag_scale=sag_scale, sag_blur_sigma=sag_blur_sigma)
204
 
205
  def add_noise_to_border_only(image_tensor, seed: int, border_size: int, image_size: int, module_size: int = 12):
206
  """
 
440
  pil_image = Image.fromarray(image_np)
441
  yield pil_image, "No errors, all good! Final QR art generated."
442
 
443
+ def _pipeline_artistic(prompt: str, qr_text: str, input_type: str, image_size: int, border_size: int, error_correction: str, module_size: int, module_drawer: str, seed: int, freeu_b1: float = 1.2, freeu_b2: float = 1.3, freeu_s1: float = 1.0, freeu_s2: float = 0.6, enable_sag: bool = True, sag_scale: float = 1.5, sag_blur_sigma: float = 1.5):
444
  # Generate QR code
445
  qr_protocol = "None" if input_type == "Plain Text" else "Https"
446
 
 
556
  freeu = FreeU_V2()
557
  freeu_model = freeu.patch(
558
  model=base_model,
559
+ b1=freeu_b1, # Backbone feature enhancement - customizable
560
+ b2=freeu_b2, # Backbone feature enhancement (layer 2) - customizable
561
+ s1=freeu_s1, # Skip connection dampening - customizable structure hiding
562
+ s2=freeu_s2 # Skip connection dampening (layer 2) - customizable scannability balance
563
  )[0]
564
 
565
+ # Apply SAG (Self-Attention Guidance) for improved structural coherence (if enabled)
566
+ if enable_sag:
567
+ smoothed_energy = NODE_CLASS_MAPPINGS["SelfAttentionGuidance"]()
568
+ enhanced_model = smoothed_energy.patch(
569
+ model=freeu_model,
570
+ scale=sag_scale, # SAG guidance scale - customizable
571
+ blur_sigma=sag_blur_sigma, # Blur amount - customizable artistic blending
572
+ )[0]
573
+ else:
574
+ enhanced_model = freeu_model
575
 
576
  # First sampling pass
577
  samples = ksampler.sample(
 
624
  step_msg = "Image upscaled (step 4/5)... final refinement pass" if border_size > 0 else "Image upscaled (step 3/4)... final refinement pass"
625
  yield upscaled_pil, step_msg
626
 
627
+ # Final ControlNet pass (second pass - upscaled refinement)
628
  controlnet_apply_final = controlnetapplyadvanced.apply_controlnet(
629
  strength=0.7,
630
  start_percent=0,
 
827
 
828
  # When clicking the button, it will trigger the main function
829
  generate_btn.click(
830
+ fn=generate_standard_qr,
831
  inputs=[prompt_input, text_input, input_type, image_size, border_size, error_correction, module_size, module_drawer, use_custom_seed, seed],
832
  outputs=[output_image, error_message]
833
  )
 
949
  module_drawer
950
  ],
951
  outputs=[output_image, error_message],
952
+ fn=generate_standard_qr,
953
  cache_examples=False
954
  )
955
 
 
1041
  info="Seed value for reproducibility. Same seed with same settings will produce the same result."
1042
  )
1043
 
1044
+ # FreeU Parameters
1045
+ gr.Markdown("### FreeU Quality Enhancement")
1046
+ freeu_b1 = gr.Slider(
1047
+ minimum=1.0,
1048
+ maximum=1.6,
1049
+ step=0.01,
1050
+ value=1.2,
1051
+ label="FreeU B1 (Backbone 1)",
1052
+ info="Backbone feature enhancement for first layer. Higher values improve detail but may reduce blending. Range: 1.0-1.6, Default: 1.2"
1053
+ )
1054
+ freeu_b2 = gr.Slider(
1055
+ minimum=1.0,
1056
+ maximum=1.6,
1057
+ step=0.01,
1058
+ value=1.3,
1059
+ label="FreeU B2 (Backbone 2)",
1060
+ info="Backbone feature enhancement for second layer. Higher values improve texture. Range: 1.0-1.6, Default: 1.3"
1061
+ )
1062
+ freeu_s1 = gr.Slider(
1063
+ minimum=0.0,
1064
+ maximum=1.5,
1065
+ step=0.01,
1066
+ value=1.0,
1067
+ label="FreeU S1 (Skip 1)",
1068
+ info="Skip connection dampening for first layer. Lower values hide QR structure more. Range: 0.0-1.5, Default: 1.0"
1069
+ )
1070
+ freeu_s2 = gr.Slider(
1071
+ minimum=0.0,
1072
+ maximum=1.5,
1073
+ step=0.01,
1074
+ value=0.6,
1075
+ label="FreeU S2 (Skip 2)",
1076
+ info="Skip connection dampening for second layer. Balances scannability. Range: 0.0-1.5, Default: 0.6"
1077
+ )
1078
+
1079
+ # SAG (Self-Attention Guidance) Parameters
1080
+ gr.Markdown("### SAG (Self-Attention Guidance)")
1081
+ enable_sag = gr.Checkbox(
1082
+ label="Enable SAG",
1083
+ value=False,
1084
+ info="Enable Self-Attention Guidance for improved structural coherence and artistic blending"
1085
+ )
1086
+ sag_scale = gr.Slider(
1087
+ minimum=0.0,
1088
+ maximum=3.0,
1089
+ step=0.1,
1090
+ value=1.5,
1091
+ label="SAG Scale",
1092
+ info="Guidance strength. Higher values provide more structural coherence. Range: 0.0-3.0, Default: 1.5"
1093
+ )
1094
+ sag_blur_sigma = gr.Slider(
1095
+ minimum=0.0,
1096
+ maximum=5.0,
1097
+ step=0.1,
1098
+ value=1.5,
1099
+ label="SAG Blur Sigma",
1100
+ info="Blur amount for artistic blending. Higher values create softer, more artistic effects. Range: 0.0-5.0, Default: 1.5"
1101
+ )
1102
+
1103
  # Add style examples with labels
1104
  gr.Markdown("### Style Examples:")
1105
 
 
1141
 
1142
  # When clicking the button, it will trigger the artistic function
1143
  artistic_generate_btn.click(
1144
+ fn=generate_artistic_qr,
1145
+ inputs=[artistic_prompt_input, artistic_text_input, artistic_input_type, artistic_image_size, artistic_border_size, artistic_error_correction, artistic_module_size, artistic_module_drawer, artistic_use_custom_seed, artistic_seed, freeu_b1, freeu_b2, freeu_s1, freeu_s2, enable_sag, sag_scale, sag_blur_sigma],
1146
  outputs=[artistic_output_image, artistic_error_message]
1147
  )
1148
 
 
1263
  artistic_module_drawer
1264
  ],
1265
  outputs=[artistic_output_image, artistic_error_message],
1266
+ fn=generate_artistic_qr,
1267
  cache_examples=False
1268
  )
1269