Kims12 commited on
Commit
2f6470a
ยท
verified ยท
1 Parent(s): 5d55591

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -39
app.py CHANGED
@@ -156,43 +156,74 @@ def preprocess_prompt(prompt, image1, image2, image3):
156
  prompt += " ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”. ์ด๋ฏธ์ง€์— ํ…์ŠคํŠธ๋‚˜ ๊ธ€์ž๋ฅผ ํฌํ•จํ•˜์ง€ ๋งˆ์„ธ์š”."
157
  return prompt
158
 
159
- def convert_to_jpg(pil_image):
160
- """์ด๋ฏธ์ง€๋ฅผ JPG ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค."""
161
- import tempfile
162
- from PIL import Image
163
- import io
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
- def save_as_jpg(img):
182
- """์ด๋ฏธ์ง€๋ฅผ ๊ฐ•์ œ๋กœ JPG๋กœ ์ €์žฅํ•˜๊ณ  ๊ทธ ๊ฒฝ๋กœ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค."""
183
- if img is None:
184
- return None
185
-
186
- # RGB๋กœ ๋ณ€ํ™˜ (JPG๋Š” ์•ŒํŒŒ ์ฑ„๋„์„ ์ง€์›ํ•˜์ง€ ์•Š์Œ)
187
- if img.mode == "RGBA":
188
- img = img.convert("RGB")
 
189
 
190
- # ์ž„์‹œ ํŒŒ์ผ๋กœ JPG ์ €์žฅ
191
- temp_jpg = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False)
192
- img.save(temp_jpg.name, format="JPEG", quality=95)
193
- temp_jpg.close()
194
-
195
- return temp_jpg.name
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- # ์ด๋ฏธ์ง€๋ฅผ JPG๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
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
- # ์ด๋ฏธ์ง€๋ฅผ JPG๋กœ ๋ณ€ํ™˜
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}")