Kims12 commited on
Commit
7b03e26
ยท
verified ยท
1 Parent(s): 395173a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -111
app.py CHANGED
@@ -4,9 +4,9 @@ from PIL import Image
4
  import os
5
  import json
6
  import tempfile
 
7
  import re
8
  import time
9
- import logging
10
 
11
  # ๋กœ๊น… ์„ค์ •
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
@@ -157,7 +157,7 @@ def generate_prompt_with_gemini(product_name, background_info, additional_info="
157
  )
158
  response = model.generate_content(
159
  prompt_request,
160
- generation_config=genai.GenerationConfig(
161
  temperature=0.7,
162
  top_p=0.95,
163
  top_k=64,
@@ -171,7 +171,8 @@ def generate_prompt_with_gemini(product_name, background_info, additional_info="
171
  except Exception as e:
172
  return f"ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
173
 
174
- # ์ด๋ฏธ์ง€ ์ƒ์„ฑ์— ํ•„์š”ํ•œ ํ•จ์ˆ˜๋“ค
 
175
  def save_binary_file(file_name, data):
176
  with open(file_name, "wb") as f:
177
  f.write(data)
@@ -185,14 +186,15 @@ def translate_prompt_to_english(prompt):
185
  prompt = prompt.replace("#3", "IMAGE_TAG_THREE")
186
 
187
  try:
188
- if not GEMINI_API_KEY:
 
189
  logger.error("Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
190
  prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
191
  prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
192
  prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
193
  return prompt
194
 
195
- model = genai.GenerativeModel('gemini-2.0-flash')
196
  translation_prompt = f"""
197
  Translate the following Korean text to English:
198
 
@@ -203,9 +205,10 @@ def translate_prompt_to_english(prompt):
203
  """
204
 
205
  logger.info(f"Translation prompt: {translation_prompt}")
206
- response = model.generate_content(
207
- translation_prompt,
208
- generation_config=genai.GenerationConfig(
 
209
  temperature=0.2,
210
  top_p=0.95,
211
  top_k=40,
@@ -213,7 +216,10 @@ def translate_prompt_to_english(prompt):
213
  )
214
  )
215
 
216
- translated_text = response.text
 
 
 
217
 
218
  if translated_text.strip():
219
  translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
@@ -284,10 +290,11 @@ def preprocess_prompt(prompt, image1, image2, image3):
284
 
285
  def generate_with_images(prompt, images, variation_index=0):
286
  try:
287
- if not GEMINI_API_KEY:
 
288
  return None, "API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
289
 
290
- model = genai.GenerativeModel('gemini-2.0-flash-exp-image-generation')
291
  logger.info(f"Gemini API ์š”์ฒญ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: {prompt}, ๋ณ€ํ˜• ์ธ๋ฑ์Šค: {variation_index}")
292
 
293
  variation_suffixes = [
@@ -308,9 +315,11 @@ def generate_with_images(prompt, images, variation_index=0):
308
  contents.append(img)
309
  logger.info(f"์ด๋ฏธ์ง€ #{idx} ์ถ”๊ฐ€๋จ")
310
 
311
- response = model.generate_content(
 
312
  contents=contents,
313
- generation_config=genai.GenerationConfig(
 
314
  temperature=1,
315
  top_p=0.95,
316
  top_k=40,
@@ -322,26 +331,19 @@ def generate_with_images(prompt, images, variation_index=0):
322
  temp_path = tmp.name
323
  result_text = ""
324
  image_found = False
325
-
326
- if hasattr(response, 'candidates') and response.candidates:
327
- candidate = response.candidates[0]
328
- if hasattr(candidate, 'content') and candidate.content:
329
- for part in candidate.content.parts:
330
- if hasattr(part, 'text') and part.text:
331
- result_text += part.text
332
- logger.info(f"์‘๋‹ต ํ…์ŠคํŠธ: {part.text}")
333
- elif hasattr(part, 'inline_data') and part.inline_data:
334
- save_binary_file(temp_path, part.inline_data.data)
335
- image_found = True
336
- logger.info("์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€ ์ถ”์ถœ ์„ฑ๊ณต")
337
-
338
  if not image_found:
339
  return None, f"API์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์‘๋‹ต ํ…์ŠคํŠธ: {result_text}"
340
-
341
  result_img = Image.open(temp_path)
342
  if result_img.mode == "RGBA":
343
  result_img = result_img.convert("RGB")
344
-
345
  return result_img, f"์ด๋ฏธ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. {result_text}"
346
  except Exception as e:
347
  logger.exception("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
@@ -427,20 +429,26 @@ def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progres
427
  return results[0], results[1], results[2], results[3], combined_status, combined_prompts
428
 
429
  def create_app():
430
- with gr.Blocks() as demo:
431
- gr.Markdown("# ๊ณ ๊ธ‰ ์ƒํ’ˆ ์ด๋ฏธ์ง€ ๋ฐฐ๊ฒฝ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ๊ธฐ")
432
- gr.Markdown("์ƒํ’ˆ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด ๊ณ ํ’ˆ์งˆ ์ƒ์—…์šฉ ํ”„๋กฌํ”„ํŠธ๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.")
433
 
 
434
  with gr.Row():
435
- with gr.Column():
436
- image_input = gr.Image(type="pil", label="์ƒํ’ˆ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ")
437
-
438
- gr.Markdown("## ๋ฐฐ๊ฒฝ ์œ ํ˜• ์„ ํƒ")
439
- background_type = gr.Radio(
440
- choices=["์‹ฌํ”Œ ๋ฐฐ๊ฒฝ", "์ŠคํŠœ๋””์˜ค ๋ฐฐ๊ฒฝ", "์ž์—ฐ ํ™˜๊ฒฝ", "์‹ค๋‚ด ํ™˜๊ฒฝ", "์ถ”์ƒ/ํŠน์ˆ˜ ๋ฐฐ๊ฒฝ"],
441
- label="๋ฐฐ๊ฒฝ ์œ ํ˜•",
442
- value="์‹ฌํ”Œ ๋ฐฐ๊ฒฝ"
443
- )
 
 
 
 
 
444
 
445
  # ์‹ฌํ”Œ ๋ฐฐ๊ฒฝ ์„ ํƒ ๋“œ๋กญ๋‹ค์šด
446
  simple_dropdown = gr.Dropdown(
@@ -469,7 +477,7 @@ def create_app():
469
  interactive=True
470
  )
471
 
472
- # ์‹ค๋‚ด ํ™˜๊ฒฝ ์„ ํƒ ๋“œ๋กญ๋‹ค์šด
473
  indoor_dropdown = gr.Dropdown(
474
  choices=list(INDOOR_BACKGROUNDS.keys()),
475
  value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None,
@@ -487,39 +495,40 @@ def create_app():
487
  interactive=True
488
  )
489
 
490
- # ์ƒํ’ˆ๋ช… ์ž…๋ ฅ
491
- product_name = gr.Textbox(label="์ƒํ’ˆ๋ช… (ํ•œ๊ตญ์–ด ์ž…๋ ฅ)", placeholder="์˜ˆ: ์Šคํ‚จ์ผ€์–ด ํŠœ๋ธŒ, ํ…€๋ธ”๋Ÿฌ ๋“ฑ")
 
 
492
 
493
  # ์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ
494
  additional_info = gr.Textbox(
495
  label="์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ (์„ ํƒ์‚ฌํ•ญ)",
496
  placeholder="์˜ˆ: ๊ณ ๊ธ‰์Šค๋Ÿฌ์šด ๋А๋‚Œ, ๋ฐ์€ ์กฐ๋ช…, ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š” ๋“ฑ",
497
- lines=3
 
498
  )
499
 
500
- generate_prompt_btn = gr.Button("ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ")
 
 
 
 
501
 
502
- with gr.Column():
503
- prompt_output = gr.Textbox(label="์ƒ์„ฑ๋œ ํ”„๋กฌํ”„ํŠธ", lines=6)
 
504
 
505
- gr.Markdown("## ์ด๋ฏธ์ง€ ์ƒ์„ฑ")
 
506
  with gr.Row():
507
- generate_single_btn = gr.Button("์ด๋ฏธ์ง€ ์ƒ์„ฑ (1์žฅ)")
508
- generate_multiple_btn = gr.Button("์ด๋ฏธ์ง€ ์ƒ์„ฑ (4์žฅ)")
509
-
510
- gr.Markdown("## ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€")
511
- with gr.Row():
512
- with gr.Column():
513
- output_image1 = gr.Image(label="์ด๋ฏธ์ง€ #1")
514
- with gr.Column():
515
- output_image2 = gr.Image(label="์ด๋ฏธ์ง€ #2")
516
- with gr.Row():
517
- with gr.Column():
518
- output_image3 = gr.Image(label="์ด๋ฏธ์ง€ #3")
519
- with gr.Column():
520
- output_image4 = gr.Image(label="์ด๋ฏธ์ง€ #4")
521
-
522
- status_output = gr.Textbox(label="์ƒํƒœ ๋ฉ”์‹œ์ง€", lines=2)
523
 
524
  # ๋ฐฐ๊ฒฝ ์œ ํ˜•์— ๋”ฐ๋ผ ๋“œ๋กญ๋‹ค์šด ํ‘œ์‹œ ์—…๋ฐ์ดํŠธ
525
  def update_dropdowns(bg_type):
@@ -558,7 +567,7 @@ def create_app():
558
  "name": nature,
559
  "english": NATURE_BACKGROUNDS.get(nature, "natural environment")
560
  }
561
- elif bg_type == "์‹ค๋‚ด ํ™˜๊ฒฝ":
562
  return {
563
  "category": "์‹ค๋‚ด ํ™˜๊ฒฝ",
564
  "name": indoor,
@@ -578,12 +587,9 @@ elif bg_type == "์‹ค๋‚ด ํ™˜๊ฒฝ":
578
  }
579
 
580
  # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ํ•จ์ˆ˜
581
- def generate_prompt(image, bg_type, simple, studio, nature, indoor, abstract, product_text, additional_text):
582
- if image is None:
583
- gr.Warning("์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”.")
584
- return "์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”."
585
-
586
- product_text = product_text.strip() or "์ œํ’ˆ"
587
 
588
  # ๋ฐฐ๊ฒฝ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
589
  background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract)
@@ -600,7 +606,6 @@ API ํ‚ค ์„ค์ • ๋ฐฉ๋ฒ•:
600
  2. ์ฝ”๋“œ ๋‚ด ์ง์ ‘ ์ž…๋ ฅ: GEMINI_API_KEY = "your-api-key"
601
  ํ‚ค ๋ฐœ๊ธ‰: https://makersuite.google.com/
602
  """
603
- return prompt
604
 
605
  return prompt
606
  except Exception as e:
@@ -608,38 +613,10 @@ API ํ‚ค ์„ค์ • ๋ฐฉ๋ฒ•:
608
  gr.Error(error_msg)
609
  return error_msg
610
 
611
- # ๋‹จ์ผ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ํ•จ์ˆ˜
612
- def generate_single_image(image, prompt):
613
- if image is None:
614
- return None, None, None, None, "์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•ด์ฃผ์„ธ์š”."
615
-
616
- if not prompt or prompt.strip() == "":
617
- return None, None, None, None, "ํ”„๋กฌํ”„ํŠธ๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”."
618
-
619
- if not GEMINI_API_KEY:
620
- return None, None, None, None, "Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
621
-
622
- try:
623
- # ์˜์–ด ํ”„๋กฌํ”„ํŠธ๋กœ ๋ณ€ํ™˜
624
- if re.search("[๊ฐ€-ํžฃ]", prompt):
625
- prompt = translate_prompt_to_english(prompt)
626
-
627
- # ์ด๋ฏธ์ง€ 1๊ฐœ ์ƒ์„ฑ (variation_index=0)
628
- result_img, status, _ = process_images_with_prompt(image, None, None, prompt, 0, 3)
629
-
630
- if result_img is None:
631
- return None, None, None, None, status
632
-
633
- return result_img, None, None, None, status
634
- except Exception as e:
635
- error_msg = f"์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
636
- return None, None, None, None, error_msg
637
-
638
- # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
639
  generate_prompt_btn.click(
640
  fn=generate_prompt,
641
  inputs=[
642
- image_input,
643
  background_type,
644
  simple_dropdown,
645
  studio_dropdown,
@@ -652,18 +629,18 @@ API ํ‚ค ์„ค์ • ๋ฐฉ๋ฒ•:
652
  outputs=prompt_output
653
  )
654
 
655
- # ๋‹จ์ผ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
656
- generate_single_btn.click(
657
- fn=generate_single_image,
658
- inputs=[image_input, prompt_output],
659
- outputs=[output_image1, output_image2, output_image3, output_image4, status_output]
660
  )
661
 
662
- # ์—ฌ๋Ÿฌ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ์—ฐ๊ฒฐ
663
- generate_multiple_btn.click(
664
- fn=lambda img, prompt: generate_multiple_images(img, None, None, prompt),
665
- inputs=[image_input, prompt_output],
666
- outputs=[output_image1, output_image2, output_image3, output_image4, status_output, prompt_output]
667
  )
668
 
669
  return demo
 
4
  import os
5
  import json
6
  import tempfile
7
+ import logging
8
  import re
9
  import time
 
10
 
11
  # ๋กœ๊น… ์„ค์ •
12
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
 
157
  )
158
  response = model.generate_content(
159
  prompt_request,
160
+ generation_config=genai.types.GenerationConfig(
161
  temperature=0.7,
162
  top_p=0.95,
163
  top_k=64,
 
171
  except Exception as e:
172
  return f"ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}"
173
 
174
+ # ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๊ด€๋ จ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋“ค
175
+
176
  def save_binary_file(file_name, data):
177
  with open(file_name, "wb") as f:
178
  f.write(data)
 
186
  prompt = prompt.replace("#3", "IMAGE_TAG_THREE")
187
 
188
  try:
189
+ api_key = GEMINI_API_KEY
190
+ if not api_key:
191
  logger.error("Gemini API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
192
  prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
193
  prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
194
  prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
195
  return prompt
196
 
197
+ client = genai.Client(api_key=api_key)
198
  translation_prompt = f"""
199
  Translate the following Korean text to English:
200
 
 
205
  """
206
 
207
  logger.info(f"Translation prompt: {translation_prompt}")
208
+ response = client.generate_content(
209
+ model="gemini-2.0-flash",
210
+ contents=[translation_prompt],
211
+ generation_config=genai.types.GenerationConfig(
212
  temperature=0.2,
213
  top_p=0.95,
214
  top_k=40,
 
216
  )
217
  )
218
 
219
+ translated_text = ""
220
+ for part in response.text:
221
+ if hasattr(part, 'text') and part.text:
222
+ translated_text += part.text
223
 
224
  if translated_text.strip():
225
  translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
 
290
 
291
  def generate_with_images(prompt, images, variation_index=0):
292
  try:
293
+ api_key = GEMINI_API_KEY
294
+ if not api_key:
295
  return None, "API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ํ™˜๊ฒฝ๋ณ€์ˆ˜๋ฅผ ํ™•์ธํ•ด์ฃผ์„ธ์š”."
296
 
297
+ client = genai.Client(api_key=api_key)
298
  logger.info(f"Gemini API ์š”์ฒญ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: {prompt}, ๋ณ€ํ˜• ์ธ๋ฑ์Šค: {variation_index}")
299
 
300
  variation_suffixes = [
 
315
  contents.append(img)
316
  logger.info(f"์ด๋ฏธ์ง€ #{idx} ์ถ”๊ฐ€๋จ")
317
 
318
+ response = client.generate_content(
319
+ model="gemini-2.0-flash-exp-image-generation",
320
  contents=contents,
321
+ generation_config=genai.types.GenerationConfig(
322
+ response_modalities=['Text', 'Image'],
323
  temperature=1,
324
  top_p=0.95,
325
  top_k=40,
 
331
  temp_path = tmp.name
332
  result_text = ""
333
  image_found = False
334
+ for part in response.candidates[0].content.parts:
335
+ if hasattr(part, 'text') and part.text:
336
+ result_text += part.text
337
+ logger.info(f"์‘๋‹ต ํ…์ŠคํŠธ: {part.text}")
338
+ elif hasattr(part, 'inline_data') and part.inline_data:
339
+ save_binary_file(temp_path, part.inline_data.data)
340
+ image_found = True
341
+ logger.info("์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€ ์ถ”์ถœ ์„ฑ๊ณต")
 
 
 
 
 
342
  if not image_found:
343
  return None, f"API์—์„œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค. ์‘๋‹ต ํ…์ŠคํŠธ: {result_text}"
 
344
  result_img = Image.open(temp_path)
345
  if result_img.mode == "RGBA":
346
  result_img = result_img.convert("RGB")
 
347
  return result_img, f"์ด๋ฏธ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. {result_text}"
348
  except Exception as e:
349
  logger.exception("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
 
429
  return results[0], results[1], results[2], results[3], combined_status, combined_prompts
430
 
431
  def create_app():
432
+ with gr.Blocks(title="์ด๋ฏธ์ง€ ์ƒ์„ฑ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜") as demo:
433
+ gr.Markdown("# ์ƒํ’ˆ ์ด๋ฏธ์ง€ ๋ฐฐ๊ฒฝ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ๊ธฐ & ์ด๋ฏธ์ง€ ์ƒ์„ฑ๊ธฐ")
434
+ gr.Markdown("์ƒํ’ˆ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ณ  ์˜ต์…˜์„ ์„ ํƒํ•˜๋ฉด ๊ณ ํ’ˆ์งˆ ์ƒ์—…์šฉ ํ”„๋กฌํ”„ํŠธ๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.")
435
 
436
+ # ์ƒํ’ˆ๋ช… ์„น์…˜
437
  with gr.Row():
438
+ product_name = gr.Textbox(label="์ƒํ’ˆ๋ช… (ํ•œ๊ตญ์–ด ์ž…๋ ฅ)", placeholder="์˜ˆ: ์Šคํ‚จ์ผ€์–ด ํŠœ๋ธŒ, ํ…€๋ธ”๋Ÿฌ ๋“ฑ", interactive=True)
439
+
440
+ # ๋ฐฐ๊ฒฝ ์œ ํ˜•์„ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•œ ๋ผ๋””์˜ค ๋ฒ„ํŠผ
441
+ background_type = gr.Radio(
442
+ choices=["์‹ฌํ”Œ ๋ฐฐ๊ฒฝ", "์ŠคํŠœ๋””์˜ค ๋ฐฐ๊ฒฝ", "์ž์—ฐ ํ™˜๊ฒฝ", "์‹ค๋‚ด ํ™˜๊ฒฝ", "์ถ”์ƒ/ํŠน์ˆ˜ ๋ฐฐ๊ฒฝ"],
443
+ label="๋ฐฐ๊ฒฝ ์œ ํ˜•",
444
+ value="์‹ฌํ”Œ ๋ฐฐ๊ฒฝ"
445
+ )
446
+
447
+ # ๊ฐ ๋ฐฐ๊ฒฝ ์œ ํ˜•์— ๋งž๋Š” ๋“œ๋กญ๋‹ค์šด ์ปดํฌ๋„ŒํŠธ๋“ค (์ฒ˜์Œ์—๋Š” ์‹ฌํ”Œ ๋ฐฐ๊ฒฝ๋งŒ ํ‘œ์‹œ)
448
+ with gr.Row():
449
+ # ์ขŒ์ธก ์ปฌ๋Ÿผ - ์ž…๋ ฅ ์ปจํŠธ๋กค
450
+ with gr.Column(scale=1):
451
+ image_input = gr.Image(label="์ƒํ’ˆ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ", type="pil")
452
 
453
  # ์‹ฌํ”Œ ๋ฐฐ๊ฒฝ ์„ ํƒ ๋“œ๋กญ๋‹ค์šด
454
  simple_dropdown = gr.Dropdown(
 
477
  interactive=True
478
  )
479
 
480
+ # ์‹ค๋‚ด ํ™˜๊ฒฝ ์„ ํƒ ๋“œ๋กญ๋‹ค์šด
481
  indoor_dropdown = gr.Dropdown(
482
  choices=list(INDOOR_BACKGROUNDS.keys()),
483
  value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None,
 
495
  interactive=True
496
  )
497
 
498
+ # ์ด๋ฏธ์ง€ ์ž…๋ ฅ ์ปดํฌ๋„ŒํŠธ (์ฐธ์กฐ ์ด๋ฏธ์ง€์šฉ)
499
+ gr.Markdown("## ์ถ”๊ฐ€ ์ฐธ์กฐ ์ด๋ฏธ์ง€ (์˜ต์…˜)")
500
+ image2_input = gr.Image(label="์ฐธ์กฐ ์ด๋ฏธ์ง€ #2 (์„ ํƒ์‚ฌํ•ญ)", type="pil", image_mode="RGB")
501
+ image3_input = gr.Image(label="์ฐธ์กฐ ์ด๋ฏธ์ง€ #3 (์„ ํƒ์‚ฌํ•ญ)", type="pil", image_mode="RGB")
502
 
503
  # ์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ
504
  additional_info = gr.Textbox(
505
  label="์ถ”๊ฐ€ ์š”์ฒญ์‚ฌํ•ญ (์„ ํƒ์‚ฌํ•ญ)",
506
  placeholder="์˜ˆ: ๊ณ ๊ธ‰์Šค๋Ÿฌ์šด ๋А๋‚Œ, ๋ฐ์€ ์กฐ๋ช…, ์ž์—ฐ์Šค๋Ÿฌ์šด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ์„ธ์š” ๋“ฑ",
507
+ lines=3,
508
+ interactive=True
509
  )
510
 
511
+ # ๋ฒ„ํŠผ ์˜์—ญ
512
+ with gr.Row():
513
+ generate_prompt_btn = gr.Button("ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ")
514
+ submit_single_btn = gr.Button("์ด๋ฏธ์ง€ ์ƒ์„ฑ (1์žฅ)")
515
+ submit_btn = gr.Button("์ด๋ฏธ์ง€ ์ƒ์„ฑ (4์žฅ)")
516
 
517
+ # ์šฐ์ธก ์ปฌ๋Ÿผ - ์ถœ๏ฟฝ๏ฟฝ ๊ฒฐ๊ณผ
518
+ with gr.Column(scale=1):
519
+ prompt_output = gr.Textbox(label="์ƒ์„ฑ๋œ ํ”„๋กฌํ”„ํŠธ", lines=10)
520
 
521
+ # ์ด๋ฏธ์ง€ ์ถœ๋ ฅ ์˜์—ญ
522
+ gr.Markdown("## ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€")
523
  with gr.Row():
524
+ output_image1 = gr.Image(label="์ด๋ฏธ์ง€ #1", type="pil")
525
+ output_image2 = gr.Image(label="์ด๋ฏธ์ง€ #2", type="pil")
526
+ with gr.Row():
527
+ output_image3 = gr.Image(label="์ด๋ฏธ์ง€ #3", type="pil")
528
+ output_image4 = gr.Image(label="์ด๋ฏธ์ง€ #4", type="pil")
529
+
530
+ output_text = gr.Textbox(label="์ƒํƒœ ๋ฉ”์‹œ์ง€", lines=2)
531
+ prompt_display = gr.Textbox(label="์‚ฌ์šฉ๋œ ํ”„๋กฌํ”„ํŠธ (์˜์–ด)", visible=True, lines=2)
 
 
 
 
 
 
 
 
532
 
533
  # ๋ฐฐ๊ฒฝ ์œ ํ˜•์— ๋”ฐ๋ผ ๋“œ๋กญ๋‹ค์šด ํ‘œ์‹œ ์—…๋ฐ์ดํŠธ
534
  def update_dropdowns(bg_type):
 
567
  "name": nature,
568
  "english": NATURE_BACKGROUNDS.get(nature, "natural environment")
569
  }
570
+ elif bg_type == "์‹ค๋‚ด ํ™˜๊ฒฝ":
571
  return {
572
  "category": "์‹ค๋‚ด ํ™˜๊ฒฝ",
573
  "name": indoor,
 
587
  }
588
 
589
  # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ํ•จ์ˆ˜
590
+ def generate_prompt(bg_type, simple, studio, nature, indoor, abstract, product_text, additional_text):
591
+ if not product_text.strip():
592
+ product_text = "์ œํ’ˆ"
 
 
 
593
 
594
  # ๋ฐฐ๊ฒฝ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
595
  background_info = get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract)
 
606
  2. ์ฝ”๋“œ ๋‚ด ์ง์ ‘ ์ž…๋ ฅ: GEMINI_API_KEY = "your-api-key"
607
  ํ‚ค ๋ฐœ๊ธ‰: https://makersuite.google.com/
608
  """
 
609
 
610
  return prompt
611
  except Exception as e:
 
613
  gr.Error(error_msg)
614
  return error_msg
615
 
616
+ # ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
617
  generate_prompt_btn.click(
618
  fn=generate_prompt,
619
  inputs=[
 
620
  background_type,
621
  simple_dropdown,
622
  studio_dropdown,
 
629
  outputs=prompt_output
630
  )
631
 
632
+ # ๋‹จ์ผ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
633
+ submit_single_btn.click(
634
+ fn=lambda image1, image2, image3, prompt: process_images_with_prompt(image1, image2, image3, prompt, 0),
635
+ inputs=[image_input, image2_input, image3_input, prompt_output],
636
+ outputs=[output_image1, output_text, prompt_display],
637
  )
638
 
639
+ # 4์žฅ ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ
640
+ submit_btn.click(
641
+ fn=generate_multiple_images,
642
+ inputs=[image_input, image2_input, image3_input, prompt_output],
643
+ outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
644
  )
645
 
646
  return demo