Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,8 @@ from PIL import Image
|
|
4 |
import gradio as gr
|
5 |
import logging
|
6 |
import re
|
7 |
-
import io
|
8 |
from io import BytesIO
|
|
|
9 |
|
10 |
from google import genai
|
11 |
from google.genai import types
|
@@ -22,111 +22,161 @@ def save_binary_file(file_name, data):
|
|
22 |
with open(file_name, "wb") as f:
|
23 |
f.write(data)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
def preprocess_prompt(prompt, image1, image2, image3):
|
26 |
"""
|
27 |
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
|
28 |
"""
|
29 |
-
# ๊ธฐ์กด preprocess_prompt ํจ์ ์ฝ๋ ์ ์ง
|
30 |
-
# ์ด๋ฏธ์ง ์๋ ์ฐธ์กฐ ํ์ธ ๋ฐ ์ฒ๋ฆฌ
|
31 |
has_img1 = image1 is not None
|
32 |
has_img2 = image2 is not None
|
33 |
has_img3 = image3 is not None
|
34 |
-
|
35 |
-
# #1, #2, #3 ์ฐธ์กฐ๋ฅผ ์ค๋ช
์ผ๋ก ๋ณํ (์ด๋ฏธ์ง๊ฐ ์๋ ๊ฒฝ์ฐ ๋ฌด์)
|
36 |
if "#1" in prompt and not has_img1:
|
37 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
38 |
else:
|
39 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
40 |
-
|
41 |
if "#2" in prompt and not has_img2:
|
42 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
43 |
else:
|
44 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
45 |
-
|
46 |
if "#3" in prompt and not has_img3:
|
47 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
48 |
else:
|
49 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
50 |
-
|
51 |
-
# ๊ธฐ๋ฅ ๋ช
๋ น ํด์
|
52 |
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
|
53 |
-
# ์ค๋ช
์ถ์ถ์ ์๋ํ์ง๋ง ์คํจํด๋ ๊ธฐ๋ณธ ํ๋กฌํํธ ์ ๊ณต
|
54 |
desc_match = re.search(r'#1์ "(.*?)"์ผ๋ก ๋ฐ๊ฟ๋ผ', prompt)
|
55 |
if desc_match:
|
56 |
description = desc_match.group(1)
|
57 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ {description}์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์๋ณธ ์ด๋ฏธ์ง์ ์ฃผ์ ๋ด์ฉ์ ์ ์งํ๋ ์๋ก์ด ๏ฟฝ๏ฟฝํ์ผ๊ณผ ๋ถ์๊ธฐ๋ก ์ฌํด์ํด์ฃผ์ธ์."
|
58 |
else:
|
59 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
60 |
-
|
61 |
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
|
62 |
-
# ์ง์ธ ํ
์คํธ ์ถ์ถ์ ์๋ํ์ง๋ง ์คํจํด๋ ๊ธฐ๋ณธ ํ๋กฌํํธ ์ ๊ณต
|
63 |
text_match = re.search(r'#1์์ "(.*?)"๋ฅผ ์ง์๋ผ', prompt)
|
64 |
if text_match:
|
65 |
text_to_remove = text_match.group(1)
|
66 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ '{text_to_remove}' ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ํ
์คํธ๊ฐ ์๋ ๋ถ๋ถ์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ์ฑ์์ฃผ์ธ์."
|
67 |
else:
|
68 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
69 |
-
|
70 |
-
elif "3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ" in prompt:
|
71 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์ผ๊ตด์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ผ๊ตด๋ก ์์ฐ์ค๋ฝ๊ฒ ๊ต์ฒดํด์ฃผ์ธ์. ์ผ๊ตด์ ํ์ ๊ณผ ํน์ง์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ๋๋จธ์ง ๋ถ๋ถ์ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
|
72 |
-
|
73 |
elif "4. ์ท๋ฐ๊พธ๊ธฐ" in prompt:
|
74 |
-
|
75 |
-
|
76 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์์์ ๋ ๋ฒ์งธ ๋๋ ์ธ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๊ต์ฒดํด์ฃผ์ธ์. ์์์ ์คํ์ผ๊ณผ ์์์ ์ฐธ์กฐ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
|
77 |
-
else:
|
78 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์์ผ๋ก ์์ฐ์ค๋ฝ๊ฒ ๊ต์ฒดํด์ฃผ์ธ์. ์์์ ์คํ์ผ๊ณผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
|
79 |
-
|
80 |
elif "5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ" in prompt:
|
81 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ผ๋ก
|
82 |
-
|
83 |
elif "6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)" in prompt:
|
84 |
-
|
85 |
-
|
86 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ ๋ฒ์งธ, ์ธ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ์ ๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํด์ฃผ์ธ์."
|
87 |
-
else:
|
88 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ์ ๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํด์ฃผ์ธ์."
|
89 |
-
|
90 |
-
elif "7. ์ด๋ฏธ์ง ํฉ์ฑ(์คํ์ผ์ ์ฉ)" in prompt:
|
91 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ด์ฉ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์คํ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ฃผ์ ํผ์ฌ์ฒด์ ๊ตฌ๋๋ ์ ์งํ๋, ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์ ์ ์คํ์ผ, ์์, ์ง๊ฐ์ ์ ์ฉํด์ฃผ์ธ์."
|
92 |
-
|
93 |
-
# ๊ฐ๋จํ ์์ ๋ณ๊ฒฝ ์์ฒญ ์ฒ๋ฆฌ
|
94 |
-
elif "์ ๋ถ์์์ผ๋ก ๋ฐ๊ฟ๋ผ" in prompt or "๋ฅผ ๋ถ์์์ผ๋ก ๋ฐ๊ฟ๋ผ" in prompt:
|
95 |
-
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ถ์์ ํค์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ ์ฒด์ ์ธ ์์์ ๋ถ์ ๊ณ์ด๋ก ์กฐ์ ํ๊ณ ์์ฐ์ค๋ฌ์ด ๋๋์ ์ ์งํด์ฃผ์ธ์."
|
96 |
-
|
97 |
-
# ๋ช
ํํ ์ด๋ฏธ์ง ์์ฑ ์์ฒญ ์ถ๊ฐ
|
98 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
99 |
-
|
100 |
return prompt
|
101 |
|
102 |
-
def generate_with_images(prompt, images):
|
103 |
"""
|
104 |
-
|
|
|
105 |
"""
|
106 |
try:
|
107 |
-
# API ํค ํ์ธ
|
108 |
api_key = os.environ.get("GEMINI_API_KEY")
|
109 |
if not api_key:
|
110 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
111 |
-
|
112 |
-
# Gemini ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
|
113 |
client = genai.Client(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
contents = []
|
119 |
-
|
120 |
-
# ํ
์คํธ ํ๋กฌํํธ ์ถ๊ฐ
|
121 |
-
contents.append(prompt)
|
122 |
-
|
123 |
-
# ์ด๋ฏธ์ง ์ถ๊ฐ
|
124 |
for idx, img in enumerate(images, 1):
|
125 |
if img is not None:
|
126 |
contents.append(img)
|
127 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
128 |
-
|
129 |
-
# ์์ฑ ์ค์ - ๊ณต์ ๋ฌธ์์ ๋ฐ๋ผ responseModalities ์ค์
|
130 |
response = client.models.generate_content(
|
131 |
model="gemini-2.0-flash-exp-image-generation",
|
132 |
contents=contents,
|
@@ -138,15 +188,11 @@ def generate_with_images(prompt, images):
|
|
138 |
max_output_tokens=8192
|
139 |
)
|
140 |
)
|
141 |
-
|
142 |
-
# ์์ ํ์ผ ์์ฑ
|
143 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
144 |
temp_path = tmp.name
|
145 |
-
|
146 |
result_text = ""
|
147 |
image_found = False
|
148 |
-
|
149 |
-
# ์๋ต ์ฒ๋ฆฌ
|
150 |
for part in response.candidates[0].content.parts:
|
151 |
if hasattr(part, 'text') and part.text:
|
152 |
result_text += part.text
|
@@ -155,183 +201,182 @@ def generate_with_images(prompt, images):
|
|
155 |
save_binary_file(temp_path, part.inline_data.data)
|
156 |
image_found = True
|
157 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
158 |
-
|
159 |
if not image_found:
|
160 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
161 |
-
|
162 |
-
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
163 |
result_img = Image.open(temp_path)
|
164 |
if result_img.mode == "RGBA":
|
165 |
result_img = result_img.convert("RGB")
|
166 |
-
|
167 |
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
168 |
-
|
169 |
except Exception as e:
|
170 |
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
171 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
172 |
|
173 |
-
def process_images_with_prompt(image1, image2, image3, prompt):
|
174 |
"""
|
175 |
-
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ
|
|
|
176 |
"""
|
177 |
try:
|
178 |
-
# ์ด๋ฏธ์ง ๊ฐ์ ํ์ธ
|
179 |
images = [image1, image2, image3]
|
180 |
valid_images = [img for img in images if img is not None]
|
181 |
-
|
182 |
if not valid_images:
|
183 |
-
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์."
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
|
|
|
|
|
|
|
|
188 |
if len(valid_images) == 1:
|
189 |
-
|
190 |
-
logger.info("
|
191 |
elif len(valid_images) == 2:
|
192 |
-
|
193 |
-
logger.info("
|
194 |
else:
|
195 |
-
|
196 |
-
logger.info("
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
# ์๋ก์ด API ํธ์ถ ๋ฐฉ์ ์ฌ์ฉ
|
202 |
-
return generate_with_images(prompt, valid_images)
|
203 |
-
|
204 |
except Exception as e:
|
205 |
logger.exception("์ด๋ฏธ์ง ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
206 |
-
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
207 |
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
|
222 |
-
# Gradio ์ธํฐํ์ด์ค
|
223 |
with gr.Blocks() as demo:
|
224 |
-
|
225 |
"""
|
226 |
<div style="text-align: center; margin-bottom: 1rem;">
|
227 |
-
<h1
|
228 |
-
<p>์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ
|
|
|
229 |
</div>
|
230 |
"""
|
231 |
)
|
232 |
|
233 |
with gr.Row():
|
234 |
with gr.Column():
|
235 |
-
# 3๊ฐ์ ์ด๋ฏธ์ง ์
๋ ฅ
|
236 |
-
with gr.Row():
|
237 |
-
image1_input = gr.Image(type="pil", label="#1", image_mode="RGB")
|
238 |
-
image2_input = gr.Image(type="pil", label="#2", image_mode="RGB")
|
239 |
-
image3_input = gr.Image(type="pil", label="#3", image_mode="RGB")
|
240 |
-
|
241 |
-
# ๊ธฐ๋ฅ ์ ํ ๋๋กญ๋ค์ด๊ณผ ์ปค์คํ
ํ
์คํธ ์
๋ ฅ
|
242 |
with gr.Row():
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
"2. ๊ธ์์ง์ฐ๊ธฐ",
|
247 |
-
"3. ์ผ๊ตด๋ฐ๊พธ๊ธฐ",
|
248 |
-
"4. ์ท๋ฐ๊พธ๊ธฐ",
|
249 |
-
"5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ",
|
250 |
-
"6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)",
|
251 |
-
"7. ์ด๋ฏธ์ง ํฉ์ฑ(์คํ์ผ์ ์ฉ)"
|
252 |
-
],
|
253 |
-
label="๊ธฐ๋ฅ ์ ํ",
|
254 |
-
value=None
|
255 |
-
)
|
256 |
-
custom_text_input = gr.Textbox(
|
257 |
-
label="์ปค์คํ
ํ
์คํธ (1, 2๋ฒ ๊ธฐ๋ฅ์ฉ)",
|
258 |
-
placeholder="์: ๋ถ์์, ์์ฑํ ์คํ์ผ, ์ค๊ตญ์ด..."
|
259 |
-
)
|
260 |
-
|
261 |
-
apply_function_btn = gr.Button("๊ธฐ๋ฅ ์ ์ฉ")
|
262 |
-
|
263 |
-
# ํ๋กฌํํธ ์
๋ ฅ (์ ํ ์ฌํญ)
|
264 |
prompt_input = gr.Textbox(
|
265 |
lines=3,
|
266 |
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค.",
|
267 |
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
|
268 |
)
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
|
|
|
|
|
|
|
|
|
|
273 |
with gr.Column():
|
274 |
-
#
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
# ์ฌ์ฉ๋ ํ๋กฌํํธ ํ์
|
279 |
-
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ", visible=True)
|
280 |
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
)
|
287 |
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
|
|
|
|
|
|
314 |
|
315 |
submit_btn.click(
|
316 |
-
fn=
|
317 |
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
318 |
-
outputs=[
|
319 |
)
|
320 |
-
|
321 |
gr.Markdown(
|
322 |
"""
|
323 |
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
324 |
|
325 |
-
1. **์๋ ํฉ์ฑ**:
|
326 |
-
2.
|
327 |
-
3.
|
328 |
-
4.
|
329 |
-
5. **์ผ๋ถ ์ด๋ฏธ์ง๋ง**: ํ์ํ ์ด๋ฏธ์ง๋ง ์
๋ก๋ํด๋ ๊ธฐ๋ฅ ์คํ์ด ๊ฐ๋ฅํฉ๋๋ค
|
330 |
|
331 |
-
> **ํ**:
|
332 |
"""
|
333 |
)
|
334 |
|
335 |
-
# ์ ํ๋ฆฌ์ผ์ด์
์คํ
|
336 |
if __name__ == "__main__":
|
337 |
demo.launch(share=True)
|
|
|
4 |
import gradio as gr
|
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
|
|
|
22 |
with open(file_name, "wb") as f:
|
23 |
f.write(data)
|
24 |
|
25 |
+
def translate_prompt_to_english(prompt):
|
26 |
+
"""
|
27 |
+
์
๋ ฅ๋ ํ๋กฌํํธ์ ํ๊ธ์ด ํฌํจ๋์ด ์์ผ๋ฉด Geminiโ2.0โflash ๋ชจ๋ธ์ ์ฌ์ฉํ์ฌ ์์ด๋ก ๋ฒ์ญํฉ๋๋ค.
|
28 |
+
ํ๊ธ์ด ์์ผ๋ฉด ์๋ณธ ํ๋กฌํํธ๋ฅผ ๊ทธ๋๋ก ๋ฐํํฉ๋๋ค.
|
29 |
+
์ค์: #1, #2, #3 ํ๊ทธ๋ ๋ฒ์ญ ์ ํ์ ๋ฐ๋์ ๋ณด์กด๋์ด์ผ ํฉ๋๋ค.
|
30 |
+
"""
|
31 |
+
if not re.search("[๊ฐ-ํฃ]", prompt):
|
32 |
+
return prompt
|
33 |
+
|
34 |
+
# #1, #2, #3 ํ๊ทธ๋ฅผ ์์ ํ ํฐ์ผ๋ก ๋์ฒดํ์ฌ ๋ณด์กด
|
35 |
+
prompt = prompt.replace("#1", "IMAGE_TAG_ONE")
|
36 |
+
prompt = prompt.replace("#2", "IMAGE_TAG_TWO")
|
37 |
+
prompt = prompt.replace("#3", "IMAGE_TAG_THREE")
|
38 |
+
|
39 |
+
try:
|
40 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
41 |
+
if not api_key:
|
42 |
+
logger.error("Gemini API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค.")
|
43 |
+
# ์์ ํ ํฐ์ ์๋ ํ๊ทธ๋ก ๋ณต์
|
44 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
45 |
+
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
|
46 |
+
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
|
47 |
+
return prompt
|
48 |
+
|
49 |
+
client = genai.Client(api_key=api_key)
|
50 |
+
translation_prompt = f"""
|
51 |
+
Translate the following Korean text to English:
|
52 |
+
|
53 |
+
{prompt}
|
54 |
+
|
55 |
+
IMPORTANT: The tokens IMAGE_TAG_ONE, IMAGE_TAG_TWO, and IMAGE_TAG_THREE are special tags
|
56 |
+
and must be preserved exactly as is in your translation. Do not translate these tokens.
|
57 |
+
"""
|
58 |
+
|
59 |
+
logger.info(f"Translation prompt: {translation_prompt}")
|
60 |
+
response = client.models.generate_content(
|
61 |
+
model="gemini-2.0-flash",
|
62 |
+
contents=[translation_prompt],
|
63 |
+
config=types.GenerateContentConfig(
|
64 |
+
response_modalities=['Text'],
|
65 |
+
temperature=0.2,
|
66 |
+
top_p=0.95,
|
67 |
+
top_k=40,
|
68 |
+
max_output_tokens=512
|
69 |
+
)
|
70 |
+
)
|
71 |
+
|
72 |
+
translated_text = ""
|
73 |
+
for part in response.candidates[0].content.parts:
|
74 |
+
if hasattr(part, 'text') and part.text:
|
75 |
+
translated_text += part.text
|
76 |
+
|
77 |
+
if translated_text.strip():
|
78 |
+
# ๋ฒ์ญ๋ ํ
์คํธ์์ ์์ ํ ํฐ์ ์๋ ํ๊ทธ๋ก ๋ณต์
|
79 |
+
translated_text = translated_text.replace("IMAGE_TAG_ONE", "#1")
|
80 |
+
translated_text = translated_text.replace("IMAGE_TAG_TWO", "#2")
|
81 |
+
translated_text = translated_text.replace("IMAGE_TAG_THREE", "#3")
|
82 |
+
logger.info(f"Translated text: {translated_text.strip()}")
|
83 |
+
return translated_text.strip()
|
84 |
+
else:
|
85 |
+
logger.warning("๋ฒ์ญ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค. ์๋ณธ ํ๋กฌํํธ ์ฌ์ฉ")
|
86 |
+
# ์์ ํ ํฐ์ ์๋ ํ๊ทธ๋ก ๋ณต์
|
87 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
88 |
+
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
|
89 |
+
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
|
90 |
+
return prompt
|
91 |
+
except Exception as e:
|
92 |
+
logger.exception("๋ฒ์ญ ์ค ์ค๋ฅ ๋ฐ์:")
|
93 |
+
# ์์ ํ ํฐ์ ์๋ ํ๊ทธ๋ก ๋ณต์
|
94 |
+
prompt = prompt.replace("IMAGE_TAG_ONE", "#1")
|
95 |
+
prompt = prompt.replace("IMAGE_TAG_TWO", "#2")
|
96 |
+
prompt = prompt.replace("IMAGE_TAG_THREE", "#3")
|
97 |
+
return prompt
|
98 |
+
|
99 |
def preprocess_prompt(prompt, image1, image2, image3):
|
100 |
"""
|
101 |
ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ๊ณ ๊ธฐ๋ฅ ๋ช
๋ น์ ํด์
|
102 |
"""
|
|
|
|
|
103 |
has_img1 = image1 is not None
|
104 |
has_img2 = image2 is not None
|
105 |
has_img3 = image3 is not None
|
106 |
+
|
|
|
107 |
if "#1" in prompt and not has_img1:
|
108 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
109 |
else:
|
110 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
111 |
+
|
112 |
if "#2" in prompt and not has_img2:
|
113 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
114 |
else:
|
115 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
116 |
+
|
117 |
if "#3" in prompt and not has_img3:
|
118 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
119 |
else:
|
120 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
121 |
+
|
|
|
122 |
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
|
|
|
123 |
desc_match = re.search(r'#1์ "(.*?)"์ผ๋ก ๋ฐ๊ฟ๋ผ', prompt)
|
124 |
if desc_match:
|
125 |
description = desc_match.group(1)
|
126 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ {description}์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์๋ณธ ์ด๋ฏธ์ง์ ์ฃผ์ ๋ด์ฉ์ ์ ์งํ๋ ์๋ก์ด ๏ฟฝ๏ฟฝํ์ผ๊ณผ ๋ถ์๊ธฐ๋ก ์ฌํด์ํด์ฃผ์ธ์."
|
127 |
else:
|
128 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
129 |
+
|
130 |
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
|
|
|
131 |
text_match = re.search(r'#1์์ "(.*?)"๋ฅผ ์ง์๋ผ', prompt)
|
132 |
if text_match:
|
133 |
text_to_remove = text_match.group(1)
|
134 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ '{text_to_remove}' ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ํ
์คํธ๊ฐ ์๋ ๋ถ๋ถ์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ์ฑ์์ฃผ์ธ์."
|
135 |
else:
|
136 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
137 |
+
|
|
|
|
|
|
|
138 |
elif "4. ์ท๋ฐ๊พธ๊ธฐ" in prompt:
|
139 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์์์ ์คํ์ผ๊ณผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
|
140 |
+
|
|
|
|
|
|
|
|
|
141 |
elif "5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ" in prompt:
|
142 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ฃผ์ ํผ์ฌ์ฒด๋ ์ ์งํ๊ณ , ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ํฉ์ฑํด์ฃผ์ธ์."
|
143 |
+
|
144 |
elif "6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)" in prompt:
|
145 |
+
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง(๋๋ ์ธ ๋ฒ์งธ ์ด๋ฏธ์ง)๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ๋๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํด์ฃผ์ธ์."
|
146 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
157 |
if not 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):
|
176 |
if img is not None:
|
177 |
contents.append(img)
|
178 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
179 |
+
|
|
|
180 |
response = client.models.generate_content(
|
181 |
model="gemini-2.0-flash-exp-image-generation",
|
182 |
contents=contents,
|
|
|
188 |
max_output_tokens=8192
|
189 |
)
|
190 |
)
|
191 |
+
|
|
|
192 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
193 |
temp_path = tmp.name
|
|
|
194 |
result_text = ""
|
195 |
image_found = False
|
|
|
|
|
196 |
for part in response.candidates[0].content.parts:
|
197 |
if hasattr(part, 'text') and part.text:
|
198 |
result_text += part.text
|
|
|
201 |
save_binary_file(temp_path, part.inline_data.data)
|
202 |
image_found = True
|
203 |
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
|
|
204 |
if not image_found:
|
205 |
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
|
|
|
|
206 |
result_img = Image.open(temp_path)
|
207 |
if result_img.mode == "RGBA":
|
208 |
result_img = result_img.convert("RGB")
|
|
|
209 |
return result_img, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
|
|
210 |
except Exception as e:
|
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๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
218 |
"""
|
219 |
try:
|
|
|
220 |
images = [image1, image2, image3]
|
221 |
valid_images = [img for img in images if img is not None]
|
|
|
222 |
if not valid_images:
|
223 |
+
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
224 |
+
|
225 |
+
if prompt and prompt.strip():
|
226 |
+
processed_prompt = preprocess_prompt(prompt, image1, image2, image3)
|
227 |
+
if re.search("[๊ฐ-ํฃ]", processed_prompt):
|
228 |
+
final_prompt = translate_prompt_to_english(processed_prompt)
|
229 |
+
else:
|
230 |
+
final_prompt = processed_prompt
|
231 |
+
else:
|
232 |
if len(valid_images) == 1:
|
233 |
+
final_prompt = "Please creatively transform this image into a more vivid and artistic version."
|
234 |
+
logger.info("Default prompt generated for single image")
|
235 |
elif len(valid_images) == 2:
|
236 |
+
final_prompt = "Please seamlessly composite these two images, integrating their key elements harmoniously into a single image."
|
237 |
+
logger.info("Default prompt generated for two images")
|
238 |
else:
|
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 |
+
<p>๋ชจ๋ ์ด๋ฏธ์ง๋ ๊ท ์ผํ ํฌ๊ธฐ๋ก ํ์๋ฉ๋๋ค.</p>
|
297 |
</div>
|
298 |
"""
|
299 |
)
|
300 |
|
301 |
with gr.Row():
|
302 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
303 |
with gr.Row():
|
304 |
+
image1_input = gr.Image(type="pil", label="#1", image_mode="RGB", height=250, width=250)
|
305 |
+
image2_input = gr.Image(type="pil", label="#2", image_mode="RGB", height=250, width=250)
|
306 |
+
image3_input = gr.Image(type="pil", label="#3", image_mode="RGB", height=250, width=250)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
prompt_input = gr.Textbox(
|
308 |
lines=3,
|
309 |
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค.",
|
310 |
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
|
311 |
)
|
312 |
+
with gr.Row():
|
313 |
+
image_change_btn = gr.Button("์ด๋ฏธ์ง๋ณ๊ฒฝ")
|
314 |
+
text_remove_btn = gr.Button("๊ธ์์ง์ฐ๊ธฐ")
|
315 |
+
clothes_change_btn = gr.Button("๊ฐ์ ์ํ์ฐฉ์ฉ")
|
316 |
+
background_change_btn = gr.Button("๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ")
|
317 |
+
composite_product_btn = gr.Button("๋ถ๋ถ์ง์ฐ๊ธฐ")
|
318 |
+
submit_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ (4์ฅ)", variant="primary")
|
319 |
+
|
320 |
+
with gr.Row():
|
321 |
with gr.Column():
|
322 |
+
output_image1 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #1", height=300, width=300)
|
323 |
+
with gr.Column():
|
324 |
+
output_image2 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #2", height=300, width=300)
|
|
|
|
|
|
|
325 |
|
326 |
+
with gr.Row():
|
327 |
+
with gr.Column():
|
328 |
+
output_image3 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #3", height=300, width=300)
|
329 |
+
with gr.Column():
|
330 |
+
output_image4 = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง #4", height=300, width=300)
|
|
|
331 |
|
332 |
+
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
333 |
+
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", visible=True)
|
334 |
+
|
335 |
+
# ์ ํ ์ต์
๋ฒํผ ํด๋ฆญ ์ ํ๋กฌํํธ ์
๋ ฅ๋ ์
๋ฐ์ดํธ (ํ๊ตญ์ด ๋ฌธ๊ตฌ)
|
336 |
+
image_change_btn.click(
|
337 |
+
fn=lambda: "#1 ์ด๋ฏธ์ง์ [๋ค๋ฅธ ๋ชจ์ต]์ผ๋ก ๋ณ๊ฒฝํ๋ผ.",
|
338 |
+
inputs=[],
|
339 |
+
outputs=prompt_input
|
340 |
+
)
|
341 |
+
text_remove_btn.click(
|
342 |
+
fn=lambda: "#1 ์ด๋ฏธ์ง์ [์ค๊ตญ์ด๋ฅผ ๋ชจ๋]๋ฅผ ์ ๊ฑฐํ๋ผ.",
|
343 |
+
inputs=[],
|
344 |
+
outputs=prompt_input
|
345 |
+
)
|
346 |
+
clothes_change_btn.click(
|
347 |
+
fn=lambda: "#1์ด๋ฏธ์ง์์ [์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ ์งํ ์ฒด] ์์[์คํ์ผ๊ณผ ์์์]#2, #3์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์.",
|
348 |
+
inputs=[],
|
349 |
+
outputs=prompt_input
|
350 |
+
)
|
351 |
+
background_change_btn.click(
|
352 |
+
fn=lambda: "#1 ์ด๋ฏธ์ง์ #2 ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ ๊ต์ฒดํ๋ผ [๋จ ์ฃผ์ ํผ์ฌ์ฒด๋ ๊ทธ๋๋ก ์ ์งํ์ฌ ๋ ์ด๋ฏธ์ง์ ๋ถ์๊ธฐ๊ฐ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ๋ง๋ค์ด์ฃผ์ธ์].",
|
353 |
+
inputs=[],
|
354 |
+
outputs=prompt_input
|
355 |
+
)
|
356 |
+
composite_product_btn.click(
|
357 |
+
fn=lambda: "#1 ์ด๋ฏธ์ง์์ [์ ๊ฑฐํ ์์ญ ๋๋ ๋์์ ์์ธํ ์์ฑํด์ฃผ์ธ์]์ ์ ๊ฑฐํ ํ, ๊ทธ ์๋ฆฌ๋ฅผ ์ฃผ๋ณ ๋ฐฐ๊ฒฝ๊ณผ ์์ฐ์ค๋ฝ๊ฒ ์ด์ฐ๋ฌ์ง๋๋ก ์ฑ์์ฃผ์ธ์. ๋จ, ์ด๋ฏธ์ง์ ์ฃผ์ ์์์ ์ ์ฒด ๋ถ์๊ธฐ๋ ๋์ผํ๊ฒ ์ ์งํด ์ฃผ์ธ์.",
|
358 |
+
inputs=[],
|
359 |
+
outputs=prompt_input
|
360 |
+
)
|
361 |
|
362 |
submit_btn.click(
|
363 |
+
fn=generate_multiple_images,
|
364 |
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
365 |
+
outputs=[output_image1, output_image2, output_image3, output_image4, output_text, prompt_display],
|
366 |
)
|
367 |
+
|
368 |
gr.Markdown(
|
369 |
"""
|
370 |
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
371 |
|
372 |
+
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค.
|
373 |
+
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
|
374 |
+
3. **์ ํ ์ต์
**: ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ๋กฌํํธ ์
๋ ฅ๋์ ํ๊ตญ์ด ๋ฌธ๊ตฌ๋ก ์
๋ ฅํ์๋ฉด ๋ฉ๋๋ค.
|
375 |
+
4. **๋ค์ํ ์ด๋ฏธ์ง**: "์ด๋ฏธ์ง ์์ฑ" ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฐจ๋ก๋ก 4์ฅ์ ์ด๋ฏธ์ง๊ฐ ์์ฑ๋ฉ๋๋ค.
|
|
|
376 |
|
377 |
+
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
|
378 |
"""
|
379 |
)
|
380 |
|
|
|
381 |
if __name__ == "__main__":
|
382 |
demo.launch(share=True)
|