Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,9 @@ import logging
|
|
6 |
import re
|
7 |
import time
|
8 |
import json
|
9 |
-
|
|
|
|
|
10 |
from dotenv import load_dotenv
|
11 |
|
12 |
load_dotenv()
|
@@ -18,20 +20,64 @@ logger = logging.getLogger(__name__)
|
|
18 |
# Gemini API ํค ์ค์
|
19 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
20 |
|
21 |
-
#
|
|
|
|
|
|
|
22 |
BACKGROUNDS_DIR = "./background"
|
23 |
|
24 |
-
#
|
|
|
|
|
|
|
|
|
25 |
if not os.path.exists(BACKGROUNDS_DIR):
|
26 |
os.makedirs(BACKGROUNDS_DIR)
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
def save_binary_file(file_name, data):
|
37 |
with open(file_name, "wb") as f:
|
@@ -39,22 +85,42 @@ def save_binary_file(file_name, data):
|
|
39 |
|
40 |
def generate_system_instruction():
|
41 |
return """๋น์ ์ ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝํ๊ธฐ ์ํ ๊ณ ํ์ง ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
42 |
-
์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก
|
|
|
43 |
๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ๋ฐ๋ผ์ผ ํฉ๋๋ค:
|
44 |
1. ์ํ์ "#1"๋ก ์ง์ ํ์ฌ ์ฐธ์กฐํฉ๋๋ค. (์: "skincare tube (#1)")
|
45 |
2. *** ๋งค์ฐ ์ค์: ์ํ์ ์๋ ํน์ฑ(๋์์ธ, ์์, ํํ, ๋ก๊ณ , ํจํค์ง ๋ฑ)์ ์ด๋ค ์ํฉ์์๋ ์ ๋ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. ***
|
46 |
-
3. *** ์ํ์ ๋ณธ์ง์ ํน์ฑ์ ์ ์งํ๋, ์์ฐ์ค๋ฌ์ด ํ๊ฒฝ ํตํฉ์ ์ํ ์กฐ๋ช
๊ณผ ๊ทธ๋ฆผ์๋
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํฉ๋๋ค.
|
49 |
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค.
|
50 |
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค.
|
51 |
-
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ํ๊ฒฝ ์์๋ค์
|
|
|
|
|
|
|
|
|
|
|
52 |
9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช
์์ ์ผ๋ก ํฌํจํ์ธ์:
|
53 |
- "highly detailed commercial photography"
|
54 |
- "award-winning product photography"
|
55 |
- "professional advertising imagery"
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
"""
|
59 |
|
60 |
def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
|
@@ -75,58 +141,93 @@ def generate_prompt_with_gemini(product_name, background_info, additional_info="
|
|
75 |
5. ํ๊ฒฝ๊ณผ์ ์์ฐ์ค๋ฌ์ด ํตํฉ์ ์ํ ์กฐ๋ช
ํจ๊ณผ์ ๊ทธ๋ฆผ์๋ ํฌํจํด์ฃผ์ธ์.
|
76 |
6. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ฐฐ๊ฒฝ ํ๊ฒฝ์ ์ค๋ช
ํด์ฃผ์ธ์.
|
77 |
7. ๊ณ ๊ธ์ค๋ฌ์ด ์์
๊ด๊ณ ํ์ง์ ์ด๋ฏธ์ง๊ฐ ๋๋๋ก ํ๊ฒฝ ์ค๋ช
์ ํด์ฃผ์ธ์.
|
|
|
78 |
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์์ด๋ก ์ ์ ํ ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์.
|
79 |
"""
|
80 |
-
genai.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
|
84 |
-
|
85 |
-
|
|
|
86 |
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
except Exception as e:
|
91 |
-
|
|
|
|
|
92 |
|
93 |
-
def
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
"english": SIMPLE_BACKGROUNDS.get(simple, "white background")
|
99 |
-
}
|
100 |
-
elif bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ":
|
101 |
-
return {
|
102 |
-
"category": "์คํ๋์ค ๋ฐฐ๊ฒฝ",
|
103 |
-
"name": studio,
|
104 |
-
"english": STUDIO_BACKGROUNDS.get(studio, "product photography studio")
|
105 |
-
}
|
106 |
-
elif bg_type == "์์ฐ ํ๊ฒฝ":
|
107 |
-
return {
|
108 |
-
"category": "์์ฐ ํ๊ฒฝ",
|
109 |
-
"name": nature,
|
110 |
-
"english": NATURE_BACKGROUNDS.get(nature, "natural environment")
|
111 |
-
}
|
112 |
-
elif bg_type == "์ค๋ด ํ๊ฒฝ":
|
113 |
-
return {
|
114 |
-
"category": "์ค๋ด ํ๊ฒฝ",
|
115 |
-
"name": indoor,
|
116 |
-
"english": INDOOR_BACKGROUNDS.get(indoor, "indoor environment")
|
117 |
-
}
|
118 |
-
elif bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ":
|
119 |
-
return {
|
120 |
-
"category": "์ถ์/ํน์ ๋ฐฐ๊ฒฝ",
|
121 |
-
"name": abstract,
|
122 |
-
"english": ABSTRACT_BACKGROUNDS.get(abstract, "abstract background")
|
123 |
-
}
|
124 |
else:
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
}
|
130 |
|
131 |
def generate_with_images(prompt, images, variation_index=0):
|
132 |
try:
|
@@ -134,7 +235,7 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
134 |
if not api_key:
|
135 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
136 |
|
137 |
-
genai.
|
138 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
139 |
|
140 |
variation_suffixes = [
|
@@ -149,44 +250,37 @@ def generate_with_images(prompt, images, variation_index=0):
|
|
149 |
else:
|
150 |
prompt = prompt + " Do not add any text, watermarks, or labels to the image."
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
parts = [prompt]
|
155 |
-
for img in images:
|
156 |
if img is not None:
|
157 |
-
|
158 |
-
|
159 |
-
img_data = f.read()
|
160 |
-
parts.append({"mime_type": "image/jpeg", "data": img_data})
|
161 |
-
else: # If it's a PIL Image
|
162 |
-
img_bytes = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
|
163 |
-
img.save(img_bytes, format="JPEG")
|
164 |
-
img_bytes.close()
|
165 |
-
|
166 |
-
with open(img_bytes.name, "rb") as f:
|
167 |
-
img_data = f.read()
|
168 |
-
|
169 |
-
parts.append({"mime_type": "image/jpeg", "data": img_data})
|
170 |
-
os.unlink(img_bytes.name) # Clean up temp file
|
171 |
|
172 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
# ์์ ํ์ผ์ ํญ์ JPG ํ์ฅ์๋ก ์์ฑ
|
175 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
|
176 |
temp_path = tmp.name
|
177 |
result_text = ""
|
178 |
image_found = False
|
179 |
-
|
180 |
-
for part in response.parts:
|
181 |
if hasattr(part, 'text') and part.text:
|
182 |
result_text += part.text
|
183 |
logger.info(f"์๋ต ํ
์คํธ: {part.text}")
|
184 |
elif hasattr(part, 'inline_data') and part.inline_data:
|
185 |
-
|
186 |
-
save_binary_file(temp_path, img_data)
|
187 |
image_found = True
|
188 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
189 |
-
|
190 |
if not image_found:
|
191 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
192 |
|
@@ -215,6 +309,7 @@ def process_images_with_prompt(image1, prompt, variation_index=0, max_retries=3)
|
|
215 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
216 |
|
217 |
if prompt and prompt.strip():
|
|
|
218 |
final_prompt = prompt
|
219 |
else:
|
220 |
final_prompt = "Please creatively transform this image into a more vivid and artistic version. Do not include any text or watermarks in the generated image."
|
@@ -271,105 +366,144 @@ def generate_multiple_images(image1, prompt, progress=gr.Progress()):
|
|
271 |
|
272 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
# UI ๊ตฌ์ฑ
|
275 |
-
with gr.Blocks(
|
276 |
-
gr.
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
image_input = gr.Image(type="pil", label="์ํ ์ด๋ฏธ์ง ์
๋ก๋", image_mode="RGB")
|
282 |
-
|
283 |
-
# ์ํ๋ช
์น์
|
284 |
-
product_name = gr.Textbox(label="์ํ๋ช
", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ํ
๋ธ๋ฌ ๋ฑ", interactive=True)
|
285 |
-
|
286 |
-
# ๋ฐฐ๊ฒฝ ์ ํ์ ์ ํํ๊ธฐ ์ํ ๋ผ๋์ค ๋ฒํผ
|
287 |
-
bg_type = gr.Radio(
|
288 |
-
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"],
|
289 |
-
label="๋ฐฐ๊ฒฝ ์ ํ",
|
290 |
-
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
291 |
-
)
|
292 |
-
|
293 |
-
# ๊ฐ ๋ฐฐ๊ฒฝ ์ ํ์ ๋ง๋ ๋๋กญ๋ค์ด ์ปดํฌ๋ํธ๋ค
|
294 |
-
with gr.Group():
|
295 |
-
simple_dropdown = gr.Dropdown(
|
296 |
-
choices=list(SIMPLE_BACKGROUNDS.keys()),
|
297 |
-
value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else None,
|
298 |
-
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
|
299 |
-
visible=True,
|
300 |
-
interactive=True
|
301 |
-
)
|
302 |
-
|
303 |
-
studio_dropdown = gr.Dropdown(
|
304 |
-
choices=list(STUDIO_BACKGROUNDS.keys()),
|
305 |
-
value=list(STUDIO_BACKGROUNDS.keys())[0] if STUDIO_BACKGROUNDS else None,
|
306 |
-
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
307 |
-
visible=False,
|
308 |
-
interactive=True
|
309 |
-
)
|
310 |
-
|
311 |
-
nature_dropdown = gr.Dropdown(
|
312 |
-
choices=list(NATURE_BACKGROUNDS.keys()),
|
313 |
-
value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else None,
|
314 |
-
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
315 |
-
visible=False,
|
316 |
-
interactive=True
|
317 |
-
)
|
318 |
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
visible=False,
|
332 |
-
interactive=True
|
333 |
-
)
|
334 |
-
|
335 |
-
additional_info = gr.Textbox(
|
336 |
-
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
337 |
-
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์ ๋ฑ",
|
338 |
-
lines=3,
|
339 |
-
interactive=True
|
340 |
-
)
|
341 |
-
|
342 |
-
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ
|
343 |
-
generate_prompt_btn = gr.Button("ํ๋กฌํํธ ์์ฑ")
|
344 |
-
|
345 |
-
# ์์ฑ๋ ํ๋กฌํํธ ํ์
|
346 |
-
prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ", lines=5)
|
347 |
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
with gr.Column(scale=1):
|
361 |
-
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ์น์
|
362 |
-
with gr.Row():
|
363 |
-
with gr.Column():
|
364 |
-
output_image1 = gr.Image(label="์ด๋ฏธ์ง #1", type="filepath")
|
365 |
-
output_image3 = gr.Image(label="์ด๋ฏธ์ง #3", type="filepath")
|
366 |
-
with gr.Column():
|
367 |
-
output_image2 = gr.Image(label="์ด๋ฏธ์ง #2", type="filepath")
|
368 |
-
output_image4 = gr.Image(label="์ด๋ฏธ์ง #4", type="filepath")
|
369 |
-
|
370 |
-
# ๊ฒฐ๊ณผ ์ ๋ณด
|
371 |
-
output_text = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=2)
|
372 |
-
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", lines=2)
|
373 |
|
374 |
# ๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ผ ๋๋กญ๋ค์ด ํ์ ์
๋ฐ์ดํธ
|
375 |
def update_dropdowns(bg_type):
|
@@ -381,8 +515,15 @@ with gr.Blocks(title="์ด๋ฏธ์ง ์์ฑ๊ธฐ & ํ๋กฌํํธ ์์ฑ๊ธฐ") as demo:
|
|
381 |
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"))
|
382 |
}
|
383 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
384 |
# ํ๋กฌํํธ ์์ฑ ํจ์
|
385 |
-
def
|
386 |
if image is None:
|
387 |
return "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
388 |
|
@@ -395,28 +536,19 @@ with gr.Blocks(title="์ด๋ฏธ์ง ์์ฑ๊ธฐ & ํ๋กฌํํธ ์์ฑ๊ธฐ") as demo:
|
|
395 |
prompt = generate_prompt_with_gemini(product_text, background_info, additional_text)
|
396 |
|
397 |
if not GEMINI_API_KEY:
|
398 |
-
return "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.
|
399 |
|
400 |
return prompt
|
401 |
except Exception as e:
|
402 |
-
|
403 |
-
|
404 |
-
# ํ๋กฌํํธ ์ถ๋ ฅ์ ์ปค์คํ
ํ๋กฌํํธ์ ๋ณต์ฌ
|
405 |
-
def copy_to_custom(prompt):
|
406 |
-
return prompt
|
407 |
-
|
408 |
-
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
409 |
-
bg_type.change(
|
410 |
-
fn=update_dropdowns,
|
411 |
-
inputs=[bg_type],
|
412 |
-
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
413 |
-
)
|
414 |
|
|
|
415 |
generate_prompt_btn.click(
|
416 |
-
fn=
|
417 |
inputs=[
|
418 |
-
|
419 |
-
|
420 |
simple_dropdown,
|
421 |
studio_dropdown,
|
422 |
nature_dropdown,
|
@@ -428,27 +560,19 @@ with gr.Blocks(title="์ด๋ฏธ์ง ์์ฑ๊ธฐ & ํ๋กฌํํธ ์์ฑ๊ธฐ") as demo:
|
|
428 |
outputs=prompt_output
|
429 |
)
|
430 |
|
431 |
-
# ์์ฑ๋ ํ๋กฌํํธ๋ฅผ ์ปค์คํ
ํ๋กฌํํธ ํ๋์ ๋ณต์ฌ
|
432 |
-
prompt_output.change(
|
433 |
-
fn=copy_to_custom,
|
434 |
-
inputs=[prompt_output],
|
435 |
-
outputs=[custom_prompt]
|
436 |
-
)
|
437 |
-
|
438 |
# ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
439 |
submit_single_btn.click(
|
440 |
fn=lambda image1, prompt: process_images_with_prompt(image1, prompt, 0),
|
441 |
-
inputs=[
|
442 |
outputs=[output_image1, output_text, prompt_display],
|
443 |
)
|
444 |
|
445 |
# 4์ฅ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
446 |
submit_btn.click(
|
447 |
fn=generate_multiple_images,
|
448 |
-
inputs=[
|
449 |
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
450 |
)
|
451 |
|
452 |
-
|
453 |
-
|
454 |
-
demo.launch()
|
|
|
6 |
import re
|
7 |
import time
|
8 |
import json
|
9 |
+
|
10 |
+
from google import genai
|
11 |
+
from google.genai import types
|
12 |
from dotenv import load_dotenv
|
13 |
|
14 |
load_dotenv()
|
|
|
20 |
# Gemini API ํค ์ค์
|
21 |
GEMINI_API_KEY = os.environ.get("GEMINI_API_KEY", "")
|
22 |
|
23 |
+
# Gemini API ์ด๊ธฐํ
|
24 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
25 |
+
|
26 |
+
# ๋ฐฐ๊ฒฝ JSON ํ์ผ ๊ฒฝ๋ก ์ค์ - ์๋ ๊ฒฝ๋ก ์ฌ์ฉ
|
27 |
BACKGROUNDS_DIR = "./background"
|
28 |
|
29 |
+
# ๋๋ฒ๊น
์ ์ํ ์ ๋ณด ์ถ๋ ฅ
|
30 |
+
print(f"ํ์ฌ ์์
๋๋ ํ ๋ฆฌ: {os.getcwd()}")
|
31 |
+
print(f"์ฌ์ฉ ์ค์ธ ๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก: {BACKGROUNDS_DIR}")
|
32 |
+
|
33 |
+
# JSON ํ์ผ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ๋๋ ํ ๋ฆฌ ์์ฑ
|
34 |
if not os.path.exists(BACKGROUNDS_DIR):
|
35 |
os.makedirs(BACKGROUNDS_DIR)
|
36 |
+
print(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๋ฅผ ์์ฑํ์ต๋๋ค: {BACKGROUNDS_DIR}")
|
37 |
+
else:
|
38 |
+
print(f"๋ฐฐ๊ฒฝ ๋๋ ํ ๋ฆฌ๊ฐ ์ด๋ฏธ ์กด์ฌํฉ๋๋ค: {BACKGROUNDS_DIR}")
|
39 |
+
try:
|
40 |
+
for file in os.listdir(BACKGROUNDS_DIR):
|
41 |
+
print(f"๋ฐ๊ฒฌ๋ ํ์ผ: {file}")
|
42 |
+
except Exception as e:
|
43 |
+
print(f"๋๋ ํ ๋ฆฌ ๋ด์ฉ์ ๋์ดํ๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
44 |
+
|
45 |
+
# JSON ํ์ผ ๋ก๋ ํจ์
|
46 |
+
def load_background_json(filename):
|
47 |
+
file_path = os.path.join(BACKGROUNDS_DIR, filename)
|
48 |
+
try:
|
49 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
50 |
+
data = json.load(f)
|
51 |
+
print(f"{filename} ํ์ผ์ ์ฑ๊ณต์ ์ผ๋ก ๋ก๋ํ์ต๋๋ค. {len(data)} ํญ๋ชฉ ํฌํจ.")
|
52 |
+
return data
|
53 |
+
except FileNotFoundError:
|
54 |
+
print(f"๊ฒฝ๊ณ : {filename} ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
55 |
+
return {}
|
56 |
+
except json.JSONDecodeError:
|
57 |
+
print(f"๊ฒฝ๊ณ : {filename} ํ์ผ์ JSON ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
58 |
+
return {}
|
59 |
+
except Exception as e:
|
60 |
+
print(f"๊ฒฝ๊ณ : {filename} ํ์ผ ๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}. ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํฉ๋๋ค.")
|
61 |
+
return {}
|
62 |
+
|
63 |
+
# ๋ฐฐ๊ฒฝ ๋ฐ์ดํฐ ๋ก๋
|
64 |
+
SIMPLE_BACKGROUNDS = load_background_json("simple_backgrounds.json")
|
65 |
+
STUDIO_BACKGROUNDS = load_background_json("studio_backgrounds.json")
|
66 |
+
NATURE_BACKGROUNDS = load_background_json("nature_backgrounds.json")
|
67 |
+
INDOOR_BACKGROUNDS = load_background_json("indoor_backgrounds.json")
|
68 |
+
ABSTRACT_BACKGROUNDS = load_background_json("abstract_backgrounds.json")
|
69 |
|
70 |
+
# ๋ฐฐ๊ฒฝ์ด ๋ก๋๋์ง ์์ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ ์ค์
|
71 |
+
if not SIMPLE_BACKGROUNDS:
|
72 |
+
SIMPLE_BACKGROUNDS = {"ํ์ดํธ ๋ฐฐ๊ฒฝ": "white background"}
|
73 |
+
if not STUDIO_BACKGROUNDS:
|
74 |
+
STUDIO_BACKGROUNDS = {"์ ํ ์ฌ์ง ์คํ๋์ค": "product photography studio"}
|
75 |
+
if not NATURE_BACKGROUNDS:
|
76 |
+
NATURE_BACKGROUNDS = {"์ด๋ ํด๋ณ": "tropical beach"}
|
77 |
+
if not INDOOR_BACKGROUNDS:
|
78 |
+
INDOOR_BACKGROUNDS = {"๋ชจ๋ ๋ฆฌ๋น๋ฃธ": "modern living room"}
|
79 |
+
if not ABSTRACT_BACKGROUNDS:
|
80 |
+
ABSTRACT_BACKGROUNDS = {"๋ค์จ ์กฐ๋ช
": "neon lights"}
|
81 |
|
82 |
def save_binary_file(file_name, data):
|
83 |
with open(file_name, "wb") as f:
|
|
|
85 |
|
86 |
def generate_system_instruction():
|
87 |
return """๋น์ ์ ์ํ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝํ๊ธฐ ์ํ ๊ณ ํ์ง ํ๋กฌํํธ๋ฅผ ์์ฑํ๋ ์ ๋ฌธ๊ฐ์
๋๋ค.
|
88 |
+
์ฌ์ฉ์๊ฐ ์ ๊ณตํ๋ ์ํ๋ช
, ๋ฐฐ๊ฒฝ ์ ํ, ์ถ๊ฐ ์์ฒญ์ฌํญ์ ๋ฐํ์ผ๋ก ๋ฏธ๋์ ๋(Midjourney)์ ์ฌ์ฉํ ์ ์๋
|
89 |
+
์์ธํ๊ณ ์ ๋ฌธ์ ์ธ ํ๋กฌํํธ๋ฅผ ์์ด๋ก ์์ฑํด์ฃผ์ธ์.
|
90 |
๋ค์ ๊ฐ์ด๋๋ผ์ธ์ ๋ฐ๋์ ๋ฐ๋ผ์ผ ํฉ๋๋ค:
|
91 |
1. ์ํ์ "#1"๋ก ์ง์ ํ์ฌ ์ฐธ์กฐํฉ๋๋ค. (์: "skincare tube (#1)")
|
92 |
2. *** ๋งค์ฐ ์ค์: ์ํ์ ์๋ ํน์ฑ(๋์์ธ, ์์, ํํ, ๋ก๊ณ , ํจํค์ง ๋ฑ)์ ์ด๋ค ์ํฉ์์๋ ์ ๋ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. ***
|
93 |
+
3. *** ์ํ์ ๋ณธ์ง์ ํน์ฑ์ ์ ์งํ๋, ์์ฐ์ค๋ฌ์ด ํ๊ฒฝ ํตํฉ์ ์ํ ์กฐ๋ช
๊ณผ ๊ทธ๋ฆผ์๋ ํ์ฉํฉ๋๋ค: ***
|
94 |
+
- ์ํ ์์ฒด์ ์์, ๋์์ธ, ํํ, ํ
์ค์ฒ๋ ์ ๋ ์์ ํ์ง ์์ต๋๋ค.
|
95 |
+
- ํ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ธ๋ฆฌ๋ ๊ทธ๋ฆผ์, ์ฃผ๋ณ ์กฐ๋ช
ํจ๊ณผ๋ ํ์ฉ๋ฉ๋๋ค.
|
96 |
+
- ์ํ์ ๋ฌผ๋ฐฉ์ธ, ์์ถ, ๊ธ, ์๊ณผ ๊ฐ์ ์ถ๊ฐ ์์๋ ๋ฌผ๋ฆฌ์ ํจ๊ณผ๋ ์ ์ฉํ์ง ์์ต๋๋ค.
|
97 |
+
- ํ๊ฒฝ์ ์ด์ธ๋ฆฌ๋ ์์ฐ์ค๋ฌ์ด ๋น ๋ฐ์ฌ, ์ฃผ๋ณ ์กฐ๋ช
, ๊ทธ๋ฆผ์๋ ์ฌ์ค์ ํตํฉ๊ฐ์ ์ํด ์ ์ฉํ ์ ์์ต๋๋ค.
|
98 |
+
4. ์ด๋ฏธ์ง ๋น์จ์ ์ ํํ 1:1(์ ์ฌ๊ฐํ) ํ์์ผ๋ก ์ง์ ํฉ๋๋ค. ํ๋กฌํํธ์ "square format", "1:1 ratio" ๋๋ "aspect ratio 1:1"์ ๋ช
์์ ์ผ๋ก ํฌํจํฉ๋๋ค.
|
99 |
5. ์ํ์ ๋ฐ๋์ ์ ์ฌ๊ฐํ ๊ตฌ๋์ ์ ์ค์์ ๋ฐฐ์น๋์ด์ผ ํฉ๋๋ค.
|
100 |
6. ์ํ์ ์ด๋ฏธ์ง์ ์ฃผ์ ์ด์ ์ผ๋ก ๋ถ๊ฐ์ํค๊ณ , ์ํ์ ๋น์จ์ด ์ ์ฒด ์ด๋ฏธ์ง์์ ํฌ๊ฒ ์ฐจ์งํ๋๋ก ํฉ๋๋ค.
|
101 |
7. ์ํ ์ด๋ฏธ์ง ์ปท์์(#1)์ ๊ธฐ๋ณธ ํํ์ ์์์ ์ ์งํ๋ฉด์, ์ ํํ ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ํตํฉ๋๋๋ก ํฉ๋๋ค.
|
102 |
+
8. ๊ณ ๊ธ์ค๋ฌ์ด ์์
์ ์ด๋ฏธ์ง๋ฅผ ์ํ ๋ค์ ํ๊ฒฝ ์์๋ค์ ํฌํจํ์ธ์:
|
103 |
+
- ์ํ๊ณผ ์ด์ธ๋ฆฌ๋ ์ฃผ๋ณ ํ๊ฒฝ/๋ฐฐ๊ฒฝ ์์๋ฅผ ์ถ๊ฐํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ฅํ ์ฃผ๋ณ์ ๊ฝ์ด๋ ํ๋ธ, ์๋ฃ ์ ํ ์์ ๊ณผ์ผ, ์ ์์ ํ ๊ทผ์ฒ์ ํ๋์ ์ํ ๋ฑ.
|
104 |
+
- ํ๊ฒฝ์ ์กฐ๋ช
ํจ๊ณผ(๋ฆผ ๋ผ์ดํธ, ๋ฐฑ๋ผ์ดํธ, ์ํํธ๋ฐ์ค ๋ฑ)๋ฅผ ์ค๋ช
ํฉ๋๋ค.
|
105 |
+
- ์ํ์ด ํ๊ฒฝ์ ์์ฐ์ค๋ฝ๊ฒ ์กด์ฌํ๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋๋ก ์ ์ ํ ๊ทธ๋ฆผ์์ ๋น ํํ์ ํฌํจํฉ๋๋ค.
|
106 |
+
- ์ํ์ ์ฉ๋๋ ์ฅ์ ์ ๊ฐ์ ์ ์ผ๋ก ์์ํ๋ ๋ฐฐ๊ฒฝ ์์๋ฅผ ํฌํจํฉ๋๋ค.
|
107 |
+
- ํ๋กํ์
๋ํ ์์
์ฌ์ง ํจ๊ณผ(์ ํ์ ํผ์ฌ๊ณ ์ฌ๋, ์ํํธ ํฌ์ปค์ค, ์คํ๋์ค ์กฐ๋ช
๋ฑ)๋ฅผ ๋ช
์ํฉ๋๋ค.
|
108 |
9. ํ๋กฌํํธ์ ๋ค์ ์์๋ค์ ๋ช
์์ ์ผ๋ก ํฌํจํ์ธ์:
|
109 |
- "highly detailed commercial photography"
|
110 |
- "award-winning product photography"
|
111 |
- "professional advertising imagery"
|
112 |
+
- "studio quality"
|
113 |
+
- "magazine advertisement quality"
|
114 |
+
10. ๋ฐฐ๊ฒฝ ํ๊ฒฝ ์์๋ฅผ ์ํ ์นดํ
๊ณ ๋ฆฌ์ ๋ง๊ฒ ์ ํํฉ๋๋ค:
|
115 |
+
- ์คํจ์ผ์ด ์ ํ: ๊นจ๋ํ ์์ค ์ ๋ฐ, ์ฐ์ํ ํ์ฅ๋, ์คํ ๊ฐ์ ํ๊ฒฝ ๋ฑ
|
116 |
+
- ์๋ฃ ์ ํ: ์ธ๋ จ๋ ํ
์ด๋ธ, ํํฐ ํ๊ฒฝ, ์ผ์ธ ํผํฌ๋ ์ฅ๋ฉด ๋ฑ
|
117 |
+
- ์ ์ ์ ํ: ์ธ๋ จ๋ ์์
๊ณต๊ฐ, ํ๋์ ์ธ ๊ฑฐ์ค, ๋ฏธ๋๋ฉํ ์ฑ
์ ๋ฑ
|
118 |
+
- ํจ์
/์๋ฅ: ์ธ๋ จ๋ ์ผ๋ฃธ, ๋์ ๊ฑฐ๋ฆฌ, ์๋ ๊ฐ์คํ ๋ผ์ดํ์คํ์ผ ํ๊ฒฝ ๋ฑ
|
119 |
+
- ์ํ ์ ํ: ๊น๋ํ ์ฃผ๋ฐฉ, ์ํ, ์๋ฆฌ ํ๊ฒฝ ๋ฑ
|
120 |
+
11. ์ฌ์ฉ์๊ฐ ์ ๊ณตํ ๊ตฌ์ฒด์ ์ธ ๋ฐฐ๊ฒฝ๊ณผ ์ถ๊ฐ ์์ฒญ์ฌํญ์ ์ ํํ ๋ฐ์ํฉ๋๋ค.
|
121 |
+
12. ํ๋กฌํํธ๋ ๋ฏธ๋์ ๋ AI์ ์ต์ ํ๋์ด์ผ ํฉ๋๋ค.
|
122 |
+
13. ํ๋กฌํํธ ๋์ "--ar 1:1 --s 750 --q 2" ํ๋ผ๋ฏธํฐ๋ฅผ ์ถ๊ฐํ์ฌ ๋ฏธ๋์ ๋์์ ๊ณ ํ์ง ์ ์ฌ๊ฐํ ๋น์จ์ ๊ฐ์ ํฉ๋๋ค.
|
123 |
+
์ถ๋ ฅ ํ์์ ์์ด๋ก ๋ ๋จ์ผ ๋จ๋ฝ์ ์์ธํ ํ๋กฌํํธ์ฌ์ผ ํ๋ฉฐ, ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ๊ฐ ํฌํจ๋์ด์ผ ํฉ๋๋ค.
|
124 |
"""
|
125 |
|
126 |
def generate_prompt_with_gemini(product_name, background_info, additional_info=""):
|
|
|
141 |
5. ํ๊ฒฝ๊ณผ์ ์์ฐ์ค๋ฌ์ด ํตํฉ์ ์ํ ์กฐ๋ช
ํจ๊ณผ์ ๊ทธ๋ฆผ์๋ ํฌํจํด์ฃผ์ธ์.
|
142 |
6. ์ํ์ ๋ ๋๋ณด์ด๊ฒ ํ๋ ๋ฐฐ๊ฒฝ ํ๊ฒฝ์ ์ค๋ช
ํด์ฃผ์ธ์.
|
143 |
7. ๊ณ ๊ธ์ค๋ฌ์ด ์์
๊ด๊ณ ํ์ง์ ์ด๋ฏธ์ง๊ฐ ๋๋๋ก ํ๊ฒฝ ์ค๋ช
์ ํด์ฃผ์ธ์.
|
144 |
+
8. ํ๋กฌํํธ ๋์ ๋ฏธ๋์ ๋ ํ๋ผ๋ฏธํฐ "--ar 1:1 --s 750 --q 2"๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์.
|
145 |
ํ๊ตญ์ด ์
๋ ฅ ๋ด์ฉ์ ์์ด๋ก ์ ์ ํ ๋ฒ์ญํ์ฌ ๋ฐ์ํด์ฃผ์ธ์.
|
146 |
"""
|
147 |
+
model = genai.GenerativeModel(
|
148 |
+
'gemini-2.0-flash',
|
149 |
+
system_instruction=generate_system_instruction()
|
150 |
+
)
|
151 |
+
response = model.generate_content(
|
152 |
+
prompt_request,
|
153 |
+
generation_config=genai.types.GenerationConfig(
|
154 |
+
temperature=0.7,
|
155 |
+
top_p=0.95,
|
156 |
+
top_k=64,
|
157 |
+
max_output_tokens=1024,
|
158 |
+
)
|
159 |
+
)
|
160 |
+
response_text = response.text.strip()
|
161 |
+
if "--ar 1:1" not in response_text:
|
162 |
+
response_text = response_text.rstrip(".") + ". --ar 1:1 --s 750 --q 2"
|
163 |
+
return response_text
|
164 |
+
except Exception as e:
|
165 |
+
return f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {str(e)}"
|
166 |
+
|
167 |
+
def translate_prompt_to_english(prompt):
|
168 |
+
if not re.search("[๊ฐ-ํฃ]", prompt):
|
169 |
+
return prompt
|
170 |
+
|
171 |
+
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
172 |
+
|
173 |
+
try:
|
174 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
175 |
+
if not api_key:
|
176 |
+
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
177 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
178 |
+
return prompt
|
179 |
+
|
180 |
+
client = genai.Client(api_key=api_key)
|
181 |
+
translation_prompt = f"""
|
182 |
+
Translate the following Korean text to English:
|
183 |
|
184 |
+
{prompt}
|
185 |
|
186 |
+
IMPORTANT: The token IMAGE_TAG_ONE is a special tag
|
187 |
+
and must be preserved exactly as is in your translation. Do not translate this token.
|
188 |
+
"""
|
189 |
|
190 |
+
logger.info(f"Translation prompt: {translation_prompt}")
|
191 |
+
response = client.models.generate_content(
|
192 |
+
model="gemini-2.0-flash",
|
193 |
+
contents=[translation_prompt],
|
194 |
+
config=types.GenerateContentConfig(
|
195 |
+
response_modalities=['Text'],
|
196 |
+
temperature=0.2,
|
197 |
+
top_p=0.95,
|
198 |
+
top_k=40,
|
199 |
+
max_output_tokens=512
|
200 |
+
)
|
201 |
+
)
|
202 |
+
|
203 |
+
translated_text = ""
|
204 |
+
for part in response.candidates[0].content.parts:
|
205 |
+
if hasattr(part, 'text') and part.text:
|
206 |
+
translated_text += part.text
|
207 |
+
|
208 |
+
if translated_text.strip():
|
209 |
+
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
210 |
+
logger.info(f"Translated text: {translated_text.strip()}")
|
211 |
+
return translated_text.strip()
|
212 |
+
else:
|
213 |
+
logger.warning("๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ์๋ณธ ํ๋กฌํํธ ์ฌ์ฉ")
|
214 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
215 |
+
return prompt
|
216 |
except Exception as e:
|
217 |
+
logger.exception("๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์:")
|
218 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
219 |
+
return prompt
|
220 |
|
221 |
+
def preprocess_prompt(prompt, image1):
|
222 |
+
has_img1 = image1 is not None
|
223 |
+
|
224 |
+
if "#1" in prompt and not has_img1:
|
225 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
226 |
else:
|
227 |
+
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
228 |
+
|
229 |
+
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
230 |
+
return prompt
|
|
|
231 |
|
232 |
def generate_with_images(prompt, images, variation_index=0):
|
233 |
try:
|
|
|
235 |
if not api_key:
|
236 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
237 |
|
238 |
+
client = genai.Client(api_key=api_key)
|
239 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
240 |
|
241 |
variation_suffixes = [
|
|
|
250 |
else:
|
251 |
prompt = prompt + " Do not add any text, watermarks, or labels to the image."
|
252 |
|
253 |
+
contents = [prompt]
|
254 |
+
for idx, img in enumerate(images, 1):
|
|
|
|
|
255 |
if img is not None:
|
256 |
+
contents.append(img)
|
257 |
+
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
|
259 |
+
response = client.models.generate_content(
|
260 |
+
model="gemini-2.0-flash-exp-image-generation",
|
261 |
+
contents=contents,
|
262 |
+
config=types.GenerateContentConfig(
|
263 |
+
response_modalities=['Text', 'Image'],
|
264 |
+
temperature=1,
|
265 |
+
top_p=0.95,
|
266 |
+
top_k=40,
|
267 |
+
max_output_tokens=8192
|
268 |
+
)
|
269 |
+
)
|
270 |
|
271 |
# ์์ ํ์ผ์ ํญ์ JPG ํ์ฅ์๋ก ์์ฑ
|
272 |
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
|
273 |
temp_path = tmp.name
|
274 |
result_text = ""
|
275 |
image_found = False
|
276 |
+
for part in response.candidates[0].content.parts:
|
|
|
277 |
if hasattr(part, 'text') and part.text:
|
278 |
result_text += part.text
|
279 |
logger.info(f"์๋ต ํ
์คํธ: {part.text}")
|
280 |
elif hasattr(part, 'inline_data') and part.inline_data:
|
281 |
+
save_binary_file(temp_path, part.inline_data.data)
|
|
|
282 |
image_found = True
|
283 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
|
|
284 |
if not image_found:
|
285 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
286 |
|
|
|
309 |
return None, "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
310 |
|
311 |
if prompt and prompt.strip():
|
312 |
+
# ์ด๋ฏธ ์์ด๋ก ๋ ํ๋กฌํํธ๋ผ๋ฉด ๊ทธ๋๋ก ์ฌ์ฉ
|
313 |
final_prompt = prompt
|
314 |
else:
|
315 |
final_prompt = "Please creatively transform this image into a more vivid and artistic version. Do not include any text or watermarks in the generated image."
|
|
|
366 |
|
367 |
return results[0], results[1], results[2], results[3], combined_status, combined_prompts
|
368 |
|
369 |
+
# ์ ํ๋ ๋ฐฐ๊ฒฝ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ
|
370 |
+
def get_selected_background_info(bg_type, simple, studio, nature, indoor, abstract):
|
371 |
+
if bg_type == "์ฌํ ๋ฐฐ๊ฒฝ":
|
372 |
+
return {
|
373 |
+
"category": "์ฌํ ๋ฐฐ๊ฒฝ",
|
374 |
+
"name": simple,
|
375 |
+
"english": SIMPLE_BACKGROUNDS.get(simple, "white background")
|
376 |
+
}
|
377 |
+
elif bg_type == "์คํ๋์ค ๋ฐฐ๊ฒฝ":
|
378 |
+
return {
|
379 |
+
"category": "์คํ๋์ค ๋ฐฐ๊ฒฝ",
|
380 |
+
"name": studio,
|
381 |
+
"english": STUDIO_BACKGROUNDS.get(studio, "product photography studio")
|
382 |
+
}
|
383 |
+
elif bg_type == "์์ฐ ํ๊ฒฝ":
|
384 |
+
return {
|
385 |
+
"category": "์์ฐ ํ๊ฒฝ",
|
386 |
+
"name": nature,
|
387 |
+
"english": NATURE_BACKGROUNDS.get(nature, "natural environment")
|
388 |
+
}
|
389 |
+
elif bg_type == "์ค๋ด ํ๊ฒฝ":
|
390 |
+
return {
|
391 |
+
"category": "์ค๋ด ํ๊ฒฝ",
|
392 |
+
"name": indoor,
|
393 |
+
"english": INDOOR_BACKGROUNDS.get(indoor, "indoor environment")
|
394 |
+
}
|
395 |
+
elif bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ":
|
396 |
+
return {
|
397 |
+
"category": "์ถ์/ํน์ ๋ฐฐ๊ฒฝ",
|
398 |
+
"name": abstract,
|
399 |
+
"english": ABSTRACT_BACKGROUNDS.get(abstract, "abstract background")
|
400 |
+
}
|
401 |
+
else:
|
402 |
+
return {
|
403 |
+
"category": "๊ธฐ๋ณธ ๋ฐฐ๊ฒฝ",
|
404 |
+
"name": "ํ์ดํธ ๋ฐฐ๊ฒฝ",
|
405 |
+
"english": "white background"
|
406 |
+
}
|
407 |
+
|
408 |
# UI ๊ตฌ์ฑ
|
409 |
+
with gr.Blocks() as demo:
|
410 |
+
with gr.Column():
|
411 |
+
with gr.Group():
|
412 |
+
with gr.Column():
|
413 |
+
with gr.Row():
|
414 |
+
image1_input = gr.Image(type="pil", label="์ด๋ฏธ์ง ์
๋ก๋", image_mode="RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
415 |
|
416 |
+
# ํ๋กฌํํธ ์
๋ ฅ ์์ญ
|
417 |
+
with gr.Row():
|
418 |
+
with gr.Column():
|
419 |
+
# ์ํ๋ช
์
๋ ฅ
|
420 |
+
product_name = gr.Textbox(label="์ํ๋ช
", placeholder="์: ์คํจ์ผ์ด ํ๋ธ, ํ
๋ธ๋ฌ ๋ฑ", interactive=True)
|
421 |
+
|
422 |
+
# ๋ฐฐ๊ฒฝ ์ ํ ์ ํ ๋ผ๋์ค ๋ฒํผ
|
423 |
+
background_type = gr.Radio(
|
424 |
+
choices=["์ฌํ ๋ฐฐ๊ฒฝ", "์คํ๋์ค ๋ฐฐ๊ฒฝ", "์์ฐ ํ๊ฒฝ", "์ค๋ด ํ๊ฒฝ", "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"],
|
425 |
+
label="๋ฐฐ๊ฒฝ ์ ํ",
|
426 |
+
value="์ฌํ ๋ฐฐ๊ฒฝ"
|
427 |
+
)
|
428 |
+
|
429 |
+
# ๊ฐ ๋ฐฐ๊ฒฝ ์ ํ๋ณ ๋๋กญ๋ค์ด
|
430 |
+
with gr.Row():
|
431 |
+
# ์ฌํ ๋ฐฐ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
432 |
+
simple_dropdown = gr.Dropdown(
|
433 |
+
choices=list(SIMPLE_BACKGROUNDS.keys()),
|
434 |
+
value=list(SIMPLE_BACKGROUNDS.keys())[0] if SIMPLE_BACKGROUNDS else None,
|
435 |
+
label="์ฌํ ๋ฐฐ๊ฒฝ ์ ํ",
|
436 |
+
visible=True,
|
437 |
+
interactive=True
|
438 |
+
)
|
439 |
+
|
440 |
+
# ์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
441 |
+
studio_dropdown = gr.Dropdown(
|
442 |
+
choices=list(STUDIO_BACKGROUNDS.keys()),
|
443 |
+
value=list(STUDIO_BACKGROUNDS.keys())[0] if STUDIO_BACKGROUNDS else None,
|
444 |
+
label="์คํ๋์ค ๋ฐฐ๊ฒฝ ์ ํ",
|
445 |
+
visible=False,
|
446 |
+
interactive=True
|
447 |
+
)
|
448 |
+
|
449 |
+
# ์์ฐ ํ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
450 |
+
nature_dropdown = gr.Dropdown(
|
451 |
+
choices=list(NATURE_BACKGROUNDS.keys()),
|
452 |
+
value=list(NATURE_BACKGROUNDS.keys())[0] if NATURE_BACKGROUNDS else None,
|
453 |
+
label="์์ฐ ํ๊ฒฝ ์ ํ",
|
454 |
+
visible=False,
|
455 |
+
interactive=True
|
456 |
+
)
|
457 |
+
|
458 |
+
# ์ค๋ด ํ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
459 |
+
indoor_dropdown = gr.Dropdown(
|
460 |
+
choices=list(INDOOR_BACKGROUNDS.keys()),
|
461 |
+
value=list(INDOOR_BACKGROUNDS.keys())[0] if INDOOR_BACKGROUNDS else None,
|
462 |
+
label="์ค๋ด ํ๊ฒฝ ์ ํ",
|
463 |
+
visible=False,
|
464 |
+
interactive=True
|
465 |
+
)
|
466 |
+
|
467 |
+
# ์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ ๋๋กญ๋ค์ด
|
468 |
+
abstract_dropdown = gr.Dropdown(
|
469 |
+
choices=list(ABSTRACT_BACKGROUNDS.keys()),
|
470 |
+
value=list(ABSTRACT_BACKGROUNDS.keys())[0] if ABSTRACT_BACKGROUNDS else None,
|
471 |
+
label="์ถ์/ํน์ ๋ฐฐ๊ฒฝ ์ ํ",
|
472 |
+
visible=False,
|
473 |
+
interactive=True
|
474 |
+
)
|
475 |
+
|
476 |
+
# ์ถ๊ฐ ์์ฒญ์ฌํญ
|
477 |
+
additional_info = gr.Textbox(
|
478 |
+
label="์ถ๊ฐ ์์ฒญ์ฌํญ (์ ํ์ฌํญ)",
|
479 |
+
placeholder="์: ๊ณ ๊ธ์ค๋ฌ์ด ๋๋, ๋ฐ์ ์กฐ๋ช
, ์์ฐ์ค๋ฌ์ด ๋ณด์กฐ์ ์ธ ๊ฐ์ฒด๋ฅผ ์ถ๊ฐํด์ฃผ์ธ์ ๋ฑ",
|
480 |
+
lines=2,
|
481 |
+
interactive=True
|
482 |
+
)
|
483 |
+
|
484 |
+
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ
|
485 |
+
generate_prompt_btn = gr.Button("ํ๋กฌํํธ ์์ฑ")
|
486 |
+
|
487 |
+
with gr.Column():
|
488 |
+
# ์์ฑ๋ ํ๋กฌํํธ ํ์
|
489 |
+
prompt_output = gr.Textbox(label="์์ฑ๋ ํ๋กฌํํธ", lines=5)
|
490 |
|
491 |
+
# ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ
|
492 |
+
with gr.Row():
|
493 |
+
submit_single_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (1์ฅ)')
|
494 |
+
submit_btn = gr.Button('์ด๋ฏธ์ง ์์ฑ (4์ฅ)')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
495 |
|
496 |
+
with gr.Column():
|
497 |
+
with gr.Row():
|
498 |
+
with gr.Column():
|
499 |
+
output_image1 = gr.Image(label="์ด๋ฏธ์ง #1", type="filepath")
|
500 |
+
output_image3 = gr.Image(label="์ด๋ฏธ์ง #3", type="filepath")
|
501 |
+
with gr.Column():
|
502 |
+
output_image2 = gr.Image(label="์ด๋ฏธ์ง #2", type="filepath")
|
503 |
+
output_image4 = gr.Image(label="์ด๋ฏธ์ง #4", type="filepath")
|
504 |
+
|
505 |
+
output_text = gr.Textbox(label="๊ฒฐ๊ณผ ์ ๋ณด", lines=2)
|
506 |
+
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", lines=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
507 |
|
508 |
# ๋ฐฐ๊ฒฝ ์ ํ์ ๋ฐ๋ผ ๋๋กญ๋ค์ด ํ์ ์
๋ฐ์ดํธ
|
509 |
def update_dropdowns(bg_type):
|
|
|
515 |
abstract_dropdown: gr.update(visible=(bg_type == "์ถ์/ํน์ ๋ฐฐ๊ฒฝ"))
|
516 |
}
|
517 |
|
518 |
+
# ๋ฐฐ๊ฒฝ ์ ํ ๋ณ๊ฒฝ ์ด๋ฒคํธ ์ฒ๋ฆฌ
|
519 |
+
background_type.change(
|
520 |
+
fn=update_dropdowns,
|
521 |
+
inputs=[background_type],
|
522 |
+
outputs=[simple_dropdown, studio_dropdown, nature_dropdown, indoor_dropdown, abstract_dropdown]
|
523 |
+
)
|
524 |
+
|
525 |
# ํ๋กฌํํธ ์์ฑ ํจ์
|
526 |
+
def generate_output(image, bg_type, simple, studio, nature, indoor, abstract, product_text, additional_text):
|
527 |
if image is None:
|
528 |
return "์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
529 |
|
|
|
536 |
prompt = generate_prompt_with_gemini(product_text, background_info, additional_text)
|
537 |
|
538 |
if not GEMINI_API_KEY:
|
539 |
+
return "Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํค๋ฅผ ๋ฐ๊ธ๋ฐ์ ์ค์ ํด์ฃผ์ธ์."
|
540 |
|
541 |
return prompt
|
542 |
except Exception as e:
|
543 |
+
error_msg = f"ํ๋กฌํํธ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
544 |
+
return error_msg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
545 |
|
546 |
+
# ํ๋กฌํํธ ์์ฑ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ
|
547 |
generate_prompt_btn.click(
|
548 |
+
fn=generate_output,
|
549 |
inputs=[
|
550 |
+
image1_input,
|
551 |
+
background_type,
|
552 |
simple_dropdown,
|
553 |
studio_dropdown,
|
554 |
nature_dropdown,
|
|
|
560 |
outputs=prompt_output
|
561 |
)
|
562 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
563 |
# ๋จ์ผ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
564 |
submit_single_btn.click(
|
565 |
fn=lambda image1, prompt: process_images_with_prompt(image1, prompt, 0),
|
566 |
+
inputs=[image1_input, prompt_output],
|
567 |
outputs=[output_image1, output_text, prompt_display],
|
568 |
)
|
569 |
|
570 |
# 4์ฅ ์ด๋ฏธ์ง ์์ฑ ๋ฒํผ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
571 |
submit_btn.click(
|
572 |
fn=generate_multiple_images,
|
573 |
+
inputs=[image1_input, prompt_output],
|
574 |
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
575 |
)
|
576 |
|
577 |
+
demo.queue()
|
578 |
+
demo.launch()
|
|