ginipick commited on
Commit
36fc032
ยท
verified ยท
1 Parent(s): 3a109ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -27
app.py CHANGED
@@ -176,33 +176,46 @@ def generate_background(prompt: str, aspect_ratio: str) -> Image.Image:
176
  def create_position_grid():
177
  """3x3 ์œ„์น˜ ์„ ํƒ ๊ทธ๋ฆฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” HTML"""
178
  return """
179
- <div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 150px; margin: auto;">
180
- <button class="position-btn" data-pos="top-left">โ†–</button>
181
- <button class="position-btn" data-pos="top-center">โ†‘</button>
182
- <button class="position-btn" data-pos="top-right">โ†—</button>
183
- <button class="position-btn" data-pos="middle-left">โ†</button>
184
- <button class="position-btn" data-pos="middle-center">โ€ข</button>
185
- <button class="position-btn" data-pos="middle-right">โ†’</button>
186
- <button class="position-btn" data-pos="bottom-left">โ†™</button>
187
- <button class="position-btn" data-pos="bottom-center" data-default="true">โ†“</button>
188
- <button class="position-btn" data-pos="bottom-right">โ†˜</button>
189
  </div>
190
  <script>
191
- const buttons = document.querySelectorAll('.position-btn');
192
- buttons.forEach(btn => {
193
- btn.style.width = '40px';
194
- btn.style.height = '40px';
195
- btn.style.border = '1px solid #ccc';
196
- btn.style.borderRadius = '4px';
197
- btn.style.cursor = 'pointer';
198
- if (btn.dataset.default === 'true') {
199
- btn.style.backgroundColor = '#2196F3';
200
- btn.style.color = 'white';
 
 
 
 
 
 
 
201
  }
202
- });
 
 
 
 
203
  </script>
204
  """
205
 
 
 
206
  def calculate_object_position(position: str, bg_size: tuple[int, int], obj_size: tuple[int, int]) -> tuple[int, int]:
207
  """์˜ค๋ธŒ์ ํŠธ์˜ ์œ„์น˜ ๊ณ„์‚ฐ"""
208
  bg_width, bg_height = bg_size
@@ -301,7 +314,9 @@ def on_change_prompt(img: Image.Image | None, prompt: str | None, bg_prompt: str
301
 
302
 
303
  # process_prompt ํ•จ์ˆ˜ ์ˆ˜์ •
304
- def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None, aspect_ratio: str = "1:1") -> tuple[Image.Image, Image.Image]:
 
 
305
  try:
306
  if img is None or prompt.strip() == "":
307
  raise gr.Error("Please provide both image and prompt")
@@ -314,7 +329,11 @@ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
314
  # Process the image
315
  results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
316
 
317
- # ํ•ฉ์„ฑ๋œ ์ด๋ฏธ์ง€์™€ ์ถ”์ถœ๋œ ์ด๋ฏธ์ง€๋งŒ ๋ฐ˜ํ™˜
 
 
 
 
318
  return results[1], results[2]
319
  except Exception as e:
320
  raise gr.Error(str(e))
@@ -457,12 +476,12 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
457
  visible=True,
458
  scale=1
459
  )
460
-
461
- # ์˜ค๋ธŒ์ ํŠธ ์œ„์น˜์™€ ํฌ๊ธฐ ์กฐ์ • ์ปจํŠธ๋กค
462
  with gr.Row(visible=False) as object_controls:
463
  with gr.Column(scale=1):
464
- gr.HTML(create_position_grid())
465
- position = gr.State(value="bottom-center")
466
  with gr.Column(scale=1):
467
  scale_slider = gr.Slider(
468
  minimum=10,
@@ -472,12 +491,16 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
472
  label="Object Size (%)"
473
  )
474
 
 
 
475
  process_btn = gr.Button(
476
  "Process",
477
  variant="primary",
478
  interactive=False
479
  )
 
480
 
 
481
  with gr.Column(scale=1):
482
  with gr.Row():
