|
import json |
|
import os |
|
import time |
|
import uuid |
|
import tempfile |
|
from PIL import Image |
|
import gradio as gr |
|
import base64 |
|
import mimetypes |
|
import logging |
|
|
|
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(text, file_name, model="gemini-2.0-flash-exp-image-generation"): |
|
logger.debug(f"generate ν¨μ μμ - ν
μ€νΈ: '{text}', νμΌλͺ
: '{file_name}', λͺ¨λΈ: '{model}'") |
|
|
|
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 ν΄λΌμ΄μΈνΈ μ΄κΈ°ν μλ£.") |
|
|
|
|
|
files = [ |
|
client.files.upload(file=file_name), |
|
] |
|
logger.debug(f"νμΌ μ
λ‘λ μλ£. URI: {files[0].uri}, MIME νμ
: {files[0].mime_type}") |
|
|
|
|
|
contents = [ |
|
types.Content( |
|
role="user", |
|
parts=[ |
|
types.Part.from_uri( |
|
file_uri=files[0].uri, |
|
mime_type=files[0].mime_type, |
|
), |
|
types.Part.from_text(text=text), |
|
], |
|
), |
|
] |
|
logger.debug(f"컨ν
μΈ κ°μ²΄ μμ± μλ£: {contents}") |
|
|
|
generate_content_config = types.GenerateContentConfig( |
|
temperature=1, |
|
top_p=0.95, |
|
top_k=40, |
|
max_output_tokens=8192, |
|
response_modalities=[ |
|
"image", |
|
"text", |
|
], |
|
response_mime_type="text/plain", |
|
) |
|
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=contents, |
|
config=generate_content_config, |
|
) |
|
|
|
logger.debug("μλ΅ μ€νΈλ¦Ό μ²λ¦¬ μμ...") |
|
for chunk in response_stream: |
|
if not chunk.candidates or not chunk.candidates[0].content or not chunk.candidates[0].content.parts: |
|
logger.warning("chunkμ ν보, 컨ν
μΈ , λλ ννΈκ° μμ΅λλ€. 건λλλλ€.") |
|
continue |
|
|
|
inline_data = chunk.candidates[0].content.parts[0].inline_data |
|
if inline_data: |
|
save_binary_file(temp_path, inline_data.data) |
|
logger.info(f"MIME νμ
{inline_data.mime_type}μ νμΌμ΄ μ μ₯λ¨: {temp_path} (ν둬ννΈ: {text})") |
|
else: |
|
logger.info(f"μμ λ ν
μ€νΈ: {chunk.text}") |
|
print(chunk.text) |
|
|
|
logger.debug(f"Raw chunk: {chunk}") |
|
|
|
del files |
|
logger.debug("μ
λ‘λλ νμΌ μ 보 μμ μλ£.") |
|
return temp_path |
|
|
|
except Exception as e: |
|
logger.exception("μ΄λ―Έμ§ μμ± μ€ μ€λ₯ λ°μ:") |
|
return None |
|
|
|
|
|
def process_image_and_prompt(composite_pil, prompt): |
|
logger.debug(f"process_image_and_prompt ν¨μ μμ - ν둬ννΈ: '{prompt}'") |
|
try: |
|
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp: |
|
composite_path = tmp.name |
|
composite_pil.save(composite_path) |
|
logger.debug(f"ν©μ± μ΄λ―Έμ§ μ μ₯ μλ£: {composite_path}") |
|
|
|
file_name = composite_path |
|
input_text = prompt |
|
model = "gemini-2.0-flash-exp-image-generation" |
|
|
|
gemma_edited_image_path = generate(text=input_text, file_name=file_name, model=model) |
|
|
|
if gemma_edited_image_path: |
|
logger.debug(f"μ΄λ―Έμ§ μμ± μλ£. κ²½λ‘: {gemma_edited_image_path}") |
|
result_img = Image.open(gemma_edited_image_path) |
|
if result_img.mode == "RGBA": |
|
result_img = result_img.convert("RGB") |
|
return [result_img] |
|
else: |
|
logger.error("generate ν¨μμμ None λ°νλ¨.") |
|
return [] |
|
|
|
except Exception as e: |
|
logger.exception("process_image_and_prompt ν¨μμμ μ€λ₯ λ°μ:") |
|
return [] |
|
|
|
|
|
|
|
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>Gemini API ν€λ νκ²½λ³μ(GEMINI_API_KEY)λ‘ μ€μ λμ΄ μμ΅λλ€.</p> |
|
</div> |
|
</div> |
|
""" |
|
) |
|
gr.Markdown("μ΄λ―Έμ§λ₯Ό μ
λ‘λνκ³ , νΈμ§ν λ΄μ©μ μ
λ ₯νμΈμ.") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
image_input = gr.Image(type="pil", label="μ΄λ―Έμ§ μ
λ‘λ", image_mode="RGBA") |
|
prompt_input = gr.Textbox( |
|
lines=2, |
|
placeholder="νΈμ§ν λ΄μ©μ μ
λ ₯νμΈμ...", |
|
label="νΈμ§ ν둬ννΈ" |
|
) |
|
submit_btn = gr.Button("μ΄λ―Έμ§ νΈμ§ μ€ν") |
|
with gr.Column(): |
|
output_gallery = gr.Gallery(label="νΈμ§ κ²°κ³Ό") |
|
|
|
submit_btn.click( |
|
fn=process_image_and_prompt, |
|
inputs=[image_input, prompt_input], |
|
outputs=output_gallery, |
|
) |
|
|
|
|
|
|
|
dummy_image = Image.new("RGBA", (100, 100), color="red") |
|
dummy_prompt = "μ΄λ―Έμ§λ₯Ό νλμμΌλ‘ λ³κ²½ν΄μ€" |
|
|
|
logger.info("process_image_and_prompt ν¨μλ₯Ό μ§μ νΈμΆν©λλ€...") |
|
result = process_image_and_prompt(dummy_image, dummy_prompt) |
|
|
|
if result: |
|
logger.info(f"μ§μ νΈμΆ μ±κ³΅. κ²°κ³Ό: {result}") |
|
else: |
|
logger.error("μ§μ νΈμΆ μ€ν¨.") |
|
|
|
demo.launch(share=True) |
|
|