Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,6 @@ 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
|
@@ -69,22 +68,22 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
69 |
has_img1 = image1 is not None
|
70 |
has_img2 = image2 is not None
|
71 |
has_img3 = image3 is not None
|
72 |
-
|
73 |
if "#1" in prompt and not has_img1:
|
74 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
75 |
else:
|
76 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
77 |
-
|
78 |
if "#2" in prompt and not has_img2:
|
79 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
80 |
else:
|
81 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
82 |
-
|
83 |
if "#3" in prompt and not has_img3:
|
84 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
85 |
else:
|
86 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
87 |
-
|
88 |
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
|
89 |
desc_match = re.search(r'#1์ "(.*?)"์ผ๋ก ๋ฐ๊ฟ๋ผ', prompt)
|
90 |
if desc_match:
|
@@ -92,7 +91,7 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
92 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ {description}์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์๋ณธ ์ด๋ฏธ์ง์ ์ฃผ์ ๋ด์ฉ์ ์ ์งํ๋ ์๋ก์ด ์คํ์ผ๊ณผ ๋ถ์๊ธฐ๋ก ์ฌํด์ํด์ฃผ์ธ์."
|
93 |
else:
|
94 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
95 |
-
|
96 |
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
|
97 |
text_match = re.search(r'#1์์ "(.*?)"๋ฅผ ์ง์๋ผ', prompt)
|
98 |
if text_match:
|
@@ -100,40 +99,37 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
100 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ '{text_to_remove}' ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ํ
์คํธ๊ฐ ์๋ ๋ถ๋ถ์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ์ฑ์์ฃผ์ธ์."
|
101 |
else:
|
102 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
103 |
-
|
104 |
elif "4. ์ท๋ฐ๊พธ๊ธฐ" in prompt:
|
105 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์์์ ์คํ์ผ๊ณผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
|
106 |
-
|
107 |
elif "5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ" in prompt:
|
108 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ฃผ์ ํผ์ฌ์ฒด๋ ์ ์งํ๊ณ , ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ํฉ์ฑํด์ฃผ์ธ์."
|
109 |
-
|
110 |
elif "6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)" in prompt:
|
111 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง(๋๋ ์ธ ๋ฒ์งธ ์ด๋ฏธ์ง)๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ๋๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํด์ฃผ์ธ์."
|
112 |
-
|
113 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
114 |
-
|
115 |
return prompt
|
116 |
|
117 |
def generate_with_images(prompt, images):
|
118 |
"""
|
119 |
-
|
120 |
"""
|
121 |
try:
|
122 |
api_key = os.environ.get("GEMINI_API_KEY")
|
123 |
if not api_key:
|
124 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
125 |
-
|
126 |
client = genai.Client(api_key=api_key)
|
127 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
|
128 |
-
|
129 |
-
contents = []
|
130 |
-
contents.append(prompt)
|
131 |
-
|
132 |
for idx, img in enumerate(images, 1):
|
133 |
if img is not None:
|
134 |
contents.append(img)
|
135 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
136 |
-
|
137 |
response = client.models.generate_content(
|
138 |
model="gemini-2.0-flash-exp-image-generation",
|
139 |
contents=contents,
|
@@ -145,7 +141,7 @@ def generate_with_images(prompt, images):
|
|
145 |
max_output_tokens=8192
|
146 |
)
|
147 |
)
|
148 |
-
|
149 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
150 |
temp_path = tmp.name
|
151 |
result_text = ""
|
@@ -170,16 +166,15 @@ def generate_with_images(prompt, images):
|
|
170 |
|
171 |
def process_images_with_prompt(image1, image2, image3, prompt):
|
172 |
"""
|
173 |
-
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ
|
174 |
-
|
175 |
"""
|
176 |
try:
|
177 |
images = [image1, image2, image3]
|
178 |
valid_images = [img for img in images if img is not None]
|
179 |
if not valid_images:
|
180 |
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
181 |
-
|
182 |
-
# ํ๋กฌํํธ๊ฐ ์
๋ ฅ๋ ๊ฒฝ์ฐ: ์ ์ฒ๋ฆฌ ํ ์ต์ข
์์ด ํ๋กฌํํธ ์์ฑ
|
183 |
if prompt and prompt.strip():
|
184 |
processed_prompt = preprocess_prompt(prompt, image1, image2, image3)
|
185 |
if re.search("[๊ฐ-ํฃ]", processed_prompt):
|
@@ -187,7 +182,6 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
187 |
else:
|
188 |
final_prompt = processed_prompt
|
189 |
else:
|
190 |
-
# ํ๋กฌํํธ ๋ฏธ์
๋ ฅ ์, ์ด๋ฏธ์ง ๊ฐ์์ ๋ฐ๋ฅธ ๊ธฐ๋ณธ ํ๋กฌํํธ (์์ด)
|
191 |
if len(valid_images) == 1:
|
192 |
final_prompt = "Please creatively transform this image into a more vivid and artistic version."
|
193 |
logger.info("Default prompt generated for single image")
|
@@ -197,7 +191,7 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
197 |
else:
|
198 |
final_prompt = "Please creatively composite these three images, combining their main elements into a cohesive and natural scene."
|
199 |
logger.info("Default prompt generated for three images")
|
200 |
-
|
201 |
result_img, status = generate_with_images(final_prompt, valid_images)
|
202 |
return result_img, status, final_prompt
|
203 |
except Exception as e:
|
@@ -205,9 +199,6 @@ def process_images_with_prompt(image1, image2, image3, prompt):
|
|
205 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
|
206 |
|
207 |
def process_and_show_prompt(image1, image2, image3, prompt):
|
208 |
-
"""
|
209 |
-
์ต์ข
์ ์ผ๋ก ์ด๋ฏธ์ง ์์ฑ ๋ฐ ์์ด ์ต์ข
ํ๋กฌํํธ๋ฅผ ๋ฐํํ๋ ํจ์.
|
210 |
-
"""
|
211 |
try:
|
212 |
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt)
|
213 |
return result_img, status, final_prompt
|
@@ -215,13 +206,42 @@ def process_and_show_prompt(image1, image2, image3, prompt):
|
|
215 |
logger.exception("์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
216 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
|
217 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
# Gradio ์ธํฐํ์ด์ค
|
219 |
with gr.Blocks() as demo:
|
220 |
gr.HTML(
|
221 |
"""
|
222 |
<div style="text-align: center; margin-bottom: 1rem;">
|
223 |
-
<h1>๊ฐ๋จํ ์ด๋ฏธ์ง
|
224 |
-
<p>์ด๋ฏธ์ง๋ฅผ
|
225 |
</div>
|
226 |
"""
|
227 |
)
|
@@ -237,7 +257,6 @@ with gr.Blocks() as demo:
|
|
237 |
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค.",
|
238 |
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
|
239 |
)
|
240 |
-
|
241 |
with gr.Row():
|
242 |
image_change_btn = gr.Button("์ด๋ฏธ์ง ๋ณ๊ฒฝ")
|
243 |
text_remove_btn = gr.Button("๊ธ์์ง์ฐ๊ธฐ")
|
@@ -250,7 +269,7 @@ with gr.Blocks() as demo:
|
|
250 |
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
251 |
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", visible=True)
|
252 |
|
253 |
-
# ์ ํ ์ต์
๋ฒํผ ํด๋ฆญ ์ ํ๋กฌํํธ ์
๋ ฅ๋ ์
๋ฐ์ดํธ (ํ๊ตญ์ด ๋ฌธ๊ตฌ
|
254 |
image_change_btn.click(
|
255 |
fn=lambda: "#1 ์ด๋ฏธ์ง์ [๋ค๋ฅธ ๋ชจ์ต]์ผ๋ก ๋ณ๊ฒฝํ๋ผ.",
|
256 |
inputs=[],
|
@@ -282,16 +301,30 @@ with gr.Blocks() as demo:
|
|
282 |
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
283 |
outputs=[output_image, output_text, prompt_display],
|
284 |
)
|
285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
286 |
gr.Markdown(
|
287 |
"""
|
288 |
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
289 |
|
290 |
-
1. **์๋ ํฉ์ฑ**:
|
291 |
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
|
292 |
-
3.
|
293 |
-
4. **์ ํ ์ต์
**: ์์
|
294 |
-
์ดํ Geminiโ2.0โflash ๋ชจ๋ธ์ ํตํด ์์ด๋ก ๋ฒ์ญ๋์ด ์ค์ API ํธ์ถ ํ๋กฌํํธ๋ก ์ ์ฉ๋ฉ๋๋ค.
|
295 |
|
296 |
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
|
297 |
"""
|
|
|
4 |
import gradio as gr
|
5 |
import logging
|
6 |
import re
|
|
|
7 |
from io import BytesIO
|
8 |
|
9 |
from google import genai
|
|
|
68 |
has_img1 = image1 is not None
|
69 |
has_img2 = image2 is not None
|
70 |
has_img3 = image3 is not None
|
71 |
+
|
72 |
if "#1" in prompt and not has_img1:
|
73 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
74 |
else:
|
75 |
prompt = prompt.replace("#1", "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
76 |
+
|
77 |
if "#2" in prompt and not has_img2:
|
78 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
79 |
else:
|
80 |
prompt = prompt.replace("#2", "๋ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
81 |
+
|
82 |
if "#3" in prompt and not has_img3:
|
83 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง(์์)")
|
84 |
else:
|
85 |
prompt = prompt.replace("#3", "์ธ ๋ฒ์งธ ์ด๋ฏธ์ง")
|
86 |
+
|
87 |
if "1. ์ด๋ฏธ์ง ๋ณ๊ฒฝ" in prompt:
|
88 |
desc_match = re.search(r'#1์ "(.*?)"์ผ๋ก ๋ฐ๊ฟ๋ผ', prompt)
|
89 |
if desc_match:
|
|
|
91 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ {description}์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์๋ณธ ์ด๋ฏธ์ง์ ์ฃผ์ ๋ด์ฉ์ ์ ์งํ๋ ์๋ก์ด ์คํ์ผ๊ณผ ๋ถ์๊ธฐ๋ก ์ฌํด์ํด์ฃผ์ธ์."
|
92 |
else:
|
93 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ฐฝ์์ ์ผ๋ก ๋ณํํด์ฃผ์ธ์. ๋ ์์ํ๊ณ ์์ ์ ์ธ ๋ฒ์ ์ผ๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
94 |
+
|
95 |
elif "2. ๊ธ์์ง์ฐ๊ธฐ" in prompt:
|
96 |
text_match = re.search(r'#1์์ "(.*?)"๋ฅผ ์ง์๋ผ', prompt)
|
97 |
if text_match:
|
|
|
99 |
prompt = f"์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ '{text_to_remove}' ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ํ
์คํธ๊ฐ ์๋ ๋ถ๋ถ์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ์ฑ์์ฃผ์ธ์."
|
100 |
else:
|
101 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์์ ๋ชจ๋ ํ
์คํธ๋ฅผ ์ฐพ์ ์์ฐ์ค๋ฝ๊ฒ ์ ๊ฑฐํด์ฃผ์ธ์. ๊น๋ํ ์ด๋ฏธ์ง๋ก ๋ง๋ค์ด์ฃผ์ธ์."
|
102 |
+
|
103 |
elif "4. ์ท๋ฐ๊พธ๊ธฐ" in prompt:
|
104 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ธ๋ฌผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์์์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์์์ ์คํ์ผ๊ณผ ์์์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ๋ฐ๋ฅด๋, ์ ์ฒด ๋น์จ๊ณผ ํฌ์ฆ๋ ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง๋ฅผ ์ ์งํด์ฃผ์ธ์."
|
105 |
+
|
106 |
elif "5. ๋ฐฐ๊ฒฝ๋ฐ๊พธ๊ธฐ" in prompt:
|
107 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณ๊ฒฝํด์ฃผ์ธ์. ์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ์ฃผ์ ํผ์ฌ์ฒด๋ ์ ์งํ๊ณ , ๋ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ฐฐ๊ฒฝ๊ณผ ์กฐํ๋กญ๊ฒ ํฉ์ฑํด์ฃผ์ธ์."
|
108 |
+
|
109 |
elif "6. ์ด๋ฏธ์ง ํฉ์ฑ(์ํํฌํจ)" in prompt:
|
110 |
prompt = "์ฒซ ๋ฒ์งธ ์ด๋ฏธ์ง์ ๋ ๋ฒ์งธ ์ด๋ฏธ์ง(๋๋ ์ธ ๋ฒ์งธ ์ด๋ฏธ์ง)๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ํฉ์ฑํด์ฃผ์ธ์. ๋ชจ๋ ์ด๋ฏธ์ง์ ์ฃผ์ ์์๋ฅผ ํฌํจํ๊ณ , ํนํ ์ํ์ด ๋๋ณด์ด๋๋ก ์กฐํ๋กญ๊ฒ ํตํฉํด์ฃผ์ธ์."
|
111 |
+
|
112 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์."
|
|
|
113 |
return prompt
|
114 |
|
115 |
def generate_with_images(prompt, images):
|
116 |
"""
|
117 |
+
API ํธ์ถ์ ํตํด ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ณ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
118 |
"""
|
119 |
try:
|
120 |
api_key = os.environ.get("GEMINI_API_KEY")
|
121 |
if not api_key:
|
122 |
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
123 |
+
|
124 |
client = genai.Client(api_key=api_key)
|
125 |
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}")
|
126 |
+
|
127 |
+
contents = [prompt]
|
|
|
|
|
128 |
for idx, img in enumerate(images, 1):
|
129 |
if img is not None:
|
130 |
contents.append(img)
|
131 |
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
132 |
+
|
133 |
response = client.models.generate_content(
|
134 |
model="gemini-2.0-flash-exp-image-generation",
|
135 |
contents=contents,
|
|
|
141 |
max_output_tokens=8192
|
142 |
)
|
143 |
)
|
144 |
+
|
145 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
146 |
temp_path = tmp.name
|
147 |
result_text = ""
|
|
|
166 |
|
167 |
def process_images_with_prompt(image1, image2, image3, prompt):
|
168 |
"""
|
169 |
+
3๊ฐ์ ์ด๋ฏธ์ง์ ํ๋กฌํํธ๋ฅผ ์ฒ๋ฆฌํ์ฌ ์ต์ข
์์ด ํ๋กฌํํธ(final_prompt)๋ฅผ ์์ฑํ ํ,
|
170 |
+
API๋ฅผ ํธ์ถํ์ฌ ๊ฒฐ๊ณผ ์ด๋ฏธ์ง๋ฅผ ๋ฐํํฉ๋๋ค.
|
171 |
"""
|
172 |
try:
|
173 |
images = [image1, image2, image3]
|
174 |
valid_images = [img for img in images if img is not None]
|
175 |
if not valid_images:
|
176 |
return None, "์ ์ด๋ ํ๋์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.", ""
|
177 |
+
|
|
|
178 |
if prompt and prompt.strip():
|
179 |
processed_prompt = preprocess_prompt(prompt, image1, image2, image3)
|
180 |
if re.search("[๊ฐ-ํฃ]", processed_prompt):
|
|
|
182 |
else:
|
183 |
final_prompt = processed_prompt
|
184 |
else:
|
|
|
185 |
if len(valid_images) == 1:
|
186 |
final_prompt = "Please creatively transform this image into a more vivid and artistic version."
|
187 |
logger.info("Default prompt generated for single image")
|
|
|
191 |
else:
|
192 |
final_prompt = "Please creatively composite these three images, combining their main elements into a cohesive and natural scene."
|
193 |
logger.info("Default prompt generated for three images")
|
194 |
+
|
195 |
result_img, status = generate_with_images(final_prompt, valid_images)
|
196 |
return result_img, status, final_prompt
|
197 |
except Exception as e:
|
|
|
199 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
|
200 |
|
201 |
def process_and_show_prompt(image1, image2, image3, prompt):
|
|
|
|
|
|
|
202 |
try:
|
203 |
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt)
|
204 |
return result_img, status, final_prompt
|
|
|
206 |
logger.exception("์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์:")
|
207 |
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}", prompt
|
208 |
|
209 |
+
# ์์ ์คํ ํจ์๋ค
|
210 |
+
def run_example_1():
|
211 |
+
"""์์ 1: ์ด๋ฏธ์ง ๋ณ๊ฒฝ ์์ """
|
212 |
+
input_path = os.path.join("down", "1_in-1.png")
|
213 |
+
output_path = os.path.join("down", "1_out-1.webp")
|
214 |
+
try:
|
215 |
+
input_img = Image.open(input_path)
|
216 |
+
except Exception as e:
|
217 |
+
return None, f"์
๋ ฅ ์ด๋ฏธ์ง ์ด๊ธฐ ์ค๋ฅ: {str(e)}", ""
|
218 |
+
prompt = "#1 ์ด๋ฏธ์ง์ [์ฒญ์ ์์ด๋ ๊ณ ๋ฅผ ๊ฒ์ ๊ณ ๋๋ ๊ณ ]์ผ๋ก ๋ณ๊ฒฝํ๋ผ."
|
219 |
+
result_img, status, final_prompt = process_and_show_prompt(input_img, None, None, prompt)
|
220 |
+
if result_img is not None:
|
221 |
+
result_img.save(output_path, "WEBP")
|
222 |
+
return result_img, status, final_prompt
|
223 |
+
|
224 |
+
def run_example_2():
|
225 |
+
"""์์ 2: ๊ธ์์ง์ฐ๊ธฐ ์์ """
|
226 |
+
input_path = os.path.join("down", "2_in-1.png")
|
227 |
+
output_path = os.path.join("down", "2_out-1.webp")
|
228 |
+
try:
|
229 |
+
input_img = Image.open(input_path)
|
230 |
+
except Exception as e:
|
231 |
+
return None, f"์
๋ ฅ ์ด๋ฏธ์ง ์ด๊ธฐ ์ค๋ฅ: {str(e)}", ""
|
232 |
+
prompt = "#1 ์ด๋ฏธ์ง์ [์ค๊ตญ์ด๋ฅผ ๋ชจ๋]๋ฅผ ์ ๊ฑฐํ๋ผ."
|
233 |
+
result_img, status, final_prompt = process_and_show_prompt(input_img, None, None, prompt)
|
234 |
+
if result_img is not None:
|
235 |
+
result_img.save(output_path, "WEBP")
|
236 |
+
return result_img, status, final_prompt
|
237 |
+
|
238 |
# Gradio ์ธํฐํ์ด์ค
|
239 |
with gr.Blocks() as demo:
|
240 |
gr.HTML(
|
241 |
"""
|
242 |
<div style="text-align: center; margin-bottom: 1rem;">
|
243 |
+
<h1>๊ฐ๋จํ ์ด๋ฏธ์ง ์์ฑ๊ธฐ (์์ ํฌํจ)</h1>
|
244 |
+
<p>์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ฑฐ๋ ์๋ ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฒ๋ฆฌ๋ ๊ฒฐ๊ณผ๊ฐ down ํด๋์ ์ ์ฅ๋ฉ๋๋ค.</p>
|
245 |
</div>
|
246 |
"""
|
247 |
)
|
|
|
257 |
placeholder="ํ๋กฌํํธ๋ฅผ ์
๋ ฅํ๊ฑฐ๋ ๋น์๋๋ฉด ์๋ ํฉ์ฑ๋ฉ๋๋ค.",
|
258 |
label="ํ๋กฌํํธ (์ ํ ์ฌํญ)"
|
259 |
)
|
|
|
260 |
with gr.Row():
|
261 |
image_change_btn = gr.Button("์ด๋ฏธ์ง ๋ณ๊ฒฝ")
|
262 |
text_remove_btn = gr.Button("๊ธ์์ง์ฐ๊ธฐ")
|
|
|
269 |
output_text = gr.Textbox(label="์ํ ๋ฉ์์ง")
|
270 |
prompt_display = gr.Textbox(label="์ฌ์ฉ๋ ํ๋กฌํํธ (์์ด)", visible=True)
|
271 |
|
272 |
+
# ์ ํ ์ต์
๋ฒํผ ํด๋ฆญ ์ ํ๋กฌํํธ ์
๋ ฅ๋ ์
๋ฐ์ดํธ (ํ๊ตญ์ด ๋ฌธ๊ตฌ)
|
273 |
image_change_btn.click(
|
274 |
fn=lambda: "#1 ์ด๋ฏธ์ง์ [๋ค๋ฅธ ๋ชจ์ต]์ผ๋ก ๋ณ๊ฒฝํ๋ผ.",
|
275 |
inputs=[],
|
|
|
301 |
inputs=[image1_input, image2_input, image3_input, prompt_input],
|
302 |
outputs=[output_image, output_text, prompt_display],
|
303 |
)
|
304 |
+
|
305 |
+
with gr.Row():
|
306 |
+
example1_btn = gr.Button("์์ 1 ์คํ")
|
307 |
+
example2_btn = gr.Button("์์ 2 ์คํ")
|
308 |
+
|
309 |
+
example1_btn.click(
|
310 |
+
fn=run_example_1,
|
311 |
+
inputs=[],
|
312 |
+
outputs=[output_image, output_text, prompt_display]
|
313 |
+
)
|
314 |
+
example2_btn.click(
|
315 |
+
fn=run_example_2,
|
316 |
+
inputs=[],
|
317 |
+
outputs=[output_image, output_text, prompt_display]
|
318 |
+
)
|
319 |
+
|
320 |
gr.Markdown(
|
321 |
"""
|
322 |
### ์ฌ์ฉ ๋ฐฉ๋ฒ:
|
323 |
|
324 |
+
1. **์๋ ํฉ์ฑ**: ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๊ณ ํ๋กฌํํธ๋ฅผ ๋น์๋๋ฉด ์๋์ผ๋ก ํฉ์ฑ๋ฉ๋๋ค.
|
325 |
2. **์ด๋ฏธ์ง ์ฐธ์กฐ**: #1, #2, #3์ผ๋ก ๊ฐ ์ด๋ฏธ์ง๋ฅผ ์ฐธ์กฐํ ์ ์์ต๋๋ค.
|
326 |
+
3. **์์ ์คํ**: ์๋ "์์ 1 ์คํ" ๋๋ "์์ 2 ์คํ" ๋ฒํผ์ ๋๋ฅด๋ฉด down ํด๋์ ์
๋ ฅ ์ด๋ฏธ์ง๋ฅผ ์ฒ๋ฆฌํ์ฌ ๊ฒฐ๊ณผ๊ฐ ์ ์ฅ๋ฉ๋๋ค.
|
327 |
+
4. **์ ํ ์ต์
**: ์์ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ํ๋กฌํํธ ์
๋ ฅ๋์ ํ๊ตญ์ด ๋ฌธ๊ตฌ๊ฐ ์ฑ์์ง๊ณ , ์ดํ Geminiโ2.0โflash ๋ชจ๋ธ์ ํตํด ์์ด๋ก ๋ฒ์ญ๋์ด ์ค์ API ํธ์ถ ํ๋กฌํํธ๋ก ์ ์ฉ๋ฉ๋๋ค.
|
|
|
328 |
|
329 |
> **ํ**: ํ๋กฌํํธ๋ฅผ ์ง์ ์์ ํ ์๋ ์์ต๋๋ค.
|
330 |
"""
|