Update app.py
Browse files
app.py
CHANGED
@@ -178,69 +178,21 @@ def convert_to_jpg(pil_image):
|
|
178 |
jpg_image = Image.open(jpg_buffer)
|
179 |
return jpg_image
|
180 |
|
181 |
-
def
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
if not api_key:
|
186 |
-
return None, "API 키가 설정되지 않았습니다. 환경변수를 확인해주세요."
|
187 |
-
|
188 |
-
client = genai.Client(api_key=api_key)
|
189 |
-
logger.info(f"Gemini API 요청 시작 - 프롬프트: {prompt}, 변형 인덱스: {variation_index}")
|
190 |
-
|
191 |
-
variation_suffixes = [
|
192 |
-
" Create this as the first variation. Do not add any text, watermarks, or labels to the image.",
|
193 |
-
" Create this as the second variation with more vivid colors. Do not add any text, watermarks, or labels to the image.",
|
194 |
-
" Create this as the third variation with a more creative style. Do not add any text, watermarks, or labels to the image.",
|
195 |
-
" Create this as the fourth variation with enhanced details. Do not add any text, watermarks, or labels to the image."
|
196 |
-
]
|
197 |
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
response = client.models.generate_content(
|
210 |
-
model="gemini-2.0-flash-exp-image-generation",
|
211 |
-
contents=contents,
|
212 |
-
config=types.GenerateContentConfig(
|
213 |
-
response_modalities=['Text', 'Image'],
|
214 |
-
temperature=1,
|
215 |
-
top_p=0.95,
|
216 |
-
top_k=40,
|
217 |
-
max_output_tokens=8192
|
218 |
-
)
|
219 |
-
)
|
220 |
-
|
221 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
222 |
-
temp_path = tmp.name
|
223 |
-
result_text = ""
|
224 |
-
image_found = False
|
225 |
-
for part in response.candidates[0].content.parts:
|
226 |
-
if hasattr(part, 'text') and part.text:
|
227 |
-
result_text += part.text
|
228 |
-
logger.info(f"응답 텍스트: {part.text}")
|
229 |
-
elif hasattr(part, 'inline_data') and part.inline_data:
|
230 |
-
save_binary_file(temp_path, part.inline_data.data)
|
231 |
-
image_found = True
|
232 |
-
logger.info("응답에서 이미지 추출 성공")
|
233 |
-
if not image_found:
|
234 |
-
return None, f"API에서 이미지를 생성하지 못했습니다. 응답 텍스트: {result_text}"
|
235 |
-
|
236 |
-
# 이미지를 JPG로 변환
|
237 |
-
result_img = Image.open(temp_path)
|
238 |
-
result_img = convert_to_jpg(result_img)
|
239 |
-
|
240 |
-
return result_img, f"이미지가 성공적으로 생성되었습니다. {result_text}"
|
241 |
-
except Exception as e:
|
242 |
-
logger.exception("이미지 생성 중 오류 발생:")
|
243 |
-
return None, f"오류 발생: {str(e)}"
|
244 |
|
245 |
def process_images_with_prompt(image1, image2, image3, prompt, variation_index=0, max_retries=3):
|
246 |
# 기존 함수 내용 유지
|
@@ -966,11 +918,11 @@ with gr.Blocks(css=custom_css) as demo:
|
|
966 |
gr.HTML('<div class="section-title"><i class="fas fa-images"></i> 생성된 이미지</div>')
|
967 |
with gr.Row():
|
968 |
with gr.Column():
|
969 |
-
output_image1 = gr.Image(label="이미지 #1", elem_classes="image-container", height=400,
|
970 |
-
output_image3 = gr.Image(label="이미지 #3", elem_classes="image-container", height=400,
|
971 |
with gr.Column():
|
972 |
-
output_image2 = gr.Image(label="이미지 #2", elem_classes="image-container", height=400,
|
973 |
-
output_image4 = gr.Image(label="이미지 #4", elem_classes="image-container", height=400,
|
974 |
|
975 |
gr.HTML('<div class="section-title"><i class="fas fa-info-circle"></i> 결과 정보</div>')
|
976 |
output_text = gr.Textbox(label="상태 메시지", lines=2, elem_classes="custom-input")
|
|
|
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 |
# 기존 함수 내용 유지
|
|
|
918 |
gr.HTML('<div class="section-title"><i class="fas fa-images"></i> 생성된 이미지</div>')
|
919 |
with gr.Row():
|
920 |
with gr.Column():
|
921 |
+
output_image1 = gr.Image(label="이미지 #1", elem_classes="image-container", height=400, type="filepath")
|
922 |
+
output_image3 = gr.Image(label="이미지 #3", elem_classes="image-container", height=400, type="filepath")
|
923 |
with gr.Column():
|
924 |
+
output_image2 = gr.Image(label="이미지 #2", elem_classes="image-container", height=400, type="filepath")
|
925 |
+
output_image4 = gr.Image(label="이미지 #4", elem_classes="image-container", height=400, type="filepath")
|
926 |
|
927 |
gr.HTML('<div class="section-title"><i class="fas fa-info-circle"></i> 결과 정보</div>')
|
928 |
output_text = gr.Textbox(label="상태 메시지", lines=2, elem_classes="custom-input")
|