Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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 |
-
|
| 192 |
-
|
| 193 |
-
btn.
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
|
|
|
|
|
|
| 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(
|