Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import gradio as gr
|
|
5 |
import logging
|
6 |
import re
|
7 |
from io import BytesIO
|
|
|
8 |
|
9 |
from google import genai
|
10 |
from google.genai import types
|
@@ -146,9 +147,10 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
146 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
147 |
return prompt
|
148 |
|
149 |
-
def generate_with_images(prompt, images):
|
150 |
"""
|
151 |
API ํธ์ถ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ณ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
|
|
152 |
"""
|
153 |
try:
|
154 |
api_key = os.environ.get("GEMINI_API_KEY")
|
@@ -156,7 +158,18 @@ def generate_with_images(prompt, images):
|
|
156 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
157 |
|
158 |
client = genai.Client(api_key=api_key)
|
159 |
-
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
contents = [prompt]
|
162 |
for idx, img in enumerate(images, 1):
|
@@ -198,7 +211,7 @@ def generate_with_images(prompt, images):
|
|
198 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
199 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
200 |
|
201 |
-
def process_images_with_prompt(image1, image2, image3, prompt):
|
202 |
"""
|
203 |
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ์ฌ ์ต์ข
์์ด ํ๋กฌํํธ(final_prompt)๋ฅผ ์์ฑํ ํ,
|
204 |
API๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
@@ -226,27 +239,60 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
226 |
final_prompt = "Please creatively composite these three images, combining their main elements into a cohesive and natural scene."
|
227 |
logger.info("Default prompt generated for three images")
|
228 |
|
229 |
-
result_img, status = generate_with_images(final_prompt, valid_images)
|
230 |
return result_img, status, final_prompt
|
231 |
except Exception as e:
|
232 |
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
233 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
|
234 |
|
235 |
-
def
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
|
243 |
# Gradio ์ธํฐํ์ด์ค
|
244 |
with gr.Blocks() as demo:
|
245 |
gr.HTML(
|
246 |
"""
|
247 |
<div style="text-align: center; margin-bottom: 1rem;">
|
248 |
-
<h1
|
249 |
-
<p>์ด๋ฏธ์ง๋ฅผ
|
250 |
</div>
|
251 |
"""
|
252 |
)
|
@@ -268,11 +314,22 @@ with gr.Blocks() as demo:
|
|
268 |
clothes_change_btn = gr.Button("๊ฐ์ ์ํ์ฐฉ์ฉ")
|
269 |
background_change_btn = gr.Button("๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ")
|
270 |
composite_product_btn = gr.Button("๋ถ๋ถ์ง์ฐ๊ธฐ")
|
271 |
-
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ", variant="primary")
|
|
|
|
|
272 |
with gr.Column():
|
273 |
-
|
274 |
-
|
275 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
276 |
|
277 |
# ์ ํ ์ต์
๋ฒํผ ํด๋ฆญ ์ ํ๋กฌํํธ ์
๋ ฅ๋ ์
๋ฐ์ดํธ (ํ๊ตญ์ด ๋ฌธ๊ตฌ)
|
278 |
image_change_btn.click(
|
@@ -302,9 +359,9 @@ with gr.Blocks() as demo:
|
|
302 |
)
|
303 |
|
304 |
submit_btn.click(
|
305 |
-
fn=
|
306 |
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
307 |
-
outputs=[
|
308 |
)
|
309 |
|
310 |
gr.Markdown(
|
@@ -314,6 +371,7 @@ with gr.Blocks() as demo:
|
|
314 |
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค.
|
315 |
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
|
316 |
3. **์ ํ ์ต์
**: ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ๋กฌํํธ ์
๋ ฅ๋์ ํ๊ตญ์ด ๋ฌธ๊ตฌ๋ก ์
๋ ฅํ์๋ฉด ๋ฉ๋๋ค.
|
|
|
317 |
|
318 |
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
|
319 |
"""
|
|
|
5 |
import logging
|
6 |
import re
|
7 |
from io import BytesIO
|
8 |
+
import time
|
9 |
|
10 |
from google import genai
|
11 |
from google.genai import types
|
|
|
147 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
148 |
return prompt
|
149 |
|
150 |
+
def generate_with_images(prompt, images, variation_index=0):
|
151 |
"""
|
152 |
API ํธ์ถ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ณ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
153 |
+
variation_index๋ก ๋ค์ํ ๋ณํ๋ฅผ ์ค๋๋ค.
|
154 |
"""
|
155 |
try:
|
156 |
api_key = os.environ.get("GEMINI_API_KEY")
|
|
|
158 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
159 |
|
160 |
client = genai.Client(api_key=api_key)
|
161 |
+
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
162 |
+
|
163 |
+
# ๋ณํ ์ธ๋ฑ์ค์ ๋ฐ๋ผ ํ๋กฌํํธ์ ์ฝ๊ฐ์ ๋ณํ ์ถ๊ฐ
|
164 |
+
variation_suffixes = [
|
165 |
+
" ์ฒซ ๋ฒ์งธ ๋ณํ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์.",
|
166 |
+
" ๋ ๋ฒ์งธ ๋ณํ์ผ๋ก ๋ ์์ํ๊ฒ ๋ง๋ค์ด์ฃผ์ธ์.",
|
167 |
+
" ์ธ ๋ฒ์งธ ๋ณํ์ผ๋ก ๋ ์ฐฝ์์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์.",
|
168 |
+
" ๋ค ๋ฒ์งธ ๋ณํ์ผ๋ก ์๊ฐ์ ๋ ๊ฐ์กฐํด์ ๋ง๋ค์ด์ฃผ์ธ์."
|
169 |
+
]
|
170 |
+
|
171 |
+
if variation_index < len(variation_suffixes):
|
172 |
+
prompt = prompt + variation_suffixes[variation_index]
|
173 |
|
174 |
contents = [prompt]
|
175 |
for idx, img in enumerate(images, 1):
|
|
|
211 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
212 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
213 |
|
214 |
+
def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0):
|
215 |
"""
|
216 |
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ์ฌ ์ต์ข
์์ด ํ๋กฌํํธ(final_prompt)๋ฅผ ์์ฑํ ํ,
|
217 |
API๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
|
|
239 |
final_prompt = "Please creatively composite these three images, combining their main elements into a cohesive and natural scene."
|
240 |
logger.info("Default prompt generated for three images")
|
241 |
|
242 |
+
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
243 |
return result_img, status, final_prompt
|
244 |
except Exception as e:
|
245 |
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
246 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
|
247 |
|
248 |
+
def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progress()):
|
249 |
+
"""
|
250 |
+
์ฌ๋ฌ ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์ฐจ๋ก๋๋ก ์์ฑํฉ๋๋ค.
|
251 |
+
"""
|
252 |
+
results = []
|
253 |
+
statuses = []
|
254 |
+
prompts = []
|
255 |
+
|
256 |
+
num_images = 4 # ์์ฑํ ์ด๋ฏธ์ง ์
|
257 |
+
|
258 |
+
progress(0, desc="์ด๋ฏธ์ง ์์ฑ ์ค๋น ์ค...")
|
259 |
+
|
260 |
+
for i in range(num_images):
|
261 |
+
progress((i / num_images), desc=f"{i+1}/{num_images} ์ด๋ฏธ์ง ์์ฑ ์ค...")
|
262 |
+
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt, i)
|
263 |
+
|
264 |
+
if result_img is not None:
|
265 |
+
results.append(result_img)
|
266 |
+
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
267 |
+
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
268 |
+
else:
|
269 |
+
# ์๋ฌ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ์๋ ๊ฒฐ๊ณผ ๋ชฉ๋ก์ None์ ์ถ๊ฐ
|
270 |
+
results.append(None)
|
271 |
+
statuses.append(f"์ด๋ฏธ์ง #{i+1} ์์ฑ ์คํจ: {status}")
|
272 |
+
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
273 |
+
|
274 |
+
# API ํธ์ถ ์ฌ์ด์ ์ฝ๊ฐ์ ๊ฐ๊ฒฉ์ ๋์ด ์๋ ์ ํ ๋ฐฉ์ง
|
275 |
+
time.sleep(1)
|
276 |
+
|
277 |
+
progress(1.0, desc="์ด๋ฏธ์ง ์์ฑ ์๋ฃ!")
|
278 |
+
|
279 |
+
# ๊ฒฐ๊ณผ๊ฐ ์๋ ๊ฒฝ์ฐ ๋น ์ฌ๋กฏ ์ฑ์ฐ๊ธฐ
|
280 |
+
while len(results) < 4:
|
281 |
+
results.append(None)
|
282 |
+
|
283 |
+
# ์ํ ๋ฉ์์ง์ ํ๋กฌํํธ ์ ๋ณด ๊ฒฐํฉ
|
284 |
+
combined_status = "\n".join(statuses)
|
285 |
+
combined_prompts = "\n".join(prompts)
|
286 |
+
|
287 |
+
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
288 |
|
289 |
# Gradio ์ธํฐํ์ด์ค
|
290 |
with gr.Blocks() as demo:
|
291 |
gr.HTML(
|
292 |
"""
|
293 |
<div style="text-align: center; margin-bottom: 1rem;">
|
294 |
+
<h1>4์ฅ ์ด๋ฏธ์ง ์์ฑ๊ธฐ (์์ ํฌํจ)</h1>
|
295 |
+
<p>์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ "์ด๋ฏธ์ง ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฐจ๋ก๋ก 4์ฅ์ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.</p>
|
296 |
</div>
|
297 |
"""
|
298 |
)
|
|
|
314 |
clothes_change_btn = gr.Button("๊ฐ์ ์ํ์ฐฉ์ฉ")
|
315 |
background_change_btn = gr.Button("๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ")
|
316 |
composite_product_btn = gr.Button("๋ถ๋ถ์ง์ฐ๊ธฐ")
|
317 |
+
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ (4์ฅ)", variant="primary")
|
318 |
+
|
319 |
+
with gr.Row():
|
320 |
with gr.Column():
|
321 |
+
output_image1 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #1")
|
322 |
+
with gr.Column():
|
323 |
+
output_image2 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #2")
|
324 |
+
|
325 |
+
with gr.Row():
|
326 |
+
with gr.Column():
|
327 |
+
output_image3 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #3")
|
328 |
+
with gr.Column():
|
329 |
+
output_image4 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #4")
|
330 |
+
|
331 |
+
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
332 |
+
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", visible=True)
|
333 |
|
334 |
# ์ ํ ์ต์
๋ฒํผ ํด๋ฆญ ์ ํ๋กฌํํธ ์
๋ ฅ๋ ์
๋ฐ์ดํธ (ํ๊ตญ์ด ๋ฌธ๊ตฌ)
|
335 |
image_change_btn.click(
|
|
|
359 |
)
|
360 |
|
361 |
submit_btn.click(
|
362 |
+
fn=generate_multiple_images,
|
363 |
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
364 |
+
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
365 |
)
|
366 |
|
367 |
gr.Markdown(
|
|
|
371 |
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค.
|
372 |
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
|
373 |
3. **์ ํ ์ต์
**: ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ๋กฌํํธ ์
๋ ฅ๋์ ํ๊ตญ์ด ๋ฌธ๊ตฌ๋ก ์
๋ ฅํ์๋ฉด ๋ฉ๋๋ค.
|
374 |
+
4. **๋ค์ํ ์ด๋ฏธ์ง**: "์ด๋ฏธ์ง ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฐจ๋ก๋ก 4์ฅ์ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.
|
375 |
|
376 |
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
|
377 |
"""
|