eiji
commited on
Commit
Β·
e0a8f84
1
Parent(s):
11992a9
change parameter controls
Browse files
README.md
CHANGED
|
@@ -118,4 +118,4 @@ This project is licensed under the Apache License 2.0 - see the LICENSE file for
|
|
| 118 |
|
| 119 |
---
|
| 120 |
|
| 121 |
-
*Built with β€οΈ using Gradio and Diffusers for professional design workflows*
|
|
|
|
| 118 |
|
| 119 |
---
|
| 120 |
|
| 121 |
+
*Built with β€οΈ using Gradio and Diffusers for professional design workflows*
|
app.py
CHANGED
|
@@ -5,6 +5,101 @@ from PIL import Image, ImageDraw
|
|
| 5 |
from tkg_dm import TKGDMPipeline
|
| 6 |
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def load_preset_boxes(preset_name):
|
| 9 |
"""Load preset bounding box configurations"""
|
| 10 |
presets = {
|
|
@@ -18,6 +113,17 @@ def load_preset_boxes(preset_name):
|
|
| 18 |
return presets.get(preset_name, "")
|
| 19 |
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
def parse_bounding_boxes(bbox_str):
|
| 22 |
"""Parse bounding boxes from string format"""
|
| 23 |
if not bbox_str or not bbox_str.strip():
|
|
@@ -38,28 +144,12 @@ def parse_bounding_boxes(bbox_str):
|
|
| 38 |
return None
|
| 39 |
|
| 40 |
|
| 41 |
-
def generate_tkg_dm_image(prompt, ch0_shift, ch1_shift, ch2_shift, ch3_shift, intensity, steps, shift_percent, blur_sigma, model_type, custom_model_id, bounding_boxes_str
|
| 42 |
"""Generate image using TKG-DM or fallback demo"""
|
| 43 |
|
| 44 |
try:
|
| 45 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 46 |
-
|
| 47 |
-
# Handle preset loading and manual box addition
|
| 48 |
-
final_bbox_str = bounding_boxes_str
|
| 49 |
-
if preset and preset != "center_box":
|
| 50 |
-
preset_str = load_preset_boxes(preset)
|
| 51 |
-
if preset_str:
|
| 52 |
-
final_bbox_str = preset_str
|
| 53 |
-
|
| 54 |
-
# Add manual box if coordinates are provided and different from default
|
| 55 |
-
if not (x1 == 0.3 and y1 == 0.3 and x2 == 0.7 and y2 == 0.7):
|
| 56 |
-
manual_box = f"{x1:.3f},{y1:.3f},{x2:.3f},{y2:.3f}"
|
| 57 |
-
if final_bbox_str.strip():
|
| 58 |
-
final_bbox_str += ";" + manual_box
|
| 59 |
-
else:
|
| 60 |
-
final_bbox_str = manual_box
|
| 61 |
-
|
| 62 |
-
bounding_boxes = parse_bounding_boxes(final_bbox_str)
|
| 63 |
|
| 64 |
model_id = custom_model_id.strip() if custom_model_id.strip() else None
|
| 65 |
pipeline = TKGDMPipeline(model_id=model_id, model_type=model_type, device=device)
|
|
@@ -107,7 +197,7 @@ def create_demo_visualization(prompt, ch0_shift, ch1_shift, ch2_shift, ch3_shift
|
|
| 107 |
px1, py1 = int(x1 * 512), int(y1 * 512)
|
| 108 |
px2, py2 = int(x2 * 512), int(y2 * 512)
|
| 109 |
draw.rectangle([px1, py1, px2, py2], outline='yellow', width=3)
|
| 110 |
-
draw.text((px1+5, py1+5), f"
|
| 111 |
|
| 112 |
draw.text((10, 10), f"TKG-DM Demo", fill='white')
|
| 113 |
draw.text((10, 30), f"Prompt: {prompt[:40]}...", fill='white')
|
|
@@ -116,101 +206,244 @@ def create_demo_visualization(prompt, ch0_shift, ch1_shift, ch2_shift, ch3_shift
|
|
| 116 |
return img
|
| 117 |
|
| 118 |
|
| 119 |
-
# Create Gradio 5.x compatible interface with
|
| 120 |
-
with gr.Blocks(title="π¨ SAWNA: Space-Aware Text-to-Image Generation") as demo:
|
| 121 |
-
gr.Markdown("
|
| 122 |
-
|
| 123 |
|
|
|
|
|
|
|
| 124 |
with gr.Row():
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
with gr.Row():
|
| 129 |
-
ch0_shift = gr.Slider(minimum=-1.0, maximum=1.0, value=0.0, label="Channel 0 (Luminance/Color)")
|
| 130 |
-
ch1_shift = gr.Slider(minimum=-1.0, maximum=1.0, value=0.0, label="Channel 1 (Pink/Yellow+, Red/Blue-)")
|
| 131 |
-
|
| 132 |
-
with gr.Row():
|
| 133 |
-
ch2_shift = gr.Slider(minimum=-1.0, maximum=1.0, value=0.0, label="Channel 2 (Pink/Yellow+, Red/Blue-)")
|
| 134 |
-
ch3_shift = gr.Slider(minimum=-1.0, maximum=1.0, value=0.0, label="Channel 3 (Luminance/Color)")
|
| 135 |
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
-
#
|
| 150 |
with gr.Group():
|
| 151 |
-
gr.Markdown("
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
)
|
| 157 |
|
|
|
|
|
|
|
| 158 |
with gr.Row():
|
| 159 |
-
x1 = gr.Number(value=0.3, minimum=0, maximum=1, label="
|
| 160 |
-
y1 = gr.Number(value=0.3, minimum=0, maximum=1, label="
|
| 161 |
-
x2 = gr.Number(value=0.7, minimum=0, maximum=1, label="
|
| 162 |
-
y2 = gr.Number(value=0.7, minimum=0, maximum=1, label="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
# Examples section
|
| 179 |
-
with gr.Accordion("π
|
| 180 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
examples = gr.Examples(
|
| 183 |
examples=[
|
| 184 |
[
|
| 185 |
-
"A majestic lion in African savanna",
|
| 186 |
0.2, 0.3, 0.0, 0.0, 1.0, 25, 0.07, 0.0, "sd1.5", "",
|
| 187 |
-
"0.3,0.3,0.7,0.7"
|
| 188 |
],
|
| 189 |
[
|
| 190 |
-
"Modern cityscape with skyscrapers at sunset",
|
| 191 |
-0.1, -0.3, 0.2, 0.1, 1.2, 30, 0.08, 0.0, "sdxl", "",
|
| 192 |
-
"0.0,0.0,1.0,0.3"
|
| 193 |
],
|
| 194 |
[
|
| 195 |
-
"Vintage luxury car on mountain road",
|
| 196 |
0.1, 0.2, -0.1, -0.2, 0.9, 25, 0.06, 0.0, "sd1.5", "",
|
| 197 |
-
"0.0,0.7,1.0,1.0"
|
| 198 |
],
|
| 199 |
[
|
| 200 |
-
"Space astronaut floating in nebula",
|
| 201 |
0.0, 0.4, -0.2, 0.3, 1.1, 35, 0.09, 1.8, "sd2.1", "",
|
| 202 |
-
"0.0,0.2,0.3,0.8;0.7,0.2,1.0,0.8"
|
| 203 |
],
|
| 204 |
[
|
| 205 |
-
"
|
| 206 |
0.2, 0.0, 0.1, -0.1, 1.3, 40, 0.12, 2.5, "sdxl", "",
|
| 207 |
-
"0.0,0.0,1.0,0.2;0.0,0.8,1.0,1.0;0.0,0.2,0.2,0.8;0.8,0.2,1.0,0.8"
|
| 208 |
]
|
| 209 |
],
|
| 210 |
inputs=[prompt, ch0_shift, ch1_shift, ch2_shift, ch3_shift,
|
| 211 |
-
intensity, steps, shift_percent, blur_sigma, model_type, custom_model_id, bounding_boxes_str
|
| 212 |
-
preset, x1, y1, x2, y2]
|
| 213 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
-
demo.launch(share=True, server_name="0.0.0.0")
|
|
|
|
| 5 |
from tkg_dm import TKGDMPipeline
|
| 6 |
|
| 7 |
|
| 8 |
+
def create_canvas_image(width=512, height=512):
|
| 9 |
+
"""Create a blank canvas for drawing bounding boxes"""
|
| 10 |
+
img = Image.new('RGB', (width, height), (240, 240, 240)) # Light gray background
|
| 11 |
+
draw = ImageDraw.Draw(img)
|
| 12 |
+
|
| 13 |
+
# Add grid lines for better visualization
|
| 14 |
+
grid_size = 64
|
| 15 |
+
for x in range(0, width, grid_size):
|
| 16 |
+
draw.line([(x, 0), (x, height)], fill=(200, 200, 200), width=1)
|
| 17 |
+
for y in range(0, height, grid_size):
|
| 18 |
+
draw.line([(0, y), (width, y)], fill=(200, 200, 200), width=1)
|
| 19 |
+
|
| 20 |
+
# Add instructions
|
| 21 |
+
draw.text((10, 10), "Reserved regions preview", fill=(100, 100, 100))
|
| 22 |
+
draw.text((10, 25), "Yellow boxes show where content will be suppressed", fill=(100, 100, 100))
|
| 23 |
+
|
| 24 |
+
return img
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def draw_boxes_on_canvas(boxes, width=512, height=512):
|
| 28 |
+
"""Draw bounding boxes on canvas"""
|
| 29 |
+
img = create_canvas_image(width, height)
|
| 30 |
+
draw = ImageDraw.Draw(img)
|
| 31 |
+
|
| 32 |
+
for i, (x1, y1, x2, y2) in enumerate(boxes):
|
| 33 |
+
# Convert normalized coordinates to pixel coordinates
|
| 34 |
+
px1, py1 = int(x1 * width), int(y1 * height)
|
| 35 |
+
px2, py2 = int(x2 * width), int(y2 * height)
|
| 36 |
+
|
| 37 |
+
# Draw bounding box
|
| 38 |
+
draw.rectangle([px1, py1, px2, py2], outline='red', width=3)
|
| 39 |
+
draw.rectangle([px1+1, py1+1, px2-1, py2-1], outline='yellow', width=2)
|
| 40 |
+
|
| 41 |
+
# Add semi-transparent fill
|
| 42 |
+
overlay = Image.new('RGBA', (width, height), (0, 0, 0, 0))
|
| 43 |
+
overlay_draw = ImageDraw.Draw(overlay)
|
| 44 |
+
overlay_draw.rectangle([px1, py1, px2, py2], fill=(255, 255, 0, 80))
|
| 45 |
+
img = Image.alpha_composite(img.convert('RGBA'), overlay).convert('RGB')
|
| 46 |
+
draw = ImageDraw.Draw(img)
|
| 47 |
+
|
| 48 |
+
# Add box label
|
| 49 |
+
label = f"Reserved Region {i+1}"
|
| 50 |
+
draw.text((px1+5, py1+5), label, fill='white')
|
| 51 |
+
draw.text((px1+4, py1+4), label, fill='black') # Shadow effect
|
| 52 |
+
|
| 53 |
+
return img
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def sync_text_to_canvas(bbox_str):
|
| 57 |
+
"""Sync text input to canvas visualization"""
|
| 58 |
+
boxes = parse_bounding_boxes(bbox_str)
|
| 59 |
+
if boxes:
|
| 60 |
+
return draw_boxes_on_canvas(boxes)
|
| 61 |
+
else:
|
| 62 |
+
return create_canvas_image()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def add_bounding_box(bbox_str, x1, y1, x2, y2):
|
| 66 |
+
"""Add a new bounding box to the string"""
|
| 67 |
+
# Ensure coordinates are in correct order and valid range
|
| 68 |
+
x1, x2 = max(0, min(x1, x2)), min(1, max(x1, x2))
|
| 69 |
+
y1, y2 = max(0, min(y1, y2)), min(1, max(y1, y2))
|
| 70 |
+
|
| 71 |
+
# Check minimum size
|
| 72 |
+
if x2 - x1 < 0.02 or y2 - y1 < 0.02:
|
| 73 |
+
return bbox_str, sync_text_to_canvas(bbox_str)
|
| 74 |
+
|
| 75 |
+
new_box = f"{x1:.3f},{y1:.3f},{x2:.3f},{y2:.3f}"
|
| 76 |
+
|
| 77 |
+
if bbox_str.strip():
|
| 78 |
+
updated_str = bbox_str + ";" + new_box
|
| 79 |
+
else:
|
| 80 |
+
updated_str = new_box
|
| 81 |
+
|
| 82 |
+
return updated_str, sync_text_to_canvas(updated_str)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def remove_last_box(bbox_str):
|
| 86 |
+
"""Remove the last bounding box"""
|
| 87 |
+
if not bbox_str.strip():
|
| 88 |
+
return "", create_canvas_image()
|
| 89 |
+
|
| 90 |
+
boxes = bbox_str.split(';')
|
| 91 |
+
if boxes:
|
| 92 |
+
boxes.pop()
|
| 93 |
+
|
| 94 |
+
updated_str = ';'.join(boxes)
|
| 95 |
+
return updated_str, sync_text_to_canvas(updated_str)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def clear_all_boxes():
|
| 99 |
+
"""Clear all bounding boxes"""
|
| 100 |
+
return "", create_canvas_image()
|
| 101 |
+
|
| 102 |
+
|
| 103 |
def load_preset_boxes(preset_name):
|
| 104 |
"""Load preset bounding box configurations"""
|
| 105 |
presets = {
|
|
|
|
| 113 |
return presets.get(preset_name, "")
|
| 114 |
|
| 115 |
|
| 116 |
+
def load_preset_handler(preset_name):
|
| 117 |
+
"""Load preset boxes and update preview"""
|
| 118 |
+
if preset_name and preset_name != "center_box":
|
| 119 |
+
preset_str = load_preset_boxes(preset_name)
|
| 120 |
+
return preset_str, sync_text_to_canvas(preset_str)
|
| 121 |
+
elif preset_name == "center_box":
|
| 122 |
+
preset_str = "0.3,0.3,0.7,0.7"
|
| 123 |
+
return preset_str, sync_text_to_canvas(preset_str)
|
| 124 |
+
return "", create_canvas_image()
|
| 125 |
+
|
| 126 |
+
|
| 127 |
def parse_bounding_boxes(bbox_str):
|
| 128 |
"""Parse bounding boxes from string format"""
|
| 129 |
if not bbox_str or not bbox_str.strip():
|
|
|
|
| 144 |
return None
|
| 145 |
|
| 146 |
|
| 147 |
+
def generate_tkg_dm_image(prompt, ch0_shift, ch1_shift, ch2_shift, ch3_shift, intensity, steps, shift_percent, blur_sigma, model_type, custom_model_id, bounding_boxes_str):
|
| 148 |
"""Generate image using TKG-DM or fallback demo"""
|
| 149 |
|
| 150 |
try:
|
| 151 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 152 |
+
bounding_boxes = parse_bounding_boxes(bounding_boxes_str)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
model_id = custom_model_id.strip() if custom_model_id.strip() else None
|
| 155 |
pipeline = TKGDMPipeline(model_id=model_id, model_type=model_type, device=device)
|
|
|
|
| 197 |
px1, py1 = int(x1 * 512), int(y1 * 512)
|
| 198 |
px2, py2 = int(x2 * 512), int(y2 * 512)
|
| 199 |
draw.rectangle([px1, py1, px2, py2], outline='yellow', width=3)
|
| 200 |
+
draw.text((px1+5, py1+5), f"Reserved {i+1}", fill='white')
|
| 201 |
|
| 202 |
draw.text((10, 10), f"TKG-DM Demo", fill='white')
|
| 203 |
draw.text((10, 30), f"Prompt: {prompt[:40]}...", fill='white')
|
|
|
|
| 206 |
return img
|
| 207 |
|
| 208 |
|
| 209 |
+
# Create Gradio 5.x compatible interface with enhanced explanations
|
| 210 |
+
with gr.Blocks(title="π¨ SAWNA: Space-Aware Text-to-Image Generation", theme=gr.themes.Default()) as demo:
|
| 211 |
+
gr.Markdown("""
|
| 212 |
+
# π¨ SAWNA: Space-Aware Text-to-Image Generation
|
| 213 |
|
| 214 |
+
## What is SAWNA?
|
| 215 |
+
**SAWNA** (Space-Aware Weighted Noise Addition) is an advanced AI image generation technique that gives you **precise control** over where content appears in your images. Unlike traditional text-to-image models that fill the entire canvas, SAWNA can **guarantee empty spaces** for your design elements.""")
|
| 216 |
with gr.Row():
|
| 217 |
+
# Left column - Controls
|
| 218 |
+
with gr.Column(scale=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
+
# Step 1: Basic Settings
|
| 221 |
+
with gr.Group():
|
| 222 |
+
gr.Markdown("## π Step 1: Basic Image Settings")
|
| 223 |
+
prompt = gr.Textbox(
|
| 224 |
+
value="A majestic lion in a natural landscape",
|
| 225 |
+
label="Text Prompt",
|
| 226 |
+
placeholder="Describe the image you want to generate...",
|
| 227 |
+
lines=2
|
| 228 |
+
)
|
| 229 |
+
|
| 230 |
+
with gr.Row():
|
| 231 |
+
model_type = gr.Dropdown(
|
| 232 |
+
choices=["sd1.5", "sdxl", "sd2.1"],
|
| 233 |
+
value="sd1.5",
|
| 234 |
+
label="Model Architecture",
|
| 235 |
+
info="SDXL = highest quality, SD1.5 = fastest"
|
| 236 |
+
)
|
| 237 |
+
steps = gr.Slider(
|
| 238 |
+
minimum=10, maximum=100, value=25,
|
| 239 |
+
label="Generation Steps",
|
| 240 |
+
info="More steps = higher quality, slower generation"
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
custom_model_id = gr.Textbox(
|
| 244 |
+
value="",
|
| 245 |
+
label="Custom Model ID (Optional)",
|
| 246 |
+
placeholder="e.g., dreamlike-art/dreamlike-diffusion-1.0",
|
| 247 |
+
info="Use any Hugging Face Stable Diffusion model"
|
| 248 |
+
)
|
| 249 |
|
| 250 |
+
# Step 2: Reserved Regions
|
| 251 |
with gr.Group():
|
| 252 |
+
gr.Markdown("## π² Step 2: Define Reserved Regions")
|
| 253 |
+
|
| 254 |
+
with gr.Row():
|
| 255 |
+
preset = gr.Dropdown(
|
| 256 |
+
choices=[
|
| 257 |
+
("Center Box", "center_box"),
|
| 258 |
+
("Top Banner", "top_strip"),
|
| 259 |
+
("Bottom Banner", "bottom_strip"),
|
| 260 |
+
("Side Panels", "left_right"),
|
| 261 |
+
("Corner Logos", "corners"),
|
| 262 |
+
("Full Frame", "frame")
|
| 263 |
+
],
|
| 264 |
+
value="center_box",
|
| 265 |
+
label="π Quick Presets",
|
| 266 |
+
info="Pre-designed layouts for common use cases"
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
bounding_boxes_str = gr.Textbox(
|
| 270 |
+
value="0.3,0.3,0.7,0.7",
|
| 271 |
+
label="π Region Coordinates",
|
| 272 |
+
placeholder="x1,y1,x2,y2;x1,y1,x2,y2 (values 0-1)",
|
| 273 |
+
info="Format: x1,y1,x2,y2 where (0,0)=top-left, (1,1)=bottom-right"
|
| 274 |
)
|
| 275 |
|
| 276 |
+
# Manual box controls
|
| 277 |
+
gr.Markdown("**Manual Box Builder:**")
|
| 278 |
with gr.Row():
|
| 279 |
+
x1 = gr.Number(value=0.3, minimum=0, maximum=1, label="Left (X1)", step=0.01)
|
| 280 |
+
y1 = gr.Number(value=0.3, minimum=0, maximum=1, label="Top (Y1)", step=0.01)
|
| 281 |
+
x2 = gr.Number(value=0.7, minimum=0, maximum=1, label="Right (X2)", step=0.01)
|
| 282 |
+
y2 = gr.Number(value=0.7, minimum=0, maximum=1, label="Bottom (Y2)", step=0.01)
|
| 283 |
+
|
| 284 |
+
with gr.Row():
|
| 285 |
+
add_box_btn = gr.Button("β Add Box", variant="secondary", size="sm")
|
| 286 |
+
remove_box_btn = gr.Button("β Remove Last", variant="secondary", size="sm")
|
| 287 |
+
clear_boxes_btn = gr.Button("ποΈ Clear All", variant="secondary", size="sm")
|
| 288 |
|
| 289 |
+
# Step 3: Advanced Controls
|
| 290 |
+
with gr.Group():
|
| 291 |
+
gr.Markdown("## Step 3: Advanced Channel & Technical Controls")
|
| 292 |
+
|
| 293 |
+
with gr.Row():
|
| 294 |
+
ch0_shift = gr.Slider(
|
| 295 |
+
minimum=-1.0, maximum=1.0, value=0.0,
|
| 296 |
+
label="Channel 1 (Luminance/Color)",
|
| 297 |
+
info="Controls brightness and overall color balance"
|
| 298 |
+
)
|
| 299 |
+
ch1_shift = gr.Slider(
|
| 300 |
+
minimum=-1.0, maximum=1.0, value=1.0,
|
| 301 |
+
label="Channel 2 (Blue+, Red-)",
|
| 302 |
+
info="Positive = Sky Blue bias, Negative = Red bias"
|
| 303 |
+
)
|
| 304 |
+
|
| 305 |
+
with gr.Row():
|
| 306 |
+
ch2_shift = gr.Slider(
|
| 307 |
+
minimum=-1.0, maximum=1.0, value=1.0,
|
| 308 |
+
label="Channel 3 (Yellow+, Blue-)",
|
| 309 |
+
info="Positive = Yellow bias, Negative = Blue bias"
|
| 310 |
+
)
|
| 311 |
+
ch3_shift = gr.Slider(
|
| 312 |
+
minimum=-1.0, maximum=1.0, value=0.0,
|
| 313 |
+
label="Channel 4 (Luminance/Color)",
|
| 314 |
+
info="Secondary luminance and color control"
|
| 315 |
+
)
|
| 316 |
+
|
| 317 |
+
gr.Markdown("**Technical Parameters:**")
|
| 318 |
+
with gr.Row():
|
| 319 |
+
intensity = gr.Slider(
|
| 320 |
+
minimum=0.5, maximum=3.0, value=1.0,
|
| 321 |
+
label="π― Effect Intensity",
|
| 322 |
+
info="Multiplier for overall SAWNA effect strength"
|
| 323 |
+
)
|
| 324 |
+
shift_percent = gr.Slider(
|
| 325 |
+
minimum=0.01, maximum=0.15, value=0.07,
|
| 326 |
+
label="π Base Shift Percent",
|
| 327 |
+
info="Fundamental noise modification percentage (7% recommended)"
|
| 328 |
+
)
|
| 329 |
+
|
| 330 |
+
blur_sigma = gr.Slider(
|
| 331 |
+
minimum=0.0, maximum=5.0, value=0.0,
|
| 332 |
+
label="π«οΈ Transition Blur Sigma",
|
| 333 |
+
info="Gaussian blur for region boundaries (0 = auto-calculate)"
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
generate_btn = gr.Button("π¨ Generate Space-Aware Image", variant="primary", size="lg")
|
| 337 |
|
| 338 |
+
# Right column - Preview and Results
|
| 339 |
+
with gr.Column(scale=1):
|
| 340 |
+
|
| 341 |
+
# Reserved regions preview
|
| 342 |
+
with gr.Group():
|
| 343 |
+
gr.Markdown("## ποΈ Reserved Regions Preview")
|
| 344 |
+
bbox_preview = gr.Image(
|
| 345 |
+
value=create_canvas_image(),
|
| 346 |
+
label="Yellow areas will be kept empty",
|
| 347 |
+
interactive=False,
|
| 348 |
+
type="pil",
|
| 349 |
+
height=300
|
| 350 |
+
)
|
| 351 |
+
gr.Markdown("*Yellow boxes show where content generation will be suppressed*")
|
| 352 |
+
|
| 353 |
+
# Generated image output
|
| 354 |
+
with gr.Group():
|
| 355 |
+
gr.Markdown("## β¨ Generated Result")
|
| 356 |
+
output_image = gr.Image(
|
| 357 |
+
label="Your space-aware image",
|
| 358 |
+
type="pil",
|
| 359 |
+
height=400
|
| 360 |
+
)
|
| 361 |
|
| 362 |
# Examples section
|
| 363 |
+
with gr.Accordion("π Professional Use Case Examples", open=False):
|
| 364 |
+
gr.Markdown("""
|
| 365 |
+
### Real-World Applications
|
| 366 |
+
|
| 367 |
+
Click any example below to see how SAWNA handles different professional design scenarios:
|
| 368 |
+
|
| 369 |
+
- **π¦ Product Photography**: Center space for product details
|
| 370 |
+
- **ποΈ Website Headers**: Top banner space for navigation
|
| 371 |
+
- **π Marketing Banners**: Bottom space for call-to-action
|
| 372 |
+
- **π Mobile UI**: Side panels for app interface elements
|
| 373 |
+
- **β E-commerce**: Frame layout for product information
|
| 374 |
+
""")
|
| 375 |
|
| 376 |
examples = gr.Examples(
|
| 377 |
examples=[
|
| 378 |
[
|
| 379 |
+
"A majestic lion in African savanna, professional product photography",
|
| 380 |
0.2, 0.3, 0.0, 0.0, 1.0, 25, 0.07, 0.0, "sd1.5", "",
|
| 381 |
+
"0.3,0.3,0.7,0.7"
|
| 382 |
],
|
| 383 |
[
|
| 384 |
+
"Modern cityscape with skyscrapers at sunset, website header background",
|
| 385 |
-0.1, -0.3, 0.2, 0.1, 1.2, 30, 0.08, 0.0, "sdxl", "",
|
| 386 |
+
"0.0,0.0,1.0,0.3"
|
| 387 |
],
|
| 388 |
[
|
| 389 |
+
"Vintage luxury car on mountain road, marketing banner",
|
| 390 |
0.1, 0.2, -0.1, -0.2, 0.9, 25, 0.06, 0.0, "sd1.5", "",
|
| 391 |
+
"0.0,0.7,1.0,1.0"
|
| 392 |
],
|
| 393 |
[
|
| 394 |
+
"Space astronaut floating in colorful nebula, mobile app background",
|
| 395 |
0.0, 0.4, -0.2, 0.3, 1.1, 35, 0.09, 1.8, "sd2.1", "",
|
| 396 |
+
"0.0,0.2,0.3,0.8;0.7,0.2,1.0,0.8"
|
| 397 |
],
|
| 398 |
[
|
| 399 |
+
"Premium luxury watch product photography, e-commerce layout",
|
| 400 |
0.2, 0.0, 0.1, -0.1, 1.3, 40, 0.12, 2.5, "sdxl", "",
|
| 401 |
+
"0.0,0.0,1.0,0.2;0.0,0.8,1.0,1.0;0.0,0.2,0.2,0.8;0.8,0.2,1.0,0.8"
|
| 402 |
]
|
| 403 |
],
|
| 404 |
inputs=[prompt, ch0_shift, ch1_shift, ch2_shift, ch3_shift,
|
| 405 |
+
intensity, steps, shift_percent, blur_sigma, model_type, custom_model_id, bounding_boxes_str]
|
|
|
|
| 406 |
)
|
| 407 |
+
|
| 408 |
+
# Event handlers
|
| 409 |
+
generate_btn.click(
|
| 410 |
+
fn=generate_tkg_dm_image,
|
| 411 |
+
inputs=[prompt, ch0_shift, ch1_shift, ch2_shift, ch3_shift,
|
| 412 |
+
intensity, steps, shift_percent, blur_sigma, model_type, custom_model_id, bounding_boxes_str],
|
| 413 |
+
outputs=output_image
|
| 414 |
+
)
|
| 415 |
+
|
| 416 |
+
# Preset dropdown handler
|
| 417 |
+
preset.change(
|
| 418 |
+
fn=load_preset_handler,
|
| 419 |
+
inputs=[preset],
|
| 420 |
+
outputs=[bounding_boxes_str, bbox_preview]
|
| 421 |
+
)
|
| 422 |
+
|
| 423 |
+
# Box building handlers
|
| 424 |
+
add_box_btn.click(
|
| 425 |
+
fn=add_bounding_box,
|
| 426 |
+
inputs=[bounding_boxes_str, x1, y1, x2, y2],
|
| 427 |
+
outputs=[bounding_boxes_str, bbox_preview]
|
| 428 |
+
)
|
| 429 |
+
|
| 430 |
+
remove_box_btn.click(
|
| 431 |
+
fn=remove_last_box,
|
| 432 |
+
inputs=[bounding_boxes_str],
|
| 433 |
+
outputs=[bounding_boxes_str, bbox_preview]
|
| 434 |
+
)
|
| 435 |
+
|
| 436 |
+
clear_boxes_btn.click(
|
| 437 |
+
fn=clear_all_boxes,
|
| 438 |
+
outputs=[bounding_boxes_str, bbox_preview]
|
| 439 |
+
)
|
| 440 |
+
|
| 441 |
+
# Text box sync handler
|
| 442 |
+
bounding_boxes_str.change(
|
| 443 |
+
fn=sync_text_to_canvas,
|
| 444 |
+
inputs=[bounding_boxes_str],
|
| 445 |
+
outputs=[bbox_preview]
|
| 446 |
+
)
|
| 447 |
|
| 448 |
if __name__ == "__main__":
|
| 449 |
+
demo.launch(share=True, server_name="0.0.0.0")
|