483
  combined_image = gr.Image(
 
176
  def create_position_grid():
177
  """3x3 ์œ„์น˜ ์„ ํƒ ๊ทธ๋ฆฌ๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” HTML"""
178
  return """
179
+ <div class="position-grid" style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; width: 150px; margin: auto;">
180
+ <button class="position-btn" data-pos="top-left" onclick="updatePosition(this)">โ†–</button>
181
+ <button class="position-btn" data-pos="top-center" onclick="updatePosition(this)">โ†‘</button>
182
+ <button class="position-btn" data-pos="top-right" onclick="updatePosition(this)">โ†—</button>
183
+ <button class="position-btn" data-pos="middle-left" onclick="updatePosition(this)">โ†</button>
184
+ <button class="position-btn" data-pos="middle-center" onclick="updatePosition(this)">โ€ข</button>
185
+ <button class="position-btn" data-pos="middle-right" onclick="updatePosition(this)">โ†’</button>
186
+ <button class="position-btn" data-pos="bottom-left" onclick="updatePosition(this)">โ†™</button>
187
+ <button class="position-btn" data-pos="bottom-center" onclick="updatePosition(this)" data-default="true">โ†“</button>
188
+ <button class="position-btn" data-pos="bottom-right" onclick="updatePosition(this)">โ†˜</button>
189
  </div>
190
  <script>
191
+ function updatePosition(btn) {
192
+ // ๋ชจ๋“  ๋ฒ„ํŠผ์˜ ์Šคํƒ€์ผ ์ดˆ๊ธฐํ™”
193
+ document.querySelectorAll('.position-btn').forEach(b => {
194
+ b.style.backgroundColor = '';
195
+ b.style.color = '';
196
+ });
197
+
198
+ // ํด๋ฆญ๋œ ๋ฒ„ํŠผ ์Šคํƒ€์ผ ๋ณ€๊ฒฝ
199
+ btn.style.backgroundColor = '#2196F3';
200
+ btn.style.color = 'white';
201
+
202
+ // position State ์—…๋ฐ์ดํŠธ
203
+ const positionState = document.querySelector('#position-state');
204
+ if (positionState) {
205
+ positionState.value = btn.dataset.pos;
206
+ // ๋ณ€๊ฒฝ ์ด๋ฒคํŠธ ๋ฐœ์ƒ
207
+ positionState.dispatchEvent(new Event('change'));
208
  }
209
+ }
210
+
211
+ // ์ดˆ๊ธฐ ์„ ํƒ ๋ฒ„ํŠผ ์Šคํƒ€์ผ ์„ค์ •
212
+ document.querySelector('[data-default="true"]').style.backgroundColor = '#2196F3';
213
+ document.querySelector('[data-default="true"]').style.color = 'white';
214
  </script>
215
  """
216
 
217
+
218
+
219
  def calculate_object_position(position: str, bg_size: tuple[int, int], obj_size: tuple[int, int]) -> tuple[int, int]:
220
  """์˜ค๋ธŒ์ ํŠธ์˜ ์œ„์น˜ ๊ณ„์‚ฐ"""
221
  bg_width, bg_height = bg_size
 
314
 
315
 
316
  # process_prompt ํ•จ์ˆ˜ ์ˆ˜์ •
317
+ def process_prompt(img: Image.Image, prompt: str, bg_prompt: str | None = None,
318
+ aspect_ratio: str = "1:1", position: str = "bottom-center",
319
+ scale_percent: float = 100) -> tuple[Image.Image, Image.Image]:
320
  try:
321
  if img is None or prompt.strip() == "":
322
  raise gr.Error("Please provide both image and prompt")
 
329
  # Process the image
330
  results, _ = _process(img, prompt, bg_prompt, aspect_ratio)
331
 
332
+ if bg_prompt:
333
+ # ์œ„์น˜์™€ ํฌ๊ธฐ ์กฐ์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ
334
+ combined = combine_with_background(results[2], results[1], position, scale_percent)
335
+ return combined, results[2]
336
+
337
  return results[1], results[2]
338
  except Exception as e:
339
  raise gr.Error(str(e))
 
476
  visible=True,
477
  scale=1
478
  )
479
+
480
+
481
  with gr.Row(visible=False) as object_controls:
482
  with gr.Column(scale=1):
483
+ position_grid = gr.HTML(create_position_grid())
484
+ position = gr.State(value="bottom-center", elem_id="position-state")
485
  with gr.Column(scale=1):
486
  scale_slider = gr.Slider(
487
  minimum=10,
 
491
  label="Object Size (%)"
492
  )
493
 
494
+
495
+
496
  process_btn = gr.Button(
497
  "Process",
498
  variant="primary",
499
  interactive=False
500
  )
501
+ # UI ๋ถ€๋ถ„์—์„œ position State ์ถ”๊ฐ€
502
 
503
+
504
  with gr.Column(scale=1):
505
  with gr.Row():
506
  combined_image = gr.Image(