Update app.py
Browse files
app.py
CHANGED
@@ -156,43 +156,74 @@ def preprocess_prompt(prompt, image1, image2, image3):
|
|
156 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
157 |
return prompt
|
158 |
|
159 |
-
def
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
if pil_image is None:
|
166 |
-
return None
|
167 |
-
|
168 |
-
# RGBA ์ด๋ฏธ์ง๋ฅผ RGB๋ก ๋ณํ (JPG๋ ์ํ ์ฑ๋์ ์ง์ํ์ง ์์)
|
169 |
-
if pil_image.mode == "RGBA":
|
170 |
-
pil_image = pil_image.convert("RGB")
|
171 |
-
|
172 |
-
# ์ด๋ฏธ์ง๋ฅผ JPG๋ก ๋ณํ
|
173 |
-
jpg_buffer = io.BytesIO()
|
174 |
-
pil_image.save(jpg_buffer, format="JPEG", quality=95)
|
175 |
-
jpg_buffer.seek(0)
|
176 |
-
|
177 |
-
# JPG ์ด๋ฏธ์ง ๋ค์ ๋ก๋
|
178 |
-
jpg_image = Image.open(jpg_buffer)
|
179 |
-
return jpg_image
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
|
|
189 |
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0, max_retries=3):
|
198 |
# ๊ธฐ์กด ํจ์ ๋ด์ฉ ์ ์ง
|
@@ -225,8 +256,7 @@ def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0
|
|
225 |
|
226 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
227 |
if result_img is not None:
|
228 |
-
#
|
229 |
-
result_img = convert_to_jpg(result_img)
|
230 |
return result_img, status, final_prompt
|
231 |
else:
|
232 |
last_error = status
|
@@ -257,8 +287,7 @@ def generate_multiple_images(image1, image2, image3, prompt, progress=gr.Progres
|
|
257 |
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt, i, max_retries)
|
258 |
|
259 |
if result_img is not None:
|
260 |
-
#
|
261 |
-
result_img = convert_to_jpg(result_img)
|
262 |
results.append(result_img)
|
263 |
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
264 |
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|
|
|
156 |
prompt += " ์ด๋ฏธ์ง๋ฅผ ์์ฑํด์ฃผ์ธ์. ์ด๋ฏธ์ง์ ํ
์คํธ๋ ๊ธ์๋ฅผ ํฌํจํ์ง ๋ง์ธ์."
|
157 |
return prompt
|
158 |
|
159 |
+
def generate_with_images(prompt, images, variation_index=0):
|
160 |
+
# ๊ธฐ์กด ํจ์ ์ ์ง
|
161 |
+
try:
|
162 |
+
api_key = os.environ.get("GEMINI_API_KEY")
|
163 |
+
if not api_key:
|
164 |
+
return None, "API ํค๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. ํ๊ฒฝ๋ณ์๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
client = genai.Client(api_key=api_key)
|
167 |
+
logger.info(f"Gemini API ์์ฒญ ์์ - ํ๋กฌํํธ: {prompt}, ๋ณํ ์ธ๋ฑ์ค: {variation_index}")
|
168 |
+
|
169 |
+
variation_suffixes = [
|
170 |
+
" Create this as the first variation. Do not add any text, watermarks, or labels to the image.",
|
171 |
+
" Create this as the second variation with more vivid colors. Do not add any text, watermarks, or labels to the image.",
|
172 |
+
" Create this as the third variation with a more creative style. Do not add any text, watermarks, or labels to the image.",
|
173 |
+
" Create this as the fourth variation with enhanced details. Do not add any text, watermarks, or labels to the image."
|
174 |
+
]
|
175 |
|
176 |
+
if variation_index < len(variation_suffixes):
|
177 |
+
prompt = prompt + variation_suffixes[variation_index]
|
178 |
+
else:
|
179 |
+
prompt = prompt + " Do not add any text, watermarks, or labels to the image."
|
180 |
+
|
181 |
+
contents = [prompt]
|
182 |
+
for idx, img in enumerate(images, 1):
|
183 |
+
if img is not None:
|
184 |
+
contents.append(img)
|
185 |
+
logger.info(f"์ด๋ฏธ์ง #{idx} ์ถ๊ฐ๋จ")
|
186 |
+
|
187 |
+
response = client.models.generate_content(
|
188 |
+
model="gemini-2.0-flash-exp-image-generation",
|
189 |
+
contents=contents,
|
190 |
+
config=types.GenerateContentConfig(
|
191 |
+
response_modalities=['Text', 'Image'],
|
192 |
+
temperature=1,
|
193 |
+
top_p=0.95,
|
194 |
+
top_k=40,
|
195 |
+
max_output_tokens=8192
|
196 |
+
)
|
197 |
+
)
|
198 |
+
|
199 |
+
# ์์ ํ์ผ์ ํญ์ JPG ํ์ฅ์๋ก ์์ฑ
|
200 |
+
with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
|
201 |
+
temp_path = tmp.name
|
202 |
+
result_text = ""
|
203 |
+
image_found = False
|
204 |
+
for part in response.candidates[0].content.parts:
|
205 |
+
if hasattr(part, 'text') and part.text:
|
206 |
+
result_text += part.text
|
207 |
+
logger.info(f"์๋ต ํ
์คํธ: {part.text}")
|
208 |
+
elif hasattr(part, 'inline_data') and part.inline_data:
|
209 |
+
save_binary_file(temp_path, part.inline_data.data)
|
210 |
+
image_found = True
|
211 |
+
logger.info("์๋ต์์ ์ด๋ฏธ์ง ์ถ์ถ ์ฑ๊ณต")
|
212 |
+
if not image_found:
|
213 |
+
return None, f"API์์ ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค. ์๋ต ํ
์คํธ: {result_text}"
|
214 |
+
|
215 |
+
result_img = Image.open(temp_path)
|
216 |
+
if result_img.mode == "RGBA":
|
217 |
+
result_img = result_img.convert("RGB") # JPG๋ ํฌ๋ช
๋๋ฅผ ์ง์ํ์ง ์์ผ๋ฏ๋ก RGB๋ก ๋ณํ
|
218 |
+
|
219 |
+
# ๋ณํ๋ ์ด๋ฏธ์ง๋ฅผ JPG๋ก ์ ์ฅ
|
220 |
+
result_img.save(temp_path, format="JPEG", quality=95)
|
221 |
+
|
222 |
+
# ํ์ผ ๊ฒฝ๋ก ๋ฐํ
|
223 |
+
return temp_path, f"์ด๋ฏธ์ง๊ฐ ์ฑ๊ณต์ ์ผ๋ก ์์ฑ๋์์ต๋๋ค. {result_text}"
|
224 |
+
except Exception as e:
|
225 |
+
logger.exception("์ด๋ฏธ์ง ์์ฑ ์ค ์ค๋ฅ ๋ฐ์:")
|
226 |
+
return None, f"์ค๋ฅ ๋ฐ์: {str(e)}"
|
227 |
|
228 |
def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0, max_retries=3):
|
229 |
# ๊ธฐ์กด ํจ์ ๋ด์ฉ ์ ์ง
|
|
|
256 |
|
257 |
result_img, status = generate_with_images(final_prompt, valid_images, variation_index)
|
258 |
if result_img is not None:
|
259 |
+
# ์ด๋ฏธ ํ์ผ ๊ฒฝ๋ก๊ฐ ๋ฐํ๋๋ฏ๋ก ์ถ๊ฐ ์ฒ๋ฆฌ ์์ด ๊ทธ๋๋ก ๋ฐํ
|
|
|
260 |
return result_img, status, final_prompt
|
261 |
else:
|
262 |
last_error = status
|
|
|
287 |
result_img, status, final_prompt = process_images_with_prompt(image1, image2, image3, prompt, i, max_retries)
|
288 |
|
289 |
if result_img is not None:
|
290 |
+
# ์ด๋ฏธ ํ์ผ ๊ฒฝ๋ก์ด๋ฏ๋ก ์ถ๊ฐ ์ฒ๋ฆฌ ์์ด ๊ทธ๋๋ก ์ถ๊ฐ
|
|
|
291 |
results.append(result_img)
|
292 |
statuses.append(f"์ด๋ฏธ์ง #{i+1}: {status}")
|
293 |
prompts.append(f"์ด๋ฏธ์ง #{i+1}: {final_prompt}")
|