test-100 / app.py
Kims12's picture
Update app.py
5ff01f8 verified
raw
history blame
10.3 kB
import json
import os
import time
import tempfile
from PIL import Image
import gradio as gr
import logging
import io
from google import genai
from google.genai import types
# .env ํŒŒ์ผ์— ์ €์žฅ๋œ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋กœ๋“œ (python-dotenv ์„ค์น˜ ํ•„์š”: pip install python-dotenv)
from dotenv import load_dotenv
load_dotenv()
# ๋กœ๊น… ์„ค์ • (๋กœ๊ทธ ๋ ˆ๋ฒจ: DEBUG)
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def save_binary_file(file_name, data):
logger.debug(f"ํŒŒ์ผ์— ์ด์ง„ ๋ฐ์ดํ„ฐ ์ €์žฅ ์ค‘: {file_name}")
with open(file_name, "wb") as f:
f.write(data)
logger.debug(f"ํŒŒ์ผ ์ €์žฅ ์™„๋ฃŒ: {file_name}")
def generate_image_from_prompt(prompt, model="gemini-2.0-flash-exp-image-generation"):
logger.debug(f"generate_image_from_prompt ํ•จ์ˆ˜ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: '{prompt}'")
try:
# API ํ‚ค๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๋ถˆ๋Ÿฌ์˜ด
effective_api_key = os.environ.get("GEMINI_API_KEY")
if effective_api_key:
logger.debug("ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ API ํ‚ค ๋ถˆ๋Ÿฌ์˜ด")
else:
logger.error("API ํ‚ค๊ฐ€ ํ™˜๊ฒฝ๋ณ€์ˆ˜์— ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
raise ValueError("API ํ‚ค๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.")
client = genai.Client(api_key=effective_api_key)
logger.debug("Gemini ํด๋ผ์ด์–ธํŠธ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ.")
# ํ”„๋กฌํ”„ํŠธ ์ฒ˜๋ฆฌ
if not prompt or not prompt.strip():
prompt = "A creative and visually appealing image that captures imagination. Use vibrant colors and interesting composition."
# ํ•œ๊ตญ์–ด ํ”„๋กฌํ”„ํŠธ์— ์˜์–ด ์„ค๋ช… ์ถ”๊ฐ€
if not any(ord(c) < 128 for c in prompt):
prompt += " Create a highly detailed, visually stunning image that captures the essence of the description."
# ์ปจํ…์ธ  ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
parts = [
types.Part.from_text(text=prompt)
]
logger.debug(f"์ปจํ…์ธ  ๊ฐ์ฒด ์ƒ์„ฑ ์™„๋ฃŒ: {len(parts)} ์•„์ดํ…œ")
# ์ƒ์„ฑ ์„ค์ •
generate_content_config = types.GenerateContentConfig(
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192,
response_modalities=["image", "text"],
)
logger.debug(f"์ƒ์„ฑ ์„ค์ •: {generate_content_config}")
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
temp_path = tmp.name
logger.debug(f"์ž„์‹œ ํŒŒ์ผ ์ƒ์„ฑ๋จ: {temp_path}")
# ์ŠคํŠธ๋ฆฌ๋ฐ ๋ฐฉ์‹์œผ๋กœ ๋ฐ์ดํ„ฐ ์ˆ˜์‹ 
response_stream = client.models.generate_content_stream(
model=model,
contents=[
types.Content(
role="user",
parts=parts,
),
],
config=generate_content_config,
)
logger.debug("์‘๋‹ต ์ŠคํŠธ๋ฆผ ์ฒ˜๋ฆฌ ์‹œ์ž‘...")
# ์‘๋‹ต์—์„œ ์ด๋ฏธ์ง€์™€ ํ…์ŠคํŠธ ์ถ”์ถœ
image_saved = False
response_text = ""
for chunk in response_stream:
logger.debug(f"chunk ์ˆ˜์‹ : {chunk}")
# ์‘๋‹ต ๊ฒ€์ฆ
if not hasattr(chunk, 'candidates') or not chunk.candidates or len(chunk.candidates) == 0:
logger.warning("chunk์— candidates๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
continue
if not hasattr(chunk.candidates[0], 'content') or chunk.candidates[0].content is None:
logger.warning("chunk.candidates[0]์— content๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
continue
if not hasattr(chunk.candidates[0].content, 'parts') or not chunk.candidates[0].content.parts:
logger.warning("chunk.candidates[0].content์— parts๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
continue
for part in chunk.candidates[0].content.parts:
if hasattr(part, 'text') and part.text:
response_text += part.text
logger.info(f"์ˆ˜์‹ ๋œ ํ…์ŠคํŠธ: {part.text}")
elif hasattr(part, 'inline_data') and part.inline_data:
save_binary_file(temp_path, part.inline_data.data)
logger.info(f"MIME ํƒ€์ž… {part.inline_data.mime_type}์˜ ํŒŒ์ผ์ด ์ €์žฅ๋จ: {temp_path}")
image_saved = True
if not image_saved:
logger.warning("์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.")
return None, response_text or "์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กฌํ”„ํŠธ๋กœ ์‹œ๋„ํ•ด๋ณด์„ธ์š”."
logger.debug("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์™„๋ฃŒ.")
return temp_path, response_text
except Exception as e:
logger.exception("์ด๋ฏธ์ง€ ์ƒ์„ฑ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
return None, f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}" # ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ None๊ณผ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
def process_image_generation(person_pil, product_pil, background_pil, prompt):
logger.debug(f"process_image_generation ํ•จ์ˆ˜ ์‹œ์ž‘ - ํ”„๋กฌํ”„ํŠธ: '{prompt}'")
try:
# ์ด๋ฏธ์ง€๋“ค์ด ์ œ๊ณต๋˜์—ˆ๋‹ค๋ฉด ๊ธฐ์กด ๋ฐฉ์‹์œผ๋กœ ์ฒ˜๋ฆฌ
if person_pil is not None or product_pil is not None or background_pil is not None:
return process_images_and_prompt(person_pil, product_pil, background_pil, prompt)
# ์ด๋ฏธ์ง€ ์—†์ด ํ”„๋กฌํ”„ํŠธ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ
result_path, response_text = generate_image_from_prompt(prompt)
# ์ด๋ฏธ์ง€ ๋ฐ˜ํ™˜ ๋ฐ ์ฒ˜๋ฆฌ
if result_path:
logger.debug(f"์ด๋ฏธ์ง€ ์ƒ์„ฑ ์™„๋ฃŒ. ๊ฒฝ๋กœ: {result_path}")
try:
result_img = Image.open(result_path)
if result_img.mode == "RGBA":
result_img = result_img.convert("RGB")
return [result_img], response_text or "์ด๋ฏธ์ง€๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."
except Exception as e:
logger.exception(f"๊ฒฐ๊ณผ ์ด๋ฏธ์ง€ ๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜: {str(e)}")
return [], f"๊ฒฐ๊ณผ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜: {str(e)}"
else:
logger.error("generate_image_from_prompt ํ•จ์ˆ˜์—์„œ None ๋ฐ˜ํ™˜๋จ.")
return [], response_text or "์ด๋ฏธ์ง€ ์ƒ์„ฑ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ํ”„๋กฌํ”„ํŠธ๋กœ ์‹œ๋„ํ•ด๋ณด์„ธ์š”."
except Exception as e:
logger.exception("process_image_generation ํ•จ์ˆ˜์—์„œ ์˜ค๋ฅ˜ ๋ฐœ์ƒ:")
return [], f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}" # ์˜ค๋ฅ˜ ์‹œ ๋นˆ ๋ฆฌ์ŠคํŠธ์™€ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
# --- Gradio ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌ์„ฑ (๊ธฐ์กด ์ฝ”๋“œ๋Š” ๋™์ผํ•˜๊ฒŒ ์œ ์ง€) ---
with gr.Blocks() as demo:
gr.HTML(
"""
<div style='display: flex; align-items: center; justify-content: center; gap: 20px'>
<div style="background-color: var(--block-background-fill); border-radius: 8px">
<img src="https://www.gstatic.com/lamda/images/gemini_favicon_f069958c85030456e93de685481c559f160ea06b.png" style="width: 100px; height: 100px;">
</div>
<div>
<h1>Gemini๋ฅผ ์ด์šฉํ•œ ์ด๋ฏธ์ง€ ์ƒ์„ฑ</h1>
<p>์‚ฌ๋žŒ, ์ƒํ’ˆ, ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ํ•ฉ์„ฑํ•˜๊ฑฐ๋‚˜ ํ…์ŠคํŠธ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.</p>
</div>
</div>
"""
)
gr.Markdown("์‚ฌ๋žŒ ์ด๋ฏธ์ง€, ์ƒํ’ˆ ์ด๋ฏธ์ง€, ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์—…๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ํ…์ŠคํŠธ๋กœ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")
with gr.Row():
with gr.Column():
# ์ด๋ฏธ์ง€ ์ž…๋ ฅ ์„น์…˜ (์„ ํƒ ์‚ฌํ•ญ)
person_input = gr.Image(type="pil", label="์‚ฌ๋žŒ ์ด๋ฏธ์ง€ (์„ ํƒ ์‚ฌํ•ญ)", image_mode="RGB")
product_input = gr.Image(type="pil", label="์ƒํ’ˆ ์ด๋ฏธ์ง€ (์„ ํƒ ์‚ฌํ•ญ)", image_mode="RGB")
background_input = gr.Image(type="pil", label="๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ (์„ ํƒ ์‚ฌํ•ญ)", image_mode="RGB")
# ํ”„๋กฌํ”„ํŠธ ์ž…๋ ฅ (ํ•„์ˆ˜)
prompt_input = gr.Textbox(
lines=3,
placeholder="์ด๋ฏธ์ง€ ์ƒ์„ฑ ์„ค๋ช…์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”. ์˜ˆ: '๊ฝƒ๋ฐญ์—์„œ ํ–‡๋น›์„ ๋ฐ›์œผ๋ฉฐ ๋ฏธ์†Œ ์ง“๋Š” ์ Š์€ ์—ฌ์„ฑ', 'ํ˜„๋Œ€์ ์ธ ๊ฑฐ์‹ค์—์„œ ๊ณ ๊ธ‰ ์Šค๋งˆํŠธํฐ์„ ์‚ฌ์šฉํ•˜๋Š” ๋น„์ฆˆ๋‹ˆ์Šค๋งจ'",
label="์ด๋ฏธ์ง€ ์ƒ์„ฑ ์„ค๋ช… (ํ•„์ˆ˜)"
)
submit_btn = gr.Button("์ด๋ฏธ์ง€ ์ƒ์„ฑ")
with gr.Column():
output_gallery = gr.Gallery(label="์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€")
output_text = gr.Textbox(label="AI ์‘๋‹ต ํ…์ŠคํŠธ", visible=True)
submit_btn.click(
fn=process_image_generation,
inputs=[person_input, product_input, background_input, prompt_input],
outputs=[output_gallery, output_text],
)
gr.HTML("""
<div style="margin-top: 20px; padding: 10px; background-color: #f8f9fa; border-radius: 8px;">
<h3>์‚ฌ์šฉ ๋ฐฉ๋ฒ•:</h3>
<ul>
<li><strong>ํ…์ŠคํŠธ๋กœ ์ด๋ฏธ์ง€ ์ƒ์„ฑ:</strong> ์„ค๋ช…๋งŒ ์ž…๋ ฅํ•˜๊ณ  ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด์„ธ์š”.</li>
<li><strong>์ด๋ฏธ์ง€ ํ•ฉ์„ฑ:</strong> ์‚ฌ๋žŒ, ์ƒํ’ˆ, ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ์„ ํƒ์ ์œผ๋กœ ์—…๋กœ๋“œํ•˜๊ณ  ํ•ฉ์„ฑ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”.</li>
<li><strong>ํ”„๋กฌํ”„ํŠธ ํŒ:</strong> ๊ตฌ์ฒด์ ์ด๊ณ  ์ƒ์ƒํ•œ ์„ค๋ช…์ผ์ˆ˜๋ก ๋” ์ข‹์€ ์ด๋ฏธ์ง€๊ฐ€ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.</li>
<li><strong>์˜ˆ์‹œ ํ”„๋กฌํ”„ํŠธ:</strong>
<ul>
<li>ํ•œ์ ํ•œ ์นดํŽ˜์—์„œ ๋…ธํŠธ๋ถ์œผ๋กœ ์ž‘์—…ํ•˜๋Š” ํฌ๋ฆฌ์—์ดํ„ฐ</li>
<li>ํ•ด๋ณ€๊ฐ€์—์„œ ๊ณ ๊ธ‰ ์„ ๊ธ€๋ผ์Šค๋ฅผ ๋ผ๊ณ  ํฌ์ฆˆ ์ทจํ•˜๋Š” ๋ชจ๋ธ</li>
<li>ํ˜„๋Œ€์ ์ธ ์ฃผ๋ฐฉ์—์„œ ์ตœ์‹  ๋ธ”๋ Œ๋”๋กœ ์Šค๋ฌด๋””๋ฅผ ๋งŒ๋“œ๋Š” ์š”๋ฆฌ์‚ฌ</li>
</ul>
</li>
<li><strong>์–ธ์–ด ํŒ:</strong> ํ•œ๊ตญ์–ด์™€ ์˜์–ด๋ฅผ ํ˜ผํ•ฉํ•ด ์‚ฌ์šฉํ•˜๋ฉด ๋” ์ •ํ™•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.</li>
</ul>
</div>
""")
# --- ์‹คํ–‰ ---
if __name__ == "__main__":
demo.launch(share=True)