Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -47,35 +47,44 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
|
|
47 |
|
48 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
49 |
|
50 |
-
def resize_and_pad(image, target_size
|
51 |
-
aspect_ratio = image.
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
new_image = Image.new('RGB', target_size, (255, 255, 255))
|
57 |
|
58 |
-
paste_x = (target_size[0] -
|
59 |
paste_y = (target_size[1] - new_height) // 2
|
60 |
|
61 |
new_image.paste(resized_image, (paste_x, paste_y))
|
62 |
|
63 |
mask = Image.new('L', target_size, 255)
|
64 |
mask_draw = ImageDraw.Draw(mask)
|
65 |
-
mask_draw.rectangle([paste_x, paste_y, paste_x +
|
66 |
|
67 |
return new_image, mask
|
68 |
|
69 |
@spaces.GPU
|
70 |
def infer(image, model_selection, width, height, overlap_width, num_inference_steps, prompt_input=None, expand_mode=False):
|
|
|
|
|
71 |
if expand_mode:
|
72 |
-
background, mask = resize_and_pad(image)
|
73 |
cnet_image = background.copy()
|
74 |
cnet_image.paste(0, (0, 0), mask)
|
75 |
else:
|
76 |
source = image
|
77 |
-
target_size = (width, height)
|
78 |
-
overlap = overlap_width
|
79 |
|
80 |
if source.width < target_size[0] and source.height < target_size[1]:
|
81 |
scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
|
@@ -98,8 +107,8 @@ def infer(image, model_selection, width, height, overlap_width, num_inference_st
|
|
98 |
mask = Image.new('L', target_size, 255)
|
99 |
mask_draw = ImageDraw.Draw(mask)
|
100 |
mask_draw.rectangle([
|
101 |
-
(margin_x +
|
102 |
-
(margin_x + source.width -
|
103 |
], fill=0)
|
104 |
|
105 |
cnet_image = background.copy()
|
@@ -133,13 +142,11 @@ def infer(image, model_selection, width, height, overlap_width, num_inference_st
|
|
133 |
|
134 |
def preload_presets(target_ratio):
|
135 |
if target_ratio == "9:16":
|
136 |
-
return 720, 1280, gr.update(
|
137 |
elif target_ratio == "16:9":
|
138 |
-
return 1280, 720, gr.update(
|
139 |
-
elif target_ratio == "Expand":
|
140 |
-
return 1024, 1024, gr.update(visible=True), gr.update(open=False)
|
141 |
elif target_ratio == "Custom":
|
142 |
-
return 720, 1280, gr.update(
|
143 |
|
144 |
def clear_result():
|
145 |
return gr.update(value=None)
|
@@ -179,15 +186,13 @@ with gr.Blocks(css=css) as demo:
|
|
179 |
with gr.Row():
|
180 |
target_ratio = gr.Radio(
|
181 |
label = "Expected Ratio",
|
182 |
-
choices = ["9:16", "16:9", "
|
183 |
value = "9:16",
|
184 |
scale = 2
|
185 |
)
|
186 |
|
187 |
run_button = gr.Button("Generate", scale=1)
|
188 |
|
189 |
-
expand_mode = gr.Checkbox(label="Use Expand Mode", visible=False)
|
190 |
-
|
191 |
with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
|
192 |
with gr.Column():
|
193 |
with gr.Row():
|
@@ -220,6 +225,8 @@ with gr.Blocks(css=css) as demo:
|
|
220 |
value=42,
|
221 |
step=1
|
222 |
)
|
|
|
|
|
223 |
|
224 |
gr.Examples(
|
225 |
examples=[
|
@@ -239,7 +246,7 @@ with gr.Blocks(css=css) as demo:
|
|
239 |
target_ratio.change(
|
240 |
fn = preload_presets,
|
241 |
inputs = [target_ratio],
|
242 |
-
outputs = [width_slider, height_slider,
|
243 |
queue = False
|
244 |
)
|
245 |
|
|
|
47 |
|
48 |
pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
|
49 |
|
50 |
+
def resize_and_pad(image, target_size):
|
51 |
+
aspect_ratio = image.width / image.height
|
52 |
+
target_aspect_ratio = target_size[0] / target_size[1]
|
53 |
+
|
54 |
+
if aspect_ratio > target_aspect_ratio:
|
55 |
+
# Image is wider than the target ratio
|
56 |
+
new_width = target_size[0]
|
57 |
+
new_height = int(new_width / aspect_ratio)
|
58 |
+
else:
|
59 |
+
# Image is taller than the target ratio
|
60 |
+
new_height = target_size[1]
|
61 |
+
new_width = int(new_height * aspect_ratio)
|
62 |
+
|
63 |
+
resized_image = image.resize((new_width, new_height), Image.LANCZOS)
|
64 |
|
65 |
new_image = Image.new('RGB', target_size, (255, 255, 255))
|
66 |
|
67 |
+
paste_x = (target_size[0] - new_width) // 2
|
68 |
paste_y = (target_size[1] - new_height) // 2
|
69 |
|
70 |
new_image.paste(resized_image, (paste_x, paste_y))
|
71 |
|
72 |
mask = Image.new('L', target_size, 255)
|
73 |
mask_draw = ImageDraw.Draw(mask)
|
74 |
+
mask_draw.rectangle([paste_x, paste_y, paste_x + new_width, paste_y + new_height], fill=0)
|
75 |
|
76 |
return new_image, mask
|
77 |
|
78 |
@spaces.GPU
|
79 |
def infer(image, model_selection, width, height, overlap_width, num_inference_steps, prompt_input=None, expand_mode=False):
|
80 |
+
target_size = (width, height)
|
81 |
+
|
82 |
if expand_mode:
|
83 |
+
background, mask = resize_and_pad(image, target_size)
|
84 |
cnet_image = background.copy()
|
85 |
cnet_image.paste(0, (0, 0), mask)
|
86 |
else:
|
87 |
source = image
|
|
|
|
|
88 |
|
89 |
if source.width < target_size[0] and source.height < target_size[1]:
|
90 |
scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
|
|
|
107 |
mask = Image.new('L', target_size, 255)
|
108 |
mask_draw = ImageDraw.Draw(mask)
|
109 |
mask_draw.rectangle([
|
110 |
+
(margin_x + overlap_width, margin_y + overlap_width),
|
111 |
+
(margin_x + source.width - overlap_width, margin_y + source.height - overlap_width)
|
112 |
], fill=0)
|
113 |
|
114 |
cnet_image = background.copy()
|
|
|
142 |
|
143 |
def preload_presets(target_ratio):
|
144 |
if target_ratio == "9:16":
|
145 |
+
return 720, 1280, gr.update(open=False)
|
146 |
elif target_ratio == "16:9":
|
147 |
+
return 1280, 720, gr.update(open=False)
|
|
|
|
|
148 |
elif target_ratio == "Custom":
|
149 |
+
return 720, 1280, gr.update(open=True)
|
150 |
|
151 |
def clear_result():
|
152 |
return gr.update(value=None)
|
|
|
186 |
with gr.Row():
|
187 |
target_ratio = gr.Radio(
|
188 |
label = "Expected Ratio",
|
189 |
+
choices = ["9:16", "16:9", "Custom"],
|
190 |
value = "9:16",
|
191 |
scale = 2
|
192 |
)
|
193 |
|
194 |
run_button = gr.Button("Generate", scale=1)
|
195 |
|
|
|
|
|
196 |
with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
|
197 |
with gr.Column():
|
198 |
with gr.Row():
|
|
|
225 |
value=42,
|
226 |
step=1
|
227 |
)
|
228 |
+
|
229 |
+
expand_mode = gr.Checkbox(label="Use Expand Mode", value=False)
|
230 |
|
231 |
gr.Examples(
|
232 |
examples=[
|
|
|
246 |
target_ratio.change(
|
247 |
fn = preload_presets,
|
248 |
inputs = [target_ratio],
|
249 |
+
outputs = [width_slider, height_slider, settings_panel],
|
250 |
queue = False
|
251 |
)
|
252 |
|