multimodalart HF staff commited on
Commit
c37eeb3
·
verified ·
1 Parent(s): 4a91cdc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -50
app.py CHANGED
@@ -47,45 +47,66 @@ pipe = StableDiffusionXLFillPipeline.from_pretrained(
47
 
48
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
49
 
50
-
51
- @spaces.GPU
52
- def infer(image, model_selection, width, height, overlap_width, num_inference_steps, prompt_input=None):
53
- source = image
54
- target_size = (width, height)
55
- target_ratio = (width, height) # Calculate aspect ratio from width and height
56
- overlap = overlap_width
57
-
58
- # Upscale if source is smaller than target in both dimensions
59
- if source.width < target_size[0] and source.height < target_size[1]:
60
- scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
61
- new_width = int(source.width * scale_factor)
62
- new_height = int(source.height * scale_factor)
63
- source = source.resize((new_width, new_height), Image.LANCZOS)
64
-
65
- if source.width > target_size[0] or source.height > target_size[1]:
66
- scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
67
- new_width = int(source.width * scale_factor)
68
- new_height = int(source.height * scale_factor)
69
- source = source.resize((new_width, new_height), Image.LANCZOS)
70
-
71
- margin_x = (target_size[0] - source.width) // 2
72
- margin_y = (target_size[1] - source.height) // 2
73
-
74
- background = Image.new('RGB', target_size, (255, 255, 255))
75
- background.paste(source, (margin_x, margin_y))
76
-
77
  mask = Image.new('L', target_size, 255)
78
  mask_draw = ImageDraw.Draw(mask)
79
- mask_draw.rectangle([
80
- (margin_x + overlap, margin_y + overlap),
81
- (margin_x + source.width - overlap, margin_y + source.height - overlap)
82
- ], fill=0)
83
 
84
- cnet_image = background.copy()
85
- cnet_image.paste(0, (0, 0), mask)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
  final_prompt = "high quality"
88
- if prompt_input.strip() != "":
89
  final_prompt += ", " + prompt_input
90
 
91
  (
@@ -112,27 +133,23 @@ def infer(image, model_selection, width, height, overlap_width, num_inference_st
112
 
113
  def preload_presets(target_ratio):
114
  if target_ratio == "9:16":
115
- changed_width = 720
116
- changed_height = 1280
117
- return changed_width, changed_height, gr.update(open=False)
118
  elif target_ratio == "16:9":
119
- changed_width = 1280
120
- changed_height = 720
121
- return changed_width, changed_height, gr.update(open=False)
122
  elif target_ratio == "Custom":
123
- return 720, 1280, gr.update(open=True)
124
 
125
  def clear_result():
126
  return gr.update(value=None)
127
 
128
-
129
  css = """
130
  .gradio-container {
131
  width: 1200px !important;
132
  }
133
  """
134
 
135
-
136
  title = """<h1 align="center">Diffusers Image Outpaint</h1>
137
  <div align="center">Drop an image you would like to extend, pick your expected ratio and hit Generate.</div>
138
  <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
@@ -162,13 +179,15 @@ with gr.Blocks(css=css) as demo:
162
  with gr.Row():
163
  target_ratio = gr.Radio(
164
  label = "Expected Ratio",
165
- choices = ["9:16", "16:9", "Custom"],
166
  value = "9:16",
167
  scale = 2
168
  )
169
 
170
  run_button = gr.Button("Generate", scale=1)
171
 
 
 
172
  with gr.Accordion(label="Advanced settings", open=False) as settings_panel:
173
  with gr.Column():
174
  with gr.Row():
@@ -177,14 +196,14 @@ with gr.Blocks(css=css) as demo:
177
  minimum=720,
178
  maximum=1440,
179
  step=8,
180
- value=720, # Set a default value
181
  )
182
  height_slider = gr.Slider(
183
  label="Height",
184
  minimum=720,
185
  maximum=1440,
186
  step=8,
187
- value=1280, # Set a default value
188
  )
189
  with gr.Row():
190
  model_selection = gr.Dropdown(
@@ -192,7 +211,7 @@ with gr.Blocks(css=css) as demo:
192
  value="RealVisXL V5.0 Lightning",
193
  label="Model",
194
  )
195
- num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8 )
196
 
197
  overlap_width = gr.Slider(
198
  label="Mask overlap width",
@@ -220,16 +239,17 @@ with gr.Blocks(css=css) as demo:
220
  target_ratio.change(
221
  fn = preload_presets,
222
  inputs = [target_ratio],
223
- outputs = [width_slider, height_slider, settings_panel],
224
  queue = False
225
  )
 
226
  run_button.click(
227
  fn=clear_result,
228
  inputs=None,
229
  outputs=result,
230
  ).then(
231
  fn=infer,
232
- inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input],
233
  outputs=result,
234
  )
235
 
@@ -239,7 +259,7 @@ with gr.Blocks(css=css) as demo:
239
  outputs=result,
240
  ).then(
241
  fn=infer,
242
- inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input],
243
  outputs=result,
244
  )
245
 
 
47
 
48
  pipe.scheduler = TCDScheduler.from_config(pipe.scheduler.config)
49
 
50
+ def resize_and_pad(image, target_size=(1024, 1024), resize_width=512):
51
+ aspect_ratio = image.height / image.width
52
+ new_height = int(resize_width * aspect_ratio)
53
+
54
+ resized_image = image.resize((resize_width, new_height), Image.LANCZOS)
55
+
56
+ new_image = Image.new('RGB', target_size, (255, 255, 255))
57
+
58
+ paste_x = (target_size[0] - resize_width) // 2
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 + resize_width, paste_y + new_height], fill=0)
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)
82
+ new_width = int(source.width * scale_factor)
83
+ new_height = int(source.height * scale_factor)
84
+ source = source.resize((new_width, new_height), Image.LANCZOS)
85
+
86
+ if source.width > target_size[0] or source.height > target_size[1]:
87
+ scale_factor = min(target_size[0] / source.width, target_size[1] / source.height)
88
+ new_width = int(source.width * scale_factor)
89
+ new_height = int(source.height * scale_factor)
90
+ source = source.resize((new_width, new_height), Image.LANCZOS)
91
+
92
+ margin_x = (target_size[0] - source.width) // 2
93
+ margin_y = (target_size[1] - source.height) // 2
94
+
95
+ background = Image.new('RGB', target_size, (255, 255, 255))
96
+ background.paste(source, (margin_x, margin_y))
97
+
98
+ mask = Image.new('L', target_size, 255)
99
+ mask_draw = ImageDraw.Draw(mask)
100
+ mask_draw.rectangle([
101
+ (margin_x + overlap, margin_y + overlap),
102
+ (margin_x + source.width - overlap, margin_y + source.height - overlap)
103
+ ], fill=0)
104
+
105
+ cnet_image = background.copy()
106
+ cnet_image.paste(0, (0, 0), mask)
107
 
108
  final_prompt = "high quality"
109
+ if prompt_input and prompt_input.strip() != "":
110
  final_prompt += ", " + prompt_input
111
 
112
  (
 
133
 
134
  def preload_presets(target_ratio):
135
  if target_ratio == "9:16":
136
+ return 720, 1280, gr.update(visible=False), gr.update(open=False)
 
 
137
  elif target_ratio == "16:9":
138
+ return 1280, 720, gr.update(visible=False), gr.update(open=False)
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(visible=False), gr.update(open=True)
143
 
144
  def clear_result():
145
  return gr.update(value=None)
146
 
 
147
  css = """
148
  .gradio-container {
149
  width: 1200px !important;
150
  }
151
  """
152
 
 
153
  title = """<h1 align="center">Diffusers Image Outpaint</h1>
154
  <div align="center">Drop an image you would like to extend, pick your expected ratio and hit Generate.</div>
155
  <div style="display: flex; justify-content: center; align-items: center; text-align: center;">
 
179
  with gr.Row():
180
  target_ratio = gr.Radio(
181
  label = "Expected Ratio",
182
+ choices = ["9:16", "16:9", "Expand", "Custom"],
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():
 
196
  minimum=720,
197
  maximum=1440,
198
  step=8,
199
+ value=720,
200
  )
201
  height_slider = gr.Slider(
202
  label="Height",
203
  minimum=720,
204
  maximum=1440,
205
  step=8,
206
+ value=1280,
207
  )
208
  with gr.Row():
209
  model_selection = gr.Dropdown(
 
211
  value="RealVisXL V5.0 Lightning",
212
  label="Model",
213
  )
214
+ num_inference_steps = gr.Slider(label="Steps", minimum=4, maximum=12, step=1, value=8)
215
 
216
  overlap_width = gr.Slider(
217
  label="Mask overlap width",
 
239
  target_ratio.change(
240
  fn = preload_presets,
241
  inputs = [target_ratio],
242
+ outputs = [width_slider, height_slider, expand_mode, settings_panel],
243
  queue = False
244
  )
245
+
246
  run_button.click(
247
  fn=clear_result,
248
  inputs=None,
249
  outputs=result,
250
  ).then(
251
  fn=infer,
252
+ inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input, expand_mode],
253
  outputs=result,
254
  )
255
 
 
259
  outputs=result,
260
  ).then(
261
  fn=infer,
262
+ inputs=[input_image, model_selection, width_slider, height_slider, overlap_width, num_inference_steps, prompt_input, expand_mode],
263
  outputs=result,
264
  )
265