|
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 |
|
|
|
|
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
|
|
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: |
|
|
|
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)}" |
|
|
|
|
|
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)}" |
|
|
|
|
|
|
|
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) |