Spaces:
Running
on
Zero
Running
on
Zero
feat: force cache clear
Browse files
app.py
CHANGED
@@ -47,6 +47,16 @@ pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
|
47 |
pipe.to("cuda")
|
48 |
print(pipe)
|
49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
@spaces.GPU(duration=12)
|
51 |
def fill_image(prompt, image, model_selection, paste_back):
|
52 |
print(f"Received image: {image}")
|
@@ -95,12 +105,12 @@ def can_expand(source_width, source_height, target_width, target_height, alignme
|
|
95 |
return False
|
96 |
return True
|
97 |
|
|
|
98 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
99 |
target_size = (width, height)
|
100 |
scale_factor = min(target_size[0] / image.width, target_size[1] / image.height)
|
101 |
new_width = int(image.width * scale_factor)
|
102 |
new_height = int(image.height * scale_factor)
|
103 |
-
|
104 |
source = image.resize((new_width, new_height), Image.LANCZOS)
|
105 |
if resize_option == "Full":
|
106 |
resize_percentage = 100
|
@@ -114,20 +124,16 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
114 |
resize_percentage = 25
|
115 |
else: # Custom
|
116 |
resize_percentage = custom_resize_percentage
|
117 |
-
|
118 |
resize_factor = resize_percentage / 100
|
119 |
new_width = int(source.width * resize_factor)
|
120 |
new_height = int(source.height * resize_factor)
|
121 |
new_width = max(new_width, 64)
|
122 |
new_height = max(new_height, 64)
|
123 |
-
|
124 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
125 |
-
|
126 |
overlap_x = int(new_width * (overlap_percentage / 100))
|
127 |
overlap_y = int(new_height * (overlap_percentage / 100))
|
128 |
overlap_x = max(overlap_x, 1)
|
129 |
overlap_y = max(overlap_y, 1)
|
130 |
-
|
131 |
if alignment == "Middle":
|
132 |
margin_x = (target_size[0] - new_width) // 2
|
133 |
margin_y = (target_size[1] - new_height) // 2
|
@@ -143,22 +149,17 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
143 |
elif alignment == "Bottom":
|
144 |
margin_x = (target_size[0] - new_width) // 2
|
145 |
margin_y = target_size[1] - new_height
|
146 |
-
|
147 |
margin_x = max(0, min(margin_x, target_size[0] - new_width))
|
148 |
margin_y = max(0, min(margin_y, target_size[1] - new_height))
|
149 |
-
|
150 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
151 |
background.paste(source, (margin_x, margin_y))
|
152 |
-
|
153 |
mask = Image.new('L', target_size, 255)
|
154 |
mask_draw = ImageDraw.Draw(mask)
|
155 |
-
|
156 |
white_gaps_patch = 2
|
157 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x + white_gaps_patch
|
158 |
right_overlap = margin_x + new_width - overlap_x if overlap_right else margin_x + new_width - white_gaps_patch
|
159 |
top_overlap = margin_y + overlap_y if overlap_top else margin_y + white_gaps_patch
|
160 |
bottom_overlap = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height - white_gaps_patch
|
161 |
-
|
162 |
if alignment == "Left":
|
163 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x
|
164 |
elif alignment == "Right":
|
@@ -166,14 +167,12 @@ def prepare_image_and_mask(image, width, height, overlap_percentage, resize_opti
|
|
166 |
elif alignment == "Top":
|
167 |
top_overlap = margin_y + overlap_y if overlap_top else margin_y
|
168 |
elif alignment == "Bottom":
|
169 |
-
|
170 |
-
|
171 |
mask_draw.rectangle([
|
172 |
(left_overlap, top_overlap),
|
173 |
(right_overlap, bottom_overlap)
|
174 |
], fill=0)
|
175 |
return background, mask
|
176 |
-
|
177 |
def preview_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
178 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
179 |
preview = background.copy().convert('RGBA')
|
@@ -303,6 +302,12 @@ def update_history(new_image, history):
|
|
303 |
history.insert(0, new_image)
|
304 |
return history
|
305 |
|
|
|
|
|
|
|
|
|
|
|
|
|
306 |
css = """
|
307 |
.nulgradio-container {
|
308 |
width: 86vw !important;
|
@@ -484,7 +489,22 @@ with gr.Blocks(css=css, fill_height=True) as demo:
|
|
484 |
use_as_input_button_outpaint = gr.Button("Use as Input Image", visible=False)
|
485 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
486 |
preview_image = gr.Image(label="Preview")
|
487 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
488 |
target_ratio.change(
|
489 |
fn=preload_presets,
|
490 |
inputs=[target_ratio, width_slider, height_slider],
|
|
|
47 |
pipe.to("cuda")
|
48 |
print(pipe)
|
49 |
|
50 |
+
def load_default_pipeline():
|
51 |
+
global pipe
|
52 |
+
pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
53 |
+
"SG161222/RealVisXL_V5.0_Lightning",
|
54 |
+
torch_dtype=torch.float16,
|
55 |
+
vae=vae,
|
56 |
+
controlnet=model,
|
57 |
+
).to("cuda")
|
58 |
+
return gr.update(value="Default pipeline loaded!")
|
59 |
+
|
60 |
@spaces.GPU(duration=12)
|
61 |
def fill_image(prompt, image, model_selection, paste_back):
|
62 |
print(f"Received image: {image}")
|
|
|
105 |
return False
|
106 |
return True
|
107 |
|
108 |
+
|
109 |
def prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
110 |
target_size = (width, height)
|
111 |
scale_factor = min(target_size[0] / image.width, target_size[1] / image.height)
|
112 |
new_width = int(image.width * scale_factor)
|
113 |
new_height = int(image.height * scale_factor)
|
|
|
114 |
source = image.resize((new_width, new_height), Image.LANCZOS)
|
115 |
if resize_option == "Full":
|
116 |
resize_percentage = 100
|
|
|
124 |
resize_percentage = 25
|
125 |
else: # Custom
|
126 |
resize_percentage = custom_resize_percentage
|
|
|
127 |
resize_factor = resize_percentage / 100
|
128 |
new_width = int(source.width * resize_factor)
|
129 |
new_height = int(source.height * resize_factor)
|
130 |
new_width = max(new_width, 64)
|
131 |
new_height = max(new_height, 64)
|
|
|
132 |
source = source.resize((new_width, new_height), Image.LANCZOS)
|
|
|
133 |
overlap_x = int(new_width * (overlap_percentage / 100))
|
134 |
overlap_y = int(new_height * (overlap_percentage / 100))
|
135 |
overlap_x = max(overlap_x, 1)
|
136 |
overlap_y = max(overlap_y, 1)
|
|
|
137 |
if alignment == "Middle":
|
138 |
margin_x = (target_size[0] - new_width) // 2
|
139 |
margin_y = (target_size[1] - new_height) // 2
|
|
|
149 |
elif alignment == "Bottom":
|
150 |
margin_x = (target_size[0] - new_width) // 2
|
151 |
margin_y = target_size[1] - new_height
|
|
|
152 |
margin_x = max(0, min(margin_x, target_size[0] - new_width))
|
153 |
margin_y = max(0, min(margin_y, target_size[1] - new_height))
|
|
|
154 |
background = Image.new('RGB', target_size, (255, 255, 255))
|
155 |
background.paste(source, (margin_x, margin_y))
|
|
|
156 |
mask = Image.new('L', target_size, 255)
|
157 |
mask_draw = ImageDraw.Draw(mask)
|
|
|
158 |
white_gaps_patch = 2
|
159 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x + white_gaps_patch
|
160 |
right_overlap = margin_x + new_width - overlap_x if overlap_right else margin_x + new_width - white_gaps_patch
|
161 |
top_overlap = margin_y + overlap_y if overlap_top else margin_y + white_gaps_patch
|
162 |
bottom_overlap = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height - white_gaps_patch
|
|
|
163 |
if alignment == "Left":
|
164 |
left_overlap = margin_x + overlap_x if overlap_left else margin_x
|
165 |
elif alignment == "Right":
|
|
|
167 |
elif alignment == "Top":
|
168 |
top_overlap = margin_y + overlap_y if overlap_top else margin_y
|
169 |
elif alignment == "Bottom":
|
170 |
+
botttom_overlap = margin = margin = margin = margin_y + new_height - overlap_y if overlap_bottom else margin_y + new_height
|
|
|
171 |
mask_draw.rectangle([
|
172 |
(left_overlap, top_overlap),
|
173 |
(right_overlap, bottom_overlap)
|
174 |
], fill=0)
|
175 |
return background, mask
|
|
|
176 |
def preview_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom):
|
177 |
background, mask = prepare_image_and_mask(image, width, height, overlap_percentage, resize_option, custom_resize_percentage, alignment, overlap_left, overlap_right, overlap_top, overlap_bottom)
|
178 |
preview = background.copy().convert('RGBA')
|
|
|
302 |
history.insert(0, new_image)
|
303 |
return history
|
304 |
|
305 |
+
def clear_cache():
|
306 |
+
global pipe
|
307 |
+
pipe = None
|
308 |
+
torch.cuda.empty_cache()
|
309 |
+
return gr.update(value="Cache cleared!")
|
310 |
+
|
311 |
css = """
|
312 |
.nulgradio-container {
|
313 |
width: 86vw !important;
|
|
|
489 |
use_as_input_button_outpaint = gr.Button("Use as Input Image", visible=False)
|
490 |
history_gallery = gr.Gallery(label="History", columns=6, object_fit="contain", interactive=False)
|
491 |
preview_image = gr.Image(label="Preview")
|
492 |
+
with gr.TabItem("Misc"):
|
493 |
+
with gr.Column():
|
494 |
+
clear_cache_button = gr.Button("Clear CUDA Cache")
|
495 |
+
clear_cache_message = gr.Markdown("")
|
496 |
+
clear_cache_button.click(
|
497 |
+
fn=clear_cache,
|
498 |
+
inputs=None,
|
499 |
+
outputs=clear_cache_message,
|
500 |
+
)
|
501 |
+
load_default_button = gr.Button("Load Default Pipeline")
|
502 |
+
load_default_message = gr.Markdown("")
|
503 |
+
load_default_button.click(
|
504 |
+
fn=load_default_pipeline,
|
505 |
+
inputs=None,
|
506 |
+
outputs=load_default_message,
|
507 |
+
)
|
508 |
target_ratio.change(
|
509 |
fn=preload_presets,
|
510 |
inputs=[target_ratio, width_slider, height_slider],
|